]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-1.1.0/test/test_attributes.rb
750408202e07ecb50b11d9fe09e83b4f77e6cba6
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / test / test_attributes.rb
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'
7
8 class TestAttributes < Test::Unit::TestCase
9   fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
10   
11   CLASSES = {
12     :single => {
13       :class => ReferenceType,
14       :primary_keys => :reference_type_id,
15     },
16     :dual   => { 
17       :class => ReferenceCode,
18       :primary_keys => [:reference_type_id, :reference_code],
19     },
20   }
21   
22   def setup
23     self.class.classes = CLASSES
24   end
25   
26   def test_brackets
27     testing_with do
28       @first.attributes.each_pair do |attr_name, value|
29         assert_equal value, @first[attr_name]
30       end
31     end
32   end
33     
34   def test_brackets_primary_key
35     testing_with do
36       assert_equal @first.id, @first[@primary_keys], "[] failing for #{@klass}"
37       assert_equal @first.id, @first[@first.class.primary_key]
38     end
39   end
40
41   def test_brackets_assignment
42     testing_with do
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]
46       end
47     end
48   end
49     
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)
72   end
73 private
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}"
82     end
83   end
84 end