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