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.
Files changed (194) hide show
  1. data/.autotest +17 -14
  2. data/.gitignore +3 -1
  3. data/FAQ +6 -5
  4. data/History.txt +5 -50
  5. data/Manifest.txt +66 -76
  6. data/QUICKLINKS +1 -1
  7. data/README.txt +21 -15
  8. data/Rakefile +6 -7
  9. data/SPECS +2 -29
  10. data/TODO +1 -1
  11. data/deps.rip +2 -0
  12. data/dm-core.gemspec +11 -15
  13. data/lib/dm-core.rb +105 -110
  14. data/lib/dm-core/adapters.rb +135 -16
  15. data/lib/dm-core/adapters/abstract_adapter.rb +251 -181
  16. data/lib/dm-core/adapters/data_objects_adapter.rb +482 -534
  17. data/lib/dm-core/adapters/in_memory_adapter.rb +90 -69
  18. data/lib/dm-core/adapters/mysql_adapter.rb +22 -115
  19. data/lib/dm-core/adapters/oracle_adapter.rb +249 -0
  20. data/lib/dm-core/adapters/postgres_adapter.rb +7 -173
  21. data/lib/dm-core/adapters/sqlite3_adapter.rb +4 -97
  22. data/lib/dm-core/adapters/yaml_adapter.rb +116 -0
  23. data/lib/dm-core/associations/many_to_many.rb +372 -90
  24. data/lib/dm-core/associations/many_to_one.rb +220 -73
  25. data/lib/dm-core/associations/one_to_many.rb +319 -255
  26. data/lib/dm-core/associations/one_to_one.rb +66 -53
  27. data/lib/dm-core/associations/relationship.rb +561 -156
  28. data/lib/dm-core/collection.rb +1101 -379
  29. data/lib/dm-core/core_ext/kernel.rb +12 -0
  30. data/lib/dm-core/core_ext/symbol.rb +10 -0
  31. data/lib/dm-core/identity_map.rb +4 -34
  32. data/lib/dm-core/migrations.rb +1283 -0
  33. data/lib/dm-core/model.rb +570 -369
  34. data/lib/dm-core/model/descendant_set.rb +81 -0
  35. data/lib/dm-core/model/hook.rb +45 -0
  36. data/lib/dm-core/model/is.rb +32 -0
  37. data/lib/dm-core/model/property.rb +247 -0
  38. data/lib/dm-core/model/relationship.rb +335 -0
  39. data/lib/dm-core/model/scope.rb +90 -0
  40. data/lib/dm-core/property.rb +808 -273
  41. data/lib/dm-core/property_set.rb +141 -98
  42. data/lib/dm-core/query.rb +1037 -483
  43. data/lib/dm-core/query/conditions/comparison.rb +872 -0
  44. data/lib/dm-core/query/conditions/operation.rb +221 -0
  45. data/lib/dm-core/query/direction.rb +43 -0
  46. data/lib/dm-core/query/operator.rb +84 -0
  47. data/lib/dm-core/query/path.rb +138 -0
  48. data/lib/dm-core/query/sort.rb +45 -0
  49. data/lib/dm-core/repository.rb +210 -94
  50. data/lib/dm-core/resource.rb +641 -421
  51. data/lib/dm-core/spec/adapter_shared_spec.rb +294 -0
  52. data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +106 -0
  53. data/lib/dm-core/support/chainable.rb +22 -0
  54. data/lib/dm-core/support/deprecate.rb +12 -0
  55. data/lib/dm-core/support/logger.rb +13 -0
  56. data/lib/dm-core/{naming_conventions.rb → support/naming_conventions.rb} +6 -6
  57. data/lib/dm-core/transaction.rb +333 -92
  58. data/lib/dm-core/type.rb +98 -60
  59. data/lib/dm-core/types/boolean.rb +1 -1
  60. data/lib/dm-core/types/discriminator.rb +34 -20
  61. data/lib/dm-core/types/object.rb +7 -4
  62. data/lib/dm-core/types/paranoid_boolean.rb +11 -9
  63. data/lib/dm-core/types/paranoid_datetime.rb +11 -9
  64. data/lib/dm-core/types/serial.rb +3 -3
  65. data/lib/dm-core/types/text.rb +3 -4
  66. data/lib/dm-core/version.rb +1 -1
  67. data/script/performance.rb +102 -109
  68. data/script/profile.rb +169 -38
  69. data/spec/lib/adapter_helpers.rb +105 -0
  70. data/spec/lib/collection_helpers.rb +18 -0
  71. data/spec/lib/counter_adapter.rb +34 -0
  72. data/spec/lib/pending_helpers.rb +27 -0
  73. data/spec/lib/rspec_immediate_feedback_formatter.rb +53 -0
  74. data/spec/public/associations/many_to_many_spec.rb +193 -0
  75. data/spec/public/associations/many_to_one_spec.rb +73 -0
  76. data/spec/public/associations/one_to_many_spec.rb +77 -0
  77. data/spec/public/associations/one_to_one_spec.rb +156 -0
  78. data/spec/public/collection_spec.rb +65 -0
  79. data/spec/public/migrations_spec.rb +359 -0
  80. data/spec/public/model/relationship_spec.rb +924 -0
  81. data/spec/public/model_spec.rb +159 -0
  82. data/spec/public/property_spec.rb +829 -0
  83. data/spec/public/resource_spec.rb +71 -0
  84. data/spec/public/sel_spec.rb +44 -0
  85. data/spec/public/setup_spec.rb +145 -0
  86. data/spec/public/shared/association_collection_shared_spec.rb +317 -0
  87. data/spec/public/shared/collection_shared_spec.rb +1670 -0
  88. data/spec/public/shared/finder_shared_spec.rb +1619 -0
  89. data/spec/public/shared/resource_shared_spec.rb +924 -0
  90. data/spec/public/shared/sel_shared_spec.rb +112 -0
  91. data/spec/public/transaction_spec.rb +129 -0
  92. data/spec/public/types/discriminator_spec.rb +130 -0
  93. data/spec/semipublic/adapters/abstract_adapter_spec.rb +30 -0
  94. data/spec/semipublic/adapters/in_memory_adapter_spec.rb +12 -0
  95. data/spec/semipublic/adapters/mysql_adapter_spec.rb +17 -0
  96. data/spec/semipublic/adapters/oracle_adapter_spec.rb +194 -0
  97. data/spec/semipublic/adapters/postgres_adapter_spec.rb +17 -0
  98. data/spec/semipublic/adapters/sqlite3_adapter_spec.rb +17 -0
  99. data/spec/semipublic/adapters/yaml_adapter_spec.rb +12 -0
  100. data/spec/semipublic/associations/many_to_one_spec.rb +53 -0
  101. data/spec/semipublic/associations/relationship_spec.rb +194 -0
  102. data/spec/semipublic/associations_spec.rb +177 -0
  103. data/spec/semipublic/collection_spec.rb +142 -0
  104. data/spec/semipublic/property_spec.rb +61 -0
  105. data/spec/semipublic/query/conditions_spec.rb +528 -0
  106. data/spec/semipublic/query/path_spec.rb +443 -0
  107. data/spec/semipublic/query_spec.rb +2626 -0
  108. data/spec/semipublic/resource_spec.rb +47 -0
  109. data/spec/semipublic/shared/condition_shared_spec.rb +9 -0
  110. data/spec/semipublic/shared/resource_shared_spec.rb +126 -0
  111. data/spec/spec.opts +3 -1
  112. data/spec/spec_helper.rb +80 -57
  113. data/tasks/ci.rb +19 -31
  114. data/tasks/dm.rb +43 -48
  115. data/tasks/doc.rb +8 -11
  116. data/tasks/gemspec.rb +5 -5
  117. data/tasks/hoe.rb +15 -16
  118. data/tasks/install.rb +8 -10
  119. metadata +74 -111
  120. data/lib/dm-core/associations.rb +0 -207
  121. data/lib/dm-core/associations/relationship_chain.rb +0 -81
  122. data/lib/dm-core/auto_migrations.rb +0 -105
  123. data/lib/dm-core/dependency_queue.rb +0 -32
  124. data/lib/dm-core/hook.rb +0 -11
  125. data/lib/dm-core/is.rb +0 -16
  126. data/lib/dm-core/logger.rb +0 -232
  127. data/lib/dm-core/migrations/destructive_migrations.rb +0 -17
  128. data/lib/dm-core/migrator.rb +0 -29
  129. data/lib/dm-core/scope.rb +0 -58
  130. data/lib/dm-core/support.rb +0 -7
  131. data/lib/dm-core/support/array.rb +0 -13
  132. data/lib/dm-core/support/assertions.rb +0 -8
  133. data/lib/dm-core/support/errors.rb +0 -23
  134. data/lib/dm-core/support/kernel.rb +0 -11
  135. data/lib/dm-core/support/symbol.rb +0 -41
  136. data/lib/dm-core/type_map.rb +0 -80
  137. data/lib/dm-core/types.rb +0 -19
  138. data/script/all +0 -4
  139. data/spec/integration/association_spec.rb +0 -1382
  140. data/spec/integration/association_through_spec.rb +0 -203
  141. data/spec/integration/associations/many_to_many_spec.rb +0 -449
  142. data/spec/integration/associations/many_to_one_spec.rb +0 -163
  143. data/spec/integration/associations/one_to_many_spec.rb +0 -188
  144. data/spec/integration/auto_migrations_spec.rb +0 -413
  145. data/spec/integration/collection_spec.rb +0 -1073
  146. data/spec/integration/data_objects_adapter_spec.rb +0 -32
  147. data/spec/integration/dependency_queue_spec.rb +0 -46
  148. data/spec/integration/model_spec.rb +0 -197
  149. data/spec/integration/mysql_adapter_spec.rb +0 -85
  150. data/spec/integration/postgres_adapter_spec.rb +0 -731
  151. data/spec/integration/property_spec.rb +0 -253
  152. data/spec/integration/query_spec.rb +0 -514
  153. data/spec/integration/repository_spec.rb +0 -61
  154. data/spec/integration/resource_spec.rb +0 -513
  155. data/spec/integration/sqlite3_adapter_spec.rb +0 -352
  156. data/spec/integration/sti_spec.rb +0 -273
  157. data/spec/integration/strategic_eager_loading_spec.rb +0 -156
  158. data/spec/integration/transaction_spec.rb +0 -75
  159. data/spec/integration/type_spec.rb +0 -275
  160. data/spec/lib/logging_helper.rb +0 -18
  161. data/spec/lib/mock_adapter.rb +0 -27
  162. data/spec/lib/model_loader.rb +0 -100
  163. data/spec/lib/publicize_methods.rb +0 -28
  164. data/spec/models/content.rb +0 -16
  165. data/spec/models/vehicles.rb +0 -34
  166. data/spec/models/zoo.rb +0 -48
  167. data/spec/unit/adapters/abstract_adapter_spec.rb +0 -133
  168. data/spec/unit/adapters/adapter_shared_spec.rb +0 -15
  169. data/spec/unit/adapters/data_objects_adapter_spec.rb +0 -632
  170. data/spec/unit/adapters/in_memory_adapter_spec.rb +0 -98
  171. data/spec/unit/adapters/postgres_adapter_spec.rb +0 -133
  172. data/spec/unit/associations/many_to_many_spec.rb +0 -32
  173. data/spec/unit/associations/many_to_one_spec.rb +0 -159
  174. data/spec/unit/associations/one_to_many_spec.rb +0 -393
  175. data/spec/unit/associations/one_to_one_spec.rb +0 -7
  176. data/spec/unit/associations/relationship_spec.rb +0 -71
  177. data/spec/unit/associations_spec.rb +0 -242
  178. data/spec/unit/auto_migrations_spec.rb +0 -111
  179. data/spec/unit/collection_spec.rb +0 -182
  180. data/spec/unit/data_mapper_spec.rb +0 -35
  181. data/spec/unit/identity_map_spec.rb +0 -126
  182. data/spec/unit/is_spec.rb +0 -80
  183. data/spec/unit/migrator_spec.rb +0 -33
  184. data/spec/unit/model_spec.rb +0 -321
  185. data/spec/unit/naming_conventions_spec.rb +0 -36
  186. data/spec/unit/property_set_spec.rb +0 -90
  187. data/spec/unit/property_spec.rb +0 -753
  188. data/spec/unit/query_spec.rb +0 -571
  189. data/spec/unit/repository_spec.rb +0 -93
  190. data/spec/unit/resource_spec.rb +0 -649
  191. data/spec/unit/scope_spec.rb +0 -142
  192. data/spec/unit/transaction_spec.rb +0 -493
  193. data/spec/unit/type_map_spec.rb +0 -114
  194. data/spec/unit/type_spec.rb +0 -119
@@ -1,98 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "..", 'spec_helper'))
2
-
3
- describe DataMapper::Adapters::InMemoryAdapter do
4
- before do
5
- DataMapper.setup(:inmem, :adapter => 'in_memory')
6
-
7
- class ::Heffalump
8
- include DataMapper::Resource
9
-
10
- def self.default_repository_name
11
- :inmem
12
- end
13
-
14
- property :color, String, :key => true # TODO: Drop the 'must have a key' limitation
15
- property :num_spots, Integer
16
- property :striped, Boolean
17
- end
18
-
19
- @heff1 = Heffalump.create(:color => 'Black', :num_spots => 0, :striped => true)
20
- @heff2 = Heffalump.create(:color => 'Brown', :num_spots => 25, :striped => false)
21
- @heff3 = Heffalump.create(:color => 'Blue', :num_spots => nil, :striped => false)
22
- end
23
-
24
- it 'should successfully save an object' do
25
- @heff1.new_record?.should be_false
26
- end
27
-
28
- it 'should be able to get the object' do
29
- Heffalump.get('Black').should == @heff1
30
- end
31
-
32
- it 'should be able to get all the objects' do
33
- Heffalump.all.should == [@heff1, @heff2, @heff3]
34
- end
35
-
36
- it 'should be able to search for objects with equal value' do
37
- Heffalump.all(:striped => true).should == [@heff1]
38
- end
39
-
40
- it 'should be able to search for objects included in an array of values' do
41
- Heffalump.all(:num_spots => [ 25, 50, 75, 100 ]).should == [@heff2]
42
- end
43
-
44
- it 'should be able to search for objects included in a range of values' do
45
- Heffalump.all(:num_spots => 25..100).should == [@heff2]
46
- end
47
-
48
- it 'should be able to search for objects with nil value' do
49
- Heffalump.all(:num_spots => nil).should == [@heff3]
50
- end
51
-
52
- it 'should be able to search for objects with not equal value' do
53
- Heffalump.all(:striped.not => true).should == [@heff2, @heff3]
54
- end
55
-
56
- it 'should be able to search for objects not included in an array of values' do
57
- Heffalump.all(:num_spots.not => [ 25, 50, 75, 100 ]).should == [@heff1, @heff3]
58
- end
59
-
60
- it 'should be able to search for objects not included in a range of values' do
61
- Heffalump.all(:num_spots.not => 25..100).should == [@heff1, @heff3]
62
- end
63
-
64
- it 'should be able to search for objects with not nil value' do
65
- Heffalump.all(:num_spots.not => nil).should == [@heff1, @heff2]
66
- end
67
-
68
- it 'should be able to search for objects that match value' do
69
- Heffalump.all(:color.like => 'Bl').should == [@heff1, @heff3]
70
- end
71
-
72
- it 'should be able to search for objects with value greater than' do
73
- Heffalump.all(:num_spots.gt => 0).should == [@heff2]
74
- end
75
-
76
- it 'should be able to search for objects with value greater than or equal to' do
77
- Heffalump.all(:num_spots.gte => 0).should == [@heff1, @heff2]
78
- end
79
-
80
- it 'should be able to search for objects with value less than' do
81
- Heffalump.all(:num_spots.lt => 1).should == [@heff1]
82
- end
83
-
84
- it 'should be able to search for objects with value less than or equal to' do
85
- Heffalump.all(:num_spots.lte => 0).should == [@heff1]
86
- end
87
-
88
- it 'should be able to update an object' do
89
- @heff1.num_spots = 10
90
- @heff1.save
91
- Heffalump.get('Black').num_spots.should == 10
92
- end
93
-
94
- it 'should be able to destroy an object' do
95
- @heff1.destroy
96
- Heffalump.all.size.should == 2
97
- end
98
- end
@@ -1,133 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "..", 'spec_helper'))
2
-
3
- if HAS_POSTGRES
4
- describe DataMapper::Adapters::PostgresAdapter do
5
- before :all do
6
- @adapter = repository(:postgres).adapter
7
- end
8
-
9
- describe '#upgrade_model_storage' do
10
- before do
11
- @repository = mock('repository', :kind_of? => true, :name => :postgres)
12
- @model = mock('model', :kind_of? => true, :storage_name => 'models')
13
- @property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
14
-
15
- @model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
16
-
17
- @command = mock('command')
18
- @connection = mock('connection', :create_command => @command, :close => true)
19
- @result = mock('result', :to_i => 0)
20
-
21
- DataObjects::Connection.stub!(:new).and_return(@connection)
22
-
23
- @adapter.stub!(:execute).and_return(@result)
24
- @adapter.stub!(:storage_exists?).and_return(true)
25
- @adapter.stub!(:query).and_return([ 0 ])
26
-
27
- @original_method = @adapter.class.superclass.instance_method(:upgrade_model_storage)
28
- @adapter.class.superclass.send(:define_method, :upgrade_model_storage) { |repository, model| }
29
- end
30
-
31
- after do
32
- method = @original_method
33
- @adapter.class.superclass.send(:define_method, :upgrade_model_storage) do |*args|
34
- method.bind(self).call(*args)
35
- end
36
- end
37
-
38
- it 'should check to make sure the sequences exist' do
39
- statement = %q[SELECT COUNT(*) FROM "information_schema"."sequences" WHERE "sequence_name" = ? AND "sequence_schema" = current_schema()]
40
- @adapter.should_receive(:query).with(statement, 'models_property_seq').and_return([ 0 ])
41
- @adapter.upgrade_model_storage(@repository, @model)
42
- end
43
-
44
- it 'should add sequences' do
45
- statement = %q[CREATE SEQUENCE "models_property_seq"]
46
- @adapter.should_receive(:execute).with(statement)
47
- @adapter.upgrade_model_storage(@repository, @model)
48
- end
49
-
50
- it 'should execute the superclass upgrade_model_storage' do
51
- rv = mock('inside super')
52
- @adapter.class.superclass.send(:define_method, :upgrade_model_storage) { |repository, model| rv }
53
- @adapter.upgrade_model_storage(@repository, @model).should == rv
54
- end
55
- end
56
-
57
- describe '#create_model_storage' do
58
- before do
59
- @repository = mock('repository', :kind_of? => true, :name => :postgres)
60
- @model = mock('model', :kind_of? => true, :storage_name => 'models')
61
- @property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
62
-
63
- @model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
64
-
65
- @adapter.stub!(:execute).and_return(@result)
66
- @adapter.stub!(:storage_exists?).and_return(true)
67
- @adapter.stub!(:query).and_return([ 0 ])
68
-
69
- @original_method = @adapter.class.superclass.instance_method(:create_table_statement)
70
- @adapter.class.superclass.send(:define_method, :create_table_statement) {}
71
- end
72
-
73
- after do
74
- method = @original_method
75
- @adapter.class.superclass.send(:define_method, :create_table_statement) do |*args|
76
- method.bind(self).call(*args)
77
- end
78
- end
79
-
80
- it 'should check to make sure the sequences exist' do
81
- statement = %q[SELECT COUNT(*) FROM "information_schema"."sequences" WHERE "sequence_name" = ? AND "sequence_schema" = current_schema()]
82
- @adapter.should_receive(:query).with(statement, 'models_property_seq').and_return([ 0 ])
83
- @adapter.create_model_storage(@repository, @model)
84
- end
85
-
86
- it 'should add sequences' do
87
- statement = %q[CREATE SEQUENCE "models_property_seq"]
88
- @adapter.should_receive(:execute).with(statement)
89
- @adapter.create_model_storage(@repository, @model)
90
- end
91
-
92
- it 'should execute the superclass upgrade_model_storage' do
93
- rv = mock('inside super')
94
- @adapter.class.superclass.send(:define_method, :create_table_statement) { |repository, model| rv }
95
- @adapter.create_table_statement(@repository, @model).should == rv
96
- end
97
- end
98
-
99
- describe '#destroy_model_storage' do
100
- before do
101
- @repository = mock('repository', :kind_of? => true, :name => :postgres)
102
- @model = mock('model', :kind_of? => true, :storage_name => 'models')
103
- @property = mock('property', :kind_of? => true, :model => @model, :serial? => true, :field => 'property')
104
-
105
- @model.should_receive(:properties).with(:postgres).any_number_of_times.and_return([@property])
106
-
107
- @original_method = @adapter.class.superclass.instance_method(:destroy_model_storage)
108
- @adapter.class.superclass.send(:define_method, :destroy_model_storage) {}
109
- end
110
-
111
- after do
112
- method = @original_method
113
- @adapter.class.superclass.send(:define_method, :destroy_model_storage) do |*args|
114
- method.bind(self).call(*args)
115
- end
116
- end
117
-
118
- it 'should not execute the superclass destroy_model_storage if the storage does not exist' do
119
- rv = mock('inside super')
120
- @adapter.class.superclass.send(:define_method, :destroy_model_storage) { |repository, model| rv }
121
- @adapter.destroy_model_storage(@repository, @model).should_not == rv
122
- end
123
-
124
- it 'should execute the superclass destroy_model_storage if the storage exists' do
125
- rv = mock('inside super')
126
- @adapter.class.superclass.send(:define_method, :destroy_model_storage) { |repository, model| rv }
127
- @adapter.stub!(:storage_exists?).and_return(true)
128
-
129
- @adapter.destroy_model_storage(@repository, @model).should == rv
130
- end
131
- end
132
- end
133
- end
@@ -1,32 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Associations::ManyToMany do
4
-
5
- load_models_for_metaphor :vehicles, :content
6
-
7
- it 'should allow a declaration' do
8
- lambda do
9
- class ::Supplier
10
- has n, :manufacturers, :through => Resource
11
- end
12
- end.should_not raise_error
13
- end
14
-
15
- it 'should handle models inside modules' do
16
- lambda do
17
- module ::Content
18
- class Dialect
19
- has n, :locales, :through => Resource, :class_name => "Language::Locale"
20
- end
21
-
22
- class Locale
23
- has n, :dialects, :through => Resource, :class_name => "Language::Dialect"
24
- end
25
- end
26
- end.should_not raise_error
27
- end
28
-
29
- end
30
-
31
- describe DataMapper::Associations::ManyToMany::Proxy do
32
- end
@@ -1,159 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
-
3
- describe DataMapper::Associations::ManyToOne do
4
-
5
- load_models_for_metaphor :vehicles
6
-
7
- it 'should allow a declaration' do
8
- lambda do
9
- class ::Vehicle
10
- belongs_to :manufacturer
11
- end
12
- end.should_not raise_error
13
- end
14
- end
15
-
16
- describe DataMapper::Associations::ManyToOne::Proxy do
17
-
18
- load_models_for_metaphor :vehicles
19
-
20
- before do
21
- @child = mock('child', :kind_of? => true)
22
- @parent = mock('parent', :nil? => false, :new_record? => false)
23
- @relationship = mock('relationship', :kind_of? => true, :get_parent => @parent, :attach_parent => nil)
24
- @association = DataMapper::Associations::ManyToOne::Proxy.new(@relationship, @child)
25
-
26
- @association.replace(@parent)
27
- end
28
-
29
- it 'should provide #replace' do
30
- @association.should respond_to(:replace)
31
- end
32
-
33
- describe '#class' do
34
- it 'should be forwarded to parent' do
35
- @parent.should_receive(:class).and_return(Manufacturer)
36
- @association.class.should == Manufacturer
37
- end
38
- end
39
-
40
- describe '#replace' do
41
- before do
42
- @other = mock('other parent')
43
- end
44
-
45
- before do
46
- @relationship.should_receive(:attach_parent).with(@child, @other)
47
- end
48
-
49
- it 'should remove the resource from the collection' do
50
- @association.should == @parent
51
- @association.replace(@other)
52
- @association.should == @other
53
- end
54
-
55
- it 'should not automatically save that the resource was removed from the association' do
56
- @other.should_not_receive(:save)
57
- @association.replace(@other)
58
- end
59
-
60
- it 'should return the association' do
61
- @association.replace(@other).object_id.should == @association.object_id
62
- end
63
- end
64
-
65
- it 'should provide #save' do
66
- @association.should respond_to(:replace)
67
- end
68
-
69
- describe '#save' do
70
- describe 'when the parent is nil' do
71
- before do
72
- @parent.stub!(:nil?).and_return(true)
73
- end
74
-
75
- it 'should not save the parent' do
76
- @association.save
77
- end
78
-
79
- it 'should return false' do
80
- @association.save.should == false
81
- end
82
- end
83
-
84
- describe 'when the parent is not a new record' do
85
- before do
86
- @parent.should_receive(:new_record?).with(no_args).and_return(false)
87
- end
88
-
89
- it 'should not save the parent' do
90
- @parent.should_not_receive(:save)
91
- @association.save
92
- end
93
-
94
- it 'should return true' do
95
- @association.save.should == true
96
- end
97
- end
98
-
99
- describe 'when the parent is a new record' do
100
- before do
101
- @parent.should_receive(:new_record?).with(no_args).and_return(true)
102
- end
103
-
104
- it 'should save the parent' do
105
- @relationship.should_receive(:with_repository).and_yield
106
- @parent.should_receive(:save).with(no_args)
107
- @association.save
108
- end
109
-
110
- it 'should return the result of the save' do
111
- child_key = mock("child_key")
112
- child_key.should_receive(:set).and_return(true)
113
- parent_key = mock("parent_key")
114
- parent_key.should_receive(:get).and_return(1)
115
- @relationship.should_receive(:with_repository).and_yield
116
- @relationship.should_receive(:child_key).and_return(child_key)
117
- @relationship.should_receive(:parent_key).and_return(parent_key)
118
- save_results = mock('save results')
119
- @parent.should_receive(:save).with(no_args).and_return(save_results)
120
- @association.save.object_id.should == save_results.object_id
121
- end
122
- end
123
- end
124
-
125
- it 'should provide #reload' do
126
- @association.should respond_to(:reload)
127
- end
128
-
129
- describe '#reload' do
130
- before(:each) do
131
- @mock_parent = mock('#reload test parent')
132
- @association.replace(@mock_parent)
133
- end
134
-
135
- it 'should set the @parent ivar to nil' do
136
- @association.__send__(:parent).should == @mock_parent # Sanity check.
137
-
138
- # We can't test the value of the instance variable since
139
- # #instance_variable_get will be run on the @parent (thanks to
140
- # Proxy#method_missing). Instead, test that Relationship#get_parent is
141
- # run -- if @parent wasn't set to nil, this expectation should fail.
142
- @relationship.should_receive(:get_parent).once.and_return(@mock_parent)
143
- @association.reload
144
-
145
- # Trigger #get_parent on the relationship.
146
- @association.__send__(:parent)
147
- end
148
-
149
- it 'should not change the foreign key in the child' do
150
- @relationship.should_not_receive(:attach_parent)
151
- @association.reload
152
- end
153
-
154
- it 'should return self' do
155
- @association.reload.should be_kind_of(DataMapper::Associations::ManyToOne::Proxy)
156
- @association.reload.object_id.should == @association.object_id
157
- end
158
- end
159
- end
@@ -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