-
Notifications
You must be signed in to change notification settings - Fork 29
/
data_objects_adapter_spec.rb
385 lines (306 loc) · 14.3 KB
/
data_objects_adapter_spec.rb
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
require 'pathname'
require Pathname(__FILE__).dirname.expand_path.parent.parent + 'spec_helper'
require ROOT_DIR + 'lib/data_mapper'
require ROOT_DIR + 'lib/data_mapper/adapters/data_objects_adapter'
require __DIR__ + 'adapter_shared_spec'
describe DataMapper::Adapters::DataObjectsAdapter do
before do
@adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
end
it_should_behave_like 'a DataMapper Adapter'
describe "#find_by_sql" do
before do
class Plupp
include DataMapper::Resource
property :id, Fixnum, :key => true
property :name, String
end
end
it "should be added to DataMapper::Resource::ClassMethods" do
DataMapper::Resource::ClassMethods.instance_methods.include?("find_by_sql").should == true
Plupp.methods.include?("find_by_sql").should == true
end
describe "when called" do
before do
@reader = mock("reader")
@reader.stub!(:next!).and_return(false)
@reader.stub!(:close)
@connection = mock("connection")
@connection.stub!(:close)
@command = mock("command")
@adapter = Plupp.repository.adapter
@repository = Plupp.repository
@repository.stub!(:adapter).and_return(@adapter)
@adapter.stub!(:create_connection).and_return(@connection)
@adapter.should_receive(:is_a?).any_number_of_times.with(DataMapper::Adapters::DataObjectsAdapter).and_return(true)
end
it "should accept a single String argument with or without options hash" do
@connection.should_receive(:create_command).twice.with("SELECT * FROM plupps").and_return(@command)
@command.should_receive(:set_types).twice.with([Fixnum, String])
@command.should_receive(:execute_reader).twice.and_return(@reader)
Plupp.should_receive(:repository).any_number_of_times.and_return(@repository)
Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(@repository)
Plupp.find_by_sql("SELECT * FROM plupps")
Plupp.find_by_sql("SELECT * FROM plupps", :repository => :plupp_repo)
end
it "should accept an Array argument with or without options hash" do
@connection.should_receive(:create_command).twice.with("SELECT * FROM plupps WHERE plur = ?").and_return(@command)
@command.should_receive(:set_types).twice.with([Fixnum, String])
@command.should_receive(:execute_reader).twice.with("my pretty plur").and_return(@reader)
Plupp.should_receive(:repository).any_number_of_times.and_return(@repository)
Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(@repository)
Plupp.find_by_sql(["SELECT * FROM plupps WHERE plur = ?", "my pretty plur"])
Plupp.find_by_sql(["SELECT * FROM plupps WHERE plur = ?", "my pretty plur"], :repository => :plupp_repo)
end
it "should accept a Query argument with or without options hash" do
@connection.should_receive(:create_command).twice.with("SELECT \"name\" FROM \"plupps\" WHERE (\"name\" = ?)").and_return(@command)
@command.should_receive(:set_types).twice.with([Fixnum, String])
@command.should_receive(:execute_reader).twice.with(Plupp.properties[:name]).and_return(@reader)
Plupp.should_receive(:repository).any_number_of_times.and_return(@repository)
Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(@repository)
Plupp.find_by_sql(DataMapper::Query.new(Plupp, "name" => "my pretty plur", :fields => ["name"]))
Plupp.find_by_sql(DataMapper::Query.new(Plupp, "name" => "my pretty plur", :fields => ["name"]), :repository => :plupp_repo)
end
it "requires a Repository that is a DataObjectsRepository to work" do
non_do_adapter = mock("non do adapter")
non_do_repo = mock("non do repo")
non_do_repo.stub!(:adapter).and_return(non_do_adapter)
Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(non_do_repo)
Proc.new do
Plupp.find_by_sql(:repository => :plupp_repo)
end.should raise_error(Exception, /DataObjectsAdapter/)
end
it "requires some kind of query to work at all" do
Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(@repository)
Proc.new do
Plupp.find_by_sql(:repository => :plupp_repo)
end.should raise_error(Exception, /requires a query/)
end
end
end
describe '#execute' do
before do
@mock_command = mock('Command', :execute_non_query => nil)
@mock_db = mock('DB Connection', :create_command => @mock_command, :close => true)
@adapter.stub!(:create_connection).and_return(@mock_db)
end
it 'should #create_command from the sql passed' do
@mock_db.should_receive(:create_command).with('SQL STRING').and_return(@mock_command)
@adapter.execute('SQL STRING')
end
it 'should pass any additional args to #execute_non_query' do
@mock_command.should_receive(:execute_non_query).with(:args)
@adapter.execute('SQL STRING', :args)
end
it 'should return the result of #execute_non_query' do
@mock_command.should_receive(:execute_non_query).and_return(:result_set)
@adapter.execute('SQL STRING').should == :result_set
end
it 'should log any errors, then re-raise them' do
pending
@mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
#DataMapper.logger.should_receive(:error)
lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
end
it 'should always close the db connection' do
@mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
@mock_db.should_receive(:close)
lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
end
end
describe '#query' do
before do
@mock_reader = mock('Reader', :fields => ['id', 'UserName', 'AGE'],
:values => [1, 'rando', 27],
:close => true)
@mock_command = mock('Command', :execute_reader => @mock_reader)
@mock_db = mock('DB Connection', :create_command => @mock_command, :close => true)
#make the while loop run exactly once
@mock_reader.stub!(:next!).and_return(true, nil)
@adapter.stub!(:create_connection).and_return(@mock_db)
end
it 'should #create_command from the sql passed' do
@mock_db.should_receive(:create_command).with('SQL STRING').and_return(@mock_command)
@adapter.query('SQL STRING')
end
it 'should pass any additional args to #execute_reader' do
@mock_command.should_receive(:execute_reader).with(:args).and_return(@mock_reader)
@adapter.query('SQL STRING', :args)
end
describe 'returning multiple fields' do
it 'should underscore the field names as members of the result struct' do
@mock_reader.should_receive(:fields).and_return(['id', 'UserName', 'AGE'])
result = @adapter.query('SQL STRING')
result.first.members.should == %w{id user_name age}
end
it 'should convert each row into the struct' do
@mock_reader.should_receive(:values).and_return([1, 'rando', 27])
@adapter.query('SQL STRING')
end
it 'should add the row structs into the results array' do
results = @adapter.query('SQL STRING')
results.should be_kind_of(Array)
row = results.first
row.should be_kind_of(Struct)
row.id.should == 1
row.user_name.should == 'rando'
row.age.should == 27
end
end
describe 'returning a single field' do
it 'should add the value to the results array' do
@mock_reader.should_receive(:fields).and_return(['username'])
@mock_reader.should_receive(:values).and_return(['rando'])
results = @adapter.query('SQL STRING')
results.should be_kind_of(Array)
results.first.should == 'rando'
end
end
it 'should log any errors, then re-raise them' do
pending
@mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
#DataMapper.logger.should_receive(:error)
lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
end
it 'should always close the db connection' do
@mock_command.should_receive(:execute_non_query).and_raise("Oh Noes!")
@mock_db.should_receive(:close)
lambda { @adapter.execute('SQL STRING') }.should raise_error("Oh Noes!")
end
end
end
describe DataMapper::Adapters::DataObjectsAdapter::SQL, "creating, reading, updating, deleting statements" do
before do
@adapter = DataMapper::Adapters::DataObjectsAdapter.new(:default, URI.parse('mock://localhost'))
class Cheese
include DataMapper::Resource
property :id, Fixnum, :serial => true
property :name, String
property :color, String
property :notes, String, :lazy => true
end
class LittleBox
include DataMapper::Resource
property :street, String, :key => true
property :color, String, :key => true
property :hillside, TrueClass, :default => true
property :notes, String, :lazy => true
end
end
describe "#create_statement" do
it 'should generate a SQL statement for all fields' do
@adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("name", "color") VALUES (?, ?)
EOS
end
it "should generate a SQL statement for only dirty fields" do
@adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("name") VALUES (?)
EOS
@adapter.create_statement(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("color") VALUES (?)
EOS
end
end
describe "#update" do
it 'should not try to update if there are no dirty attributes' do
repository = mock("repository")
resource = mock("resource")
resource.stub!(:dirty_attributes).and_return({})
@adapter.update(repository, resource).should == false
end
end
describe "#create_statement_with_returning" do
it 'should generate a SQL statement for all fields' do
@adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("name", "color") VALUES (?, ?) RETURNING "id"
EOS
end
it "should generate a SQL statement for only dirty fields" do
@adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("name") VALUES (?) RETURNING "id"
EOS
@adapter.create_statement_with_returning(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
INSERT INTO "cheeses" ("color") VALUES (?) RETURNING "id"
EOS
end
end
describe "#update_statement" do
it 'should generate a SQL statement for all fields' do
@adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:name, :color)).should == <<-EOS.compress_lines
UPDATE "cheeses" SET
"name" = ?,
"color" = ?
WHERE "id" = ?
EOS
end
it "should generate a SQL statement for only dirty fields" do
@adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:name)).should == <<-EOS.compress_lines
UPDATE "cheeses" SET "name" = ? WHERE "id" = ?
EOS
@adapter.update_statement(Cheese, Cheese.properties(@adapter.name).slice(:color)).should == <<-EOS.compress_lines
UPDATE "cheeses" SET "color" = ? WHERE "id" = ?
EOS
end
it "should generate a SQL statement that includes a Composite Key" do
@adapter.update_statement(LittleBox, LittleBox.properties(@adapter.name).slice(:hillside)).should == <<-EOS.compress_lines
UPDATE "little_boxes" SET "hillside" = ? WHERE "street" = ? AND "color" = ?
EOS
@adapter.update_statement(LittleBox, LittleBox.properties(@adapter.name).slice(:color, :hillside)).should == <<-EOS.compress_lines
UPDATE "little_boxes" SET "color" = ?, "hillside" = ? WHERE "street" = ? AND "color" = ?
EOS
end
end
describe "#delete_statement" do
it 'should generate a SQL statement for a serial Key' do
@adapter.delete_statement(Cheese).should == <<-EOS.compress_lines
DELETE FROM "cheeses" WHERE "id" = ?
EOS
end
it "should generate a SQL statement for a Composite Key" do
@adapter.delete_statement(LittleBox).should == <<-EOS.compress_lines
DELETE FROM "little_boxes" WHERE "street" = ? AND "color" = ?
EOS
end
end
describe "#read_statement (without lazy attributes)" do
it 'should generate a SQL statement for a serial Key' do
@adapter.read_statement(Cheese, [1]).should == <<-EOS.compress_lines
SELECT "id", "name", "color" FROM "cheeses" WHERE "id" = ?
EOS
end
it "should generate a SQL statement that includes a Composite Key" do
@adapter.read_statement(LittleBox, ['Shady Drive', 'Blue']).should == <<-EOS.compress_lines
SELECT "street", "color", "hillside" FROM "little_boxes" WHERE "street" = ? AND "color" = ?
EOS
end
end
end
describe '#uri options' do
it 'should transform a fully specified option hash into a URI' do
options = {
:adapter => 'mysql',
:host => 'davidleal.com',
:username => 'me',
:password => 'mypass',
:port => 5000,
:database => 'you_can_call_me_al',
:socket => 'nosock'
}
adapter = DataMapper::Adapters::DataObjectsAdapter.new(:spec, options)
adapter.uri.should ==
URI.parse("mysql://me:mypass@davidleal.com:5000/you_can_call_me_al?socket=nosock")
end
it 'should transform a minimal options hash into a URI' do
options = {
:adapter => 'mysql',
:database => 'you_can_call_me_al'
}
adapter = DataMapper::Adapters::DataObjectsAdapter.new(:spec, options)
adapter.uri.should == URI.parse("mysql:///you_can_call_me_al")
end
it 'should accept the uri when no overrides exist' do
uri = URI.parse("protocol:///")
DataMapper::Adapters::DataObjectsAdapter.new(:spec, uri).uri.should == uri
end
end