activerecord 1.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- data/CHANGELOG +5518 -76
- data/README.rdoc +222 -0
- data/examples/performance.rb +162 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +192 -80
- data/lib/active_record/association_preload.rb +403 -0
- data/lib/active_record/associations/association_collection.rb +545 -53
- data/lib/active_record/associations/association_proxy.rb +295 -0
- data/lib/active_record/associations/belongs_to_association.rb +91 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +78 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +127 -36
- data/lib/active_record/associations/has_many_association.rb +108 -84
- data/lib/active_record/associations/has_many_through_association.rb +116 -0
- data/lib/active_record/associations/has_one_association.rb +143 -0
- data/lib/active_record/associations/has_one_through_association.rb +40 -0
- data/lib/active_record/associations/through_association_scope.rb +154 -0
- data/lib/active_record/associations.rb +2086 -368
- data/lib/active_record/attribute_methods/before_type_cast.rb +33 -0
- data/lib/active_record/attribute_methods/dirty.rb +95 -0
- data/lib/active_record/attribute_methods/primary_key.rb +50 -0
- data/lib/active_record/attribute_methods/query.rb +39 -0
- data/lib/active_record/attribute_methods/read.rb +116 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -0
- data/lib/active_record/attribute_methods/write.rb +37 -0
- data/lib/active_record/attribute_methods.rb +60 -0
- data/lib/active_record/autosave_association.rb +369 -0
- data/lib/active_record/base.rb +1603 -721
- data/lib/active_record/callbacks.rb +176 -225
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +365 -0
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +113 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +57 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +329 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +81 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +72 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +739 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +543 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +165 -279
- data/lib/active_record/connection_adapters/mysql_adapter.rb +594 -82
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +988 -135
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +53 -0
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +365 -71
- data/lib/active_record/counter_cache.rb +115 -0
- data/lib/active_record/dynamic_finder_match.rb +53 -0
- data/lib/active_record/dynamic_scope_match.rb +32 -0
- data/lib/active_record/errors.rb +172 -0
- data/lib/active_record/fixtures.rb +941 -105
- data/lib/active_record/locale/en.yml +40 -0
- data/lib/active_record/locking/optimistic.rb +172 -0
- data/lib/active_record/locking/pessimistic.rb +55 -0
- data/lib/active_record/log_subscriber.rb +48 -0
- data/lib/active_record/migration.rb +617 -0
- data/lib/active_record/named_scope.rb +138 -0
- data/lib/active_record/nested_attributes.rb +417 -0
- data/lib/active_record/observer.rb +105 -36
- data/lib/active_record/persistence.rb +291 -0
- data/lib/active_record/query_cache.rb +36 -0
- data/lib/active_record/railtie.rb +91 -0
- data/lib/active_record/railties/controller_runtime.rb +38 -0
- data/lib/active_record/railties/databases.rake +512 -0
- data/lib/active_record/reflection.rb +364 -87
- data/lib/active_record/relation/batches.rb +89 -0
- data/lib/active_record/relation/calculations.rb +286 -0
- data/lib/active_record/relation/finder_methods.rb +355 -0
- data/lib/active_record/relation/predicate_builder.rb +41 -0
- data/lib/active_record/relation/query_methods.rb +261 -0
- data/lib/active_record/relation/spawn_methods.rb +112 -0
- data/lib/active_record/relation.rb +393 -0
- data/lib/active_record/schema.rb +59 -0
- data/lib/active_record/schema_dumper.rb +195 -0
- data/lib/active_record/serialization.rb +60 -0
- data/lib/active_record/serializers/xml_serializer.rb +244 -0
- data/lib/active_record/session_store.rb +340 -0
- data/lib/active_record/test_case.rb +67 -0
- data/lib/active_record/timestamp.rb +88 -0
- data/lib/active_record/transactions.rb +329 -75
- data/lib/active_record/validations/associated.rb +48 -0
- data/lib/active_record/validations/uniqueness.rb +185 -0
- data/lib/active_record/validations.rb +58 -179
- data/lib/active_record/version.rb +9 -0
- data/lib/active_record.rb +100 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +17 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +38 -0
- data/lib/rails/generators/active_record/model/templates/migration.rb +16 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +5 -0
- data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +2 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +24 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +16 -0
- data/lib/rails/generators/active_record.rb +27 -0
- metadata +216 -158
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
data/test/associations_test.rb
DELETED
@@ -1,477 +0,0 @@
|
|
1
|
-
require 'abstract_unit'
|
2
|
-
require 'fixtures/developer'
|
3
|
-
require 'fixtures/project'
|
4
|
-
# require File.dirname(__FILE__) + '/../dev-utils/eval_debugger'
|
5
|
-
require 'fixtures/company'
|
6
|
-
require 'fixtures/topic'
|
7
|
-
require 'fixtures/reply'
|
8
|
-
|
9
|
-
# Can't declare new classes in test case methods, so tests before that
|
10
|
-
bad_collection_keys = false
|
11
|
-
begin
|
12
|
-
class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
|
13
|
-
rescue ActiveRecord::ActiveRecordError
|
14
|
-
bad_collection_keys = true
|
15
|
-
end
|
16
|
-
raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
|
17
|
-
|
18
|
-
|
19
|
-
class AssociationsTest < Test::Unit::TestCase
|
20
|
-
def setup
|
21
|
-
create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
|
22
|
-
@signals37 = Firm.find(1)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_force_reload
|
26
|
-
firm = Firm.new
|
27
|
-
firm.save
|
28
|
-
firm.clients.each {|c|} # forcing to load all clients
|
29
|
-
assert firm.clients.empty?, "New firm shouldn't have client objects"
|
30
|
-
assert !firm.has_clients?, "New firm shouldn't have clients"
|
31
|
-
assert_equal 0, firm.clients.size, "New firm should have 0 clients"
|
32
|
-
|
33
|
-
client = Client.new("firm_id" => firm.id)
|
34
|
-
client.save
|
35
|
-
|
36
|
-
assert firm.clients.empty?, "New firm should have cached no client objects"
|
37
|
-
assert !firm.has_clients?, "New firm should have cached a no-clients response"
|
38
|
-
assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
|
39
|
-
|
40
|
-
assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
|
41
|
-
assert_equal 1, firm.clients(true).size, "New firm should have reloaded clients count"
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_storing_in_pstore
|
45
|
-
store_filename = "/tmp/ar-pstore-association-test"
|
46
|
-
File.delete(store_filename) if File.exists?(store_filename)
|
47
|
-
require "pstore"
|
48
|
-
apple = Firm.create("name" => "Apple")
|
49
|
-
natural = Client.new("name" => "Natural Company")
|
50
|
-
apple.clients << natural
|
51
|
-
|
52
|
-
db = PStore.new(store_filename)
|
53
|
-
db.transaction do
|
54
|
-
db["apple"] = apple
|
55
|
-
end
|
56
|
-
|
57
|
-
db = PStore.new(store_filename)
|
58
|
-
db.transaction do
|
59
|
-
assert_equal "Natural Company", db["apple"].clients.first.name
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class HasOneAssociationsTest < Test::Unit::TestCase
|
65
|
-
def setup
|
66
|
-
create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
|
67
|
-
@signals37 = Firm.find(1)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_has_one
|
71
|
-
assert_equal @signals37.account, Account.find(1)
|
72
|
-
assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
|
73
|
-
assert @signals37.has_account?, "37signals should have an account"
|
74
|
-
assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
|
75
|
-
assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
|
76
|
-
|
77
|
-
assert !Account.find(2).has_firm?, "Unknown isn't linked"
|
78
|
-
assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_type_mismatch
|
82
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.account = 1 }
|
83
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.account = Project.find(1) }
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_natural_assignment
|
87
|
-
apple = Firm.create("name" => "Apple")
|
88
|
-
citibank = Account.create("credit_limit" => 10)
|
89
|
-
apple.account = citibank
|
90
|
-
assert_equal apple.id, citibank.firm_id
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_natural_assignment_to_nil
|
94
|
-
old_account_id = @signals37.account.id
|
95
|
-
@signals37.account = nil
|
96
|
-
@signals37.save
|
97
|
-
assert_nil @signals37.account
|
98
|
-
assert_nil Account.find(old_account_id).firm_id
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_build
|
102
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
103
|
-
firm.save
|
104
|
-
|
105
|
-
account = firm.build_account("credit_limit" => 1000)
|
106
|
-
assert account.save
|
107
|
-
assert_equal account, firm.account
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_failing_build_association
|
111
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
112
|
-
firm.save
|
113
|
-
|
114
|
-
account = firm.build_account
|
115
|
-
assert !account.save
|
116
|
-
assert_equal "can't be empty", account.errors.on("credit_limit")
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_create
|
120
|
-
firm = Firm.new("name" => "GlobalMegaCorp")
|
121
|
-
firm.save
|
122
|
-
assert_equal firm.create_account("credit_limit" => 1000), firm.account
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_dependence
|
126
|
-
firm = Firm.find(1)
|
127
|
-
assert !firm.account.nil?
|
128
|
-
firm.destroy
|
129
|
-
assert_equal 1, Account.find_all.length
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_dependence_with_missing_association
|
133
|
-
Account.destroy_all
|
134
|
-
firm = Firm.find(1)
|
135
|
-
assert !firm.has_account?
|
136
|
-
firm.destroy
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
|
141
|
-
class HasManyAssociationsTest < Test::Unit::TestCase
|
142
|
-
def setup
|
143
|
-
create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
|
144
|
-
@signals37 = Firm.find(1)
|
145
|
-
end
|
146
|
-
|
147
|
-
def force_signal37_to_load_all_clients_of_firm
|
148
|
-
@signals37.clients_of_firm.each {|f| }
|
149
|
-
end
|
150
|
-
|
151
|
-
def test_finding
|
152
|
-
assert_equal 2, Firm.find_first.clients.length
|
153
|
-
end
|
154
|
-
|
155
|
-
def test_finding_default_orders
|
156
|
-
assert_equal "Summit", Firm.find_first.clients.first.name
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_finding_with_different_class_name_and_order
|
160
|
-
assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_finding_with_foreign_key
|
164
|
-
assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_finding_with_condition
|
168
|
-
assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_finding_using_sql
|
172
|
-
firm = Firm.find_first
|
173
|
-
firm.clients_using_sql.first
|
174
|
-
assert_equal "Microsoft", firm.clients_using_sql.first.name
|
175
|
-
assert_equal 1, firm.clients_using_sql.size
|
176
|
-
assert_equal 1, Firm.find_first.clients_using_sql.size
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_find_all
|
180
|
-
assert_equal 2, Firm.find_first.clients.find_all("type = 'Client'").length
|
181
|
-
assert_equal 1, Firm.find_first.clients.find_all("name = 'Summit'").length
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_find_in_collection
|
185
|
-
assert_equal Client.find(2).name, @signals37.clients.find(2).name
|
186
|
-
assert_equal Client.find(2).name, @signals37.clients.find {|c| c.name == @signals37.clients.find(2).name }.name
|
187
|
-
assert_raises(ActiveRecord::RecordNotFound) { @signals37.clients.find(6) }
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_adding
|
191
|
-
force_signal37_to_load_all_clients_of_firm
|
192
|
-
natural = Client.new("name" => "Natural Company")
|
193
|
-
@signals37.clients_of_firm << natural
|
194
|
-
assert_equal 2, @signals37.clients_of_firm.size # checking via the collection
|
195
|
-
assert_equal 2, @signals37.clients_of_firm(true).size # checking using the db
|
196
|
-
assert_equal natural, @signals37.clients_of_firm.last
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_adding_a_mismatch_class
|
200
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << nil }
|
201
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << 1 }
|
202
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << Topic.find(1) }
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_adding_a_collection
|
206
|
-
force_signal37_to_load_all_clients_of_firm
|
207
|
-
@signals37.clients_of_firm.concat(Client.new("name" => "Natural Company"), Client.new("name" => "Apple"))
|
208
|
-
assert_equal 3, @signals37.clients_of_firm.size
|
209
|
-
assert_equal 3, @signals37.clients_of_firm(true).size
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_build
|
213
|
-
new_client = @signals37.clients_of_firm.build("name" => "Another Client")
|
214
|
-
assert_equal "Another Client", new_client.name
|
215
|
-
assert new_client.save
|
216
|
-
assert_equal 2, @signals37.clients_of_firm(true).size
|
217
|
-
end
|
218
|
-
|
219
|
-
def test_create
|
220
|
-
force_signal37_to_load_all_clients_of_firm
|
221
|
-
new_client = @signals37.clients_of_firm.create("name" => "Another Client")
|
222
|
-
assert_equal new_client, @signals37.clients_of_firm.last
|
223
|
-
assert_equal new_client, @signals37.clients_of_firm(true).last
|
224
|
-
end
|
225
|
-
|
226
|
-
def test_deleting
|
227
|
-
force_signal37_to_load_all_clients_of_firm
|
228
|
-
@signals37.clients_of_firm.delete(@signals37.clients_of_firm.first)
|
229
|
-
assert_equal 0, @signals37.clients_of_firm.size
|
230
|
-
assert_equal 0, @signals37.clients_of_firm(true).size
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_deleting_a_collection
|
234
|
-
force_signal37_to_load_all_clients_of_firm
|
235
|
-
@signals37.clients_of_firm.create("name" => "Another Client")
|
236
|
-
assert_equal 2, @signals37.clients_of_firm.size
|
237
|
-
@signals37.clients_of_firm.delete([@signals37.clients_of_firm[0], @signals37.clients_of_firm[1]])
|
238
|
-
assert_equal 0, @signals37.clients_of_firm.size
|
239
|
-
assert_equal 0, @signals37.clients_of_firm(true).size
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_deleting_a_association_collection
|
243
|
-
force_signal37_to_load_all_clients_of_firm
|
244
|
-
@signals37.clients_of_firm.create("name" => "Another Client")
|
245
|
-
assert_equal 2, @signals37.clients_of_firm.size
|
246
|
-
@signals37.clients_of_firm.delete(@signals37.clients_of_firm)
|
247
|
-
assert_equal 0, @signals37.clients_of_firm.size
|
248
|
-
assert_equal 0, @signals37.clients_of_firm(true).size
|
249
|
-
end
|
250
|
-
|
251
|
-
def test_deleting_a_item_which_is_not_in_the_collection
|
252
|
-
force_signal37_to_load_all_clients_of_firm
|
253
|
-
summit = Client.find_first("name = 'Summit'")
|
254
|
-
@signals37.clients_of_firm.delete(summit)
|
255
|
-
assert_equal 1, @signals37.clients_of_firm.size
|
256
|
-
assert_equal 1, @signals37.clients_of_firm(true).size
|
257
|
-
assert_equal 2, summit.client_of
|
258
|
-
end
|
259
|
-
|
260
|
-
def test_destroy_all
|
261
|
-
force_signal37_to_load_all_clients_of_firm
|
262
|
-
assert !@signals37.clients_of_firm.empty?
|
263
|
-
@signals37.clients_of_firm.destroy_all
|
264
|
-
assert @signals37.clients_of_firm.empty?
|
265
|
-
assert @signals37.clients_of_firm(true).empty?
|
266
|
-
end
|
267
|
-
|
268
|
-
def test_dependence
|
269
|
-
assert_equal 2, Client.find_all.length
|
270
|
-
Firm.find_first.destroy
|
271
|
-
assert_equal 0, Client.find_all.length
|
272
|
-
end
|
273
|
-
|
274
|
-
def test_dependence_with_transaction_support_on_failure
|
275
|
-
assert_equal 2, Client.find_all.length
|
276
|
-
firm = Firm.find_first
|
277
|
-
clients = firm.clients
|
278
|
-
clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
|
279
|
-
|
280
|
-
firm.destroy rescue "do nothing"
|
281
|
-
|
282
|
-
assert_equal 2, Client.find_all.length
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_dependence_on_account
|
286
|
-
assert_equal 2, Account.find_all.length
|
287
|
-
@signals37.destroy
|
288
|
-
assert_equal 1, Account.find_all.length
|
289
|
-
end
|
290
|
-
|
291
|
-
def test_included_in_collection
|
292
|
-
assert @signals37.clients.include?(Client.find(2))
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
class BelongsToAssociationsTest < Test::Unit::TestCase
|
297
|
-
def setup
|
298
|
-
create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
|
299
|
-
@signals37 = Firm.find(1)
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_belongs_to
|
303
|
-
Client.find(3).firm.name
|
304
|
-
assert_equal @signals37.name, Client.find(3).firm.name
|
305
|
-
assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_type_mismatch
|
309
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
|
310
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
|
311
|
-
end
|
312
|
-
|
313
|
-
def test_natural_assignment
|
314
|
-
apple = Firm.create("name" => "Apple")
|
315
|
-
citibank = Account.create("credit_limit" => 10)
|
316
|
-
citibank.firm = apple
|
317
|
-
assert_equal apple.id, citibank.firm_id
|
318
|
-
end
|
319
|
-
|
320
|
-
def test_natural_assignment_to_nil
|
321
|
-
client = Client.find(3)
|
322
|
-
client.firm = nil
|
323
|
-
client.save
|
324
|
-
assert_nil client.firm(true)
|
325
|
-
assert_nil client.client_of
|
326
|
-
end
|
327
|
-
|
328
|
-
def test_with_different_class_name
|
329
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
|
330
|
-
assert !Company.find(3).firm_with_other_name.empty?, "Microsoft should have a firm"
|
331
|
-
end
|
332
|
-
|
333
|
-
def test_with_condition
|
334
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
|
335
|
-
assert !Company.find(3).firm_with_condition.empty?, "Microsoft should have a firm"
|
336
|
-
end
|
337
|
-
|
338
|
-
def test_belongs_to_counter
|
339
|
-
debate = Topic.create("title" => "debate")
|
340
|
-
assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
|
341
|
-
|
342
|
-
trash = debate.replies.create("title" => "blah!", "content" => "world around!")
|
343
|
-
assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
|
344
|
-
|
345
|
-
trash.destroy
|
346
|
-
assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
|
347
|
-
end
|
348
|
-
|
349
|
-
def xtest_counter_cache
|
350
|
-
apple = Firm.create("name" => "Apple")
|
351
|
-
final_cut = apple.clients.create("name" => "Final Cut")
|
352
|
-
|
353
|
-
apple.clients.to_s
|
354
|
-
assert_equal 1, apple.clients.size, "Created one client"
|
355
|
-
|
356
|
-
apple.companies_count = 2
|
357
|
-
apple.save
|
358
|
-
|
359
|
-
apple = Firm.find_first("name = 'Apple'")
|
360
|
-
assert_equal 2, apple.clients.size, "Should use the new cached number"
|
361
|
-
|
362
|
-
apple.clients.to_s
|
363
|
-
assert_equal 1, apple.clients.size, "Should not use the cached number, but go to the database"
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
|
368
|
-
class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
|
369
|
-
def setup
|
370
|
-
create_fixtures "accounts"
|
371
|
-
create_fixtures "companies"
|
372
|
-
create_fixtures "accounts"
|
373
|
-
create_fixtures "developers"
|
374
|
-
create_fixtures "projects"
|
375
|
-
create_fixtures "developers_projects"
|
376
|
-
@signals37 = Firm.find(1)
|
377
|
-
end
|
378
|
-
|
379
|
-
def test_has_and_belongs_to_many
|
380
|
-
david = Developer.find(1)
|
381
|
-
|
382
|
-
assert !david.projects.empty?
|
383
|
-
assert_equal 2, david.projects.size
|
384
|
-
|
385
|
-
active_record = Project.find(1)
|
386
|
-
assert !active_record.developers.empty?
|
387
|
-
assert_equal 2, active_record.developers.size
|
388
|
-
assert_equal david.name, active_record.developers.first.name
|
389
|
-
end
|
390
|
-
|
391
|
-
def test_addings
|
392
|
-
jamis = Developer.find(2)
|
393
|
-
jamis.projects.id # causing the collection to load
|
394
|
-
action_controller = Project.find(2)
|
395
|
-
assert_equal 1, jamis.projects.size
|
396
|
-
assert_equal 1, action_controller.developers.size
|
397
|
-
|
398
|
-
jamis.projects << action_controller
|
399
|
-
|
400
|
-
assert_equal 2, jamis.projects.size
|
401
|
-
assert_equal 2, jamis.projects(true).size
|
402
|
-
assert_equal 2, action_controller.developers(true).size
|
403
|
-
end
|
404
|
-
|
405
|
-
def test_adding_type_mismatch
|
406
|
-
jamis = Developer.find(2)
|
407
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
|
408
|
-
assert_raises(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_adding_from_the_project
|
412
|
-
jamis = Developer.find(2)
|
413
|
-
action_controller = Project.find(2)
|
414
|
-
action_controller.developers.id
|
415
|
-
assert_equal 1, jamis.projects.size
|
416
|
-
assert_equal 1, action_controller.developers.size
|
417
|
-
|
418
|
-
action_controller.developers << jamis
|
419
|
-
|
420
|
-
assert_equal 2, jamis.projects(true).size
|
421
|
-
assert_equal 2, action_controller.developers.size
|
422
|
-
assert_equal 2, action_controller.developers(true).size
|
423
|
-
end
|
424
|
-
|
425
|
-
def test_adding_a_collection
|
426
|
-
aridridel = Developer.new("name" => "Aridridel")
|
427
|
-
aridridel.save
|
428
|
-
aridridel.projects.id
|
429
|
-
aridridel.projects.concat([ Project.find(1), Project.find(2) ])
|
430
|
-
assert_equal 2, aridridel.projects.size
|
431
|
-
assert_equal 2, aridridel.projects(true).size
|
432
|
-
end
|
433
|
-
|
434
|
-
def test_deleting
|
435
|
-
david = Developer.find(1)
|
436
|
-
active_record = Project.find(1)
|
437
|
-
david.projects.id
|
438
|
-
assert_equal 2, david.projects.size
|
439
|
-
assert_equal 2, active_record.developers.size
|
440
|
-
|
441
|
-
david.projects.delete(active_record)
|
442
|
-
|
443
|
-
assert_equal 1, david.projects.size
|
444
|
-
assert_equal 1, david.projects(true).size
|
445
|
-
assert_equal 1, active_record.developers(true).size
|
446
|
-
end
|
447
|
-
|
448
|
-
def test_deleting_a_collection
|
449
|
-
david = Developer.find(1)
|
450
|
-
david.projects.id
|
451
|
-
david.projects.delete(Project.find_all)
|
452
|
-
assert_equal 0, david.projects.size
|
453
|
-
assert_equal 0, david.projects(true).size
|
454
|
-
end
|
455
|
-
|
456
|
-
def test_deleting_a_association_collection
|
457
|
-
david = Developer.find(1)
|
458
|
-
david.projects.id
|
459
|
-
david.projects.delete(david.projects)
|
460
|
-
assert_equal 0, david.projects.size
|
461
|
-
assert_equal 0, david.projects(true).size
|
462
|
-
end
|
463
|
-
|
464
|
-
def test_removing_associations_on_destroy
|
465
|
-
Developer.find(1).destroy
|
466
|
-
assert Developer.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = '1'").empty?
|
467
|
-
end
|
468
|
-
|
469
|
-
def test_destroy_all
|
470
|
-
david = Developer.find(1)
|
471
|
-
david.projects.id
|
472
|
-
assert !david.projects.empty?
|
473
|
-
david.projects.destroy_all
|
474
|
-
assert david.projects.empty?
|
475
|
-
assert david.projects(true).empty?
|
476
|
-
end
|
477
|
-
end
|