db64bbb8062e5e205ff6beb305f536d2d1f58fce
[feedcatcher.git] / vendor / rails / activerecord / test / cases / reflection_test.rb
1 require "cases/helper"
2 require 'models/topic'
3 require 'models/customer'
4 require 'models/company'
5 require 'models/company_in_module'
6 require 'models/subscriber'
7 require 'models/pirate'
8
9 class ReflectionTest < ActiveRecord::TestCase
10 fixtures :topics, :customers, :companies, :subscribers
11
12 def setup
13 @first = Topic.find(1)
14 end
15
16 def test_column_null_not_null
17 subscriber = Subscriber.find(:first)
18 assert subscriber.column_for_attribute("name").null
19 assert !subscriber.column_for_attribute("nick").null
20 end
21
22 def test_read_attribute_names
23 assert_equal(
24 %w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count parent_id type ).sort,
25 @first.attribute_names
26 )
27 end
28
29 def test_columns
30 assert_equal 12, Topic.columns.length
31 end
32
33 def test_columns_are_returned_in_the_order_they_were_declared
34 column_names = Topic.columns.map { |column| column.name }
35 assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count parent_id type), column_names
36 end
37
38 def test_content_columns
39 content_columns = Topic.content_columns
40 content_column_names = content_columns.map {|column| column.name}
41 assert_equal 8, content_columns.length
42 assert_equal %w(title author_name author_email_address written_on bonus_time last_read content approved).sort, content_column_names.sort
43 end
44
45 def test_column_string_type_and_limit
46 assert_equal :string, @first.column_for_attribute("title").type
47 assert_equal 255, @first.column_for_attribute("title").limit
48 end
49
50 def test_column_null_not_null
51 subscriber = Subscriber.find(:first)
52 assert subscriber.column_for_attribute("name").null
53 assert !subscriber.column_for_attribute("nick").null
54 end
55
56 def test_human_name_for_column
57 assert_equal "Author name", @first.column_for_attribute("author_name").human_name
58 end
59
60 def test_integer_columns
61 assert_equal :integer, @first.column_for_attribute("id").type
62 end
63
64 def test_reflection_klass_for_nested_class_name
65 reflection = ActiveRecord::Reflection::MacroReflection.new(nil, nil, { :class_name => 'MyApplication::Business::Company' }, nil)
66 assert_nothing_raised do
67 assert_equal MyApplication::Business::Company, reflection.klass
68 end
69 end
70
71 def test_aggregation_reflection
72 reflection_for_address = ActiveRecord::Reflection::AggregateReflection.new(
73 :composed_of, :address, { :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ] }, Customer
74 )
75
76 reflection_for_balance = ActiveRecord::Reflection::AggregateReflection.new(
77 :composed_of, :balance, { :class_name => "Money", :mapping => %w(balance amount) }, Customer
78 )
79
80 reflection_for_gps_location = ActiveRecord::Reflection::AggregateReflection.new(
81 :composed_of, :gps_location, { }, Customer
82 )
83
84 assert Customer.reflect_on_all_aggregations.include?(reflection_for_gps_location)
85 assert Customer.reflect_on_all_aggregations.include?(reflection_for_balance)
86 assert Customer.reflect_on_all_aggregations.include?(reflection_for_address)
87
88 assert_equal reflection_for_address, Customer.reflect_on_aggregation(:address)
89
90 assert_equal Address, Customer.reflect_on_aggregation(:address).klass
91
92 assert_equal Money, Customer.reflect_on_aggregation(:balance).klass
93 end
94
95 def test_reflect_on_all_autosave_associations
96 expected = Pirate.reflect_on_all_associations.select { |r| r.options[:autosave] }
97 received = Pirate.reflect_on_all_autosave_associations
98
99 assert !received.empty?
100 assert_not_equal Pirate.reflect_on_all_associations.length, received.length
101 assert_equal expected, received
102 end
103
104 def test_has_many_reflection
105 reflection_for_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :clients, { :order => "id", :dependent => :destroy }, Firm)
106
107 assert_equal reflection_for_clients, Firm.reflect_on_association(:clients)
108
109 assert_equal Client, Firm.reflect_on_association(:clients).klass
110 assert_equal 'companies', Firm.reflect_on_association(:clients).table_name
111
112 assert_equal Client, Firm.reflect_on_association(:clients_of_firm).klass
113 assert_equal 'companies', Firm.reflect_on_association(:clients_of_firm).table_name
114 end
115
116 def test_has_one_reflection
117 reflection_for_account = ActiveRecord::Reflection::AssociationReflection.new(:has_one, :account, { :foreign_key => "firm_id", :dependent => :destroy }, Firm)
118 assert_equal reflection_for_account, Firm.reflect_on_association(:account)
119
120 assert_equal Account, Firm.reflect_on_association(:account).klass
121 assert_equal 'accounts', Firm.reflect_on_association(:account).table_name
122 end
123
124 def test_belongs_to_inferred_foreign_key_from_assoc_name
125 Company.belongs_to :foo
126 assert_equal "foo_id", Company.reflect_on_association(:foo).primary_key_name
127 Company.belongs_to :bar, :class_name => "Xyzzy"
128 assert_equal "bar_id", Company.reflect_on_association(:bar).primary_key_name
129 Company.belongs_to :baz, :class_name => "Xyzzy", :foreign_key => "xyzzy_id"
130 assert_equal "xyzzy_id", Company.reflect_on_association(:baz).primary_key_name
131 end
132
133 def test_association_reflection_in_modules
134 assert_reflection MyApplication::Business::Firm,
135 :clients_of_firm,
136 :klass => MyApplication::Business::Client,
137 :class_name => 'Client',
138 :table_name => 'companies'
139
140 assert_reflection MyApplication::Billing::Account,
141 :firm,
142 :klass => MyApplication::Business::Firm,
143 :class_name => 'MyApplication::Business::Firm',
144 :table_name => 'companies'
145
146 assert_reflection MyApplication::Billing::Account,
147 :qualified_billing_firm,
148 :klass => MyApplication::Billing::Firm,
149 :class_name => 'MyApplication::Billing::Firm',
150 :table_name => 'companies'
151
152 assert_reflection MyApplication::Billing::Account,
153 :unqualified_billing_firm,
154 :klass => MyApplication::Billing::Firm,
155 :class_name => 'Firm',
156 :table_name => 'companies'
157
158 assert_reflection MyApplication::Billing::Account,
159 :nested_qualified_billing_firm,
160 :klass => MyApplication::Billing::Nested::Firm,
161 :class_name => 'MyApplication::Billing::Nested::Firm',
162 :table_name => 'companies'
163
164 assert_reflection MyApplication::Billing::Account,
165 :nested_unqualified_billing_firm,
166 :klass => MyApplication::Billing::Nested::Firm,
167 :class_name => 'Nested::Firm',
168 :table_name => 'companies'
169 end
170
171 def test_reflection_of_all_associations
172 # FIXME these assertions bust a lot
173 assert_equal 28, Firm.reflect_on_all_associations.size
174 assert_equal 21, Firm.reflect_on_all_associations(:has_many).size
175 assert_equal 7, Firm.reflect_on_all_associations(:has_one).size
176 assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
177 end
178
179 def test_reflection_should_not_raise_error_when_compared_to_other_object
180 assert_nothing_raised { Firm.reflections[:clients] == Object.new }
181 end
182
183 def test_has_many_through_reflection
184 assert_kind_of ActiveRecord::Reflection::ThroughReflection, Subscriber.reflect_on_association(:books)
185 end
186
187 private
188 def assert_reflection(klass, association, options)
189 assert reflection = klass.reflect_on_association(association)
190 options.each do |method, value|
191 assert_equal(value, reflection.send(method))
192 end
193 end
194 end