Merge branch 'master' into copyright
[rails.git] / vendor / gems / composite_primary_keys-2.2.2 / test / test_associations.rb
1 require 'abstract_unit'
2 require 'fixtures/article'
3 require 'fixtures/product'
4 require 'fixtures/tariff'
5 require 'fixtures/product_tariff'
6 require 'fixtures/suburb'
7 require 'fixtures/street'
8 require 'fixtures/restaurant'
9 require 'fixtures/dorm'
10 require 'fixtures/room'
11 require 'fixtures/room_attribute'
12 require 'fixtures/room_attribute_assignment'
13 require 'fixtures/student'
14 require 'fixtures/room_assignment'
15 require 'fixtures/user'
16 require 'fixtures/reading'
17
18 class TestAssociations < Test::Unit::TestCase
19   fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
20            :dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings
21   
22   def test_has_many_through_with_conditions_when_through_association_is_not_composite
23     user = User.find(:first)
24     assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
25   end
26
27   def test_has_many_through_with_conditions_when_through_association_is_composite
28     room = Room.find(:first)
29     assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "keg"]).size
30   end
31
32   def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
33     user = User.find(:first)
34     assert_equal 1, user.find_custom_articles.size
35   end
36
37   def test_has_many_through_on_custom_finder_when_through_association_is_composite
38     room = Room.find(:first)
39     assert_equal 0, room.find_custom_room_attributes.size
40   end
41   
42   def test_count
43     assert_equal 2, Product.count(:include => :product_tariffs)
44     assert_equal 3, Tariff.count(:include => :product_tariffs)
45     assert_equal 2, Tariff.count(:group => :start_date).size
46   end
47   
48   def test_products
49     assert_not_nil products(:first_product).product_tariffs
50     assert_equal 2, products(:first_product).product_tariffs.length
51     assert_not_nil products(:first_product).tariffs
52     assert_equal 2, products(:first_product).tariffs.length
53     assert_not_nil products(:first_product).product_tariff
54   end
55   
56   def test_product_tariffs
57     assert_not_nil product_tariffs(:first_flat).product
58     assert_not_nil product_tariffs(:first_flat).tariff
59     assert_equal Product, product_tariffs(:first_flat).product.class
60     assert_equal Tariff, product_tariffs(:first_flat).tariff.class
61   end
62   
63   def test_tariffs
64     assert_not_nil tariffs(:flat).product_tariffs
65     assert_equal 1, tariffs(:flat).product_tariffs.length
66     assert_not_nil tariffs(:flat).products
67     assert_equal 1, tariffs(:flat).products.length
68     assert_not_nil tariffs(:flat).product_tariff
69   end
70   
71   # Its not generating the instances of associated classes from the rows
72   def test_find_includes_products
73     assert @products = Product.find(:all, :include => :product_tariffs)
74     assert_equal 2, @products.length
75     assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
76     assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length}, 
77       "Incorrect number of product_tariffs returned"
78   end
79   
80   def test_find_includes_tariffs
81     assert @tariffs = Tariff.find(:all, :include => :product_tariffs)
82     assert_equal 3, @tariffs.length
83     assert_not_nil @tariffs.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
84     assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length}, 
85       "Incorrect number of product_tariffs returnedturned"
86   end
87   
88   def test_find_includes_product
89     assert @product_tariffs = ProductTariff.find(:all, :include => :product)
90     assert_equal 3, @product_tariffs.length
91     assert_not_nil @product_tariffs.first.instance_variable_get('@product'), '@product not set'
92   end
93   
94   def test_find_includes_comp_belongs_to_tariff
95     assert @product_tariffs = ProductTariff.find(:all, :include => :tariff)
96     assert_equal 3, @product_tariffs.length
97     assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
98   end
99   
100   def test_find_includes_extended
101     assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
102     assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
103       "Incorrect number of product_tariffs returned"
104     
105     assert @tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
106     assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length}, 
107       "Incorrect number of product_tariffs returned"
108   end
109   
110   def test_join_where_clause
111     @product = Product.find(:first, :include => :product_tariffs)
112     where_clause = @product.product_tariffs.composite_where_clause(
113       ['foo','bar'], [1,2]
114     )
115     assert_equal('(foo=1 AND bar=2)', where_clause)
116   end
117   
118   def test_has_many_through
119     @products = Product.find(:all, :include => :tariffs)
120     assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@tariffs').length},
121       "Incorrect number of tariffs returned"
122   end
123   
124   def test_has_many_through_when_not_pre_loaded
125         student = Student.find(:first)
126         rooms = student.rooms
127         assert_equal 1, rooms.size
128         assert_equal 1, rooms.first.dorm_id
129         assert_equal 1, rooms.first.room_id
130   end
131   
132   def test_has_many_through_when_through_association_is_composite
133     dorm = Dorm.find(:first)
134     assert_equal 1, dorm.rooms.length
135     assert_equal 1, dorm.rooms.first.room_attributes.length
136     assert_equal 'keg', dorm.rooms.first.room_attributes.first.name
137   end
138
139   def test_associations_with_conditions
140     @suburb = Suburb.find([2, 1])
141     assert_equal 2, @suburb.streets.size
142
143     @suburb = Suburb.find([2, 1])
144     assert_equal 1, @suburb.first_streets.size
145
146     @suburb = Suburb.find([2, 1], :include => :streets)
147     assert_equal 2, @suburb.streets.size
148
149     @suburb = Suburb.find([2, 1], :include => :first_streets)
150     assert_equal 1, @suburb.first_streets.size
151   end
152   
153   def test_has_and_belongs_to_many
154     @restaurant = Restaurant.find([1,1])
155     assert_equal 2, @restaurant.suburbs.size
156     
157     @restaurant = Restaurant.find([1,1], :include => :suburbs)
158     assert_equal 2, @restaurant.suburbs.size  
159   end
160 end