datamapper-dm-core 0.9.11 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +17 -14
- data/.gitignore +3 -1
- data/FAQ +6 -5
- data/History.txt +5 -39
- data/Manifest.txt +67 -76
- data/QUICKLINKS +1 -1
- data/README.txt +21 -15
- data/Rakefile +16 -15
- data/SPECS +2 -29
- data/TODO +1 -1
- data/dm-core.gemspec +11 -15
- data/lib/dm-core/adapters/abstract_adapter.rb +182 -185
- data/lib/dm-core/adapters/data_objects_adapter.rb +482 -534
- data/lib/dm-core/adapters/in_memory_adapter.rb +90 -69
- data/lib/dm-core/adapters/mysql_adapter.rb +22 -115
- data/lib/dm-core/adapters/oracle_adapter.rb +249 -0
- data/lib/dm-core/adapters/postgres_adapter.rb +7 -173
- data/lib/dm-core/adapters/sqlite3_adapter.rb +4 -97
- data/lib/dm-core/adapters/yaml_adapter.rb +116 -0
- data/lib/dm-core/adapters.rb +135 -16
- data/lib/dm-core/associations/many_to_many.rb +372 -90
- data/lib/dm-core/associations/many_to_one.rb +220 -73
- data/lib/dm-core/associations/one_to_many.rb +319 -255
- data/lib/dm-core/associations/one_to_one.rb +66 -53
- data/lib/dm-core/associations/relationship.rb +560 -158
- data/lib/dm-core/collection.rb +1104 -381
- data/lib/dm-core/core_ext/kernel.rb +12 -0
- data/lib/dm-core/core_ext/symbol.rb +10 -0
- data/lib/dm-core/identity_map.rb +4 -34
- data/lib/dm-core/migrations.rb +1283 -0
- data/lib/dm-core/model/descendant_set.rb +81 -0
- data/lib/dm-core/model/hook.rb +45 -0
- data/lib/dm-core/model/is.rb +32 -0
- data/lib/dm-core/model/property.rb +248 -0
- data/lib/dm-core/model/relationship.rb +335 -0
- data/lib/dm-core/model/scope.rb +90 -0
- data/lib/dm-core/model.rb +570 -369
- data/lib/dm-core/property.rb +753 -280
- data/lib/dm-core/property_set.rb +141 -98
- data/lib/dm-core/query/conditions/comparison.rb +814 -0
- data/lib/dm-core/query/conditions/operation.rb +247 -0
- data/lib/dm-core/query/direction.rb +43 -0
- data/lib/dm-core/query/operator.rb +42 -0
- data/lib/dm-core/query/path.rb +102 -0
- data/lib/dm-core/query/sort.rb +45 -0
- data/lib/dm-core/query.rb +974 -492
- data/lib/dm-core/repository.rb +147 -107
- data/lib/dm-core/resource.rb +644 -429
- data/lib/dm-core/spec/adapter_shared_spec.rb +294 -0
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +106 -0
- data/lib/dm-core/support/chainable.rb +20 -0
- data/lib/dm-core/support/deprecate.rb +12 -0
- data/lib/dm-core/support/equalizer.rb +23 -0
- data/lib/dm-core/support/logger.rb +13 -0
- data/lib/dm-core/{naming_conventions.rb → support/naming_conventions.rb} +6 -6
- data/lib/dm-core/transaction.rb +333 -92
- data/lib/dm-core/type.rb +98 -60
- data/lib/dm-core/types/boolean.rb +1 -1
- data/lib/dm-core/types/discriminator.rb +34 -20
- data/lib/dm-core/types/object.rb +7 -4
- data/lib/dm-core/types/paranoid_boolean.rb +11 -9
- data/lib/dm-core/types/paranoid_datetime.rb +11 -9
- data/lib/dm-core/types/serial.rb +3 -3
- data/lib/dm-core/types/text.rb +3 -4
- data/lib/dm-core/version.rb +1 -1
- data/lib/dm-core.rb +106 -110
- data/script/performance.rb +102 -109
- data/script/profile.rb +169 -38
- data/spec/lib/adapter_helpers.rb +105 -0
- data/spec/lib/collection_helpers.rb +18 -0
- data/spec/lib/counter_adapter.rb +34 -0
- data/spec/lib/pending_helpers.rb +27 -0
- data/spec/lib/rspec_immediate_feedback_formatter.rb +53 -0
- data/spec/public/associations/many_to_many_spec.rb +193 -0
- data/spec/public/associations/many_to_one_spec.rb +73 -0
- data/spec/public/associations/one_to_many_spec.rb +77 -0
- data/spec/public/associations/one_to_one_spec.rb +156 -0
- data/spec/public/collection_spec.rb +65 -0
- data/spec/public/model/relationship_spec.rb +924 -0
- data/spec/public/model_spec.rb +159 -0
- data/spec/public/property_spec.rb +829 -0
- data/spec/public/resource_spec.rb +71 -0
- data/spec/public/sel_spec.rb +44 -0
- data/spec/public/setup_spec.rb +145 -0
- data/spec/public/shared/association_collection_shared_spec.rb +317 -0
- data/spec/public/shared/collection_shared_spec.rb +1723 -0
- data/spec/public/shared/finder_shared_spec.rb +1619 -0
- data/spec/public/shared/resource_shared_spec.rb +924 -0
- data/spec/public/shared/sel_shared_spec.rb +112 -0
- data/spec/public/transaction_spec.rb +129 -0
- data/spec/public/types/discriminator_spec.rb +130 -0
- data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
- data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
- data/spec/semipublic/adapters/mysql_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/oracle_adapter_spec.rb +194 -0
- data/spec/semipublic/adapters/postgres_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +17 -0
- data/spec/semipublic/adapters/yaml_adapter_spec.rb +12 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +53 -0
- data/spec/semipublic/associations/relationship_spec.rb +194 -0
- data/spec/semipublic/associations_spec.rb +177 -0
- data/spec/semipublic/collection_spec.rb +142 -0
- data/spec/semipublic/property_spec.rb +61 -0
- data/spec/semipublic/query/conditions_spec.rb +528 -0
- data/spec/semipublic/query/path_spec.rb +443 -0
- data/spec/semipublic/query_spec.rb +2626 -0
- data/spec/semipublic/resource_spec.rb +47 -0
- data/spec/semipublic/shared/resource_shared_spec.rb +126 -0
- data/spec/spec.opts +3 -1
- data/spec/spec_helper.rb +80 -57
- data/tasks/ci.rb +19 -31
- data/tasks/dm.rb +43 -48
- data/tasks/doc.rb +8 -11
- data/tasks/gemspec.rb +5 -5
- data/tasks/hoe.rb +15 -16
- data/tasks/install.rb +8 -10
- metadata +72 -93
- data/lib/dm-core/associations/relationship_chain.rb +0 -81
- data/lib/dm-core/associations.rb +0 -207
- data/lib/dm-core/auto_migrations.rb +0 -105
- data/lib/dm-core/dependency_queue.rb +0 -32
- data/lib/dm-core/hook.rb +0 -11
- data/lib/dm-core/is.rb +0 -16
- data/lib/dm-core/logger.rb +0 -232
- data/lib/dm-core/migrations/destructive_migrations.rb +0 -17
- data/lib/dm-core/migrator.rb +0 -29
- data/lib/dm-core/scope.rb +0 -58
- data/lib/dm-core/support/array.rb +0 -13
- data/lib/dm-core/support/assertions.rb +0 -8
- data/lib/dm-core/support/errors.rb +0 -23
- data/lib/dm-core/support/kernel.rb +0 -11
- data/lib/dm-core/support/symbol.rb +0 -41
- data/lib/dm-core/support.rb +0 -7
- data/lib/dm-core/type_map.rb +0 -80
- data/lib/dm-core/types.rb +0 -19
- data/script/all +0 -4
- data/spec/integration/association_spec.rb +0 -1382
- data/spec/integration/association_through_spec.rb +0 -203
- data/spec/integration/associations/many_to_many_spec.rb +0 -449
- data/spec/integration/associations/many_to_one_spec.rb +0 -163
- data/spec/integration/associations/one_to_many_spec.rb +0 -188
- data/spec/integration/auto_migrations_spec.rb +0 -413
- data/spec/integration/collection_spec.rb +0 -1073
- data/spec/integration/data_objects_adapter_spec.rb +0 -32
- data/spec/integration/dependency_queue_spec.rb +0 -46
- data/spec/integration/model_spec.rb +0 -197
- data/spec/integration/mysql_adapter_spec.rb +0 -85
- data/spec/integration/postgres_adapter_spec.rb +0 -731
- data/spec/integration/property_spec.rb +0 -253
- data/spec/integration/query_spec.rb +0 -514
- data/spec/integration/repository_spec.rb +0 -61
- data/spec/integration/resource_spec.rb +0 -513
- data/spec/integration/sqlite3_adapter_spec.rb +0 -352
- data/spec/integration/sti_spec.rb +0 -273
- data/spec/integration/strategic_eager_loading_spec.rb +0 -156
- data/spec/integration/transaction_spec.rb +0 -75
- data/spec/integration/type_spec.rb +0 -275
- data/spec/lib/logging_helper.rb +0 -18
- data/spec/lib/mock_adapter.rb +0 -27
- data/spec/lib/model_loader.rb +0 -100
- data/spec/lib/publicize_methods.rb +0 -28
- data/spec/models/content.rb +0 -16
- data/spec/models/vehicles.rb +0 -34
- data/spec/models/zoo.rb +0 -48
- data/spec/unit/adapters/abstract_adapter_spec.rb +0 -133
- data/spec/unit/adapters/adapter_shared_spec.rb +0 -15
- data/spec/unit/adapters/data_objects_adapter_spec.rb +0 -632
- data/spec/unit/adapters/in_memory_adapter_spec.rb +0 -98
- data/spec/unit/adapters/postgres_adapter_spec.rb +0 -133
- data/spec/unit/associations/many_to_many_spec.rb +0 -32
- data/spec/unit/associations/many_to_one_spec.rb +0 -159
- data/spec/unit/associations/one_to_many_spec.rb +0 -393
- data/spec/unit/associations/one_to_one_spec.rb +0 -7
- data/spec/unit/associations/relationship_spec.rb +0 -71
- data/spec/unit/associations_spec.rb +0 -242
- data/spec/unit/auto_migrations_spec.rb +0 -111
- data/spec/unit/collection_spec.rb +0 -182
- data/spec/unit/data_mapper_spec.rb +0 -35
- data/spec/unit/identity_map_spec.rb +0 -126
- data/spec/unit/is_spec.rb +0 -80
- data/spec/unit/migrator_spec.rb +0 -33
- data/spec/unit/model_spec.rb +0 -321
- data/spec/unit/naming_conventions_spec.rb +0 -36
- data/spec/unit/property_set_spec.rb +0 -90
- data/spec/unit/property_spec.rb +0 -753
- data/spec/unit/query_spec.rb +0 -571
- data/spec/unit/repository_spec.rb +0 -93
- data/spec/unit/resource_spec.rb +0 -649
- data/spec/unit/scope_spec.rb +0 -142
- data/spec/unit/transaction_spec.rb +0 -493
- data/spec/unit/type_map_spec.rb +0 -114
- data/spec/unit/type_spec.rb +0 -119
@@ -1,393 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe DataMapper::Associations::OneToMany do
|
4
|
-
|
5
|
-
load_models_for_metaphor :vehicles
|
6
|
-
|
7
|
-
before do
|
8
|
-
@class = Class.new do
|
9
|
-
def self.name
|
10
|
-
'User'
|
11
|
-
end
|
12
|
-
|
13
|
-
include DataMapper::Resource
|
14
|
-
|
15
|
-
property :user_id, Integer, :key => true
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should provide #has' do
|
20
|
-
@class.should respond_to(:has)
|
21
|
-
end
|
22
|
-
|
23
|
-
describe '#has' do
|
24
|
-
it 'should return a Relationship' do
|
25
|
-
@class.has(@class.n, :orders).should be_kind_of(DataMapper::Associations::Relationship)
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'relationship' do
|
29
|
-
before do
|
30
|
-
@relationship = mock('relationship')
|
31
|
-
DataMapper::Associations::Relationship.stub!(:new).and_return(@relationship)
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should receive the name' do
|
35
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |name,_,_,_,_|
|
36
|
-
name.should == :orders
|
37
|
-
end
|
38
|
-
@class.has(@class.n, :orders)
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should receive the repository name' do
|
42
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |_,repository_name,_,_,_|
|
43
|
-
repository_name.should == :mock
|
44
|
-
end
|
45
|
-
repository(:mock) do
|
46
|
-
@class.has(@class.n, :orders)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'should receive the child model name when passed in as class_name' do
|
51
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,child_model_name,_,_|
|
52
|
-
child_model_name.should == 'Company::Order'
|
53
|
-
end
|
54
|
-
@class.has(@class.n, :orders, :class_name => 'Company::Order')
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'should receive the child model name when class_name not passed in' do
|
58
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,child_model_name,_,_|
|
59
|
-
child_model_name.should == 'Order'
|
60
|
-
end
|
61
|
-
@class.has(@class.n, :orders)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'should receive the parent model name' do
|
65
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,_,parent_model_name,_|
|
66
|
-
parent_model_name.should == @class
|
67
|
-
end
|
68
|
-
@class.has(@class.n, :orders)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'should receive the parent model name' do
|
72
|
-
options = { :min => 0, :max => 100 }
|
73
|
-
DataMapper::Associations::Relationship.should_receive(:new) do |_,_,_,parent_model_name,_|
|
74
|
-
options.object_id.should == options.object_id
|
75
|
-
end
|
76
|
-
@class.has(@class.n, :orders, options)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'should add an accessor for the proxy' do
|
81
|
-
@class.new.should_not respond_to(:orders)
|
82
|
-
@class.has(@class.n, :orders)
|
83
|
-
@class.new.should respond_to(:orders)
|
84
|
-
end
|
85
|
-
|
86
|
-
describe 'proxy accessor' do
|
87
|
-
before :all do
|
88
|
-
class ::User
|
89
|
-
include DataMapper::Resource
|
90
|
-
end
|
91
|
-
|
92
|
-
class ::Order
|
93
|
-
include DataMapper::Resource
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'should return a OneToMany::Proxy' do
|
98
|
-
@class.has(@class.n, :orders)
|
99
|
-
@class.new.orders.should be_kind_of(DataMapper::Associations::OneToMany::Proxy)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'should work with classes inside modules'
|
105
|
-
end
|
106
|
-
|
107
|
-
describe DataMapper::Associations::OneToMany::Proxy do
|
108
|
-
before do
|
109
|
-
@parent = mock('parent', :new_record? => true, :kind_of? => true)
|
110
|
-
@resource = mock('resource', :null_object => true)
|
111
|
-
@collection = []
|
112
|
-
@parent_key = mock('parent key', :get => [])
|
113
|
-
@repository = mock('repository', :save => nil, :kind_of? => true)
|
114
|
-
@relationship = mock('relationship', :get_children => @collection, :query => {}, :kind_of? => true, :child_key => [], :parent_key => @parent_key)
|
115
|
-
@association = DataMapper::Associations::OneToMany::Proxy.new(@relationship, @parent)
|
116
|
-
end
|
117
|
-
|
118
|
-
describe 'a method that relates the resource', :shared => true do
|
119
|
-
it 'should add the resource to the collection' do
|
120
|
-
@association.should_not include(@resource)
|
121
|
-
do_add.should == return_value
|
122
|
-
@association.should include(@resource)
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'should not automatically save that the resource was added to the association' do
|
126
|
-
@relationship.should_not_receive(:attach_parent)
|
127
|
-
do_add.should == return_value
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'should persist the addition after saving the association' do
|
131
|
-
@relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
|
132
|
-
do_add.should == return_value
|
133
|
-
@relationship.should_receive(:attach_parent).with(@resource, @parent)
|
134
|
-
@association.save
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
describe 'a method that orphans the resource', :shared => true do
|
139
|
-
before do
|
140
|
-
@association << @resource
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'should remove the resource from the collection' do
|
144
|
-
@association.should include(@resource)
|
145
|
-
do_remove.should == return_value
|
146
|
-
@association.should_not include(@resource)
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'should not automatically save that the resource was removed from the association' do
|
150
|
-
@relationship.should_not_receive(:attach_parent)
|
151
|
-
do_remove.should == return_value
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'should persist the removal after saving the association' do
|
155
|
-
@relationship.should_receive(:with_repository).with(@resource).and_yield(@repository)
|
156
|
-
do_remove.should == return_value
|
157
|
-
@relationship.should_receive(:attach_parent).with(@resource, nil)
|
158
|
-
@association.save
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'should provide #<<' do
|
163
|
-
@association.should respond_to(:<<)
|
164
|
-
end
|
165
|
-
|
166
|
-
describe '#<<' do
|
167
|
-
def do_add
|
168
|
-
@association << @resource
|
169
|
-
end
|
170
|
-
|
171
|
-
def return_value
|
172
|
-
@association
|
173
|
-
end
|
174
|
-
|
175
|
-
it_should_behave_like 'a method that relates the resource'
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'should provide #push' do
|
179
|
-
@association.should respond_to(:push)
|
180
|
-
end
|
181
|
-
|
182
|
-
describe '#push' do
|
183
|
-
def do_add
|
184
|
-
@association.push(@resource)
|
185
|
-
end
|
186
|
-
|
187
|
-
def return_value
|
188
|
-
@association
|
189
|
-
end
|
190
|
-
|
191
|
-
it_should_behave_like 'a method that relates the resource'
|
192
|
-
end
|
193
|
-
|
194
|
-
it 'should provide #unshift' do
|
195
|
-
@association.should respond_to(:unshift)
|
196
|
-
end
|
197
|
-
|
198
|
-
describe '#unshift' do
|
199
|
-
def do_add
|
200
|
-
@association.unshift(@resource)
|
201
|
-
end
|
202
|
-
|
203
|
-
def return_value
|
204
|
-
@association
|
205
|
-
end
|
206
|
-
|
207
|
-
it_should_behave_like 'a method that relates the resource'
|
208
|
-
end
|
209
|
-
|
210
|
-
it 'should provide #replace' do
|
211
|
-
@association.should respond_to(:replace)
|
212
|
-
end
|
213
|
-
|
214
|
-
describe '#replace' do
|
215
|
-
before do
|
216
|
-
@children = [
|
217
|
-
mock('child 1', :save => true),
|
218
|
-
mock('child 2', :save => true),
|
219
|
-
]
|
220
|
-
@collection << @resource
|
221
|
-
@collection.stub!(:loaded?).and_return(true)
|
222
|
-
@relationship.stub!(:attach_parent)
|
223
|
-
end
|
224
|
-
|
225
|
-
def do_replace
|
226
|
-
@association.replace(@children)
|
227
|
-
end
|
228
|
-
|
229
|
-
def return_value
|
230
|
-
@association
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'should remove the resource from the collection' do
|
234
|
-
@association.should include(@resource)
|
235
|
-
do_replace.should == return_value
|
236
|
-
@association.should_not include(@resource)
|
237
|
-
end
|
238
|
-
|
239
|
-
it 'should not automatically save that the resource was removed from the association' do
|
240
|
-
@relationship.should_not_receive(:attach_parent)
|
241
|
-
do_replace.should == return_value
|
242
|
-
end
|
243
|
-
|
244
|
-
it 'should persist the removal after saving the association' do
|
245
|
-
do_replace.should == return_value
|
246
|
-
@relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
|
247
|
-
@relationship.should_receive(:attach_parent).with(@resource, nil)
|
248
|
-
@association.save
|
249
|
-
end
|
250
|
-
|
251
|
-
it 'should not automatically save that the children were added to the association' do
|
252
|
-
@relationship.should_not_receive(:attach_parent)
|
253
|
-
do_replace.should == return_value
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'should persist the addition after saving the association' do
|
257
|
-
do_replace.should == return_value
|
258
|
-
@relationship.should_receive(:with_repository).exactly(3).times.and_yield(@repository)
|
259
|
-
@relationship.should_receive(:attach_parent).with(@children[0], @parent)
|
260
|
-
@relationship.should_receive(:attach_parent).with(@children[1], @parent)
|
261
|
-
@association.save
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
it 'should provide #pop' do
|
266
|
-
@association.should respond_to(:pop)
|
267
|
-
end
|
268
|
-
|
269
|
-
describe '#pop' do
|
270
|
-
def do_remove
|
271
|
-
@association.pop
|
272
|
-
end
|
273
|
-
|
274
|
-
def return_value
|
275
|
-
@resource
|
276
|
-
end
|
277
|
-
|
278
|
-
it_should_behave_like 'a method that orphans the resource'
|
279
|
-
end
|
280
|
-
|
281
|
-
it 'should provide #shift' do
|
282
|
-
@association.should respond_to(:shift)
|
283
|
-
end
|
284
|
-
|
285
|
-
describe '#shift' do
|
286
|
-
def do_remove
|
287
|
-
@association.shift
|
288
|
-
end
|
289
|
-
|
290
|
-
def return_value
|
291
|
-
@resource
|
292
|
-
end
|
293
|
-
|
294
|
-
it_should_behave_like 'a method that orphans the resource'
|
295
|
-
end
|
296
|
-
|
297
|
-
it 'should provide #delete' do
|
298
|
-
@association.should respond_to(:delete)
|
299
|
-
end
|
300
|
-
|
301
|
-
describe '#delete' do
|
302
|
-
def do_remove
|
303
|
-
@association.delete(@resource)
|
304
|
-
end
|
305
|
-
|
306
|
-
def return_value
|
307
|
-
@resource
|
308
|
-
end
|
309
|
-
|
310
|
-
it_should_behave_like 'a method that orphans the resource'
|
311
|
-
end
|
312
|
-
|
313
|
-
it 'should provide #delete_at' do
|
314
|
-
@association.should respond_to(:delete_at)
|
315
|
-
end
|
316
|
-
|
317
|
-
describe '#delete_at' do
|
318
|
-
def do_remove
|
319
|
-
@association.delete_at(0)
|
320
|
-
end
|
321
|
-
|
322
|
-
def return_value
|
323
|
-
@resource
|
324
|
-
end
|
325
|
-
|
326
|
-
it_should_behave_like 'a method that orphans the resource'
|
327
|
-
end
|
328
|
-
|
329
|
-
it 'should provide #clear' do
|
330
|
-
@association.should respond_to(:clear)
|
331
|
-
end
|
332
|
-
|
333
|
-
describe '#clear' do
|
334
|
-
def do_remove
|
335
|
-
@association.clear
|
336
|
-
end
|
337
|
-
|
338
|
-
def return_value
|
339
|
-
@association
|
340
|
-
end
|
341
|
-
|
342
|
-
it_should_behave_like 'a method that orphans the resource'
|
343
|
-
|
344
|
-
it 'should empty the collection' do
|
345
|
-
@association << mock('other resource', :new_record? => false)
|
346
|
-
@association.should have(2).entries
|
347
|
-
do_remove
|
348
|
-
@association.should be_empty
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
it 'should provide #reload' do
|
353
|
-
@association.should respond_to(:reload)
|
354
|
-
end
|
355
|
-
|
356
|
-
describe '#reload' do
|
357
|
-
before do
|
358
|
-
@children = [ mock('child 1', :save => true), mock('child 2', :save => true) ]
|
359
|
-
@relationship.stub!(:get_children).and_return(@children)
|
360
|
-
end
|
361
|
-
|
362
|
-
it 'should set the @children ivar to nil' do
|
363
|
-
@association.__send__(:children).should == @children # Sanity check.
|
364
|
-
|
365
|
-
# We can't test the value of the @children instance variable since
|
366
|
-
# #instance_variable_get will be run on @children (thanks to
|
367
|
-
# Proxy#method_missing). Instead, test that Relationship#get_children is
|
368
|
-
# run -- if @children wasn't set to nil, this expectation should fail.
|
369
|
-
@relationship.should_receive(:get_children).once.and_return(@children)
|
370
|
-
@association.reload
|
371
|
-
|
372
|
-
# Trigger #get_children on the relationship.
|
373
|
-
@association.__send__(:children).should == @children
|
374
|
-
end
|
375
|
-
|
376
|
-
it 'should return self' do
|
377
|
-
@association.reload.should be_kind_of(DataMapper::Associations::OneToMany::Proxy)
|
378
|
-
@association.reload.object_id.should == @association.object_id
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
|
-
describe 'when deleting the parent' do
|
383
|
-
it 'should delete all the children without calling destroy if relationship :dependent is :delete_all'
|
384
|
-
|
385
|
-
it 'should destroy all the children if relationship :dependent is :destroy'
|
386
|
-
|
387
|
-
it 'should set the parent key for each child to nil if relationship :dependent is :nullify'
|
388
|
-
|
389
|
-
it 'should restrict the parent from being deleted if a child remains if relationship :dependent is restrict'
|
390
|
-
|
391
|
-
it 'should be restrict by default if relationship :dependent is not specified'
|
392
|
-
end
|
393
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe DataMapper::Associations::Relationship do
|
4
|
-
|
5
|
-
load_models_for_metaphor :vehicles
|
6
|
-
|
7
|
-
it "should describe an association" do
|
8
|
-
belongs_to = DataMapper::Associations::Relationship.new(
|
9
|
-
:manufacturer,
|
10
|
-
:mock,
|
11
|
-
Vehicle,
|
12
|
-
Manufacturer,
|
13
|
-
{ :child_key => [ :manufacturer_id ] }
|
14
|
-
)
|
15
|
-
|
16
|
-
belongs_to.should respond_to(:name)
|
17
|
-
belongs_to.should respond_to(:with_repository)
|
18
|
-
belongs_to.should respond_to(:child_key)
|
19
|
-
belongs_to.should respond_to(:parent_key)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should map properties explicitly when an association method passes them in its options" do
|
23
|
-
belongs_to = DataMapper::Associations::Relationship.new(
|
24
|
-
:manufacturer,
|
25
|
-
:mock,
|
26
|
-
Vehicle,
|
27
|
-
Manufacturer,
|
28
|
-
{ :child_key => [ :manufacturer_id ], :parent_key => [ :id ] }
|
29
|
-
)
|
30
|
-
|
31
|
-
belongs_to.name.should == :manufacturer
|
32
|
-
belongs_to.with_repository do |r|
|
33
|
-
r.name.should == :mock
|
34
|
-
end
|
35
|
-
|
36
|
-
belongs_to.child_key.should be_a_kind_of(DataMapper::PropertySet)
|
37
|
-
belongs_to.parent_key.should be_a_kind_of(DataMapper::PropertySet)
|
38
|
-
|
39
|
-
belongs_to.child_key.to_a.should == Vehicle.properties(:mock).slice(:manufacturer_id)
|
40
|
-
belongs_to.parent_key.to_a.should == Manufacturer.properties(:mock).key
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should infer properties when options aren't passed" do
|
44
|
-
has_many = DataMapper::Associations::Relationship.new(
|
45
|
-
:models,
|
46
|
-
:mock,
|
47
|
-
Vehicle,
|
48
|
-
Manufacturer,
|
49
|
-
{ :child_key => [:model_id] }
|
50
|
-
)
|
51
|
-
|
52
|
-
has_many.name.should == :models
|
53
|
-
has_many.with_repository do |r|
|
54
|
-
r.name.should == :mock
|
55
|
-
end
|
56
|
-
|
57
|
-
has_many.child_key.should be_a_kind_of(DataMapper::PropertySet)
|
58
|
-
has_many.parent_key.should be_a_kind_of(DataMapper::PropertySet)
|
59
|
-
# Vehicle.has n, :models, :class_name => 'Manufacturer', :child_key => "models_id"
|
60
|
-
has_many.child_key.to_a.should == Vehicle.properties(:mock).slice(:model_id)
|
61
|
-
has_many.parent_key.to_a.should == Manufacturer.properties(:mock).key
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should generate child properties with a safe subset of the parent options" do
|
65
|
-
pending
|
66
|
-
# For example, :size would be an option you'd want a generated child Property to copy,
|
67
|
-
# but :serial or :key obviously not. So need to take a good look at Property::OPTIONS to
|
68
|
-
# see what applies and what doesn't.
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
@@ -1,242 +0,0 @@
|
|
1
|
-
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
-
|
3
|
-
describe "DataMapper::Associations" do
|
4
|
-
|
5
|
-
load_models_for_metaphor :vehicles
|
6
|
-
|
7
|
-
before do
|
8
|
-
@relationship = mock(DataMapper::Associations::Relationship)
|
9
|
-
@n = 1.0/0
|
10
|
-
|
11
|
-
Manufacturer.mock_relationship = Vehicle.mock_relationship = @relationship
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "#many_to_one_relationships" do
|
15
|
-
before :all do
|
16
|
-
module ::MTORelationships
|
17
|
-
class A
|
18
|
-
include DataMapper::Resource
|
19
|
-
def self.default_repository_name
|
20
|
-
:a_db
|
21
|
-
end
|
22
|
-
repository(:b_db) do
|
23
|
-
belongs_to :b
|
24
|
-
end
|
25
|
-
repository(:c_db) do
|
26
|
-
belongs_to :c
|
27
|
-
end
|
28
|
-
end
|
29
|
-
class B
|
30
|
-
include DataMapper::Resource
|
31
|
-
def self.default_repository_name
|
32
|
-
:b_db
|
33
|
-
end
|
34
|
-
end
|
35
|
-
class C
|
36
|
-
include DataMapper::Resource
|
37
|
-
def self.default_repository_name
|
38
|
-
:c_db
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
it "should list all relationships that are one-to-many" do
|
44
|
-
MTORelationships::A.many_to_one_relationships.sort_by { |r| r.name.to_s }.should == [MTORelationships::A.relationships(:b_db)[:b], MTORelationships::A.relationships(:c_db)[:c]]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe ".relationships" do
|
49
|
-
class ::B
|
50
|
-
include DataMapper::Resource
|
51
|
-
end
|
52
|
-
|
53
|
-
class ::C
|
54
|
-
include DataMapper::Resource
|
55
|
-
|
56
|
-
repository(:mock) do
|
57
|
-
has 1, :b
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
class ::D
|
62
|
-
include DataMapper::Resource
|
63
|
-
has 1, :b
|
64
|
-
end
|
65
|
-
|
66
|
-
class ::E < D
|
67
|
-
end
|
68
|
-
|
69
|
-
class ::F < D
|
70
|
-
has 1, :a
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should assume the default repository when no arguments are passed" do
|
74
|
-
lambda do
|
75
|
-
C.relationships
|
76
|
-
end.should_not raise_error
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should return the right set of relationships given the repository name" do
|
80
|
-
C.relationships.should be_empty
|
81
|
-
C.relationships(:mock).should_not be_empty
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should return the right set of relationships given the inheritance" do
|
85
|
-
E.relationships.should have(1).entries
|
86
|
-
D.relationships.should have(1).entries
|
87
|
-
F.relationships.should have(2).entries
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe ".has" do
|
92
|
-
|
93
|
-
it "should allow a declaration" do
|
94
|
-
lambda do
|
95
|
-
class ::Manufacturer
|
96
|
-
has 1, :halo_car
|
97
|
-
end
|
98
|
-
end.should_not raise_error
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should not allow a constraint that is not an Integer, Range or Infinity" do
|
102
|
-
lambda do
|
103
|
-
class ::Manufacturer
|
104
|
-
has '1', :halo_car
|
105
|
-
end
|
106
|
-
end.should raise_error(ArgumentError)
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should not allow a constraint where the min is larger than the max" do
|
110
|
-
lambda do
|
111
|
-
class ::Manufacturer
|
112
|
-
has 1..0, :halo_car
|
113
|
-
end
|
114
|
-
end.should raise_error(ArgumentError)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should not allow overwriting of the auto assigned min/max values with keys" do
|
118
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
119
|
-
with(:vehicles, Manufacturer, { :min => 1, :max => 2 }).
|
120
|
-
and_return(@relationship)
|
121
|
-
|
122
|
-
class ::Manufacturer
|
123
|
-
has(1..2, :vehicles, :min => 5, :max => 10).should == mock_relationship
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe "one-to-one syntax" do
|
128
|
-
it "should create a basic one-to-one association with fixed constraint" do
|
129
|
-
DataMapper::Associations::OneToOne.should_receive(:setup).
|
130
|
-
with(:halo_car, Manufacturer, { :min => 1, :max => 1 }).
|
131
|
-
and_return(@relationship)
|
132
|
-
|
133
|
-
class ::Manufacturer
|
134
|
-
has(1, :halo_car).should == mock_relationship
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should create a basic one-to-one association with min/max constraints" do
|
139
|
-
DataMapper::Associations::OneToOne.should_receive(:setup).
|
140
|
-
with(:halo_car, Manufacturer, { :min => 0, :max => 1 }).
|
141
|
-
and_return(@relationship)
|
142
|
-
|
143
|
-
class ::Manufacturer
|
144
|
-
has(0..1, :halo_car).should == mock_relationship
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should create a one-to-one association with options" do
|
149
|
-
DataMapper::Associations::OneToOne.should_receive(:setup).
|
150
|
-
with(:halo_car, Manufacturer, { :min => 1, :max => 1, :class_name => 'Car' }).
|
151
|
-
and_return(@relationship)
|
152
|
-
|
153
|
-
class ::Manufacturer
|
154
|
-
has(1, :halo_car, :class_name => 'Car').should == mock_relationship
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
describe "one-to-many syntax" do
|
160
|
-
it "should create a basic one-to-many association with no constraints" do
|
161
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
162
|
-
with(:vehicles, Manufacturer, { :min => 0, :max => @n }).
|
163
|
-
and_return(@relationship)
|
164
|
-
|
165
|
-
class ::Manufacturer
|
166
|
-
has(n, :vehicles).should == mock_relationship
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
it "should create a one-to-many association with fixed constraint" do
|
171
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
172
|
-
with(:vehicles, Manufacturer, { :min => 4, :max => 4 }).
|
173
|
-
and_return(@relationship)
|
174
|
-
|
175
|
-
class ::Manufacturer
|
176
|
-
has(4, :vehicles).should == mock_relationship
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
it "should create a one-to-many association with min/max constraints" do
|
181
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
182
|
-
with(:vehicles, Manufacturer, { :min => 2, :max => 4 }).
|
183
|
-
and_return(@relationship)
|
184
|
-
|
185
|
-
class ::Manufacturer
|
186
|
-
has(2..4, :vehicles).should == mock_relationship
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
it "should create a one-to-many association with options" do
|
191
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
192
|
-
with(:vehicles, Manufacturer, { :min => 1, :max => @n, :class_name => 'Car' }).
|
193
|
-
and_return(@relationship)
|
194
|
-
|
195
|
-
class ::Manufacturer
|
196
|
-
has(1..n, :vehicles, :class_name => 'Car').should == mock_relationship
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
# do not remove or change this spec.
|
201
|
-
it "should raise an exception when n..n is used for the cardinality" do
|
202
|
-
lambda do
|
203
|
-
class ::Manufacturer
|
204
|
-
has n..n, :subsidiaries, :class_name => 'Manufacturer'
|
205
|
-
end
|
206
|
-
end.should raise_error(ArgumentError)
|
207
|
-
end
|
208
|
-
|
209
|
-
it "should create one-to-many association and pass the :through option if specified" do
|
210
|
-
DataMapper::Associations::OneToMany.should_receive(:setup).
|
211
|
-
with(:suppliers, Vehicle, { :min => 0, :max => @n, :through => :manufacturers }).
|
212
|
-
and_return(@relationship)
|
213
|
-
|
214
|
-
class ::Vehicle
|
215
|
-
has(n, :suppliers, :through => :manufacturers).should == mock_relationship
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe ".belongs_to" do
|
222
|
-
it "should create a basic many-to-one association" do
|
223
|
-
DataMapper::Associations::ManyToOne.should_receive(:setup).
|
224
|
-
with(:vehicle, Manufacturer, {}).
|
225
|
-
and_return(@relationship)
|
226
|
-
|
227
|
-
class ::Manufacturer
|
228
|
-
belongs_to(:vehicle).should == mock_relationship
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
it "should create a many-to-one association with options" do
|
233
|
-
DataMapper::Associations::ManyToOne.should_receive(:setup).
|
234
|
-
with(:vehicle, Manufacturer, :class_name => 'Car', :child_key => [ :car_id ]).
|
235
|
-
and_return(@relationship)
|
236
|
-
|
237
|
-
class ::Manufacturer
|
238
|
-
belongs_to(:vehicle, :class_name => 'Car', :child_key => [ :car_id ]).should == mock_relationship
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|