1 require 'abstract_unit'
2 require 'fixtures/reference_type'
3 require 'fixtures/reference_code'
4 require 'fixtures/product'
5 require 'fixtures/tariff'
6 require 'fixtures/product_tariff'
8 class TestAttributes < Test::Unit::TestCase
9 fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
13 :class => ReferenceType,
14 :primary_keys => :reference_type_id,
17 :class => ReferenceCode,
18 :primary_keys => [:reference_type_id, :reference_code],
23 self.class.classes = CLASSES
28 @first.attributes.each_pair do |attr_name, value|
29 assert_equal value, @first[attr_name]
34 def test_brackets_primary_key
36 assert_equal @first.id, @first[@primary_keys], "[] failing for #{@klass}"
37 assert_equal @first.id, @first[@first.class.primary_key]
41 def test_brackets_assignment
43 @first.attributes.each_pair do |attr_name, value|
44 @first[attr_name]= !value.nil? ? value * 2 : '1'
45 assert_equal !value.nil? ? value * 2 : '1', @first[attr_name]
50 def test_brackets_foreign_key_assignment
51 @flat = Tariff.find(1, Date.today.to_s(:db))
52 @second_free = ProductTariff.find(2,2,Date.today.to_s(:db))
53 @second_free_fk = [:tariff_id, :tariff_start_date]
54 @second_free[key = @second_free_fk] = @flat.id
55 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
56 assert_equal @flat.id, @second_free[key]
57 @second_free[key = @second_free_fk.to_composite_ids] = @flat.id
58 assert_equal @flat.id, @second_free[key]
59 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
60 @second_free[key = @second_free_fk.to_composite_ids] = @flat.id.to_s
61 assert_equal @flat.id, @second_free[key]
62 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
63 @second_free[key = @second_free_fk.to_composite_ids] = @flat.id.to_s
64 assert_equal @flat.id, @second_free[key]
65 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
66 @second_free[key = @second_free_fk.to_composite_ids.to_s] = @flat.id
67 assert_equal @flat.id, @second_free[key]
68 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
69 @second_free[key = @second_free_fk.to_composite_ids.to_s] = @flat.id.to_s
70 assert_equal @flat.id, @second_free[key]
71 compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
74 def compare_indexes(obj_name1, indexes1, obj_name2, indexes2)
75 obj1, obj2 = eval "[#{obj_name1}, #{obj_name2}]"
76 indexes1.length.times do |key_index|
77 assert_equal obj1[indexes1[key_index].to_s],
78 obj2[indexes2[key_index].to_s],
79 "#{obj_name1}[#{indexes1[key_index]}]=#{obj1[indexes1[key_index].to_s].inspect} != " +
80 "#{obj_name2}[#{indexes2[key_index]}]=#{obj2[indexes2[key_index].to_s].inspect}; " +
81 "#{obj_name2} = #{obj2.inspect}"