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