dm-core 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/.autotest +26 -0
  2. data/{CHANGELOG → History.txt} +78 -77
  3. data/Manifest.txt +123 -0
  4. data/{README → README.txt} +0 -0
  5. data/Rakefile +29 -0
  6. data/SPECS +63 -0
  7. data/TODO +1 -0
  8. data/lib/dm-core.rb +6 -1
  9. data/lib/dm-core/adapters/data_objects_adapter.rb +29 -32
  10. data/lib/dm-core/adapters/mysql_adapter.rb +1 -1
  11. data/lib/dm-core/adapters/postgres_adapter.rb +1 -1
  12. data/lib/dm-core/adapters/sqlite3_adapter.rb +2 -2
  13. data/lib/dm-core/associations.rb +26 -0
  14. data/lib/dm-core/associations/many_to_many.rb +34 -25
  15. data/lib/dm-core/associations/many_to_one.rb +4 -4
  16. data/lib/dm-core/associations/one_to_many.rb +48 -13
  17. data/lib/dm-core/associations/one_to_one.rb +4 -4
  18. data/lib/dm-core/associations/relationship.rb +144 -42
  19. data/lib/dm-core/associations/relationship_chain.rb +31 -24
  20. data/lib/dm-core/auto_migrations.rb +0 -4
  21. data/lib/dm-core/collection.rb +40 -7
  22. data/lib/dm-core/dependency_queue.rb +31 -0
  23. data/lib/dm-core/hook.rb +2 -2
  24. data/lib/dm-core/is.rb +2 -2
  25. data/lib/dm-core/logger.rb +10 -10
  26. data/lib/dm-core/model.rb +94 -41
  27. data/lib/dm-core/property.rb +72 -41
  28. data/lib/dm-core/property_set.rb +8 -14
  29. data/lib/dm-core/query.rb +34 -9
  30. data/lib/dm-core/repository.rb +0 -0
  31. data/lib/dm-core/resource.rb +13 -13
  32. data/lib/dm-core/scope.rb +25 -2
  33. data/lib/dm-core/type.rb +3 -3
  34. data/lib/dm-core/types/discriminator.rb +10 -8
  35. data/lib/dm-core/types/object.rb +4 -0
  36. data/lib/dm-core/types/paranoid_boolean.rb +15 -4
  37. data/lib/dm-core/types/paranoid_datetime.rb +15 -4
  38. data/lib/dm-core/version.rb +3 -0
  39. data/script/all +5 -0
  40. data/script/performance.rb +191 -0
  41. data/script/profile.rb +86 -0
  42. data/spec/integration/association_spec.rb +288 -204
  43. data/spec/integration/association_through_spec.rb +9 -3
  44. data/spec/integration/associations/many_to_many_spec.rb +97 -31
  45. data/spec/integration/associations/many_to_one_spec.rb +41 -6
  46. data/spec/integration/associations/one_to_many_spec.rb +18 -2
  47. data/spec/integration/auto_migrations_spec.rb +0 -0
  48. data/spec/integration/collection_spec.rb +89 -42
  49. data/spec/integration/dependency_queue_spec.rb +58 -0
  50. data/spec/integration/model_spec.rb +67 -8
  51. data/spec/integration/postgres_adapter_spec.rb +19 -20
  52. data/spec/integration/property_spec.rb +17 -8
  53. data/spec/integration/query_spec.rb +273 -191
  54. data/spec/integration/resource_spec.rb +108 -10
  55. data/spec/integration/strategic_eager_loading_spec.rb +138 -0
  56. data/spec/integration/transaction_spec.rb +3 -3
  57. data/spec/integration/type_spec.rb +121 -0
  58. data/spec/lib/logging_helper.rb +18 -0
  59. data/spec/lib/model_loader.rb +91 -0
  60. data/spec/lib/publicize_methods.rb +28 -0
  61. data/spec/models/vehicles.rb +34 -0
  62. data/spec/models/zoo.rb +48 -0
  63. data/spec/spec.opts +3 -0
  64. data/spec/spec_helper.rb +25 -62
  65. data/spec/unit/adapters/data_objects_adapter_spec.rb +1 -0
  66. data/spec/unit/associations/many_to_many_spec.rb +3 -0
  67. data/spec/unit/associations/many_to_one_spec.rb +9 -1
  68. data/spec/unit/associations/one_to_many_spec.rb +12 -4
  69. data/spec/unit/associations/relationship_spec.rb +19 -15
  70. data/spec/unit/associations_spec.rb +37 -0
  71. data/spec/unit/collection_spec.rb +8 -0
  72. data/spec/unit/data_mapper_spec.rb +14 -0
  73. data/spec/unit/model_spec.rb +2 -2
  74. data/spec/unit/property_set_spec.rb +0 -13
  75. data/spec/unit/property_spec.rb +92 -21
  76. data/spec/unit/query_spec.rb +49 -4
  77. data/spec/unit/resource_spec.rb +122 -60
  78. data/spec/unit/scope_spec.rb +11 -0
  79. data/tasks/ci.rb +68 -0
  80. data/tasks/dm.rb +63 -0
  81. data/tasks/doc.rb +20 -0
  82. data/tasks/hoe.rb +38 -0
  83. data/tasks/install.rb +20 -0
  84. metadata +63 -22
@@ -1,8 +1,76 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
+ if HAS_SQLITE3
4
+ describe DataMapper::Associations do
5
+ before :all do
6
+ db1 = File.expand_path(File.join(File.dirname(__FILE__), "custom_db1_sqlite3.db"))
7
+ db2 = File.expand_path(File.join(File.dirname(__FILE__), "custom_db2_sqlite3.db"))
8
+ FileUtils.touch(db1)
9
+ FileUtils.touch(db2)
10
+ DataMapper.setup(:custom_db1, "sqlite3://#{db1}")
11
+ DataMapper.setup(:custom_db2, "sqlite3://#{db2}")
12
+ class CustomParent
13
+ include DataMapper::Resource
14
+ def self.default_repository_name
15
+ :custom_db1
16
+ end
17
+ property :id, Serial
18
+ property :name, String
19
+ repository(:custom_db2) do
20
+ has n, :custom_childs
21
+ end
22
+ end
23
+ class CustomChild
24
+ include DataMapper::Resource
25
+ def self.default_repository_name
26
+ :custom_db2
27
+ end
28
+ property :id, Serial
29
+ property :name, String
30
+ repository(:custom_db1) do
31
+ belongs_to :custom_parent
32
+ end
33
+ end
34
+
35
+ end
36
+ before :each do
37
+ [ CustomChild, CustomParent ].each { |m| m.auto_migrate! }
38
+
39
+ parent = CustomParent.create(:name => "mother")
40
+ child1 = parent.custom_childs.create(:name => "son")
41
+ child2 = parent.custom_childs.create(:name => "daughter")
42
+
43
+ @parent = CustomParent.first(:name => "mother")
44
+ @child1 = CustomChild.first(:name => "son")
45
+ @child2 = CustomChild.first(:name => "daughter")
46
+ end
47
+ it "should be able to handle has_many relationships to other repositories" do
48
+ @parent.custom_childs.size.should == 2
49
+ @parent.custom_childs.include?(@child1).should == true
50
+ @parent.custom_childs.include?(@child2).should == true
51
+ @parent.custom_childs.delete(@child1)
52
+ @parent.custom_childs.save
53
+ @parent.reload
54
+ @parent.custom_childs.size.should == 1
55
+ @parent.custom_childs.include?(@child2).should == true
56
+ end
57
+ it "should be able to handle belongs_to relationships to other repositories" do
58
+ @child1.custom_parent.should == @parent
59
+ @child2.custom_parent.should == @parent
60
+ @child1.custom_parent = nil
61
+ @child1.save
62
+ @child1.reload
63
+ @child1.custom_parent.should == nil
64
+ @parent.reload
65
+ @parent.custom_childs.size.should == 1
66
+ @parent.custom_childs.include?(@child2).should == true
67
+ end
68
+ end
69
+ end
70
+
3
71
  if ADAPTER
4
72
  repository(ADAPTER) do
5
- class Engine
73
+ class Machine
6
74
  include DataMapper::Resource
7
75
 
8
76
  def self.default_repository_name
@@ -12,11 +80,11 @@ if ADAPTER
12
80
  property :id, Serial
13
81
  property :name, String
14
82
 
15
- has n, :yards
16
- has n, :fussy_yards, :class_name => 'Yard', :rating.gte => 3, :type => 'particular'
83
+ has n, :areas
84
+ has n, :fussy_areas, :class_name => 'Area', :rating.gte => 3, :type => 'particular'
17
85
  end
18
86
 
19
- class Yard
87
+ class Area
20
88
  include DataMapper::Resource
21
89
 
22
90
  def self.default_repository_name
@@ -28,7 +96,7 @@ if ADAPTER
28
96
  property :rating, Integer
29
97
  property :type, String
30
98
 
31
- belongs_to :engine
99
+ belongs_to :machine
32
100
  end
33
101
 
34
102
  class Pie
@@ -57,7 +125,7 @@ if ADAPTER
57
125
  has 1, :pie
58
126
  end
59
127
 
60
- class Host
128
+ class Ultrahost
61
129
  include DataMapper::Resource
62
130
 
63
131
  def self.default_repository_name
@@ -67,10 +135,10 @@ if ADAPTER
67
135
  property :id, Serial
68
136
  property :name, String
69
137
 
70
- has n, :slices, :order => [:id.desc]
138
+ has n, :ultraslices, :order => [:id.desc]
71
139
  end
72
140
 
73
- class Slice
141
+ class Ultraslice
74
142
  include DataMapper::Resource
75
143
 
76
144
  def self.default_repository_name
@@ -80,7 +148,7 @@ if ADAPTER
80
148
  property :id, Serial
81
149
  property :name, String
82
150
 
83
- belongs_to :host
151
+ belongs_to :ultrahost
84
152
  end
85
153
 
86
154
  class Node
@@ -108,8 +176,8 @@ if ADAPTER
108
176
  property :title, String, :length => 255, :key => true
109
177
  property :summary, DataMapper::Types::Text
110
178
 
111
- has n, :tasks, :class_name => 'Models::Task'
112
- has 1, :goal, :class_name => 'Models::Goal'
179
+ has n, :tasks
180
+ has 1, :goal
113
181
  end
114
182
 
115
183
  class Goal
@@ -122,7 +190,7 @@ if ADAPTER
122
190
  property :title, String, :length => 255, :key => true
123
191
  property :summary, DataMapper::Types::Text
124
192
 
125
- belongs_to :project, :class_name => "Models::Project"
193
+ belongs_to :project
126
194
  end
127
195
 
128
196
  class Task
@@ -135,7 +203,7 @@ if ADAPTER
135
203
  property :title, String, :length => 255, :key => true
136
204
  property :description, DataMapper::Types::Text
137
205
 
138
- belongs_to :project, :class_name => 'Models::Project'
206
+ belongs_to :project
139
207
  end
140
208
  end
141
209
 
@@ -187,8 +255,8 @@ if ADAPTER
187
255
  end
188
256
 
189
257
  it 'should allow namespaced classes in parent and child for one <=> one' do
190
- g = Models::Goal.new(:title => "g2", :description => "desc 2")
191
- p = Models::Project.create!(:title => "p2", :summary => "sum 2", :goal => g)
258
+ g = Models::Goal.new(:title => "g2", :summary => "desc 2")
259
+ p = Models::Project.create(:title => "p2", :summary => "sum 2", :goal => g)
192
260
 
193
261
  pp = Models::Project.first(:title => 'p2')
194
262
  pp.goal.title.should == "g2"
@@ -204,30 +272,38 @@ if ADAPTER
204
272
 
205
273
  describe 'many to one associations' do
206
274
  before do
207
- Engine.auto_migrate!(ADAPTER)
208
- Yard.auto_migrate!(ADAPTER)
275
+ Machine.auto_migrate!(ADAPTER)
276
+ Area.auto_migrate!(ADAPTER)
209
277
 
210
- engine1 = Engine.create!(:name => 'engine1')
211
- engine2 = Engine.create!(:name => 'engine2')
212
- yard1 = Yard.create!(:name => 'yard1', :engine => engine1)
213
- yard2 = Yard.create!(:name => 'yard2')
278
+ machine1 = Machine.create(:name => 'machine1')
279
+ machine2 = Machine.create(:name => 'machine2')
280
+ area1 = Area.create(:name => 'area1', :machine => machine1)
281
+ area2 = Area.create(:name => 'area2')
214
282
  end
215
283
 
216
284
  it '#belongs_to' do
217
- yard = Yard.new
218
- yard.should respond_to(:engine)
219
- yard.should respond_to(:engine=)
285
+ area = Area.new
286
+ area.should respond_to(:machine)
287
+ area.should respond_to(:machine=)
220
288
  end
221
289
 
222
290
  it 'should load without the parent'
223
291
 
224
292
  it 'should allow substituting the parent' do
225
- yard1 = Yard.first(:name => 'yard1')
226
- engine2 = Engine.first(:name => 'engine2')
293
+ area1 = Area.first(:name => 'area1')
294
+ machine2 = Machine.first(:name => 'machine2')
227
295
 
228
- yard1.engine = engine2
229
- yard1.save
230
- Yard.first(:name => 'yard1').engine.should == engine2
296
+ area1.machine = machine2
297
+ area1.save
298
+ Area.first(:name => 'area1').machine.should == machine2
299
+ end
300
+
301
+ it 'should save both the object and parent if both are new' do
302
+ pending "This is fixed"
303
+ area1 = Area.new(:name => 'area1')
304
+ area1.machine = Machine.new(:name => 'machine1')
305
+ area1.machine.save
306
+ area1.machine_id.should == area1.machine.id
231
307
  end
232
308
 
233
309
  it '#belongs_to with namespaced models' do
@@ -246,42 +322,42 @@ if ADAPTER
246
322
  end
247
323
 
248
324
  it 'should load the associated instance' do
249
- engine1 = Engine.first(:name => 'engine1')
250
- Yard.first(:name => 'yard1').engine.should == engine1
325
+ machine1 = Machine.first(:name => 'machine1')
326
+ Area.first(:name => 'area1').machine.should == machine1
251
327
  end
252
328
 
253
329
  it 'should save the association key in the child' do
254
- engine2 = Engine.first(:name => 'engine2')
330
+ machine2 = Machine.first(:name => 'machine2')
255
331
 
256
- Yard.create!(:name => 'yard3', :engine => engine2)
257
- Yard.first(:name => 'yard3').engine.should == engine2
332
+ Area.create(:name => 'area3', :machine => machine2)
333
+ Area.first(:name => 'area3').machine.should == machine2
258
334
  end
259
335
 
260
336
  it 'should set the association key immediately' do
261
- engine = Engine.first(:name => 'engine1')
262
- Yard.new(:engine => engine).engine_id.should == engine.id
337
+ machine = Machine.first(:name => 'machine1')
338
+ Area.new(:machine => machine).machine_id.should == machine.id
263
339
  end
264
340
 
265
341
  it 'should save the parent upon saving of child' do
266
- e = Engine.new(:name => 'engine10')
267
- y = Yard.create!(:name => 'yard10', :engine => e)
342
+ e = Machine.new(:name => 'machine10')
343
+ y = Area.create(:name => 'area10', :machine => e)
268
344
 
269
- y.engine.name.should == 'engine10'
270
- Engine.first(:name => 'engine10').should_not be_nil
345
+ y.machine.name.should == 'machine10'
346
+ Machine.first(:name => 'machine10').should_not be_nil
271
347
  end
272
348
 
273
349
  it 'should convert NULL parent ids into nils' do
274
- Yard.first(:name => 'yard2').engine.should be_nil
350
+ Area.first(:name => 'area2').machine.should be_nil
275
351
  end
276
352
 
277
353
  it 'should save nil parents as NULL ids' do
278
- y1 = Yard.create!(:id => 20, :name => 'yard20')
279
- y2 = Yard.create!(:id => 30, :name => 'yard30', :engine => nil)
354
+ y1 = Area.create(:id => 20, :name => 'area20')
355
+ y2 = Area.create(:id => 30, :name => 'area30', :machine => nil)
280
356
 
281
357
  y1.id.should == 20
282
- y1.engine.should be_nil
358
+ y1.machine.should be_nil
283
359
  y2.id.should == 30
284
- y2.engine.should be_nil
360
+ y2.machine.should be_nil
285
361
  end
286
362
 
287
363
  it 'should respect length on foreign keys' do
@@ -296,10 +372,10 @@ if ADAPTER
296
372
  end
297
373
 
298
374
  it 'should be reloaded when calling Resource#reload' do
299
- e = Engine.new(:name => 'engine40')
300
- y = Yard.create!(:name => 'yard40', :engine => e)
375
+ e = Machine.new(:name => 'machine40')
376
+ y = Area.create(:name => 'area40', :machine => e)
301
377
 
302
- y.send(:engine_association).should_receive(:reload).once
378
+ y.send(:machine_association).should_receive(:reload).once
303
379
 
304
380
  lambda { y.reload }.should_not raise_error
305
381
  end
@@ -310,9 +386,9 @@ if ADAPTER
310
386
  Sky.auto_migrate!(ADAPTER)
311
387
  Pie.auto_migrate!(ADAPTER)
312
388
 
313
- pie1 = Pie.create!(:name => 'pie1')
314
- pie2 = Pie.create!(:name => 'pie2')
315
- sky1 = Sky.create!(:name => 'sky1', :pie => pie1)
389
+ pie1 = Pie.create(:name => 'pie1')
390
+ pie2 = Pie.create(:name => 'pie2')
391
+ sky1 = Sky.create(:name => 'sky1', :pie => pie1)
316
392
  end
317
393
 
318
394
  it '#has 1' do
@@ -346,13 +422,13 @@ if ADAPTER
346
422
  it 'should save the association key in the child' do
347
423
  pie2 = Pie.first(:name => 'pie2')
348
424
 
349
- sky2 = Sky.create!(:id => 2, :name => 'sky2', :pie => pie2)
425
+ sky2 = Sky.create(:id => 2, :name => 'sky2', :pie => pie2)
350
426
  pie2.sky.should == sky2
351
427
  end
352
428
 
353
429
  it 'should save the children upon saving of parent' do
354
430
  p = Pie.new(:id => 10, :name => 'pie10')
355
- s = Sky.create!(:id => 10, :name => 'sky10', :pie => p)
431
+ s = Sky.create(:id => 10, :name => 'sky10', :pie => p)
356
432
 
357
433
  p.sky.should == s
358
434
 
@@ -360,8 +436,8 @@ if ADAPTER
360
436
  end
361
437
 
362
438
  it 'should save nil parents as NULL ids' do
363
- p1 = Pie.create!(:id => 20, :name => 'pie20')
364
- p2 = Pie.create!(:id => 30, :name => 'pie30', :sky => nil)
439
+ p1 = Pie.create(:id => 20, :name => 'pie20')
440
+ p2 = Pie.create(:id => 30, :name => 'pie30', :sky => nil)
365
441
 
366
442
  p1.id.should == 20
367
443
  p1.sky.should be_nil
@@ -378,154 +454,162 @@ if ADAPTER
378
454
 
379
455
  describe 'one to many associations' do
380
456
  before do
381
- Host.auto_migrate!(ADAPTER)
382
- Slice.auto_migrate!(ADAPTER)
383
- Engine.auto_migrate!(ADAPTER)
384
- Yard.auto_migrate!(ADAPTER)
457
+ Ultrahost.auto_migrate!(ADAPTER)
458
+ Ultraslice.auto_migrate!(ADAPTER)
459
+ Machine.auto_migrate!(ADAPTER)
460
+ Area.auto_migrate!(ADAPTER)
385
461
 
386
- host1 = Host.create!(:name => 'host1')
387
- host2 = Host.create!(:name => 'host2')
388
- slice1 = Slice.create!(:name => 'slice1', :host => host1)
389
- slice2 = Slice.create!(:name => 'slice2', :host => host1)
390
- slice3 = Slice.create!(:name => 'slice3')
462
+ ultrahost1 = Ultrahost.create(:name => 'ultrahost1')
463
+ ultrahost2 = Ultrahost.create(:name => 'ultrahost2')
464
+ ultraslice1 = Ultraslice.create(:name => 'ultraslice1', :ultrahost => ultrahost1)
465
+ ultraslice2 = Ultraslice.create(:name => 'ultraslice2', :ultrahost => ultrahost1)
466
+ ultraslice3 = Ultraslice.create(:name => 'ultraslice3')
391
467
  end
392
468
 
393
469
  it '#has n' do
394
- h = Host.new
395
- h.should respond_to(:slices)
470
+ h = Ultrahost.new
471
+ h.should respond_to(:ultraslices)
396
472
  end
397
473
 
398
474
  it 'should allow removal of a child through a loaded association' do
399
- host1 = Host.first(:name => 'host1')
400
- slice2 = host1.slices.first
475
+ ultrahost1 = Ultrahost.first(:name => 'ultrahost1')
476
+ ultraslice2 = ultrahost1.ultraslices.first
401
477
 
402
- host1.slices.size.should == 2
403
- host1.slices.delete(slice2)
404
- host1.slices.size.should == 1
478
+ ultrahost1.ultraslices.size.should == 2
479
+ ultrahost1.ultraslices.delete(ultraslice2)
480
+ ultrahost1.ultraslices.size.should == 1
405
481
 
406
- slice2 = Slice.first(:name => 'slice2')
407
- slice2.host.should_not be_nil
482
+ ultraslice2 = Ultraslice.first(:name => 'ultraslice2')
483
+ ultraslice2.ultrahost.should_not be_nil
408
484
 
409
- host1.save
485
+ ultrahost1.save
410
486
 
411
- slice2.reload.host.should be_nil
487
+ ultraslice2.reload.ultrahost.should be_nil
412
488
  end
413
489
 
414
490
  it 'should use the IdentityMap correctly' do
415
491
  repository(ADAPTER) do
416
- host1 = Host.first(:name => 'host1')
492
+ ultrahost1 = Ultrahost.first(:name => 'ultrahost1')
417
493
 
418
- slice = host1.slices.first
419
- slice2 = host1.slices(:order => [:id]).last # should be the same as 1
420
- slice3 = Slice.get(2) # should be the same as 1
494
+ ultraslice = ultrahost1.ultraslices.first
495
+ ultraslice2 = ultrahost1.ultraslices(:order => [:id]).last # should be the same as 1
496
+ ultraslice3 = Ultraslice.get(2) # should be the same as 1
421
497
 
422
- slice.object_id.should == slice2.object_id
423
- slice.object_id.should == slice3.object_id
498
+ ultraslice.object_id.should == ultraslice2.object_id
499
+ ultraslice.object_id.should == ultraslice3.object_id
424
500
  end
425
501
  end
426
502
 
427
503
  it '#<< should add exactly the parameters' do
428
- engine = Engine.new(:name => 'my engine')
504
+ machine = Machine.new(:name => 'my machine')
429
505
  4.times do |i|
430
- engine.yards << Yard.new(:name => "yard nr #{i}")
506
+ machine.areas << Area.new(:name => "area nr #{i}")
431
507
  end
432
- engine.save
433
- engine.yards.size.should == 4
508
+ machine.save
509
+ machine.areas.size.should == 4
434
510
  4.times do |i|
435
- engine.yards.any? do |yard|
436
- yard.name == "yard nr #{i}"
511
+ machine.areas.any? do |area|
512
+ area.name == "area nr #{i}"
437
513
  end.should == true
438
514
  end
439
- engine = Engine.get!(engine.id)
440
- engine.yards.size.should == 4
515
+ machine = Machine.get!(machine.id)
516
+ machine.areas.size.should == 4
441
517
  4.times do |i|
442
- engine.yards.any? do |yard|
443
- yard.name == "yard nr #{i}"
518
+ machine.areas.any? do |area|
519
+ area.name == "area nr #{i}"
444
520
  end.should == true
445
521
  end
446
522
  end
447
523
 
524
+ it "#<< should add the correct number of elements if they are created" do
525
+ machine = Machine.create(:name => 'my machine')
526
+ 4.times do |i|
527
+ machine.areas << Area.create(:name => "area nr #{i}", :machine => machine)
528
+ end
529
+ machine.areas.size.should == 4
530
+ end
531
+
448
532
  it '#<< should add default values for relationships that have conditions' do
449
533
  # it should add default values
450
- engine = Engine.new(:name => 'my engine')
451
- engine.fussy_yards << Yard.new(:name => 'yard 1', :rating => 4 )
452
- engine.save
453
- Yard.first(:name => 'yard 1').type.should == 'particular'
534
+ machine = Machine.new(:name => 'my machine')
535
+ machine.fussy_areas << Area.new(:name => 'area 1', :rating => 4 )
536
+ machine.save
537
+ Area.first(:name => 'area 1').type.should == 'particular'
454
538
  # it should not add default values if the condition's property already has a value
455
- engine.fussy_yards << Yard.new(:name => 'yard 2', :rating => 4, :type => 'not particular')
456
- engine.save
457
- Yard.first(:name => 'yard 2').type.should == 'not particular'
539
+ machine.fussy_areas << Area.new(:name => 'area 2', :rating => 4, :type => 'not particular')
540
+ machine.save
541
+ Area.first(:name => 'area 2').type.should == 'not particular'
458
542
  # it should ignore non :eql conditions
459
- engine.fussy_yards << Yard.new(:name => 'yard 3')
460
- engine.save
461
- Yard.first(:name => 'yard 3').rating.should == nil
543
+ machine.fussy_areas << Area.new(:name => 'area 3')
544
+ machine.save
545
+ Area.first(:name => 'area 3').rating.should == nil
462
546
  end
463
547
 
464
548
  it 'should load the associated instances, in the correct order' do
465
- host1 = Host.first(:name => 'host1')
549
+ ultrahost1 = Ultrahost.first(:name => 'ultrahost1')
466
550
 
467
- host1.slices.should_not be_nil
468
- host1.slices.size.should == 2
469
- host1.slices.first.name.should == 'slice2' # ordered by [:id.desc]
470
- host1.slices.last.name.should == 'slice1'
551
+ ultrahost1.ultraslices.should_not be_nil
552
+ ultrahost1.ultraslices.size.should == 2
553
+ ultrahost1.ultraslices.first.name.should == 'ultraslice2' # ordered by [:id.desc]
554
+ ultrahost1.ultraslices.last.name.should == 'ultraslice1'
471
555
 
472
- slice3 = Slice.first(:name => 'slice3')
556
+ ultraslice3 = Ultraslice.first(:name => 'ultraslice3')
473
557
 
474
- slice3.host.should be_nil
558
+ ultraslice3.ultrahost.should be_nil
475
559
  end
476
560
 
477
561
  it 'should add and save the associated instance' do
478
- host1 = Host.first(:name => 'host1')
479
- host1.slices << Slice.new(:id => 4, :name => 'slice4')
480
- host1.save
562
+ ultrahost1 = Ultrahost.first(:name => 'ultrahost1')
563
+ ultrahost1.ultraslices << Ultraslice.new(:id => 4, :name => 'ultraslice4')
564
+ ultrahost1.save
481
565
 
482
- Slice.first(:name => 'slice4').host.should == host1
566
+ Ultraslice.first(:name => 'ultraslice4').ultrahost.should == ultrahost1
483
567
  end
484
568
 
485
569
  it 'should not save the associated instance if the parent is not saved' do
486
- h = Host.new(:id => 10, :name => 'host10')
487
- h.slices << Slice.new(:id => 10, :name => 'slice10')
570
+ h = Ultrahost.new(:id => 10, :name => 'ultrahost10')
571
+ h.ultraslices << Ultraslice.new(:id => 10, :name => 'ultraslice10')
488
572
 
489
- Slice.first(:name => 'slice10').should be_nil
573
+ Ultraslice.first(:name => 'ultraslice10').should be_nil
490
574
  end
491
575
 
492
576
  it 'should save the associated instance upon saving of parent' do
493
- h = Host.new(:id => 10, :name => 'host10')
494
- h.slices << Slice.new(:id => 10, :name => 'slice10')
577
+ h = Ultrahost.new(:id => 10, :name => 'ultrahost10')
578
+ h.ultraslices << Ultraslice.new(:id => 10, :name => 'ultraslice10')
495
579
  h.save
496
580
 
497
- s = Slice.first(:name => 'slice10')
581
+ s = Ultraslice.first(:name => 'ultraslice10')
498
582
 
499
583
  s.should_not be_nil
500
- s.host.should == h
584
+ s.ultrahost.should == h
501
585
  end
502
586
 
503
587
  it 'should save the associated instances upon saving of parent when mass-assigned' do
504
- h = Host.create!(:id => 10, :name => 'host10', :slices => [ Slice.new(:id => 10, :name => 'slice10') ])
588
+ h = Ultrahost.create(:id => 10, :name => 'ultrahost10', :ultraslices => [ Ultraslice.new(:id => 10, :name => 'ultraslice10') ])
505
589
 
506
- s = Slice.first(:name => 'slice10')
590
+ s = Ultraslice.first(:name => 'ultraslice10')
507
591
 
508
592
  s.should_not be_nil
509
- s.host.should == h
593
+ s.ultrahost.should == h
510
594
  end
511
595
 
512
596
  it 'should have finder-functionality' do
513
- h = Host.first(:name => 'host1')
597
+ h = Ultrahost.first(:name => 'ultrahost1')
514
598
 
515
- h.slices.should have(2).entries
599
+ h.ultraslices.should have(2).entries
516
600
 
517
- s = h.slices.all(:name => 'slice2')
601
+ s = h.ultraslices.all(:name => 'ultraslice2')
518
602
 
519
603
  s.should have(1).entries
520
604
  s.first.id.should == 2
521
605
 
522
- h.slices.first(:name => 'slice2').should == s.first
606
+ h.ultraslices.first(:name => 'ultraslice2').should == s.first
523
607
  end
524
608
 
525
609
  it 'should be reloaded when calling Resource#reload' do
526
- host = Host.first(:name => 'host1')
527
- host.send(:slices_association).should_receive(:reload).once
528
- lambda { host.reload }.should_not raise_error
610
+ ultrahost = Ultrahost.first(:name => 'ultrahost1')
611
+ ultrahost.send(:ultraslices_association).should_receive(:reload).once
612
+ lambda { ultrahost.reload }.should_not raise_error
529
613
  end
530
614
  end
531
615
 
@@ -533,12 +617,12 @@ if ADAPTER
533
617
  before do
534
618
  Node.auto_migrate!(ADAPTER)
535
619
 
536
- Node.create!(:name => 'r1')
537
- Node.create!(:name => 'r2')
538
- Node.create!(:name => 'r1c1', :parent_id => 1)
539
- Node.create!(:name => 'r1c2', :parent_id => 1)
540
- Node.create!(:name => 'r1c3', :parent_id => 1)
541
- Node.create!(:name => 'r1c1c1', :parent_id => 3)
620
+ Node.create(:name => 'r1')
621
+ Node.create(:name => 'r2')
622
+ Node.create(:name => 'r1c1', :parent_id => 1)
623
+ Node.create(:name => 'r1c2', :parent_id => 1)
624
+ Node.create(:name => 'r1c3', :parent_id => 1)
625
+ Node.create(:name => 'r1c1c1', :parent_id => 3)
542
626
  end
543
627
 
544
628
  it 'should properly set #parent' do
@@ -562,7 +646,7 @@ if ADAPTER
562
646
  end
563
647
 
564
648
  it 'should allow to create root nodes' do
565
- r = Node.create!(:name => 'newroot')
649
+ r = Node.create(:name => 'newroot')
566
650
  r.parent.should be_nil
567
651
  r.children.size.should == 0
568
652
  end
@@ -589,21 +673,21 @@ if ADAPTER
589
673
  end
590
674
  property :id, Serial
591
675
  property :name, String
592
- has n, :cakes, :class_name => 'Sweets::Cake' # has n
593
- has n, :recipes, :through => :cakes, :class_name => 'Sweets::Recipe' # has n => has 1
594
- has n, :ingredients, :through => :cakes, :class_name => 'Sweets::Ingredient' # has n => has 1 => has n
595
- has n, :creators, :through => :cakes, :class_name => 'Sweets::Creator' # has n => has 1 => has 1
596
- has n, :slices, :through => :cakes, :class_name => 'Sweets::Slice' # has n => has n
597
- has n, :bites, :through => :cakes, :class_name => 'Sweets::Bite' # has n => has n => has n
598
- has n, :shapes, :through => :cakes, :class_name => 'Sweets::Shape' # has n => has n => has 1
599
- has n, :customers, :through => :cakes, :class_name => 'Sweets::Customer' # has n => belongs_to (pending)
600
- has 1, :shop_owner, :class_name => 'Sweets::ShopOwner' # has 1
601
- has 1, :wife, :through => :shop_owner, :class_name => 'Sweets::Wife' # has 1 => has 1
602
- has 1, :ring, :through => :shop_owner, :class_name => 'Sweets::Ring' # has 1 => has 1 => has 1
603
- has n, :coats, :through => :shop_owner, :class_name => 'Sweets::Coat' # has 1 => has 1 => has n
604
- has n, :children, :through => :shop_owner, :class_name => 'Sweets::Child' # has 1 => has n
605
- has n, :toys, :through => :shop_owner, :class_name => 'Sweets::Toy' # has 1 => has n => has n
606
- has n, :boogers, :through => :shop_owner, :class_name => 'Sweets::Booger' # has 1 => has n => has 1
676
+ has n, :cakes # has n
677
+ has n, :recipes, :through => :cakes # has n => has 1
678
+ has n, :ingredients, :through => :cakes # has n => has 1 => has n
679
+ has n, :creators, :through => :cakes # has n => has 1 => has 1
680
+ has n, :ultraslices, :through => :cakes # has n => has n
681
+ has n, :bites, :through => :cakes # has n => has n => has n
682
+ has n, :shapes, :through => :cakes # has n => has n => has 1
683
+ has n, :customers, :through => :cakes # has n => belongs_to (pending)
684
+ has 1, :shop_owner # has 1
685
+ has 1, :wife, :through => :shop_owner # has 1 => has 1
686
+ has 1, :ring, :through => :shop_owner # has 1 => has 1 => has 1
687
+ has n, :coats, :through => :shop_owner # has 1 => has 1 => has n
688
+ has n, :children, :through => :shop_owner # has 1 => has n
689
+ has n, :toys, :through => :shop_owner # has 1 => has n => has n
690
+ has n, :boogers, :through => :shop_owner # has 1 => has n => has 1
607
691
  end
608
692
 
609
693
  class ShopOwner
@@ -614,13 +698,13 @@ if ADAPTER
614
698
  property :id, Serial
615
699
  property :name, String
616
700
  belongs_to :shop, :class_name => 'Sweets::Shop'
617
- has 1, :wife, :class_name => 'Sweets::Wife'
618
- has n, :children, :class_name => 'Sweets::Child'
619
- has n, :toys, :through => :children, :class_name => 'Sweets::Toy'
620
- has n, :boogers, :through => :children, :class_name => 'Sweets::Booger'
621
- has n, :coats, :through => :wife, :class_name => 'Sweets::Coat'
622
- has 1, :ring, :through => :wife, :class_name => 'Sweets::Ring'
623
- has n, :schools, :through => :children, :class_name => 'Sweets::School'
701
+ has 1, :wife
702
+ has n, :children
703
+ has n, :toys, :through => :children
704
+ has n, :boogers, :through => :children
705
+ has n, :coats, :through => :wife
706
+ has 1, :ring, :through => :wife
707
+ has n, :schools, :through => :children
624
708
  end
625
709
 
626
710
  class Wife
@@ -630,9 +714,9 @@ if ADAPTER
630
714
  end
631
715
  property :id, Serial
632
716
  property :name, String
633
- belongs_to :shop_owner, :class_name => 'Sweets::ShopOwner'
634
- has 1, :ring, :class_name => 'Sweets::Ring'
635
- has n, :coats, :class_name => 'Sweets::Coat'
717
+ belongs_to :shop_owner
718
+ has 1, :ring
719
+ has n, :coats
636
720
  end
637
721
 
638
722
  class Coat
@@ -642,7 +726,7 @@ if ADAPTER
642
726
  end
643
727
  property :id, Serial
644
728
  property :name, String
645
- belongs_to :wife, :class_name => 'Sweets::Wife'
729
+ belongs_to :wife
646
730
  end
647
731
 
648
732
  class Ring
@@ -652,7 +736,7 @@ if ADAPTER
652
736
  end
653
737
  property :id, Serial
654
738
  property :name, String
655
- belongs_to :wife, :class_name => 'Sweets::Wife'
739
+ belongs_to :wife
656
740
  end
657
741
 
658
742
  class Child
@@ -662,9 +746,9 @@ if ADAPTER
662
746
  end
663
747
  property :id, Serial
664
748
  property :name, String
665
- belongs_to :shop_owner, :class_name => 'Sweets::ShopOwner'
666
- has n, :toys, :class_name => 'Sweets::Toy'
667
- has 1, :booger, :class_name => 'Sweets::Booger'
749
+ belongs_to :shop_owner
750
+ has n, :toys
751
+ has 1, :booger
668
752
  end
669
753
 
670
754
  class Booger
@@ -674,7 +758,7 @@ if ADAPTER
674
758
  end
675
759
  property :id, Serial
676
760
  property :name, String
677
- belongs_to :child, :class_name => 'Sweets::Child'
761
+ belongs_to :child
678
762
  end
679
763
 
680
764
  class Toy
@@ -684,7 +768,7 @@ if ADAPTER
684
768
  end
685
769
  property :id, Serial
686
770
  property :name, String
687
- belongs_to :child, :class_name => 'Sweets::Child'
771
+ belongs_to :child
688
772
  end
689
773
 
690
774
  class Cake
@@ -694,14 +778,14 @@ if ADAPTER
694
778
  end
695
779
  property :id, Serial
696
780
  property :name, String
697
- belongs_to :shop, :class_name => 'Sweets::Shop'
698
- belongs_to :customer, :class_name => 'Sweets::Customer'
699
- has n, :slices, :class_name => 'Sweets::Slice'
700
- has n, :bites, :through => :slices, :class_name => 'Sweets::Bite'
701
- has 1, :recipe, :class_name => 'Sweets::Recipe'
702
- has n, :ingredients, :through => :recipe, :class_name => 'Sweets::Ingredient'
703
- has 1, :creator, :through => :recipe, :class_name => 'Sweets::Creator'
704
- has n, :shapes, :through => :slices, :class_name => 'Sweets::Shape'
781
+ belongs_to :shop
782
+ belongs_to :customer
783
+ has n, :ultraslices
784
+ has n, :bites, :through => :ultraslices
785
+ has 1, :recipe
786
+ has n, :ingredients, :through => :recipe
787
+ has 1, :creator, :through => :recipe
788
+ has n, :shapes, :through => :ultraslices
705
789
  end
706
790
 
707
791
  class Recipe
@@ -711,9 +795,9 @@ if ADAPTER
711
795
  end
712
796
  property :id, Serial
713
797
  property :name, String
714
- belongs_to :cake, :class_name => 'Sweets::Cake'
715
- has n, :ingredients, :class_name => 'Sweets::Ingredient'
716
- has 1, :creator, :class_name => 'Sweets::Creator'
798
+ belongs_to :cake
799
+ has n, :ingredients
800
+ has 1, :creator
717
801
  end
718
802
 
719
803
  class Customer
@@ -723,7 +807,7 @@ if ADAPTER
723
807
  end
724
808
  property :id, Serial
725
809
  property :name, String
726
- has n, :cakes, :class_name => 'Sweets::Cake'
810
+ has n, :cakes
727
811
  end
728
812
 
729
813
  class Creator
@@ -733,7 +817,7 @@ if ADAPTER
733
817
  end
734
818
  property :id, Serial
735
819
  property :name, String
736
- belongs_to :recipe, :class_name => 'Sweets::Recipe'
820
+ belongs_to :recipe
737
821
  end
738
822
 
739
823
  class Ingredient
@@ -743,19 +827,19 @@ if ADAPTER
743
827
  end
744
828
  property :id, Serial
745
829
  property :name, String
746
- belongs_to :recipe, :class_name => 'Sweets::Recipe'
830
+ belongs_to :recipe
747
831
  end
748
832
 
749
- class Slice
833
+ class Ultraslice
750
834
  include DataMapper::Resource
751
835
  def self.default_repository_name
752
836
  ADAPTER
753
837
  end
754
838
  property :id, Serial
755
839
  property :size, Integer
756
- belongs_to :cake, :class_name => 'Sweets::Cake'
757
- has n, :bites, :class_name => 'Sweets::Bite'
758
- has 1, :shape, :class_name => 'Sweets::Shape'
840
+ belongs_to :cake
841
+ has n, :bites
842
+ has 1, :shape
759
843
  end
760
844
 
761
845
  class Shape
@@ -765,7 +849,7 @@ if ADAPTER
765
849
  end
766
850
  property :id, Serial
767
851
  property :name, String
768
- belongs_to :slice, :class_name => 'Sweets::Slice'
852
+ belongs_to :ultraslice
769
853
  end
770
854
 
771
855
  class Bite
@@ -775,7 +859,7 @@ if ADAPTER
775
859
  end
776
860
  property :id, Serial
777
861
  property :name, String
778
- belongs_to :slice, :class_name => 'Sweets::Slice'
862
+ belongs_to :ultraslice
779
863
  end
780
864
 
781
865
  DataMapper::Resource.descendants.each do |descendant|
@@ -832,31 +916,31 @@ if ADAPTER
832
916
 
833
917
  # has n => has n
834
918
 
835
- 10.times do |i| german_chocolate.slices << Slice.new(:size => i) end
836
- 5.times do |i| short_cake.slices << Slice.new(:size => i) end
837
- german_chocolate.slices.size.should == 10
919
+ 10.times do |i| german_chocolate.ultraslices << Ultraslice.new(:size => i) end
920
+ 5.times do |i| short_cake.ultraslices << Ultraslice.new(:size => i) end
921
+ german_chocolate.ultraslices.size.should == 10
838
922
  # has n => has n => has 1
839
923
 
840
- german_chocolate.slices.each do |slice|
924
+ german_chocolate.ultraslices.each do |ultraslice|
841
925
  shape = Shape.new(:name => 'square')
842
- slice.shape = shape
926
+ ultraslice.shape = shape
843
927
  shape.save
844
928
  end
845
- short_cake.slices.each do |slice|
929
+ short_cake.ultraslices.each do |ultraslice|
846
930
  shape = Shape.new(:name => 'round')
847
- slice.shape = shape
931
+ ultraslice.shape = shape
848
932
  shape.save
849
933
  end
850
934
 
851
935
  # has n => has n => has n
852
- german_chocolate.slices.each do |slice|
936
+ german_chocolate.ultraslices.each do |ultraslice|
853
937
  6.times do |i|
854
- slice.bites << Bite.new(:name => "Big bite nr #{i}")
938
+ ultraslice.bites << Bite.new(:name => "Big bite nr #{i}")
855
939
  end
856
940
  end
857
- short_cake.slices.each do |slice|
941
+ short_cake.ultraslices.each do |ultraslice|
858
942
  3.times do |i|
859
- slice.bites << Bite.new(:name => "Small bite nr #{i}")
943
+ ultraslice.bites << Bite.new(:name => "Small bite nr #{i}")
860
944
  end
861
945
  end
862
946
 
@@ -919,10 +1003,10 @@ if ADAPTER
919
1003
  #
920
1004
 
921
1005
  it 'should return the right children for has n => has n relationships' do
922
- Sweets::Shop.first.slices.size.should == 15
1006
+ Sweets::Shop.first.ultraslices.size.should == 15
923
1007
  10.times do |i|
924
- Sweets::Shop.first.slices.select do |slice|
925
- slice.cake == Sweets::Cake.first(:name => 'German Chocolate') && slice.size == i
1008
+ Sweets::Shop.first.ultraslices.select do |ultraslice|
1009
+ ultraslice.cake == Sweets::Cake.first(:name => 'German Chocolate') && ultraslice.size == i
926
1010
  end
927
1011
  end
928
1012
  end
@@ -940,10 +1024,10 @@ if ADAPTER
940
1024
  it 'should return the right children for has n => has n => has n' do
941
1025
  Sweets::Shop.first.bites.size.should == 75
942
1026
  Sweets::Shop.first.bites.select do |bite|
943
- bite.slice.cake == Sweets::Cake.first(:name => 'German Chocolate')
1027
+ bite.ultraslice.cake == Sweets::Cake.first(:name => 'German Chocolate')
944
1028
  end.size.should == 60
945
1029
  Sweets::Shop.first.bites.select do |bite|
946
- bite.slice.cake == Sweets::Cake.first(:name => 'Short Cake')
1030
+ bite.ultraslice.cake == Sweets::Cake.first(:name => 'Short Cake')
947
1031
  end.size.should == 15
948
1032
  end
949
1033