Permalink
Browse files

Merge pull request #9 from ihough/master

Fix nullification when there are units
  • Loading branch information...
2 parents e7f374d + 6c941b6 commit 1a9253323b9f179a96721180a954d4749e5ee401 @seamusabshere committed Oct 4, 2011
Showing with 63 additions and 4 deletions.
  1. +8 −4 lib/data_miner/attribute.rb
  2. +2 −0 test/support/test_database.rb
  3. +53 −0 test/test_data_miner_attribute.rb
@@ -70,6 +70,7 @@ def value_in_source(row)
value = do_split(value) if wants_split?
value.gsub! /[ ]+/, ' '
value.strip!
+ return nil if value.blank? and wants_nullification?
value.upcase! if wants_upcase?
value = do_convert row, value if wants_conversion?
value = do_sprintf value if wants_sprintf?
@@ -86,16 +87,19 @@ def value_from_row(row)
return value if value.is_a? ::ActiveRecord::Base # carry through trapdoor
value = value_in_dictionary value if wants_dictionary?
value = synthesize.call(row) if wants_synthesize?
- value = nil if value.blank? and wants_nullification?
value
end
-
+
def set_record_from_row(record, row)
return false if !wants_overwriting? and !record.send(name).nil?
record.send "#{name}=", value_from_row(row)
- record.send "#{name}_units=", (to_units || unit_from_source(row)).to_s if wants_units?
+ if wants_units?
+ unit = (to_units || unit_from_source(row)).to_s
+ unit = nil if unit.blank? and wants_nullification?
+ record.send "#{name}_units=", unit
+ end
end
-
+
def unit_from_source(row)
row[units_field_name || units_field_number].to_s.strip.underscore.to_sym
end
@@ -181,7 +181,9 @@ def load_schema
t.datetime "created_at"
t.datetime "updated_at"
t.float "emission_factor"
+ t.string "emission_factor_units"
t.float "annual_distance"
+ t.string "annual_distance_units"
t.string "code"
end
execute "ALTER TABLE automobile_fuel_types ADD PRIMARY KEY (code);"
@@ -55,4 +55,57 @@ class TestDataMinerAttribute < Test::Unit::TestCase
end
end
end
+
+ context '#set_record_from_row' do
+ setup do
+ @automobile_fuel_type = AutomobileFuelType.new
+ end
+ context 'nullify is true, wants units' do
+ setup do
+ @attribute = DataMiner::Attribute.new @automobile_fuel_type, 'annual_distance', :nullify => true, :units_field_name => 'annual_distance_units'
+ end
+ should 'set value and units to nil if field is blank' do
+ @attribute.set_record_from_row(@automobile_fuel_type,
+ 'name' => 'electricity',
+ 'annual_distance' => '',
+ 'annual_distance_units' => ''
+ )
+ assert_nil @automobile_fuel_type.annual_distance
+ assert_nil @automobile_fuel_type.annual_distance_units
+ end
+ should 'set value and units if field is not blank' do
+ @attribute.set_record_from_row(@automobile_fuel_type,
+ 'name' => 'electricity',
+ 'annual_distance' => '100.0',
+ 'annual_distance_units' => 'kilometres'
+ )
+ assert_equal 100.0, @automobile_fuel_type.annual_distance
+ assert_equal 'kilometres', @automobile_fuel_type.annual_distance_units
+ end
+ end
+
+ context 'nullify is false, wants units' do
+ setup do
+ @attribute = DataMiner::Attribute.new @automobile_fuel_type, 'annual_distance', :units_field_name => 'annual_distance_units'
+ end
+ should 'set value and units to blank if field is blank' do
+ @attribute.set_record_from_row(@automobile_fuel_type,
+ 'name' => 'electricity',
+ 'annual_distance' => '',
+ 'annual_distance_units' => ''
+ )
+ assert_equal 0.0, @automobile_fuel_type.annual_distance
+ assert_equal '', @automobile_fuel_type.annual_distance_units
+ end
+ should 'set value and units if field is not blank' do
+ @attribute.set_record_from_row(@automobile_fuel_type,
+ 'name' => 'electricity',
+ 'annual_distance' => '100.0',
+ 'annual_distance_units' => 'kilometres'
+ )
+ assert_equal 100.0, @automobile_fuel_type.annual_distance
+ assert_equal 'kilometres', @automobile_fuel_type.annual_distance_units
+ end
+ end
+ end
end

0 comments on commit 1a92533

Please sign in to comment.