Rubernate 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -1
- data/lib/rubernate/callbacks.rb +17 -20
- data/lib/rubernate/impl/dbi_generic.rb +77 -50
- data/lib/rubernate/impl/dbi_mysql.rb +2 -2
- data/lib/rubernate/impl/dbi_oracle.rb +29 -3
- data/lib/rubernate/impl/dbi_pg.rb +2 -2
- data/lib/rubernate/impl/memory.rb +14 -12
- data/lib/rubernate/init/init_mysql.rb +2 -2
- data/lib/rubernate/init/init_oracle.rb +2 -2
- data/lib/rubernate/lazyload.rb +63 -0
- data/lib/rubernate/mixins.rb +6 -4
- data/lib/rubernate/persistent.rb +21 -21
- data/lib/rubernate/queries.rb +33 -11
- data/lib/rubernate/runtime.rb +49 -27
- data/lib/rubernate.rb +51 -33
- data/tests/config.rb +8 -5
- data/tests/rubernate/impl/dbi_generic_stub.rb +102 -35
- data/tests/rubernate/impl/dbi_oracle_test.rb +20 -0
- data/tests/rubernate/impl/memory_test.rb +16 -17
- data/tests/rubernate/rubernate_test.rb +352 -302
- metadata +3 -2
@@ -21,12 +21,14 @@ module DBI::GenericTests
|
|
21
21
|
}
|
22
22
|
@runtime = @factory.create
|
23
23
|
Thread.current[:on_save] = Thread.current[:on_load] = nil
|
24
|
+
Rubernate.settings.clear
|
24
25
|
end
|
25
26
|
|
26
27
|
def teardown
|
27
28
|
@runtime.dbh.disconnect if @runtime and @runtime.dbh
|
29
|
+
Rubernate.settings.clear
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
def test_connect
|
31
33
|
assert @runtime
|
32
34
|
assert @runtime.dbh
|
@@ -40,12 +42,12 @@ module DBI::GenericTests
|
|
40
42
|
|
41
43
|
def test_create
|
42
44
|
o = C1.new
|
43
|
-
o.
|
45
|
+
o.__peer = Peer.new
|
44
46
|
@runtime.create o
|
45
47
|
rows = @runtime.dbh.select_all 'select * from r_objects'
|
46
48
|
assert_equal 1, rows.size
|
47
49
|
assert_equal C1.name, rows[0][1]
|
48
|
-
assert o.
|
50
|
+
assert o.__peer.dirty?
|
49
51
|
end
|
50
52
|
|
51
53
|
def test_save_objects
|
@@ -55,7 +57,7 @@ module DBI::GenericTests
|
|
55
57
|
@runtime.save objects
|
56
58
|
|
57
59
|
for object in objects
|
58
|
-
assert !object.
|
60
|
+
assert !object.__peer.dirty?
|
59
61
|
end
|
60
62
|
|
61
63
|
rows = @runtime.dbh.select_all 'select * from r_objects order by object_pk'
|
@@ -248,7 +250,7 @@ module DBI::GenericTests
|
|
248
250
|
object = objects[i - 1]
|
249
251
|
assert_equal C1, object.class
|
250
252
|
assert_equal i, object.p1
|
251
|
-
assert !object.
|
253
|
+
assert !object.__peer.dirty?
|
252
254
|
end
|
253
255
|
end
|
254
256
|
|
@@ -263,7 +265,7 @@ module DBI::GenericTests
|
|
263
265
|
|
264
266
|
o0 = @runtime.load_by_pk o0.primary_key
|
265
267
|
assert o0
|
266
|
-
assert_equal 2, o0.
|
268
|
+
assert_equal 2, o0.__peer.size
|
267
269
|
assert_equal 'o0.p1', o0.p1
|
268
270
|
assert_equal o0.p2, o1
|
269
271
|
end
|
@@ -353,52 +355,63 @@ module DBI::GenericTests
|
|
353
355
|
end
|
354
356
|
|
355
357
|
def test_param_all_types
|
356
|
-
pk, time, date = nil, Time.now, Date.today
|
358
|
+
pk, rk, time, date = nil, nil, Time.now, Date.today
|
357
359
|
Rubernate.session do
|
358
|
-
o = Full.new.attach
|
359
|
-
pk = o.primary_key
|
360
|
+
o, r = Full.new.attach, C1.new.attach
|
361
|
+
pk, rk = o.primary_key, r.primary_key
|
360
362
|
o.p_nil = nil
|
361
363
|
o.p_int = 123
|
362
364
|
o.p_float = 77.5
|
363
365
|
o.p_str = 'Test String'
|
364
366
|
o.p_time = time
|
365
367
|
o.p_date = date
|
366
|
-
o.p_ref =
|
367
|
-
o.p_array = [o,
|
368
|
-
o.p_hash = {
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
368
|
+
o.p_ref = r
|
369
|
+
o.p_array = [o, r, r]
|
370
|
+
o.p_hash = {
|
371
|
+
nil => o,
|
372
|
+
333 => o,
|
373
|
+
777 => r,
|
374
|
+
1.23 => r,
|
375
|
+
'Str.Key' => r,
|
376
|
+
time => r,
|
373
377
|
date => o
|
374
378
|
}
|
375
379
|
end
|
376
380
|
Rubernate.session do
|
377
381
|
o = find_by_pk pk
|
382
|
+
r = find_by_pk rk
|
378
383
|
assert_nil o.p_nil
|
379
384
|
assert_equal 123, o.p_int
|
380
385
|
assert_equal 77.5, o.p_float
|
381
386
|
assert_equal 'Test String', o.p_str
|
382
387
|
assert_equal time.to_a[0..5], o.p_time.to_a[0..5] # Ignore time zone
|
383
|
-
assert_equal date, o.p_date
|
384
|
-
assert_equal
|
385
|
-
assert_equal [o,
|
386
|
-
assert_equal
|
388
|
+
assert_equal date, o.p_date
|
389
|
+
assert_equal r, o.p_ref
|
390
|
+
assert_equal [o, r, r], o.p_array
|
391
|
+
assert_equal 7, o.p_hash.size
|
387
392
|
|
388
393
|
o.p_hash.each {|key, val|
|
389
|
-
|
390
|
-
|
394
|
+
if key.is_a? Time
|
395
|
+
assert_equal time.to_a[0..5], key.to_a[0..5]
|
396
|
+
assert_equal r, val
|
397
|
+
end
|
398
|
+
if key.is_a? Date
|
399
|
+
assert_equal date, key
|
400
|
+
assert_equal o, val
|
401
|
+
end
|
391
402
|
}
|
392
403
|
o.p_hash.delete_if {|key, val| key.is_a? Time or key.is_a? Date}
|
393
404
|
|
394
|
-
assert_equal ({
|
395
|
-
|
396
|
-
|
397
|
-
|
405
|
+
assert_equal ({
|
406
|
+
nil => o,
|
407
|
+
333 => o,
|
408
|
+
777 => r,
|
409
|
+
1.23 => r,
|
410
|
+
'Str.Key' => r
|
398
411
|
}), o.p_hash
|
399
412
|
end
|
400
|
-
end
|
401
|
-
end
|
413
|
+
end
|
414
|
+
end
|
402
415
|
|
403
416
|
module GenericRuntimeTests
|
404
417
|
include Rubernate
|
@@ -415,8 +428,13 @@ module GenericRuntimeTests
|
|
415
428
|
}
|
416
429
|
Rubernate.configuration = @factory
|
417
430
|
Thread.current[:on_save] = Thread.current[:on_load] = nil
|
431
|
+
Rubernate.settings.clear
|
418
432
|
end
|
419
433
|
|
434
|
+
def teardown
|
435
|
+
Rubernate.settings.clear
|
436
|
+
end
|
437
|
+
|
420
438
|
def test_session
|
421
439
|
Rubernate.session {|runtime|
|
422
440
|
assert runtime
|
@@ -496,14 +514,14 @@ module GenericRuntimeTests
|
|
496
514
|
assert pk
|
497
515
|
Rubernate.session { |runtime|
|
498
516
|
o = runtime.find_by_pk pk
|
499
|
-
assert !o.
|
517
|
+
assert !o.__peer.dirty?
|
500
518
|
assert_not_nil Thread.current[:on_load]
|
501
519
|
o.p0 = 'o.p0'
|
502
520
|
}
|
503
521
|
assert_not_nil Thread.current[:on_save]
|
504
522
|
Rubernate.session { |runtime|
|
505
523
|
o = runtime.find_by_pk pk
|
506
|
-
assert !o.
|
524
|
+
assert !o.__peer.dirty?
|
507
525
|
assert_equal 'o.p0', o.p0
|
508
526
|
}
|
509
527
|
end
|
@@ -674,14 +692,63 @@ module GenericRuntimeTests
|
|
674
692
|
o1, o1.p1, pk1 = C1.new, 'obj1'
|
675
693
|
o2, o2.p1, pk2 = C2.new, o1
|
676
694
|
o2.p2 = [o2]
|
677
|
-
Rubernate.session
|
678
|
-
|
695
|
+
Rubernate.session do
|
696
|
+
o1.attach; pk1 = o1.primary_key
|
697
|
+
o2.attach; pk2 = o2.primary_key
|
698
|
+
end
|
699
|
+
Rubernate.session do
|
679
700
|
o1 = find_by_pk pk1
|
680
701
|
o2 = find_by_pk pk2
|
681
702
|
assert_equal 'obj1', o1.p1
|
682
|
-
assert_equal o1,
|
683
|
-
assert_equal [o2],
|
703
|
+
assert_equal o1, o2.p1
|
704
|
+
assert_equal [o2], o2.p2
|
684
705
|
end
|
685
|
-
end
|
706
|
+
end
|
707
|
+
|
708
|
+
def test_lazy_load_strategies
|
709
|
+
o1, pk = nil
|
710
|
+
Rubernate.session do
|
711
|
+
o1 = C2.new.attach
|
712
|
+
pk = o1.primary_key
|
713
|
+
o1.p1 = C1.new.attach
|
714
|
+
o1.p1.p1 = 'zero'
|
715
|
+
o1.p2 = {}
|
716
|
+
o1.p2[1] = C1.new.attach
|
717
|
+
o1.p2[1].p1 = 'first'
|
718
|
+
o1.p2[2] = C1.new.attach
|
719
|
+
o1.p2[2].p1 = 'second'
|
720
|
+
end
|
721
|
+
|
722
|
+
Rubernate.session do
|
723
|
+
o1 = find_by_pk pk
|
724
|
+
assert_equal 'first', o1.p2[1].p1
|
725
|
+
assert o1.p1.__peer.is_a?(LazyLoader)
|
726
|
+
assert o1.p2[1].__peer.is_a?(Peer)
|
727
|
+
assert o1.p2[2].__peer.is_a?(LazyLoader)
|
728
|
+
end
|
729
|
+
|
730
|
+
# Test bulk param lazy loading
|
731
|
+
Rubernate.session do |rt|
|
732
|
+
rt.settings[:lazy_load] = :collection
|
733
|
+
o1 = find_by_pk pk
|
734
|
+
assert o1.p2[1].__peer.is_a?(ParamLazyLoader)
|
735
|
+
assert o1.p2[2].__peer.is_a?(ParamLazyLoader)
|
736
|
+
assert_equal 'first', o1.p2[1].p1
|
737
|
+
assert o1.p1.__peer.is_a?(LazyLoader)
|
738
|
+
assert o1.p2[1].__peer.is_a?(Peer)
|
739
|
+
assert o1.p2[2].__peer.is_a?(Peer)
|
740
|
+
end
|
741
|
+
|
742
|
+
Rubernate.settings[:lazy_load] = :holder
|
743
|
+
# Test host object lazy loading
|
744
|
+
Rubernate.session do |rt|
|
745
|
+
o1 = find_by_pk pk
|
746
|
+
assert o1.p2[1].__peer.is_a?(HolderLazyLoader)
|
747
|
+
assert o1.p2[2].__peer.is_a?(HolderLazyLoader)
|
748
|
+
assert_equal 'zero', o1.p1.p1
|
749
|
+
assert o1.p2[1].__peer.is_a?(Peer)
|
750
|
+
assert o1.p2[2].__peer.is_a?(Peer)
|
751
|
+
end
|
752
|
+
end
|
686
753
|
end
|
687
754
|
end
|
@@ -12,6 +12,26 @@ module DBI
|
|
12
12
|
def setup
|
13
13
|
init ORA_RUNTIME_CLASS, ORA_DB_URL, ORA_DB_USER, ORA_DB_PWD
|
14
14
|
end
|
15
|
+
=begin
|
16
|
+
# This methods test bug appers when we try
|
17
|
+
# to load or store more then 1000 objects
|
18
|
+
# it is commented because it works very slow.
|
19
|
+
def test_load_1010
|
20
|
+
Rubernate.session do
|
21
|
+
for i in 1..1010
|
22
|
+
o = C1.new.attach
|
23
|
+
o.p1 = 777
|
24
|
+
end
|
25
|
+
end
|
26
|
+
Rubernate.session do
|
27
|
+
objs = find_by_query "Select :o; Where o.klass == #{C1}"
|
28
|
+
assert_equal 1010, objs.size
|
29
|
+
for o in objs
|
30
|
+
assert_equal 777, o.p1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
=end
|
15
35
|
end
|
16
36
|
|
17
37
|
class OracleRuntimeTest < Test::Unit::TestCase
|
@@ -33,7 +33,7 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
|
|
33
33
|
# Interface Tests
|
34
34
|
def test_load_by_pk
|
35
35
|
assert_nil @runtime.load_by_pk(1)
|
36
|
-
o, o.
|
36
|
+
o, o.__peer = C4.new(1), Peer.new
|
37
37
|
@runtime.save o
|
38
38
|
o = @runtime.load_by_pk(1)
|
39
39
|
assert_equal C4, o.class
|
@@ -42,8 +42,8 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_load_by_query_all
|
45
|
-
o1, o1.
|
46
|
-
o2, o2.
|
45
|
+
o1, o1.__peer, o1.p1 = C1.new, Peer.new, 'o1.p1'
|
46
|
+
o2, o2.__peer, o2.p1, o2.p2 = C2.new, Peer.new, 'o2.p1', o1
|
47
47
|
@runtime.create o1
|
48
48
|
@runtime.create o2
|
49
49
|
result = @runtime.load_by_query "all"
|
@@ -62,11 +62,11 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def test_create
|
65
|
-
o, o.
|
65
|
+
o, o.__peer = C2.new, Peer.new
|
66
66
|
assert_nil o.primary_key
|
67
67
|
@runtime.create o
|
68
68
|
assert_not_nil o.primary_key
|
69
|
-
assert_not_nil o.
|
69
|
+
assert_not_nil o.__peer
|
70
70
|
end
|
71
71
|
|
72
72
|
def test_create_find
|
@@ -77,41 +77,40 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def test_load_created
|
80
|
-
o, o.
|
80
|
+
o, o.__peer = C2.new, Peer.new
|
81
81
|
@runtime.create o
|
82
82
|
o = @runtime.load_by_pk o.primary_key
|
83
83
|
assert_not_nil o
|
84
|
-
assert !o.
|
84
|
+
assert !o.__peer.dirty?
|
85
85
|
assert !o.dirty?
|
86
86
|
end
|
87
87
|
|
88
88
|
def test_create_dirty
|
89
|
-
o1, o1.
|
89
|
+
o1, o1.__peer = C2.new, Peer.new
|
90
90
|
@runtime.create o1
|
91
|
-
assert_not_nil o1.
|
92
|
-
assert !o1.
|
91
|
+
assert_not_nil o1.__peer
|
92
|
+
assert !o1.__peer.dirty?
|
93
93
|
end
|
94
94
|
|
95
95
|
def test_save
|
96
|
-
o, o.
|
96
|
+
o, o.__peer = C4.new(1), Peer.new
|
97
97
|
o.p1, o.p2 = C4.new(2), {'k1' => C4.new(3)}
|
98
98
|
@runtime.save o
|
99
99
|
o = @runtime.load_by_pk 1
|
100
100
|
assert_not_nil o
|
101
|
-
assert_not_nil o.
|
102
|
-
assert_equal 2, o.
|
101
|
+
assert_not_nil o.__peer
|
102
|
+
assert_equal 2, o.__peer.size
|
103
103
|
assert_equal 2, o.p1.primary_key
|
104
104
|
assert_equal 3, o.p2['k1'].primary_key
|
105
105
|
end
|
106
106
|
|
107
107
|
def test_delete
|
108
|
-
o, o.
|
108
|
+
o, o.__peer = C1.new, Peer.new
|
109
109
|
@runtime.create o
|
110
110
|
pk = o.primary_key
|
111
111
|
@runtime.delete o
|
112
112
|
assert_nil @runtime.load_by_pk(pk)
|
113
|
-
end
|
114
|
-
|
113
|
+
end
|
115
114
|
|
116
115
|
# Internal Tests
|
117
116
|
def test_reference_eq
|
@@ -122,7 +121,7 @@ class Rubernate::Memory::RuntimeTest < Test::Unit::TestCase
|
|
122
121
|
end
|
123
122
|
|
124
123
|
def test_save_peer
|
125
|
-
obj, obj.
|
124
|
+
obj, obj.__peer = C4.new(1), Peer.new
|
126
125
|
obj.p1, obj.p2 = 2, [C4.new(2)]
|
127
126
|
data = @runtime.save_peer obj
|
128
127
|
assert_equal({:p1 => 2, :p2 => [ref(2, C4)]}, data)
|