dm-hibernate-adapter 0.1pre-java
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dm-hibernate-adapter.rb +471 -0
- data/lib/dm-hibernate-adapter/dialects.rb +37 -0
- data/lib/dm-hibernate-adapter/hibernate.rb +403 -0
- data/lib/dm-hibernate-adapter/spec/setup.rb +27 -0
- data/lib/dm-hibernate-adapter/transaction.rb +27 -0
- data/lib/dm-hibernate-adapter_ext.jar +0 -0
- data/lib/jibernate.rb +2 -0
- data/spec/abstract_adapter/adapter_shared_spec.rb +514 -0
- data/spec/abstract_adapter/dm-hibernate-adapter_spec.rb +25 -0
- data/spec/abstract_adapter/rcov.opts +6 -0
- data/spec/abstract_adapter/spec.opts +4 -0
- data/spec/abstract_adapter/spec_helper.rb +8 -0
- data/spec/dm_core/adapter_spec.rb +12 -0
- data/spec/dm_core/rcov.opts +6 -0
- data/spec/dm_core/spec.opts +5 -0
- data/spec/dm_core/spec_helper.rb +42 -0
- data/spec/log4j.properties +11 -0
- data/spec/transient/dm-hibernate-adapter_spec.rb +57 -0
- data/spec/transient/lib/adapter_helpers.rb +107 -0
- data/spec/transient/lib/collection_helpers.rb +18 -0
- data/spec/transient/lib/counter_adapter.rb +38 -0
- data/spec/transient/lib/pending_helpers.rb +46 -0
- data/spec/transient/lib/rspec_immediate_feedback_formatter.rb +54 -0
- data/spec/transient/rcov.opts +6 -0
- data/spec/transient/shared/adapter_shared_spec.rb +408 -0
- data/spec/transient/shared/finder_shared_spec.rb +1513 -0
- data/spec/transient/shared/model_spec.rb +165 -0
- data/spec/transient/shared/property_spec.rb +412 -0
- data/spec/transient/shared/resource_shared_spec.rb +1226 -0
- data/spec/transient/shared/resource_spec.rb +133 -0
- data/spec/transient/shared/sel_shared_spec.rb +112 -0
- data/spec/transient/spec.opts +4 -0
- data/spec/transient/spec_helper.rb +14 -0
- metadata +210 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
module Hibernate
|
2
|
+
class Transaction
|
3
|
+
|
4
|
+
attr_reader :session
|
5
|
+
|
6
|
+
def initialize()
|
7
|
+
@session = Hibernate.session()
|
8
|
+
end
|
9
|
+
|
10
|
+
def close()
|
11
|
+
@session.close() if @session
|
12
|
+
end
|
13
|
+
|
14
|
+
def begin()
|
15
|
+
@session.begin_transaction()
|
16
|
+
end
|
17
|
+
|
18
|
+
def commit()
|
19
|
+
@session.transaction().commit()
|
20
|
+
end
|
21
|
+
|
22
|
+
def rollback()
|
23
|
+
@session.transaction().rollback() if @session
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
Binary file
|
data/lib/jibernate.rb
ADDED
@@ -0,0 +1,514 @@
|
|
1
|
+
share_examples_for 'An Adapter' do
|
2
|
+
|
3
|
+
def self.adapter_supports?(*methods)
|
4
|
+
methods.all? do |method|
|
5
|
+
# TODO: figure out a way to see if the instance method is only inherited
|
6
|
+
# from the Abstract Adapter, and not defined in it's class. If that is
|
7
|
+
# the case return false
|
8
|
+
|
9
|
+
# CRUD methods can be inherited from parent class
|
10
|
+
described_type.instance_methods.any? { |instance_method| method.to_s == instance_method.to_s }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
before(:all) do
|
15
|
+
|
16
|
+
raise '+@adapter+ should be defined in before block' unless instance_variable_get('@adapter')
|
17
|
+
|
18
|
+
class ::Heffalump
|
19
|
+
include DataMapper::Resource
|
20
|
+
|
21
|
+
property :id, Serial, :field => "hid"
|
22
|
+
property :color, String, :required => false, :length => 64, :unique_index => true
|
23
|
+
property :alpha, String, :required => true, :length => 4, :default => "done", :index => true
|
24
|
+
property :num_spots, Integer, :index => :big
|
25
|
+
property :number, Integer, :unique => true
|
26
|
+
property :striped, Boolean, :index => :big #[:big, :small]
|
27
|
+
property :weight, Float, :precision => 12, :unique_index => :usmall
|
28
|
+
property :distance, Decimal, :unique_index => :usamle #[:ubig, :usmall]
|
29
|
+
property :birthdate, Date, :required => false, :field => "birth_date"
|
30
|
+
property :modified_at, DateTime, :required => false
|
31
|
+
property :expiration, Time, :required => false
|
32
|
+
property :comment, Text, :required => false, :length => 6, :index => :big
|
33
|
+
end
|
34
|
+
|
35
|
+
# TODO ?
|
36
|
+
# if @repository.respond_to?(:auto_migrate!)
|
37
|
+
Heffalump.auto_migrate!
|
38
|
+
# end
|
39
|
+
end
|
40
|
+
|
41
|
+
if adapter_supports?(:create)
|
42
|
+
describe '#create' do
|
43
|
+
it 'should not raise any errors' do
|
44
|
+
lambda {
|
45
|
+
Heffalump.create(:color => 'peach', :alpha => '1234', :weight => 1.234, :birthdate => Date.new, :comment => "123456", :expiration => Time.new, :modified_at => DateTime.new, :distance => BigDecimal.new("432423424"))
|
46
|
+
}.should_not raise_error
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should set the identity field for the resource' do
|
50
|
+
heffalump = Heffalump.new(:color => 'peach')
|
51
|
+
heffalump.id.should be_nil
|
52
|
+
heffalump.save
|
53
|
+
heffalump.id.should_not be_nil
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "property constraints set via annotations" do
|
57
|
+
it 'should obey required == true' do
|
58
|
+
h = Heffalump.create(:color => 'peach', :alpha => nil)
|
59
|
+
h.saved?.should be_false
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should obey length on not required' do
|
63
|
+
lambda {
|
64
|
+
Heffalump.create(:color => 'peach', :comment => '1234567')
|
65
|
+
}.should raise_error(NativeException)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should obey length on required' do
|
69
|
+
lambda {
|
70
|
+
Heffalump.create(:color => 'peach', :alpha => '12345')
|
71
|
+
}.should raise_error(NativeException)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should obey unique' do
|
75
|
+
lambda {
|
76
|
+
Heffalump.create(:color => 'peach', :number => 12345)
|
77
|
+
Heffalump.create(:color => 'peach', :number => 12345)
|
78
|
+
}.should raise_error(NativeException)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
else
|
83
|
+
it 'needs to support #create'
|
84
|
+
end
|
85
|
+
|
86
|
+
if adapter_supports?(:read)
|
87
|
+
|
88
|
+
# <added>
|
89
|
+
# XXX this part is added to dm_core's specs
|
90
|
+
describe '#read specific object' do
|
91
|
+
before :all do
|
92
|
+
@heffalump = Heffalump.create(:color => 'brownish hue')
|
93
|
+
#just going to borrow this, so I can check the return values
|
94
|
+
@query = Heffalump.all.query
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should not raise any errors' do
|
98
|
+
lambda {
|
99
|
+
Heffalump.get(@heffalump.id)
|
100
|
+
}.should_not raise_error
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should raise ObjectNotFoundError' do
|
104
|
+
lambda {
|
105
|
+
id = -600
|
106
|
+
Heffalump.get!(id)
|
107
|
+
}.should raise_error(DataMapper::ObjectNotFoundError)
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should return correct result' do
|
111
|
+
id = @heffalump.id
|
112
|
+
Heffalump.get(id).id.should == id
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
# </added>
|
117
|
+
|
118
|
+
describe '#read' do
|
119
|
+
before :all do
|
120
|
+
@heffalump = Heffalump.create(:color => 'brownish hue')
|
121
|
+
#just going to borrow this, so I can check the return values
|
122
|
+
@query = Heffalump.all.query
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should not raise any errors' do
|
126
|
+
lambda {
|
127
|
+
Heffalump.all()
|
128
|
+
}.should_not raise_error
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should return stuff' do
|
132
|
+
Heffalump.all.should be_include(@heffalump)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
else
|
136
|
+
it 'needs to support #read'
|
137
|
+
end
|
138
|
+
|
139
|
+
if adapter_supports?(:update)
|
140
|
+
|
141
|
+
# <added>
|
142
|
+
# XXX this part is added to dm_core's specs
|
143
|
+
describe '#update called directly' do
|
144
|
+
before do
|
145
|
+
@heffalump = Heffalump.create(:color => 'indigo')
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should not raise any errors' do
|
149
|
+
lambda{
|
150
|
+
@heffalump.update(:color => 'violet')
|
151
|
+
}.should_not raise_error
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should not alter the identity field' do
|
155
|
+
id = @heffalump.id
|
156
|
+
@heffalump.update(:color => 'violet')
|
157
|
+
@heffalump.id.should == id
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'should update altered fields' do
|
161
|
+
@heffalump.update(:color => 'violet')
|
162
|
+
Heffalump.get(*@heffalump.key).color.should == 'violet'
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should not alter other fields' do
|
166
|
+
color = @heffalump.color
|
167
|
+
@heffalump.update(:num_spots => 567)
|
168
|
+
Heffalump.get(*@heffalump.key).color.should == color
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
# </added>
|
173
|
+
|
174
|
+
describe '#update' do
|
175
|
+
before do
|
176
|
+
@heffalump = Heffalump.create(:color => 'indigo')
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'should not raise any errors' do
|
180
|
+
lambda {
|
181
|
+
@heffalump.color = 'violet'
|
182
|
+
@heffalump.save
|
183
|
+
}.should_not raise_error
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'should not alter the identity field' do
|
187
|
+
id = @heffalump.id
|
188
|
+
@heffalump.color = 'violet'
|
189
|
+
@heffalump.save
|
190
|
+
@heffalump.id.should == id
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should update altered fields' do
|
194
|
+
@heffalump.color = 'violet'
|
195
|
+
@heffalump.save
|
196
|
+
Heffalump.get(*@heffalump.key).color.should == 'violet'
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'should not alter other fields' do
|
200
|
+
color = @heffalump.color
|
201
|
+
@heffalump.num_spots = 3
|
202
|
+
@heffalump.save
|
203
|
+
Heffalump.get(*@heffalump.key).color.should == color
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'should obey required == true' do
|
207
|
+
@heffalump.update(:alpha => nil).should be_false
|
208
|
+
end
|
209
|
+
end
|
210
|
+
else
|
211
|
+
it 'needs to support #update'
|
212
|
+
end
|
213
|
+
|
214
|
+
if adapter_supports?(:delete)
|
215
|
+
describe '#delete' do
|
216
|
+
before do
|
217
|
+
@heffalump = Heffalump.create(:color => 'forest green')
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'should not raise any errors' do
|
221
|
+
lambda {
|
222
|
+
@heffalump.destroy
|
223
|
+
}.should_not raise_error
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should delete the requested resource' do
|
227
|
+
id = @heffalump.id
|
228
|
+
@heffalump.destroy
|
229
|
+
Heffalump.get(id).should be_nil
|
230
|
+
end
|
231
|
+
end
|
232
|
+
else
|
233
|
+
it 'needs to support #delete'
|
234
|
+
end
|
235
|
+
|
236
|
+
if adapter_supports?(:read, :create)
|
237
|
+
describe 'query matching' do
|
238
|
+
before :all do
|
239
|
+
@red = Heffalump.create(:color => 'red')
|
240
|
+
@two = Heffalump.create(:num_spots => 2)
|
241
|
+
@five = Heffalump.create(:num_spots => 5)
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'conditions' do
|
245
|
+
describe 'eql' do
|
246
|
+
it 'should be able to search for objects included in an inclusive range of values' do
|
247
|
+
Heffalump.all(:num_spots => 1..5).should be_include(@five)
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'should be able to search for objects included in an exclusive range of values' do
|
251
|
+
Heffalump.all(:num_spots => 1...6).should be_include(@five)
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'should not be able to search for values not included in an inclusive range of values' do
|
255
|
+
Heffalump.all(:num_spots => 1..4).should_not be_include(@five)
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'should not be able to search for values not included in an exclusive range of values' do
|
259
|
+
Heffalump.all(:num_spots => 1...5).should_not be_include(@five)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe 'not' do
|
264
|
+
it 'should be able to search for objects with not equal value' do
|
265
|
+
Heffalump.all(:color.not => 'red').should_not be_include(@red)
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'should include objects that are not like the value' do
|
269
|
+
Heffalump.all(:color.not => 'black').should be_include(@red)
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'should be able to search for objects with not nil value' do
|
273
|
+
Heffalump.all(:color.not => nil).should be_include(@red)
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'should not include objects with a nil value' do
|
277
|
+
Heffalump.all(:color.not => nil).should_not be_include(@two)
|
278
|
+
end
|
279
|
+
|
280
|
+
it 'should be able to search for object with a nil value using required properties' do
|
281
|
+
Heffalump.all(:id.not => nil).should == [ @red, @two, @five ]
|
282
|
+
end
|
283
|
+
|
284
|
+
# XXX That case generates SICK sql code !
|
285
|
+
it 'should be able to search for objects not in an empty list (match all)' do
|
286
|
+
Heffalump.all(:color.not => []).should == [ @red, @two, @five ]
|
287
|
+
end
|
288
|
+
|
289
|
+
it 'should be able to search for objects in an empty list and another OR condition (match none on the empty list)' do
|
290
|
+
Heffalump.all(:conditions => DataMapper::Query::Conditions::Operation.new(
|
291
|
+
:or,
|
292
|
+
DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:color], []),
|
293
|
+
DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:num_spots], [5]))).should == [ @five ]
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'should be able to search for objects not included in an array of values' do
|
297
|
+
Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'should be able to search for objects not included in an array of values' do
|
301
|
+
Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should_not be_include(@five)
|
302
|
+
end
|
303
|
+
|
304
|
+
it 'should be able to search for objects not included in an inclusive range of values' do
|
305
|
+
Heffalump.all(:num_spots.not => 1..4).should be_include(@five)
|
306
|
+
end
|
307
|
+
|
308
|
+
it 'should be able to search for objects not included in an exclusive range of values' do
|
309
|
+
Heffalump.all(:num_spots.not => 1...5).should be_include(@five)
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'should not be able to search for values not included in an inclusive range of values' do
|
313
|
+
Heffalump.all(:num_spots.not => 1..5).should_not be_include(@five)
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'should not be able to search for values not included in an exclusive range of values' do
|
317
|
+
Heffalump.all(:num_spots.not => 1...6).should_not be_include(@five)
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
describe 'like' do
|
322
|
+
it 'should be able to search for objects that match value' do
|
323
|
+
Heffalump.all(:color.like => '%ed').should be_include(@red)
|
324
|
+
end
|
325
|
+
|
326
|
+
it 'should not search for objects that do not match the value' do
|
327
|
+
Heffalump.all(:color.like => '%blak%').should_not be_include(@red)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
# <added>
|
332
|
+
# XXX this part is added to dm_core's specs
|
333
|
+
# XX ie. HSQLDB support "Java" regexps only
|
334
|
+
describe 'Java regexp' do
|
335
|
+
before do
|
336
|
+
if (defined?(DataMapper::Adapters::Sqlite3Adapter) && @adapter.kind_of?(DataMapper::Adapters::Sqlite3Adapter) ||
|
337
|
+
defined?(DataMapper::Adapters::SqlserverAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
|
338
|
+
pending 'delegate regexp matches to same system that the InMemory and YAML adapters use'
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'should be able to search for objects that match value' do
|
343
|
+
Heffalump.all(:color => /.*ed.*/).should be_include(@red)
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'should not be able to search for objects that do not match the value' do
|
347
|
+
Heffalump.all(:color => /.*blak.*/).should_not be_include(@red)
|
348
|
+
end
|
349
|
+
|
350
|
+
it 'should be able to do a negated search for objects that match value' do
|
351
|
+
Heffalump.all(:color.not => /.*blak.*/).should be_include(@red)
|
352
|
+
end
|
353
|
+
|
354
|
+
it 'should not be able to do a negated search for objects that do not match value' do
|
355
|
+
Heffalump.all(:color.not => /.*ed.*/).should_not be_include(@red)
|
356
|
+
end
|
357
|
+
|
358
|
+
end
|
359
|
+
# <added>
|
360
|
+
|
361
|
+
describe 'regexp' do
|
362
|
+
before do
|
363
|
+
if (defined?(DataMapper::Adapters::Sqlite3Adapter) && @adapter.kind_of?(DataMapper::Adapters::Sqlite3Adapter) ||
|
364
|
+
defined?(DataMapper::Adapters::SqlserverAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
|
365
|
+
pending 'delegate regexp matches to same system that the InMemory and YAML adapters use'
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
it 'should be able to search for objects that match value' do
|
370
|
+
Heffalump.all(:color => /ed/).should be_include(@red)
|
371
|
+
end
|
372
|
+
|
373
|
+
it 'should not be able to search for objects that do not match the value' do
|
374
|
+
Heffalump.all(:color => /blak/).should_not be_include(@red)
|
375
|
+
end
|
376
|
+
|
377
|
+
it 'should be able to do a negated search for objects that match value' do
|
378
|
+
Heffalump.all(:color.not => /blak/).should be_include(@red)
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'should not be able to do a negated search for objects that do not match value' do
|
382
|
+
Heffalump.all(:color.not => /ed/).should_not be_include(@red)
|
383
|
+
end
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
describe 'gt' do
|
388
|
+
it 'should be able to search for objects with value greater than' do
|
389
|
+
Heffalump.all(:num_spots.gt => 1).should be_include(@two)
|
390
|
+
end
|
391
|
+
|
392
|
+
it 'should not find objects with a value less than' do
|
393
|
+
Heffalump.all(:num_spots.gt => 3).should_not be_include(@two)
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
397
|
+
describe 'gte' do
|
398
|
+
it 'should be able to search for objects with value greater than' do
|
399
|
+
Heffalump.all(:num_spots.gte => 1).should be_include(@two)
|
400
|
+
end
|
401
|
+
|
402
|
+
it 'should be able to search for objects with values equal to' do
|
403
|
+
Heffalump.all(:num_spots.gte => 2).should be_include(@two)
|
404
|
+
end
|
405
|
+
|
406
|
+
it 'should not find objects with a value less than' do
|
407
|
+
Heffalump.all(:num_spots.gte => 3).should_not be_include(@two)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
describe 'lt' do
|
412
|
+
it 'should be able to search for objects with value less than' do
|
413
|
+
Heffalump.all(:num_spots.lt => 3).should be_include(@two)
|
414
|
+
end
|
415
|
+
|
416
|
+
it 'should not find objects with a value less than' do
|
417
|
+
Heffalump.all(:num_spots.gt => 2).should_not be_include(@two)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
describe 'lte' do
|
422
|
+
it 'should be able to search for objects with value less than' do
|
423
|
+
Heffalump.all(:num_spots.lte => 3).should be_include(@two)
|
424
|
+
end
|
425
|
+
|
426
|
+
it 'should be able to search for objects with values equal to' do
|
427
|
+
Heffalump.all(:num_spots.lte => 2).should be_include(@two)
|
428
|
+
end
|
429
|
+
|
430
|
+
it 'should not find objects with a value less than' do
|
431
|
+
Heffalump.all(:num_spots.lte => 1).should_not be_include(@two)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
describe 'limits' do
|
437
|
+
it 'should be able to limit the objects' do
|
438
|
+
Heffalump.all(:limit => 2).length.should == 2
|
439
|
+
end
|
440
|
+
end
|
441
|
+
end
|
442
|
+
else
|
443
|
+
it 'needs to support #read and #create to test query matching'
|
444
|
+
end
|
445
|
+
|
446
|
+
before :all do
|
447
|
+
class ::User
|
448
|
+
include DataMapper::Resource
|
449
|
+
|
450
|
+
property :id, Serial
|
451
|
+
property :name, String, :required => true
|
452
|
+
property :login, String, :required => true
|
453
|
+
property :password, String, :required => true
|
454
|
+
|
455
|
+
has n, :groups
|
456
|
+
end
|
457
|
+
|
458
|
+
class Group
|
459
|
+
include DataMapper::Resource
|
460
|
+
|
461
|
+
property :id, Serial
|
462
|
+
property :name, String
|
463
|
+
|
464
|
+
belongs_to :user
|
465
|
+
end
|
466
|
+
|
467
|
+
User.auto_migrate!
|
468
|
+
Group.auto_migrate!
|
469
|
+
end
|
470
|
+
|
471
|
+
describe 'One to Many Associations' do
|
472
|
+
|
473
|
+
before(:all) do
|
474
|
+
@user = User.create(:name => 'UserName', :login => 'user', :password => 'pwd')
|
475
|
+
@group = @user.groups.create(:name => 'admin')
|
476
|
+
end
|
477
|
+
|
478
|
+
it 'should have load children' do
|
479
|
+
User.first.groups.should == [@group]
|
480
|
+
end
|
481
|
+
|
482
|
+
it 'should have find elements through association' do
|
483
|
+
admin_users = User.all(:groups => { :name => 'admin'})
|
484
|
+
admin_users.should == [@user]
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
before :all do
|
489
|
+
class ::Friend
|
490
|
+
include DataMapper::Resource
|
491
|
+
|
492
|
+
property :id, Serial
|
493
|
+
property :name, String
|
494
|
+
|
495
|
+
belongs_to :creator, "Friend"
|
496
|
+
end
|
497
|
+
Friend.auto_migrate!
|
498
|
+
end
|
499
|
+
|
500
|
+
describe "self referecing and direct sql" do
|
501
|
+
|
502
|
+
it 'should needs repository with execute method' do
|
503
|
+
repository.adapter.respond_to?( :execute_update).should be_true
|
504
|
+
end
|
505
|
+
|
506
|
+
it 'should create a self referencing enitity' do
|
507
|
+
repository.adapter.execute_update("insert into friends (id, name, creator_id) values(1, 'god', 1)")
|
508
|
+
Friend.all.size.should == 1
|
509
|
+
f = Friend.first
|
510
|
+
f.name.should == 'god'
|
511
|
+
f.creator.should == f
|
512
|
+
end
|
513
|
+
end
|
514
|
+
end
|