public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Search Repo:
Moved unit specifications into spec/unit

* spec/unit/* specs should specify the behaviour of the associated
  code with no external dependencies.
* Updated autotest rules to match file layout change
* Updated require paths within the unit specs
dkubb (author)
Thu Apr 10 00:00:26 -0700 2008
commit  81d2787c0e427c0e805d15be69190c80a8c21b2d
tree    17c75929c709e7e0584d8a07d63d0fe63875b9de
parent  19d6c9829a297c4140f7b3e772ac3e05035abf96
...
1
2
 
 
3
4
5
6
7
 
 
8
9
10
 
11
12
13
14
15
16
17
18
19
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
 
2
3
4
5
6
 
 
7
8
9
10
 
11
12
 
 
 
 
 
 
 
 
 
 
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
0
@@ -1,22 +1,26 @@
0
 Autotest.add_hook :initialize do |at|
0
- ignore = %w{.git burn www log plugins script tasks bin CHANGELOG FAQ MIT-LICENSE PERFORMANCE QUICKLINKS README}
0
+ ignore = %w[ .git burn www log plugins script tasks bin CHANGELOG FAQ MIT-LICENSE PERFORMANCE QUICKLINKS README ]
0
+
0
   unless ENV['AUTOTEST'] == 'integration'
0
     ignore << 'spec/integration'
0
   end
0
-
0
- ignore.each do |exception|
0
+
0
+ ignore.each do |exception|
0
     at.add_exception(exception)
0
   end
0
-
0
+
0
   at.clear_mappings
0
- at.add_mapping(%r%^spec/.*\.rb$%) { |filename, _|
0
- filename
0
- }
0
- at.add_mapping(%r%^lib/data_mapper/(.*)\.rb$%) { |_, m|
0
- ["spec/#{m[1]}_spec.rb"]
0
- }
0
- at.add_mapping(%r%^spec/spec_helper.rb$%) {
0
- at.files_matching %r%^spec/.*_spec\.rb$%
0
- }
0
-
0
-end
0
\ No newline at end of file
0
+
0
+ at.add_mapping(%r{^spec/.+_spec\.rb$}) do |filename,_|
0
+ filename
0
+ end
0
+
0
+ at.add_mapping(%r{^lib/data_mapper/(.+)\.rb$}) do |_,match|
0
+ [ "spec/unit/#{match[1]}_spec.rb" ] +
0
+ at.files_matching(%r{^spec/integration/.+_spec\.rb$})
0
+ end
0
+
0
+ at.add_mapping(%r{^spec/spec_helper\.rb$}) do
0
+ at.files_matching(%r{^spec/.+_spec\.rb$})
0
+ end
0
+end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,16 +0,0 @@
0
-
0
-describe "a DataMapper Adapter", :shared => true do
0
-
0
- it "should initialize the connection uri" do
0
- new_adapter = @adapter.class.new(:default, URI.parse('some://uri/string'))
0
- new_adapter.instance_variable_get('@uri').to_s.should == URI.parse('some://uri/string').to_s
0
- end
0
-
0
- %w{create read update delete read_one read_set delete_set} .each do |meth|
0
- it "should have a #{meth} method" do
0
- @adapter.should respond_to(meth.intern)
0
- end
0
- end
0
-
0
-end
0
-
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,42 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-require __DIR__.parent.parent + 'lib/data_mapper/adapters/abstract_adapter'
0
-require __DIR__.parent + 'adapter_shared_spec'
0
-
0
-describe DataMapper::Adapters::AbstractAdapter do
0
- before do
0
- @adapter = DataMapper::Adapters::AbstractAdapter.new(:default, 'mock_uri_string')
0
- end
0
-
0
- it_should_behave_like 'a DataMapper Adapter'
0
-
0
- it "should raise NotImplementedError when #create is called" do
0
- lambda { @adapter.create(:repository, :instance) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #read is called" do
0
- lambda { @adapter.read(:repository, :resource, [:key]) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #update is called" do
0
- lambda { @adapter.update(:repository, :instance) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #delete is called" do
0
- lambda { @adapter.delete(:repository, :instance) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #read_one is called" do
0
- lambda { @adapter.read_one(:repository, :query) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #read_set is called" do
0
- lambda { @adapter.read_set(:repository, :query) }.should raise_error(NotImplementedError)
0
- end
0
-
0
- it "should raise NotImplementedError when #delete_set is called" do
0
- lambda { @adapter.delete_set(:repository, :query) }.should raise_error(NotImplementedError)
0
- end
0
-
0
-end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,294 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-require __DIR__.parent.parent + 'lib/data_mapper'
0
-require __DIR__.parent.parent + 'lib/data_mapper/adapters/data_objects_adapter'
0
-require __DIR__.parent + 'adapter_shared_spec'
0
-
0
-describe DataMapper::Adapters::DataObjectsAdapter do
0
- before do
0
- @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
0
- end
0
-
0
- it_should_behave_like 'a DataMapper Adapter'
0
-
0
- describe '#execute' do
0
- before do
0
- @mock_command = mock('Command', :execute_non_query => nil)
0
- @mock_db = mock('DB Connection', :create_command => @mock_command, :close => true)
0
-
0
- @adapter.stub!(:create_connection).and_return(@mock_db)
0
- end
0
-
0
- it 'should #create_command from the sql passed' do
0
- @mock_db.should_receive(:create_command).with('SQL STRING').and_return(@mock_command)
0
- @adapter.execute('SQL STRING')
0
- end
0
-
0
- it 'should pass any additional args to #execute_non_query' do
0
- @mock_command.should_receive(:execute_non_query).with(:args)
0
- @adapter.execute('SQL STRING', :args)
0
- end
0
-
0
- it 'should return the result of #execute_non_query' do
0
- @mock_command.should_receive(:execute_non_query).and_return(:result_set)
0
-
0
- @adapter.execute('SQL STRING').should == :result_set
0
- end
0
-
0
- it 'should log any errors, then re-raise them' do
0
- pending
0
- @mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
0
- #DataMapper.logger.should_receive(:error)
0
-
0
- lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
0
- end
0
-
0
- it 'should always close the db connection' do
0
- @mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
0
- @mock_db.should_receive(:close)
0
-
0
- lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
0
- end
0
- end
0
-
0
- describe '#query' do
0
- before do
0
- @mock_reader = mock('Reader', :fields => ['id', 'UserName', 'AGE'],
0
- :values => [1, 'rando', 27],
0
- :close => true)
0
- @mock_command = mock('Command', :execute_reader => @mock_reader)
0
- @mock_db = mock('DB Connection', :create_command => @mock_command, :close => true)
0
-
0
- #make the while loop run exactly once
0
- @mock_reader.stub!(:next!).and_return(true, nil)
0
- @adapter.stub!(:create_connection).and_return(@mock_db)
0
- end
0
-
0
- it 'should #create_command from the sql passed' do
0
- @mock_db.should_receive(:create_command).with('SQL STRING').and_return(@mock_command)
0
- @adapter.query('SQL STRING')
0
- end
0
-
0
- it 'should pass any additional args to #execute_reader' do
0
- @mock_command.should_receive(:execute_reader).with(:args).and_return(@mock_reader)
0
- @adapter.query('SQL STRING', :args)
0
- end
0
-
0
- describe 'returning multiple fields' do
0
-
0
- it 'should underscore the field names as members of the result struct' do
0
- @mock_reader.should_receive(:fields).and_return(['id', 'UserName', 'AGE'])
0
-
0
- result = @adapter.query('SQL STRING')
0
-
0
- result.first.members.should == %w{id user_name age}
0
- end
0
-
0
- it 'should convert each row into the struct' do
0
- @mock_reader.should_receive(:values).and_return([1, 'rando', 27])
0
-
0
- @adapter.query('SQL STRING')
0
- end
0
-
0
- it 'should add the row structs into the results array' do
0
- results = @adapter.query('SQL STRING')
0
-
0
- results.should be_kind_of(Array)
0
-
0
- row = results.first
0
- row.should be_kind_of(Struct)
0
-
0
- row.id.should == 1
0
- row.user_name.should == 'rando'
0
- row.age.should == 27
0
- end
0
-
0
- end
0
-
0
- describe 'returning a single field' do
0
-
0
- it 'should add the value to the results array' do
0
- @mock_reader.should_receive(:fields).and_return(['username'])
0
- @mock_reader.should_receive(:values).and_return(['rando'])
0
-
0
- results = @adapter.query('SQL STRING')
0
-
0
- results.should be_kind_of(Array)
0
- results.first.should == 'rando'
0
- end
0
-
0
- end
0
-
0
- it 'should log any errors, then re-raise them' do
0
- pending
0
- @mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
0
- #DataMapper.logger.should_receive(:error)
0
-
0
- lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
0
- end
0
-
0
- it 'should always close the db connection' do
0
- @mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
0
- @mock_db.should_receive(:close)
0
-
0
- lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
0
- end
0
- end
0
-end
0
-
0
-describe DataMapper::Adapters::DataObjectsAdapter::SQL, "creating, reading, updating, deleting statements" do
0
- before do
0
- @adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
0
-
0
- class Cheese
0
- include DataMapper::Resource
0
- property :id, Fixnum, :serial => true
0
- property :name, String
0
- property :color, String
0
- property :notes, String, :lazy => true
0
- end
0
-
0
- class LittleBox
0
- include DataMapper::Resource
0
- property :street, String, :key => true
0
- property :color, String, :key => true
0
- property :hillside, TrueClass, :default => true
0
- property :notes, String, :lazy => true
0
- end
0
- end
0
-
0
- describe "#create_statement" do
0
- it 'should generate a SQL statement for all fields' do
0
- @adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("name", "color") VALUES (?, ?)
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement for only dirty fields" do
0
- @adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("name") VALUES (?)
0
- EOS
0
-
0
- @adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("color") VALUES (?)
0
- EOS
0
- end
0
- end
0
-
0
- describe "#create_statement_with_returning" do
0
-
0
- it 'should generate a SQL statement for all fields' do
0
- @adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("name", "color") VALUES (?, ?) RETURNING "id"
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement for only dirty fields" do
0
- @adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("name") VALUES (?) RETURNING "id"
0
- EOS
0
-
0
- @adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
0
- INSERT INTO "cheeses" ("color") VALUES (?) RETURNING "id"
0
- EOS
0
- end
0
-
0
- end
0
-
0
- describe "#update_statement" do
0
-
0
- it 'should generate a SQL statement for all fields' do
0
- @adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
0
- UPDATE "cheeses" SET
0
- "name" = ?,
0
- "color" = ?
0
- WHERE "id" = ?
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement for only dirty fields" do
0
- @adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
0
- UPDATE "cheeses" SET "name" = ? WHERE "id" = ?
0
- EOS
0
-
0
- @adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
0
- UPDATE "cheeses" SET "color" = ? WHERE "id" = ?
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement that includes a Composite Key" do
0
- @adapter.update_statement(LittleBox, LittleBox.properties(@adapter.name).slice(:hillside)).should == <<-EOS.compress_lines
0
- UPDATE "little_boxes" SET "hillside" = ? WHERE "street" = ? AND "color" = ?
0
- EOS
0
-
0
- @adapter.update_statement(LittleBox, LittleBox.properties(@adapter.name).slice(:color, :hillside)).should == <<-EOS.compress_lines
0
- UPDATE "little_boxes" SET "color" = ?, "hillside" = ? WHERE "street" = ? AND "color" = ?
0
- EOS
0
- end
0
-
0
- end
0
-
0
- describe "#delete_statement" do
0
-
0
- it 'should generate a SQL statement for a serial Key' do
0
- @adapter.delete_statement(Cheese).should == <<-EOS.compress_lines
0
- DELETE FROM "cheeses" WHERE "id" = ?
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement for a Composite Key" do
0
- @adapter.delete_statement(LittleBox).should == <<-EOS.compress_lines
0
- DELETE FROM "little_boxes" WHERE "street" = ? AND "color" = ?
0
- EOS
0
- end
0
-
0
- end
0
-
0
- describe "#read_statement (without lazy attributes)" do
0
- it 'should generate a SQL statement for a serial Key' do
0
- @adapter.read_statement(Cheese, [1]).should == <<-EOS.compress_lines
0
- SELECT "id", "name", "color" FROM "cheeses" WHERE "id" = ?
0
- EOS
0
- end
0
-
0
- it "should generate a SQL statement that includes a Composite Key" do
0
- @adapter.read_statement(LittleBox, ['Shady Drive', 'Blue']).should == <<-EOS.compress_lines
0
- SELECT "street", "color", "hillside" FROM "little_boxes" WHERE "street" = ? AND "color" = ?
0
- EOS
0
- end
0
- end
0
-end
0
-
0
- describe '#uri options' do
0
- it 'should transform a fully specified option hash into a URI' do
0
- options = {
0
- :adapter => 'mysql',
0
- :host => 'davidleal.com',
0
- :user => 'me',
0
- :password => 'mypass',
0
- :port => 5000,
0
- :database => 'you_can_call_me_al',
0
- :socket => 'nosock'
0
- }
0
-
0
- adapter = DataMapper::Adapters::DataObjectsAdapter.allocate
0
- adapter.uri(options).should ==
0
- URI.parse("mysql://me:mypass@davidleal.com:5000/you_can_call_me_al?socket=nosock")
0
- end
0
-
0
- it 'should transform a minimal options hash into a URI' do
0
- options = {
0
- :adapter => 'mysql',
0
- :database => 'you_can_call_me_al'
0
- }
0
-
0
- adapter = DataMapper::Adapters::DataObjectsAdapter.allocate
0
- adapter.uri(options).should == URI.parse("mysql:///you_can_call_me_al")
0
- end
0
-
0
- it 'should accept the uri when no overrides exist' do
0
- uri = URI.parse("protocol:///")
0
- DataMapper::Adapters::DataObjectsAdapter.allocate.uri(uri).should == uri
0
- end
0
- end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,29 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-describe "DataMapper::Associations::ManyToMany" do
0
-
0
- before do
0
- @adapter = DataMapper::Repository.adapters[:relationship_spec] || DataMapper.setup(:relationship_spec, 'mock://localhost')
0
- end
0
-
0
- it "should allow a declaration" do
0
-
0
- lambda do
0
- class Supplier
0
- many_to_many :manufacturers
0
- end
0
- end.should_not raise_error
0
- end
0
-
0
- describe DataMapper::Associations::ManyToMany::Instance do
0
- before do
0
- @this = mock("this")
0
- @that = mock("that")
0
- @relationship = mock("relationship")
0
- @association = DataMapper::Associations::ManyToMany::Instance.new(@relationship, @that, nil)
0
- end
0
-
0
- end
0
-
0
-end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,31 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-describe "DataMapper::Associations::ManyToOne" do
0
-
0
- it "should allow a declaration" do
0
- lambda do
0
- class Vehicle
0
- many_to_one :manufacturer
0
- end
0
- end.should_not raise_error
0
- end
0
-
0
- describe DataMapper::Associations::ManyToOne::Instance do
0
- before do
0
- @child = mock("child")
0
- @parent = mock("parent")
0
- @relationship = mock("relationship")
0
- @association = DataMapper::Associations::ManyToOne::Instance.new(@relationship, @child)
0
- end
0
-
0
- describe "when the parent exists" do
0
- it "should attach the parent to the child" do
0
- @parent.should_receive(:new_record?).and_return(false)
0
- @relationship.should_receive(:attach_parent).with(@child, @parent)
0
-
0
- @association.parent = @parent
0
- end
0
- end
0
- end
0
-end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,19 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-describe "DataMapper::Associations::OneToMany" do
0
-
0
- before do
0
- @adapter = DataMapper::Repository.adapters[:relationship_spec] || DataMapper.setup(:relationship_spec, 'mock://localhost')
0
- end
0
-
0
- it "should allow a declaration" do
0
-
0
- lambda do
0
- class Manufacturer
0
- one_to_many :vehicles
0
- end
0
- end.should_not raise_error
0
- end
0
-
0
-end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,14 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-require __DIR__.parent.parent + 'lib/data_mapper/associations/one_to_one'
0
-
0
-describe "DataMapper::Associations::OneToOne" do
0
-
0
- it "should allow a declaration" do
0
- lambda do
0
- class Manufacturer
0
- one_to_one :halo_car, :class => 'Vehicle'
0
- end
0
- end.should_not raise_error
0
- end
0
-end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,155 +0,0 @@
0
-require 'pathname'
0
-require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
-
0
-describe DataMapper::Associations::Relationship do
0
-
0
- before do
0
- @adapter = DataMapper::Repository.adapters[:relationship_spec] || DataMapper.setup(:relationship_spec, 'mock://localhost')
0
- end
0
-
0
- it "should describe an association" do
0
- belongs_to = DataMapper::Associations::Relationship.new(
0
- :manufacturer,
0
- :relationship_spec,
0
- 'Vehicle',
0
- [ :manufacturer_id ],
0
- 'Manufacturer',
0
- nil
0
- )
0
-
0
- belongs_to.should respond_to(:name)
0
- belongs_to.should respond_to(:repository_name)
0
- belongs_to.should respond_to(:child_key)
0
- belongs_to.should respond_to(:parent_key)
0
- end
0
-
0
- it "should map properties explicitly when an association method passes them in its options" do
0
- repository_name = :relationship_spec
0
-
0
- belongs_to = DataMapper::Associations::Relationship.new(
0
- :manufacturer,
0
- repository_name,
0
- 'Vehicle',
0
- [ :manufacturer_id ],
0
- 'Manufacturer',
0
- [ :id ]
0
- )
0
-
0
- belongs_to.name.should == :manufacturer
0
- belongs_to.repository_name.should == repository_name
0
-
0
- belongs_to.child_key.should be_a_kind_of(DataMapper::PropertySet)
0
- belongs_to.parent_key.should be_a_kind_of(DataMapper::PropertySet)
0
-
0
- belongs_to.child_key.to_a.should == Vehicle.properties(repository_name).slice(:manufacturer_id)
0
- belongs_to.parent_key.to_a.should == Manufacturer.properties(repository_name).key
0
- end
0
-
0
- it "should infer properties when options aren't passed" do
0
- repository_name = :relationship_spec
0
-
0
- has_many = DataMapper::Associations::Relationship.new(
0
- :models,
0
- repository_name,
0
- 'Vehicle',
0
- nil,
0
- 'Manufacturer',
0
- nil
0
- )
0
-
0
- has_many.name.should == :models
0
- has_many.repository_name.should == repository_name
0
-
0
- has_many.child_key.should be_a_kind_of(DataMapper::PropertySet)
0
- has_many.parent_key.should be_a_kind_of(DataMapper::PropertySet)
0
-
0
- has_many.child_key.to_a.should == Vehicle.properties(repository_name).slice(:models_id)
0
- has_many.parent_key.to_a.should == Manufacturer.properties(repository_name).key
0
- end
0
-
0
- it "should generate child properties with a safe subset of the parent options" do
0
- pending
0
- # For example, :size would be an option you'd want a generated child Property to copy,
0
- # but :serial or :key obviously not. So need to take a good look at Property::OPTIONS to
0
- # see what applies and what doesn't.
0
- end
0
-
0
-end
0
-
0
-__END__
0
-class LazyLoadedSet < LoadedSet
0
-
0
- def initialize(*args, &b)
0
- super(*args)
0
- @on_demand_loader = b
0
- end
0
-
0
- def each
0
- @on_demand_loader[self]
0
- class << self
0
- def each
0
- super
0
- end
0
- end
0
-
0
- super
0
- end
0
-
0
-end
0
-
0
-set = LazyLoadedSet.new(repository, Zoo, { Property<:id> => 1, Property<:name> => 2, Property<:notes> => 3 }) do |lls|
0
- connection = create_connection
0
- command = connection.create_command("SELECT id, name, notes FROM zoos")
0
- command.set_types([Fixnum, String, String])
0
- reader = command.execute_reader
0
-
0
- while(reader.next!)
0
- lls.load(reader.values)
0
- end
0
-
0
- reader.close
0
- connection.close
0
-end
0
-
0
-class AssociationSet
0
-
0
- def initialize(relationship)
0
- @relationship = relationship
0
- end
0
-
0
- def each
0
- # load some stuff
0
- end
0
-
0
- def <<(value)
0
- # add some stuff and track it.
0
- end
0
-end
0
-
0
-class Vehicle
0
- belongs_to :manufacturer
0
-
0
- def manufacturer
0
- manufacturer_association_set.first
0
- end
0
-
0
- def manufacturer=(value)
0
- manufacturer_association_set.set(value)
0
- end
0
-
0
- private
0
- # This is all class-evaled code defined by belongs_to:
0
- def manufacturer_association_set
0
- @manufacturer_association_set ||= AssociationSet.new(
0
- self.class.associations(repository.name)[:manufacturer]
0
- ) do |set|
0
- # This block is the part that will change between different associations.
0
-
0
- # Parent is the Array of PK properties remember.
0
- resource = set.relationship.parent.first.resource
0
-
0
- resource.all(resource.key => self.loaded_set.keys)
0
- end
0
- end
0
-
0
-end