require 'test_helper' class CsvDictionaryExtTest < Test::Unit::TestCase def setup CsvDictionaryTestHelper.setup_database end def teardown Aircraft.clear_csv_dictionaries CsvDictionaryTestHelper.teardown_database end # sanity check def test_should_have_dictionary_value_name assert_nothing_raised do Aircraft.new.brighter_planet_aircraft_class_code end end # sanity check def test_should_not_have_dictionary_key_name assert_raise NoMethodError do Aircraft.new.bts_aircraft_type end end def test_should_mixin_methods assert Aircraft.respond_to?(:csv_dictionary) assert Aircraft.respond_to?(:clear_csv_dictionaries) assert Aircraft.respond_to?(:replace_csv_dictionary_attributes!) end def test_should_not_affect_other_activerecord_models Aircraft.csv_dictionary :bts_aircraft_type, :url => CSV_URL assert Hash === Aircraft.csv_dictionaries assert NilClass === Scrum.csv_dictionaries end def test_should_configure_csv_dictionary key_name = :bts_aircraft_type options = { :url => GDOCS_URL, :sheet => 1 } CsvDictionary.expects(:new).with(key_name, options) Aircraft.csv_dictionary key_name, options end def test_should_replace_attributes Aircraft.csv_dictionary :bts_aircraft_type, :url => CSV_URL Aircraft.csv_dictionaries[:bts_aircraft_type].expects(:replace_attributes!) Aircraft.replace_csv_dictionary_attributes!({}) end def test_should_recognize_dynamic_finders fixture = Aircraft.find_or_create_by_brighter_planet_aircraft_class_code('SJ') Aircraft.csv_dictionary :bts_aircraft_type, :url => CSV_URL Aircraft.csv_dictionaries[:bts_aircraft_type].expects(:lookup).with('103').returns('SJ').times(4) assert_equal fixture, Aircraft.find_by_bts_aircraft_type('103') assert_equal fixture, Aircraft.find_by_bts_aircraft_type_and_brighter_planet_aircraft_class_code('103', 'SJ') assert_equal [fixture], Aircraft.find_all_by_bts_aircraft_type('103') assert_equal [fixture], Aircraft.find_all_by_bts_aircraft_type_and_brighter_planet_aircraft_class_code('103', 'SJ') end def test_should_recognize_dynamic_initializers fixture = Aircraft.find_or_create_by_brighter_planet_aircraft_class_code('SJ') Aircraft.csv_dictionary :bts_aircraft_type, :url => CSV_URL Aircraft.csv_dictionaries[:bts_aircraft_type].expects(:lookup).with('103').returns('SJ').twice assert_equal fixture, Aircraft.find_or_create_by_bts_aircraft_type('103') assert_equal fixture, Aircraft.find_or_create_by_bts_aircraft_type_and_brighter_planet_aircraft_class_code('103', 'SJ') end def test_should_not_recognize_bad_dynamic_finders Aircraft.csv_dictionary :bts_aircraft_type, :url => CSV_URL assert_raise NoMethodError do Aircraft.find_or_create_by_foobar('X') end assert_raise NoMethodError do Aircraft.find_or_create_by_foobar_and_id('X', 'Y') end assert_raise NoMethodError do Aircraft.find_or_create_by_foobar_and_bts_aircraft_type('X', 'Y') end end end