This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
adapter_shared_spec.rb
303 lines (248 loc) · 9.91 KB
/
adapter_shared_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
share_examples_for 'An Adapter' do
def self.adapter_supports?(*methods)
methods.all? do |method|
# TODO: figure out a way to see if the instance method is only inherited
# from the Abstract Adapter, and not defined in it's class. If that is
# the case return false
# CRUD methods can be inherited from parent class
described_type.instance_methods.any? { |instance_method| method.to_s == instance_method.to_s }
end
end
before :all do
raise '+@adapter+ should be defined in before block' unless instance_variable_get('@adapter')
class ::Heffalump
include DataMapper::Resource
property :id, Serial
property :color, String
property :num_spots, Integer
property :striped, Boolean
end
# create all tables and constraints before each spec
if @repository.respond_to?(:auto_migrate!)
Heffalump.auto_migrate!
end
end
if adapter_supports?(:create)
describe '#create' do
it 'should not raise any errors' do
lambda {
Heffalump.create(:color => 'peach')
}.should_not raise_error
end
it 'should set the identity field for the resource' do
heffalump = Heffalump.new(:color => 'peach')
heffalump.id.should be_nil
heffalump.save
heffalump.id.should_not be_nil
end
end
else
it 'needs to support #create'
end
if adapter_supports?(:read)
describe '#read' do
before :all do
@heffalump = Heffalump.create(:color => 'brownish hue')
#just going to borrow this, so I can check the return values
@query = Heffalump.all.query
end
it 'should not raise any errors' do
lambda {
Heffalump.all()
}.should_not raise_error
end
it 'should return stuff' do
Heffalump.all.should be_include(@heffalump)
end
end
else
it 'needs to support #read'
end
if adapter_supports?(:update)
describe '#update' do
before do
@heffalump = Heffalump.create(:color => 'indigo')
end
it 'should not raise any errors' do
lambda {
@heffalump.color = 'violet'
@heffalump.save
}.should_not raise_error
end
it 'should not alter the identity field' do
id = @heffalump.id
@heffalump.color = 'violet'
@heffalump.save
@heffalump.id.should == id
end
it 'should update altered fields' do
@heffalump.color = 'violet'
@heffalump.save
Heffalump.get(*@heffalump.key).color.should == 'violet'
end
it 'should not alter other fields' do
color = @heffalump.color
@heffalump.num_spots = 3
@heffalump.save
Heffalump.get(*@heffalump.key).color.should == color
end
end
else
it 'needs to support #update'
end
if adapter_supports?(:delete)
describe '#delete' do
before do
@heffalump = Heffalump.create(:color => 'forest green')
end
it 'should not raise any errors' do
lambda {
@heffalump.destroy
}.should_not raise_error
end
it 'should delete the requested resource' do
id = @heffalump.id
@heffalump.destroy
Heffalump.get(id).should be_nil
end
end
else
it 'needs to support #delete'
end
if adapter_supports?(:read, :create)
describe 'query matching' do
before :all do
@red = Heffalump.create(:color => 'red')
@two = Heffalump.create(:num_spots => 2)
@five = Heffalump.create(:num_spots => 5)
end
describe 'conditions' do
describe 'eql' do
it 'should be able to search for objects included in an inclusive range of values' do
Heffalump.all(:num_spots => 1..5).should be_include(@five)
end
it 'should be able to search for objects included in an exclusive range of values' do
Heffalump.all(:num_spots => 1...6).should be_include(@five)
end
it 'should not be able to search for values not included in an inclusive range of values' do
Heffalump.all(:num_spots => 1..4).should_not be_include(@five)
end
it 'should not be able to search for values not included in an exclusive range of values' do
Heffalump.all(:num_spots => 1...5).should_not be_include(@five)
end
end
describe 'not' do
it 'should be able to search for objects with not equal value' do
Heffalump.all(:color.not => 'red').should_not be_include(@red)
end
it 'should include objects that are not like the value' do
Heffalump.all(:color.not => 'black').should be_include(@red)
end
it 'should be able to search for objects with not nil value' do
Heffalump.all(:color.not => nil).should be_include(@red)
end
it 'should not include objects with a nil value' do
Heffalump.all(:color.not => nil).should_not be_include(@two)
end
it 'should be able to search for object with a nil value using not-nullable properties' do
Heffalump.all(:id.not => nil).should == [ @red, @two, @five ]
end
it 'should be able to search for objects not in an empty list (match all)' do
Heffalump.all(:color.not => []).should == [ @red, @two, @five ]
end
it 'should be able to search for objects not included in an array of values' do
Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
end
it 'should be able to search for objects not included in an array of values' do
Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should_not be_include(@five)
end
it 'should be able to search for objects not included in an inclusive range of values' do
Heffalump.all(:num_spots.not => 1..4).should be_include(@five)
end
it 'should be able to search for objects not included in an exclusive range of values' do
Heffalump.all(:num_spots.not => 1...5).should be_include(@five)
end
it 'should not be able to search for values not included in an inclusive range of values' do
Heffalump.all(:num_spots.not => 1..5).should_not be_include(@five)
end
it 'should not be able to search for values not included in an exclusive range of values' do
Heffalump.all(:num_spots.not => 1...6).should_not be_include(@five)
end
end
describe 'like' do
it 'should be able to search for objects that match value' do
Heffalump.all(:color.like => '%ed').should be_include(@red)
end
it 'should not search for objects that do not match the value' do
Heffalump.all(:color.like => '%blak%').should_not be_include(@red)
end
end
describe 'regexp' do
before do
if (defined?(DataMapper::Adapters::Sqlite3Adapter) && @adapter.kind_of?(DataMapper::Adapters::Sqlite3Adapter) ||
defined?(DataMapper::Adapters::SqlserverAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
pending 'delegate regexp matches to same system that the InMemory and YAML adapters use'
end
end
it 'should be able to search for objects that match value' do
Heffalump.all(:color => /ed/).should be_include(@red)
end
it 'should not be able to search for objects that do not match the value' do
Heffalump.all(:color => /blak/).should_not be_include(@red)
end
it 'should be able to do a negated search for objects that match value' do
Heffalump.all(:color.not => /blak/).should be_include(@red)
end
it 'should not be able to do a negated search for objects that do not match value' do
Heffalump.all(:color.not => /ed/).should_not be_include(@red)
end
end
describe 'gt' do
it 'should be able to search for objects with value greater than' do
Heffalump.all(:num_spots.gt => 1).should be_include(@two)
end
it 'should not find objects with a value less than' do
Heffalump.all(:num_spots.gt => 3).should_not be_include(@two)
end
end
describe 'gte' do
it 'should be able to search for objects with value greater than' do
Heffalump.all(:num_spots.gte => 1).should be_include(@two)
end
it 'should be able to search for objects with values equal to' do
Heffalump.all(:num_spots.gte => 2).should be_include(@two)
end
it 'should not find objects with a value less than' do
Heffalump.all(:num_spots.gte => 3).should_not be_include(@two)
end
end
describe 'lt' do
it 'should be able to search for objects with value less than' do
Heffalump.all(:num_spots.lt => 3).should be_include(@two)
end
it 'should not find objects with a value less than' do
Heffalump.all(:num_spots.gt => 2).should_not be_include(@two)
end
end
describe 'lte' do
it 'should be able to search for objects with value less than' do
Heffalump.all(:num_spots.lte => 3).should be_include(@two)
end
it 'should be able to search for objects with values equal to' do
Heffalump.all(:num_spots.lte => 2).should be_include(@two)
end
it 'should not find objects with a value less than' do
Heffalump.all(:num_spots.lte => 1).should_not be_include(@two)
end
end
end
describe 'limits' do
it 'should be able to limit the objects' do
Heffalump.all(:limit => 2).length.should == 2
end
end
end
else
it 'needs to support #read and #create to test query matching'
end
end