Permalink
Browse files

test attributes using elapsed minutes

  • Loading branch information...
1 parent 90f1fb9 commit 72b7c5cea43aa2c7b6ec5508627d55b05c5fb580 S. Brent Faulkner committed Feb 22, 2010
Showing with 55 additions and 41 deletions.
  1. +31 −31 test/helpers_test.rb
  2. +5 −3 test/test_helper.rb
  3. +19 −7 test/validations_test.rb
View
@@ -38,99 +38,99 @@ def test_time_select
end
def test_elapsed_time_select
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, :elapsed_time => true)
+ assert_dom_equal expected, time_select(:job, :actual_seconds, :elapsed_time => true)
end
def test_elapsed_time_select_with_seconds
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_6i" name="job[estimate(6i)]">\n)
+ expected << %(<select id="job_actual_seconds_6i" name="job[actual_seconds(6i)]">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 45}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, :elapsed_time => true, :include_seconds => true)
+ assert_dom_equal expected, time_select(:job, :actual_seconds, :elapsed_time => true, :include_seconds => true)
end
def test_elapsed_time_select_with_html_options
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]" class="selector">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]" class="selector">\n)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]" class="selector">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]" class="selector">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, {:elapsed_time => true}, :class => 'selector')
+ assert_dom_equal expected, time_select(:job, :actual_seconds, {:elapsed_time => true}, :class => 'selector')
end
def test_elapsed_time_select_with_separator
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << %(<span class="time-separator">:</span>)
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, :elapsed_time => true, :time_separator => '<span class="time-separator">:</span>')
+ assert_dom_equal expected, time_select(:job, :actual_seconds, :elapsed_time => true, :time_separator => '<span class="time-separator">:</span>')
end
def test_elapsed_time_select_with_default_prompt
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
expected << %(<option value=\"\">Hours</option>)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
expected << %(<option value=\"\">Minutes</option>)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, :elapsed_time => true, :prompt => true)
+ assert_dom_equal expected, time_select(:job, :actual_seconds, :elapsed_time => true, :prompt => true)
end
def test_elapsed_time_select_with_custom_prompt
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
expected << %(<option value=\"\">Choose hours</option>)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
expected << %(<option value=\"\">Choose minutes</option>)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
- assert_dom_equal expected, time_select(:job, :estimate, :elapsed_time => true, :prompt => {:hour => 'Choose hours', :minute => 'Choose minutes'})
+ assert_dom_equal expected, time_select(:job, :actual_seconds, :elapsed_time => true, :prompt => {:hour => 'Choose hours', :minute => 'Choose minutes'})
end
def test_elapsed_time_select_should_not_change_passed_options_hash
- @job = Job.new(:estimate => 12345)
+ @job = Job.new(:actual_seconds => 12345)
options = {
:default => { :hour => 23, :minute => 30, :second => 1 },
@@ -139,7 +139,7 @@ def test_elapsed_time_select_should_not_change_passed_options_hash
:include_seconds => true,
:elapsed_time => true
}
- time_select(:job, :estimate, options)
+ time_select(:job, :actual_seconds, options)
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
@@ -153,17 +153,17 @@ def test_elapsed_time_select_should_not_change_passed_options_hash
end
def test_elapsed_time_select_within_fields_for
- job = Job.new(:estimate => 12345)
+ job = Job.new(:actual_seconds => 12345)
fields_for :job, job do |f|
- concat f.time_select :estimate, :elapsed_time => true
+ concat f.time_select :actual_seconds, :elapsed_time => true
end
- expected = %(<select id="job_estimate_4i" name="job[estimate(4i)]">\n)
+ expected = %(<select id="job_actual_seconds_4i" name="job[actual_seconds(4i)]">\n)
0.upto(99) { |i| expected << %(<option value="#{sprintf("%d", i)}"#{' selected="selected"' if i == 3}>#{sprintf("%d", i)}</option>\n) }
expected << "</select>\n"
expected << " : "
- expected << %(<select id="job_estimate_5i" name="job[estimate(5i)]">\n)
+ expected << %(<select id="job_actual_seconds_5i" name="job[actual_seconds(5i)]">\n)
0.upto(59) { |i| expected << %(<option value="#{sprintf("%02d", i)}"#{' selected="selected"' if i == 25}>#{sprintf("%02d", i)}</option>\n) }
expected << "</select>\n"
View
@@ -10,8 +10,9 @@
require File.join(File.dirname(__FILE__), '..', 'init')
class Job < ActiveRecord::Base
- elapsed_time :estimate
- validates_elapsed_time_of :estimate, :allow_nil => true
+ elapsed_time :estimated_minutes, :unit => :minutes
+ elapsed_time :actual_seconds
+ validates_elapsed_time_of :estimated_minutes, :actual_seconds, :allow_nil => true
end
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
@@ -27,7 +28,8 @@ def setup_db
ActiveRecord::Schema.define(:version => 1) do
create_table :jobs do |t|
- t.integer :estimate
+ t.integer :estimated_minutes
+ t.integer :actual_seconds
t.timestamps
end
end
View
@@ -10,26 +10,38 @@ def teardown
end
def test_elapsed_time
- job = Job.new(:estimate => 12345)
+ job = Job.new(:actual_seconds => 12345)
assert job.save, job.errors.full_messages.to_sentence
- assert_equal 12345, job.estimate
+ assert_equal 12345, job.actual_seconds
end
def test_nil_elapsed_time
job = Job.new
assert job.save, job.errors.full_messages.to_sentence
- assert_nil job.estimate
+ assert_nil job.actual_seconds
end
def test_parsed_time
- job = Job.new(:estimate => '1 hour, 23 minutes and 45 seconds')
+ job = Job.new(:actual_seconds => '1 hour, 23 minutes and 45 seconds')
assert job.save, job.errors.full_messages.to_sentence
- assert_equal 5025, job.estimate
+ assert_equal 5025, job.actual_seconds
end
def test_bad_time
- job = Job.new(:estimate => '4 eons')
+ job = Job.new(:actual_seconds => '4 eons')
assert !job.save, 'saved with invalid elapsed time text'
- assert_equal 'is not an elapsed time', job.errors.on(:estimate)
+ assert_equal 'is not an elapsed time', job.errors.on(:actual_seconds)
+ end
+
+ def test_elapsed_minutes
+ job = Job.new(:estimated_minutes => 1234)
+ assert job.save, job.errors.full_messages.to_sentence
+ assert_equal 1234, job.estimated_minutes
+ end
+
+ def test_parsed_minutes
+ job = Job.new(:estimated_minutes => '1 hour, 23 minutes')
+ assert job.save, job.errors.full_messages.to_sentence
+ assert_equal 83, job.estimated_minutes
end
end

0 comments on commit 72b7c5c

Please sign in to comment.