odba 1.1.9 → 1.2.1

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/devenv.yml +34 -0
  3. data/.github/workflows/ruby.yml +8 -8
  4. data/.gitignore +15 -0
  5. data/History.md +131 -0
  6. data/README.md +77 -1
  7. data/Rakefile +3 -15
  8. data/devenv.lock +171 -0
  9. data/devenv.nix +66 -0
  10. data/devenv.yaml +16 -0
  11. data/lib/odba/cache.rb +395 -344
  12. data/lib/odba/cache_entry.rb +42 -31
  13. data/lib/odba/connection_pool.rb +99 -72
  14. data/lib/odba/drbwrapper.rb +26 -18
  15. data/lib/odba/id_server.rb +20 -20
  16. data/lib/odba/index.rb +249 -215
  17. data/lib/odba/index_definition.rb +17 -17
  18. data/lib/odba/marshal.rb +15 -14
  19. data/lib/odba/odba.rb +40 -32
  20. data/lib/odba/odba_error.rb +4 -2
  21. data/lib/odba/persistable.rb +460 -414
  22. data/lib/odba/storage.rb +328 -277
  23. data/lib/odba/stub.rb +166 -153
  24. data/lib/odba/version.rb +1 -1
  25. data/lib/odba.rb +9 -9
  26. data/odba.gemspec +8 -3
  27. data/test/example.rb +47 -0
  28. data/test/helper.rb +6 -0
  29. data/test/suite.rb +7 -0
  30. data/test/test_array.rb +38 -33
  31. data/test/test_cache.rb +666 -622
  32. data/test/test_cache_entry.rb +51 -71
  33. data/test/test_connection_pool.rb +70 -25
  34. data/test/test_drbwrapper.rb +31 -20
  35. data/test/test_hash.rb +98 -89
  36. data/test/test_id_server.rb +30 -32
  37. data/test/test_index.rb +191 -158
  38. data/test/test_marshal.rb +15 -25
  39. data/test/test_persistable.rb +378 -369
  40. data/test/test_storage.rb +510 -471
  41. data/test/test_stub.rb +147 -135
  42. metadata +63 -20
  43. data/Guide.txt +0 -36
  44. data/History.txt +0 -91
  45. data/Manifest.txt +0 -38
  46. data/install.rb +0 -1098
  47. data/lib/odba/18_19_loading_compatibility.rb +0 -77
data/test/test_cache.rb CHANGED
@@ -1,593 +1,631 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: utf-8
3
2
  # ODBA::TestCache -- odba -- 09.12.2011 -- mhatakeyama@ywesee.com
4
-
5
- $: << File.dirname(__FILE__)
6
- $: << File.expand_path('../lib/', File.dirname(__FILE__))
7
-
8
- require 'minitest/autorun'
9
- require 'flexmock/test_unit'
10
- require 'flexmock'
11
- require 'odba/cache'
12
- require 'odba/cache_entry'
13
- require 'odba/persistable'
14
- require 'odba/index'
15
- require 'odba/index_definition'
16
- require 'odba/odba_error'
17
- require 'odba/odba'
3
+ require_relative "helper"
4
+ require "odba/cache"
5
+ require "odba/cache_entry"
6
+ require "odba/persistable"
7
+ require "odba/index"
8
+ require "odba/index_definition"
9
+ require "odba/odba_error"
10
+ require "odba/odba"
18
11
 
19
12
  module ODBA
20
- class Cache
21
- CLEANING_INTERVAL = 0
22
- MAIL_RECIPIENTS = []
23
- MAIL_FROM = "test@testfirst.local"
24
- attr_accessor :cleaner, :fetched, :cache_entry_factory, :prefetched
25
- attr_writer :indices
26
- public :load_object
27
- end
28
- class TestCache < Minitest::Test
13
+ class Cache
14
+ CLEANING_INTERVAL = 0
15
+ MAIL_RECIPIENTS = []
16
+ MAIL_FROM = "test@testfirst.local"
17
+ attr_accessor :cleaner, :fetched, :cache_entry_factory, :prefetched
18
+ attr_writer :indices
19
+ public :load_object
20
+ end
21
+
22
+ class TestCache < Test::Unit::TestCase
29
23
  include FlexMock::TestCase
30
- class ODBAContainerInCache
31
- include ODBA::Persistable
32
- attr_accessor :odba_connection, :odba_id
33
- end
34
- def setup
35
- @storage = ODBA.storage = flexmock("storage")
36
- @marshal = ODBA.marshaller = flexmock("marshaller")
37
- ODBA.cache = @cache = ODBA::Cache.instance
38
- @cache.fetched = {}
39
- @cache.prefetched = {}
40
- @cache.indices = {}
41
- end
42
- def teardown
24
+ class ODBAContainerInCache
25
+ include ODBA::Persistable
26
+ attr_accessor :odba_connection, :odba_id
27
+ end
28
+
29
+ def setup
30
+ @storage = ODBA.storage = flexmock("storage")
31
+ @marshal = ODBA.marshaller = flexmock("marshaller")
32
+ ODBA.cache = @cache = ODBA::Cache.instance
33
+ @cache.fetched = {}
34
+ @cache.prefetched = {}
35
+ @cache.indices = {}
36
+ ## CacheEntry registers an ObjectSpace finalizer that calls
37
+ ## ODBA.cache.invalidate, which deletes the odba_id from the shared
38
+ ## singleton cache. Objects left behind by earlier tests are collected at
39
+ ## unpredictable moments, so without dropping their finalizer bookkeeping
40
+ ## a stray GC run deletes the very ids this test just stored. That is the
41
+ ## long standing flakiness in test_clean, test_clean__prefetched and
42
+ ## test_transaction: they failed on some Ruby versions and not others,
43
+ ## and a different test failed on each run of identical code.
44
+ ODBA::CacheEntry.class_variable_set(:@@id_table, {})
45
+ end
46
+
47
+ def teardown
43
48
  super
44
- ODBA.storage = nil
45
- ODBA.marshaller = nil
46
- @cache.fetched.clear
47
- @cache.prefetched.clear
48
- @cache.indices.clear
49
- end
50
- def test_fetch_named_ok
51
- obj = flexmock
52
- obj.instance_variable_set("@odba_name", 'the_name')
53
- obj.instance_variable_set("@odba_id", 2)
54
- @marshal.should_receive(:load).with('dump2').and_return(obj)
55
- @storage.should_receive(:restore_named).and_return('dump2')
56
- @storage.should_receive(:restore_collection).and_return([])
57
- load_1 = @cache.fetch_named('the_name', nil)
58
- assert_instance_of(FlexMock, load_1)
59
- assert_equal('the_name', load_1.odba_name)
60
- assert_equal(2, load_1.odba_id)
61
- load_2 = @cache.fetch_named('the_name', nil)
62
- assert_equal(load_1, load_2)
63
- load_3 = @cache.fetch(2, nil)
64
- assert_equal(load_1, load_3)
65
- end
66
- def test_bulk_fetch_load_all
67
- old_marshal = ODBA.marshaller
68
- ODBA.marshaller = marshal = flexmock('Marshal')
69
- array = [2, 3]
70
- obj1 = Object.new
71
- obj1.instance_variable_set("@odba_id", 2)
72
- obj1.instance_variable_set("@odba_prefetch", false)
73
- obj1.instance_variable_set("@odba_name", nil)
74
- obj2 = Object.new
75
- obj2.instance_variable_set("@odba_id", 3)
76
- obj2.instance_variable_set("@odba_prefetch", true)
77
- obj2.instance_variable_set("@odba_name", nil)
78
- dump_1 = Marshal.dump(obj1)
79
- dump_2 = Marshal.dump(obj2)
80
- @storage.should_receive(:bulk_restore)\
81
- .and_return([[2, dump_1],[3, dump_2]])
49
+ ODBA.storage = nil
50
+ ODBA.marshaller = nil
51
+ @cache.fetched.clear
52
+ @cache.prefetched.clear
53
+ @cache.indices.clear
54
+ end
55
+
56
+ def test_fetch_named_ok
57
+ obj = flexmock
58
+ obj.instance_variable_set(:@odba_name, "the_name")
59
+ obj.instance_variable_set(:@odba_id, 2)
60
+ @marshal.should_receive(:load).with("dump2").and_return(obj)
61
+ @storage.should_receive(:restore_named).and_return("dump2")
62
+ @storage.should_receive(:restore_collection).and_return([])
63
+ load_1 = @cache.fetch_named("the_name", nil)
64
+ assert_instance_of(FlexMock, load_1)
65
+ assert_equal("the_name", load_1.odba_name)
66
+ assert_equal(2, load_1.odba_id)
67
+ load_2 = @cache.fetch_named("the_name", nil)
68
+ assert_equal(load_1, load_2)
69
+ load_3 = @cache.fetch(2, nil)
70
+ assert_equal(load_1, load_3)
71
+ end
72
+
73
+ def test_bulk_fetch_load_all
74
+ old_marshal = ODBA.marshaller
75
+ ODBA.marshaller = marshal = flexmock("Marshal")
76
+ array = [2, 3]
77
+ obj1 = Object.new
78
+ obj1.instance_variable_set(:@odba_id, 2)
79
+ obj1.instance_variable_set(:@odba_prefetch, false)
80
+ obj1.instance_variable_set(:@odba_name, nil)
81
+ obj2 = Object.new
82
+ obj2.instance_variable_set(:@odba_id, 3)
83
+ obj2.instance_variable_set(:@odba_prefetch, true)
84
+ obj2.instance_variable_set(:@odba_name, nil)
85
+ dump_1 = Marshal.dump(obj1)
86
+ dump_2 = Marshal.dump(obj2)
87
+ @storage.should_receive(:bulk_restore)
88
+ .and_return([[2, dump_1], [3, dump_2]])
82
89
  dumps = [dump_1, dump_2]
83
90
  objs = [obj1, obj2]
84
- marshal.should_receive(:load).and_return { |dump|
85
- assert_equal(dumps.shift, dump)
86
- objs.shift
87
- }
88
- @storage.should_receive(:restore_collection).and_return([])
89
- loaded = @cache.bulk_fetch(array, nil)
90
- assert_equal([obj1, obj2], loaded)
91
- assert_equal([2], @cache.fetched.keys)
92
- assert_equal([3], @cache.prefetched.keys)
93
- assert_instance_of(CacheEntry, @cache.fetched[2])
94
- assert_instance_of(CacheEntry, @cache.prefetched[3])
95
- ensure
96
- ODBA.marshaller = old_marshal
97
- end
98
- def test_bulk_fetch
99
- old_marshal = ODBA.marshaller
100
- ODBA.marshaller = marshal = flexmock('Marshal')
101
- array = [2, 3, 7]
102
- baz = flexmock('loaded')
103
- baz.should_receive(:odba_add_reference).and_return { |odba_caller| }
104
- baz.should_receive(:odba_object).and_return { 'foo' }
105
- @cache.fetched = {
106
- 7 => baz
107
- }
108
- obj1 = Object.new
109
- obj1.instance_variable_set("@odba_id", 2)
110
- obj1.instance_variable_set("@odba_prefetch", false)
111
- obj1.instance_variable_set("@odba_name", nil)
112
- obj2 = Object.new
113
- obj2.instance_variable_set("@odba_id", 3)
114
- obj2.instance_variable_set("@odba_prefetch", false)
115
- obj2.instance_variable_set("@odba_name", nil)
116
- dump_1 = Marshal.dump(obj1)
117
- dump_2 = Marshal.dump(obj2)
91
+ marshal.should_receive(:load).and_return { |dump|
92
+ assert_equal(dumps.shift, dump)
93
+ objs.shift
94
+ }
95
+ @storage.should_receive(:restore_collection).and_return([])
96
+ loaded = @cache.bulk_fetch(array, nil)
97
+ assert_equal([obj1, obj2], loaded)
98
+ assert_equal([2], @cache.fetched.keys)
99
+ assert_equal([3], @cache.prefetched.keys)
100
+ assert_instance_of(CacheEntry, @cache.fetched[2])
101
+ assert_instance_of(CacheEntry, @cache.prefetched[3])
102
+ ensure
103
+ ODBA.marshaller = old_marshal
104
+ end
105
+
106
+ def test_bulk_fetch
107
+ old_marshal = ODBA.marshaller
108
+ ODBA.marshaller = marshal = flexmock("Marshal")
109
+ array = [2, 3, 7]
110
+ baz = flexmock("loaded")
111
+ baz.should_receive(:odba_add_reference).and_return { |odba_caller| }
112
+ baz.should_receive(:odba_object).and_return { "foo" }
113
+ @cache.fetched = {
114
+ 7 => baz
115
+ }
116
+ obj1 = Object.new
117
+ obj1.instance_variable_set(:@odba_id, 2)
118
+ obj1.instance_variable_set(:@odba_prefetch, false)
119
+ obj1.instance_variable_set(:@odba_name, nil)
120
+ obj2 = Object.new
121
+ obj2.instance_variable_set(:@odba_id, 3)
122
+ obj2.instance_variable_set(:@odba_prefetch, false)
123
+ obj2.instance_variable_set(:@odba_name, nil)
124
+ dump_1 = Marshal.dump(obj1)
125
+ dump_2 = Marshal.dump(obj2)
118
126
  dumps = [dump_1, dump_2]
119
127
  objs = [obj1, obj2]
120
- marshal.should_receive(:load).and_return { |dump|
121
- assert_equal(dumps.shift, dump)
122
- objs.shift
123
- }
124
- @storage.should_receive(:bulk_restore).and_return([[2, dump_1],[3, dump_2]])
125
- @storage.should_receive(:restore_collection).and_return([])
126
- @cache.bulk_fetch(array, nil)
127
- assert_equal(true, @cache.fetched.has_key?(2))
128
- assert_equal(3, @cache.fetched.size)
129
- assert_equal(true, @cache.fetched.has_key?(3))
130
- ODBA.marshaller = old_marshal
131
- end
132
- def test_bulk_restore_in_fetchedadd_caller
133
- obj = Object.new
134
- cache_entry = flexmock('cache_entry')
135
- cache_entry.should_receive(:odba_object).and_return {
136
- obj
137
- }
138
- cache_entry.should_receive(:_odba_object).and_return {
139
- obj
140
- }
141
- cache_entry.should_receive(:odba_add_reference).and_return {|caller|
142
- #this assert_equal is interesting
143
- assert_equal('Reference', caller)
144
- }
145
- @cache.fetched.store(1, cache_entry)
146
- rows = [[1, '']]
147
- retrieved = @cache.bulk_restore(rows, 'Reference')
148
- end
149
- def test_clean
150
- obj1 = flexmock
151
- obj2 = flexmock
152
- @cache.fetched.store(2, obj1)
153
- @cache.fetched.store(3, obj2)
154
- assert_equal(2, @cache.fetched.size)
155
- obj1.should_receive(:odba_old?).and_return { true }
156
- obj1.should_receive(:odba_retire).and_return { }
157
- obj2.should_receive(:odba_old?).and_return { false }
158
- @cache.clean
159
- assert_equal(2, @cache.fetched.size)
160
- end
161
- def test_clean__prefetched
162
- obj1 = flexmock
163
- obj2 = flexmock
164
- @cache.prefetched.store(2, obj1)
165
- @cache.prefetched.store(3, obj2)
166
- assert_equal(2, @cache.prefetched.size)
167
- obj1.should_receive(:ready_to_destroy?).and_return { false }
168
- obj1.should_receive(:odba_old?).and_return { true }
169
- obj1.should_receive(:odba_retire).and_return { }
170
- obj2.should_receive(:ready_to_destroy?).and_return { false }
171
- obj2.should_receive(:odba_old?).and_return { false }
172
- @cache.clean_prefetched
173
- assert_equal(2, @cache.prefetched.size)
174
- end
175
- def test_clear
176
- value = flexmock("value")
177
- obj = flexmock('object')
178
- prefetched = flexmock('prefetched')
179
- @cache.fetched.store(12, value)
180
- @cache.prefetched.store(13, prefetched)
181
- assert_equal(2, @cache.size)
128
+ marshal.should_receive(:load).and_return { |dump|
129
+ assert_equal(dumps.shift, dump)
130
+ objs.shift
131
+ }
132
+ @storage.should_receive(:bulk_restore).and_return([[2, dump_1], [3, dump_2]])
133
+ @storage.should_receive(:restore_collection).and_return([])
134
+ @cache.bulk_fetch(array, nil)
135
+ assert_equal(true, @cache.fetched.has_key?(2))
136
+ assert_equal(3, @cache.fetched.size)
137
+ assert_equal(true, @cache.fetched.has_key?(3))
138
+ ODBA.marshaller = old_marshal
139
+ end
140
+
141
+ def test_bulk_restore_in_fetchedadd_caller
142
+ obj = Object.new
143
+ cache_entry = flexmock("cache_entry")
144
+ cache_entry.should_receive(:odba_object).and_return {
145
+ obj
146
+ }
147
+ cache_entry.should_receive(:_odba_object).and_return {
148
+ obj
149
+ }
150
+ cache_entry.should_receive(:odba_add_reference).and_return { |caller|
151
+ # this assert_equal is interesting
152
+ assert_equal("Reference", caller)
153
+ }
154
+ @cache.fetched.store(1, cache_entry)
155
+ rows = [[1, ""]]
156
+ @cache.bulk_restore(rows, "Reference")
157
+ end
158
+
159
+ def test_clean
160
+ obj1 = flexmock
161
+ obj2 = flexmock
162
+ @cache.fetched.store(2, obj1)
163
+ @cache.fetched.store(3, obj2)
164
+ assert_equal(2, @cache.fetched.size)
165
+ obj1.should_receive(:odba_old?).and_return { true }
166
+ obj1.should_receive(:odba_retire).and_return {}
167
+ obj2.should_receive(:odba_old?).and_return { false }
168
+ @cache.clean
169
+ assert_equal(2, @cache.fetched.size)
170
+ end
171
+
172
+ def test_clean__prefetched
173
+ obj1 = flexmock
174
+ obj2 = flexmock
175
+ @cache.prefetched.store(2, obj1)
176
+ @cache.prefetched.store(3, obj2)
177
+ assert_equal(2, @cache.prefetched.size)
178
+ obj1.should_receive(:ready_to_destroy?).and_return { false }
179
+ obj1.should_receive(:odba_old?).and_return { true }
180
+ obj1.should_receive(:odba_retire).and_return {}
181
+ obj2.should_receive(:ready_to_destroy?).and_return { false }
182
+ obj2.should_receive(:odba_old?).and_return { false }
183
+ @cache.clean_prefetched
184
+ assert_equal(2, @cache.prefetched.size)
185
+ end
186
+
187
+ def test_clear
188
+ value = flexmock("value")
189
+ flexmock("object")
190
+ prefetched = flexmock("prefetched")
191
+ @cache.fetched.store(12, value)
192
+ @cache.prefetched.store(13, prefetched)
193
+ assert_equal(2, @cache.size)
182
194
  @cache.clear
183
- assert_equal(0, @cache.size)
184
- end
185
- def test_fetch_named_block
186
- restore = flexmock("restore")
187
- caller = flexmock("caller")
188
- caller2 = flexmock("caller2")
189
- @storage.should_receive(:restore_named).and_return { |name| }
190
- restore.should_receive(:odba_name=).and_return { |name| }
191
- restore.should_receive(:odba_store).and_return { |obj| }
192
- result = @cache.fetch_named("foo", caller2) {
193
- restore
194
- }
195
- assert_equal(restore, result)
196
- end
197
- def prepare_fetch(id, receiver)
198
- @storage.should_receive(:restore).and_return { |odba_id|
199
- assert_equal(id, odba_id)
200
- odba_id
201
- }
202
- @marshal.should_receive(:load).and_return { receiver }
203
- if(receiver.is_a?(FlexMock))
204
- receiver.should_receive(:odba_restore).and_return {}
205
- receiver.should_receive(:odba_name).and_return {}
206
- end
207
- end
208
- def test_fetch_has_name
209
- caller1 = flexmock('Caller1')
210
- caller2 = flexmock('Caller2')
211
- caller3 = flexmock('Caller3')
212
- receiver = flexmock('Receiver')
213
- @storage.should_receive(:restore).and_return { |odba_id|
214
- assert_equal(23, odba_id)
215
- odba_id
216
- }
217
- @marshal.should_receive(:load).and_return {|dump|
218
- receiver
219
- }
220
- @storage.should_receive(:restore_collection).and_return {|*args|
221
- []
222
- }
223
- receiver.instance_variable_set("@odba_id", 23)
224
- receiver.instance_variable_set("@odba_name", 'name')
225
- first_fetch = @cache.fetch(23, caller1)
226
- assert_equal(receiver, first_fetch)
227
- assert_equal(2, @cache.fetched.size)
228
- assert(@cache.fetched.include?('name'))
229
- second_fetch = @cache.fetch(23, caller2)
230
- assert_equal(receiver, second_fetch)
231
- named_fetch = @cache.fetch_named('name', caller3)
232
- assert_equal(receiver, named_fetch)
233
- end
234
- def test_fetch_error
235
- receiver = flexmock
236
- @storage.should_receive(:restore).and_return { |odba_id|
237
- nil
238
- }
239
- assert_raises(OdbaError) {
240
- @cache.load_object(23, receiver)
241
- }
242
- end
243
- def test_fetch__adds_reference
244
- obj = flexmock
245
- obj.instance_variable_set("@odba_id", 23)
246
- obj.instance_variable_set("@odba_prefetch", false)
247
- @storage.should_receive(:restore).and_return { |id|
248
- assert_equal(23, id)
249
- 'dump'
250
- }
251
- @storage.should_receive(:restore_collection).and_return { |id|
252
- []
253
- }
254
- @marshal.should_receive(:load).and_return { |dump|
255
- assert_equal('dump', dump)
256
- obj
257
- }
258
- callr = flexmock
259
- res = @cache.fetch(23, callr)
260
- cache_entry = @cache.fetched[23]
261
- assert_instance_of(CacheEntry, cache_entry)
195
+ assert_equal(0, @cache.size)
196
+ end
197
+
198
+ def test_fetch_named_block
199
+ restore = flexmock("restore")
200
+ flexmock("caller")
201
+ caller2 = flexmock("caller2")
202
+ @storage.should_receive(:restore_named).and_return { |name| }
203
+ restore.should_receive(:odba_name=).and_return { |name| }
204
+ restore.should_receive(:odba_store).and_return { |obj| }
205
+ result = @cache.fetch_named("foo", caller2) {
206
+ restore
207
+ }
208
+ assert_equal(restore, result)
209
+ end
210
+
211
+ def prepare_fetch(id, receiver)
212
+ @storage.should_receive(:restore).and_return { |odba_id|
213
+ assert_equal(id, odba_id)
214
+ odba_id
215
+ }
216
+ @marshal.should_receive(:load).and_return { receiver }
217
+ if receiver.is_a?(FlexMock)
218
+ receiver.should_receive(:odba_restore).and_return {}
219
+ receiver.should_receive(:odba_name).and_return {}
220
+ end
221
+ end
222
+
223
+ def test_fetch_has_name
224
+ caller1 = flexmock("Caller1")
225
+ caller2 = flexmock("Caller2")
226
+ caller3 = flexmock("Caller3")
227
+ receiver = flexmock("Receiver")
228
+ @storage.should_receive(:restore).and_return { |odba_id|
229
+ assert_equal(23, odba_id)
230
+ odba_id
231
+ }
232
+ @marshal.should_receive(:load).and_return { |dump|
233
+ receiver
234
+ }
235
+ @storage.should_receive(:restore_collection).and_return { |*args|
236
+ []
237
+ }
238
+ receiver.instance_variable_set(:@odba_id, 23)
239
+ receiver.instance_variable_set(:@odba_name, "name")
240
+ first_fetch = @cache.fetch(23, caller1)
241
+ assert_equal(receiver, first_fetch)
242
+ assert_equal(2, @cache.fetched.size)
243
+ assert(@cache.fetched.include?("name"))
244
+ second_fetch = @cache.fetch(23, caller2)
245
+ assert_equal(receiver, second_fetch)
246
+ named_fetch = @cache.fetch_named("name", caller3)
247
+ assert_equal(receiver, named_fetch)
248
+ end
249
+
250
+ def test_fetch_error
251
+ receiver = flexmock
252
+ @storage.should_receive(:restore).and_return { |odba_id|
253
+ nil
254
+ }
255
+ assert_raises(OdbaError) {
256
+ @cache.load_object(23, receiver)
257
+ }
258
+ end
259
+
260
+ def test_fetch__adds_reference
261
+ obj = flexmock
262
+ obj.instance_variable_set(:@odba_id, 23)
263
+ obj.instance_variable_set(:@odba_prefetch, false)
264
+ @storage.should_receive(:restore).and_return { |id|
265
+ assert_equal(23, id)
266
+ "dump"
267
+ }
268
+ @storage.should_receive(:restore_collection).and_return { |id|
269
+ []
270
+ }
271
+ @marshal.should_receive(:load).and_return { |dump|
272
+ assert_equal("dump", dump)
273
+ obj
274
+ }
275
+ callr = flexmock
276
+ res = @cache.fetch(23, callr)
277
+ cache_entry = @cache.fetched[23]
278
+ assert_instance_of(CacheEntry, cache_entry)
262
279
  assert_equal([callr.object_id], cache_entry.accessed_by.keys)
263
- assert_equal(obj, res)
280
+ assert_equal(obj, res)
264
281
  ## test for duplicates
265
- @cache.fetch(23, callr)
282
+ @cache.fetch(23, callr)
266
283
  assert_equal([callr.object_id], cache_entry.accessed_by.keys)
267
- end
268
- def test_store
269
- cont = flexmock('CacheEntry')
284
+ end
285
+
286
+ def test_store
287
+ cont = flexmock("CacheEntry")
270
288
  @cache.fetched.store(3, cont)
271
- save_obj = flexmock("save_obj")
289
+ save_obj = flexmock("save_obj")
272
290
  save_obj.should_receive(:odba_target_ids).and_return([3])
273
291
  save_obj.should_receive(:odba_observers).and_return { [] }
274
- prepare_store([save_obj])
292
+ prepare_store([save_obj])
275
293
  save_obj.should_receive(:odba_add_observer)
276
- cont.should_receive(:odba_add_reference).with(save_obj)\
294
+ cont.should_receive(:odba_add_reference).with(save_obj)
277
295
  .and_return { assert(true) }
278
- @cache.store(save_obj)
279
- end
280
- def test_store_collection_elements
281
- old_mar = ODBA.marshaller
282
- ODBA.marshaller = Marshal
283
-
284
- old_collection = [['key1', 'val1'], ['key2', 'val2']]
285
- new_collection = [['key2', 'val2'], ['key3', 'val3']]
286
-
287
- cache_entry = flexmock('cache_entry')
288
- cache_entry.should_receive(:collection).and_return { old_collection }
289
- cache_entry.should_receive(:collection=).and_return { |col|
290
- assert_equal(new_collection, col)
291
- }
292
-
293
- @storage.should_receive(:restore_collection).and_return {
296
+ @cache.store(save_obj)
297
+ end
298
+
299
+ def test_store_collection_elements
300
+ old_mar = ODBA.marshaller
301
+ ODBA.marshaller = Marshal
302
+
303
+ old_collection = [["key1", "val1"], ["key2", "val2"]]
304
+ new_collection = [["key2", "val2"], ["key3", "val3"]]
305
+
306
+ cache_entry = flexmock("cache_entry")
307
+ cache_entry.should_receive(:collection).and_return { old_collection }
308
+ cache_entry.should_receive(:collection=).and_return { |col|
309
+ assert_equal(new_collection, col)
310
+ }
311
+
312
+ @storage.should_receive(:restore_collection).and_return {
294
313
  old_collection.collect { |key, val|
295
314
  [Marshal.dump(key.odba_isolated_stub),
296
315
  Marshal.dump(val.odba_isolated_stub)]
297
316
  }
298
317
  }
299
- @storage.should_receive(:collection_remove).and_return { |odba_id, key|
300
- assert_equal(54, odba_id)
301
- assert_equal(Marshal.dump('key1'.odba_isolated_stub), key)
302
- }
303
- @storage.should_receive(:collection_store).and_return { |odba_id, key, value|
304
- assert_equal(54, odba_id)
305
- assert_equal(Marshal.dump('key3'.odba_isolated_stub), key)
306
- assert_equal(Marshal.dump('val3'.odba_isolated_stub), value)
307
- }
308
-
309
- @cache.fetched = {
310
- 54 => cache_entry
311
- }
312
-
313
- obj = flexmock('Obj')
314
- obj.should_receive(:odba_id).and_return { 54 }
315
- obj.should_receive(:odba_collection).and_return { new_collection }
316
- @cache.store_collection_elements(obj)
318
+ @storage.should_receive(:collection_remove).and_return { |odba_id, key|
319
+ assert_equal(54, odba_id)
320
+ assert_equal(Marshal.dump("key1".odba_isolated_stub), key)
321
+ }
322
+ @storage.should_receive(:collection_store).and_return { |odba_id, key, value|
323
+ assert_equal(54, odba_id)
324
+ assert_equal(Marshal.dump("key3".odba_isolated_stub), key)
325
+ assert_equal(Marshal.dump("val3".odba_isolated_stub), value)
326
+ }
327
+
328
+ @cache.fetched = {
329
+ 54 => cache_entry
330
+ }
331
+
332
+ obj = flexmock("Obj")
333
+ obj.should_receive(:odba_id).and_return { 54 }
334
+ obj.should_receive(:odba_collection).and_return { new_collection }
335
+ @cache.store_collection_elements(obj)
317
336
  ensure
318
- ODBA.marshaller = old_mar
319
- end
320
- def test_store_object_connections
321
- @storage.should_receive(:ensure_object_connections).and_return { |id,target_ids|
322
- assert_equal(4, id)
323
- assert_equal([1,2], target_ids)
324
- }
325
- @cache.store_object_connections(4, [1,2])
326
- end
327
- def test_load__and_restore_object
328
- caller1 = flexmock
329
- loaded = flexmock
330
- @storage.should_receive(:restore).and_return { |odba_id|
331
- assert_equal(23, odba_id)
332
- 'dump'
333
- }
334
- @marshal.should_receive(:load).and_return { |dump|
335
- assert_equal('dump', dump)
336
- loaded
337
- }
338
- @storage.should_receive(:restore_collection).and_return { [] }
339
-
340
- loaded.instance_variable_set('@odba_id', 23)
341
- @cache.load_object(23, caller1)
342
- end
343
- def test_prefetch
344
- foo = flexmock("foo")
345
- @storage.should_receive(:restore_prefetchable).and_return {
346
- [[2, foo]]
347
- }
348
- prepare_bulk_restore([foo])
349
- @cache.prefetch
350
- end
351
- def test_fill_index
352
- foo = flexmock("foo")
353
- foo.should_receive(:fill).and_return { |target|
354
- assert_equal("baz", target)
355
- }
356
- @cache.indices = {
357
- "foo" => foo
358
- }
359
- @cache.fill_index("foo", "baz")
360
- end
361
- def test_create_index
337
+ ODBA.marshaller = old_mar
338
+ end
339
+
340
+ def test_store_object_connections
341
+ @storage.should_receive(:ensure_object_connections).and_return { |id, target_ids|
342
+ assert_equal(4, id)
343
+ assert_equal([1, 2], target_ids)
344
+ }
345
+ @cache.store_object_connections(4, [1, 2])
346
+ end
347
+
348
+ def test_load__and_restore_object
349
+ caller1 = flexmock
350
+ loaded = flexmock
351
+ @storage.should_receive(:restore).and_return { |odba_id|
352
+ assert_equal(23, odba_id)
353
+ "dump"
354
+ }
355
+ @marshal.should_receive(:load).and_return { |dump|
356
+ assert_equal("dump", dump)
357
+ loaded
358
+ }
359
+ @storage.should_receive(:restore_collection).and_return { [] }
360
+
361
+ loaded.instance_variable_set(:@odba_id, 23)
362
+ @cache.load_object(23, caller1)
363
+ end
364
+
365
+ def test_prefetch
366
+ foo = flexmock("foo")
367
+ @storage.should_receive(:restore_prefetchable).and_return {
368
+ [[2, foo]]
369
+ }
370
+ prepare_bulk_restore([foo])
371
+ @cache.prefetch
372
+ end
373
+
374
+ def test_fill_index
375
+ foo = flexmock("foo")
376
+ foo.should_receive(:fill).and_return { |target|
377
+ assert_equal("baz", target)
378
+ }
379
+ @cache.indices = {
380
+ "foo" => foo
381
+ }
382
+ @cache.fill_index("foo", "baz")
383
+ end
384
+
385
+ def test_create_index
362
386
  df = IndexDefinition.new
363
- df.index_name = 'index'
364
- indices = flexmock('indices')
365
- indices.should_receive(:store).and_return { |key, val|
366
- assert_instance_of(Index, val)
367
- }
368
- indices.should_receive(:odba_store_unsaved).times(1)
369
- @cache.instance_variable_set('@indices', indices)
370
- @storage.should_receive(:transaction).and_return { |block| block.call }
371
- @storage.should_receive(:create_index).times(1)
372
- index = @cache.create_index(df, flexmock)
373
- assert_instance_of(Index, index)
374
- @cache.instance_variable_set('@indices', {})
375
- end
376
- def test_create_index__fulltext
387
+ df.index_name = "index"
388
+ indices = flexmock("indices")
389
+ indices.should_receive(:store).and_return { |key, val|
390
+ assert_instance_of(Index, val)
391
+ }
392
+ indices.should_receive(:odba_store_unsaved).times(1)
393
+ @cache.instance_variable_set(:@indices, indices)
394
+ @storage.should_receive(:transaction).and_return { |block| block.call }
395
+ @storage.should_receive(:create_index).times(1)
396
+ index = @cache.create_index(df, flexmock)
397
+ assert_instance_of(Index, index)
398
+ @cache.instance_variable_set(:@indices, {})
399
+ end
400
+
401
+ def test_create_index__fulltext
377
402
  df = IndexDefinition.new
378
- df.index_name = 'index'
403
+ df.index_name = "index"
379
404
  df.fulltext = true
380
- indices = flexmock('indices')
381
- indices.should_receive(:store).and_return { |key, val|
382
- assert_instance_of(FulltextIndex, val)
383
- }
384
- indices.should_receive(:odba_store_unsaved).times(1)
385
- @cache.instance_variable_set('@indices', indices)
386
- @storage.should_receive(:transaction).and_return { |block| block.call }
387
- @storage.should_receive(:create_fulltext_index).times(1)
388
- index = @cache.create_index(df, flexmock)
389
- assert_instance_of(FulltextIndex, index)
390
- @cache.instance_variable_set('@indices', {})
391
- end
392
- def test_create_index__condition
405
+ indices = flexmock("indices")
406
+ indices.should_receive(:store).and_return { |key, val|
407
+ assert_instance_of(FulltextIndex, val)
408
+ }
409
+ indices.should_receive(:odba_store_unsaved).times(1)
410
+ @cache.instance_variable_set(:@indices, indices)
411
+ @storage.should_receive(:transaction).and_return { |block| block.call }
412
+ @storage.should_receive(:create_fulltext_index).times(1)
413
+ index = @cache.create_index(df, flexmock)
414
+ assert_instance_of(FulltextIndex, index)
415
+ @cache.instance_variable_set(:@indices, {})
416
+ end
417
+
418
+ def test_create_index__condition
393
419
  df = IndexDefinition.new
394
- df.index_name = 'index'
395
- df.resolve_search_term = { }
396
- indices = flexmock('indices')
397
- indices.should_receive(:store).and_return { |key, val|
398
- assert_instance_of(ConditionIndex, val)
399
- }
400
- indices.should_receive(:odba_store_unsaved).times(1)
401
- @cache.instance_variable_set('@indices', indices)
402
- @storage.should_receive(:transaction).and_return { |block| block.call }
403
- @storage.should_receive(:create_condition_index).times(1)
404
- index = @cache.create_index(df, flexmock)
405
- assert_instance_of(ConditionIndex, index)
406
- @cache.instance_variable_set('@indices', {})
407
- end
408
- def prepare_fetch_collection(obj)
409
- end
410
- def prepare_store(store_array, &block)
411
- store_array.each { |mock|
412
- # store
413
- mock.should_receive(:odba_id).and_return { || }
414
- mock.should_receive(:odba_name).and_return { || }
415
- mock.should_receive(:odba_notify_observers).and_return { |key, id1, id2|
420
+ df.index_name = "index"
421
+ df.resolve_search_term = {}
422
+ indices = flexmock("indices")
423
+ indices.should_receive(:store).and_return { |key, val|
424
+ assert_instance_of(ConditionIndex, val)
425
+ }
426
+ indices.should_receive(:odba_store_unsaved).times(1)
427
+ @cache.instance_variable_set(:@indices, indices)
428
+ @storage.should_receive(:transaction).and_return { |block| block.call }
429
+ @storage.should_receive(:create_condition_index).times(1)
430
+ index = @cache.create_index(df, flexmock)
431
+ assert_instance_of(ConditionIndex, index)
432
+ @cache.instance_variable_set(:@indices, {})
433
+ end
434
+
435
+ def prepare_fetch_collection(obj)
436
+ end
437
+
438
+ def prepare_store(store_array, &block)
439
+ store_array.each { |mock|
440
+ # store
441
+ mock.should_receive(:odba_id).and_return {}
442
+ mock.should_receive(:odba_name).and_return {}
443
+ mock.should_receive(:odba_notify_observers).and_return { |key, id1, id2|
416
444
  assert_equal(:store, key)
417
445
  }
418
- mock.should_receive(:odba_isolated_dump).and_return { || }
419
-
420
- # store_collection_elements
421
- mock.should_receive(:odba_id).and_return { || }
422
- mock.should_receive(:odba_collection).and_return {|| []}
423
- mock.should_receive(:odba_id).and_return { || }
424
-
425
- # store
426
- mock.should_receive(:odba_prefetch?).and_return { || }
427
-
428
- # store_object_connections
429
- mock.should_receive(:odba_target_ids).and_return { || [] }
430
-
431
- # update_indices
432
- mock.should_receive(:odba_indexable?).and_return {}
433
-
434
- # store_cache_entry
435
- mock.should_receive(:odba_prefetch?).and_return { || }
436
- mock.should_receive(:odba_collection).and_return { || [] }
437
-
438
- @storage.should_receive(:restore_collection).and_return { [] }
439
- if(block)
440
- @storage.should_receive(:store, &block).and_return
441
- else
442
- @storage.should_receive(:store).and_return {
443
- assert(true)
444
- }
445
- end
446
- @storage.should_receive(:ensure_object_connections).and_return {|*args|}
447
- }
448
- end
449
- def test_delete
450
- delete_item = ODBAContainerInCache.new
451
- delete_item.odba_id = 1
452
- origin_obj = ODBAContainerInCache.new
453
- origin_obj.odba_id = 2
454
- origin_obj.odba_connection = delete_item
455
- @cache.fetched.store(1, delete_item)
456
- @storage.should_receive(:retrieve_connected_objects).and_return { |id|
457
- [[2]]
458
- }
459
- prepare_fetch(2, origin_obj)
460
- @storage.should_receive(:restore_collection).and_return { |*args|
461
- []
462
- }
463
- @storage.should_receive(:store).and_return { |id, dump, name, prefetch, klass| }
464
- @storage.should_receive(:ensure_object_connections).and_return { }
465
- @storage.should_receive(:delete_persistable).and_return { |id| }
466
- @marshal.should_receive(:dump).and_return { |ob| "foo"}
467
- @cache.delete(delete_item)
468
- assert_equal(1, @cache.fetched.size)
469
- assert_nil(origin_obj.odba_connection)
470
- end
471
- def prepare_delete(mock, name, id)
472
- mock.should_receive(:odba_id).and_return { id }
473
- mock.should_receive(:odba_name).and_return { name }
474
- mock.should_receive(:odba_notify_observers).and_return { |key, id1, id2|
446
+ mock.should_receive(:odba_isolated_dump).and_return {}
447
+
448
+ # store_collection_elements
449
+ mock.should_receive(:odba_id).and_return {}
450
+ mock.should_receive(:odba_collection).and_return { [] }
451
+ mock.should_receive(:odba_id).and_return {}
452
+
453
+ # store
454
+ mock.should_receive(:odba_prefetch?).and_return {}
455
+
456
+ # store_object_connections
457
+ mock.should_receive(:odba_target_ids).and_return { [] }
458
+
459
+ # update_indices
460
+ mock.should_receive(:odba_indexable?).and_return {}
461
+
462
+ # store_cache_entry
463
+ mock.should_receive(:odba_prefetch?).and_return {}
464
+ mock.should_receive(:odba_collection).and_return { [] }
465
+
466
+ @storage.should_receive(:restore_collection).and_return { [] }
467
+ if block
468
+ @storage.should_receive(:store, &block).and_return
469
+ else
470
+ @storage.should_receive(:store).and_return {
471
+ assert(true)
472
+ }
473
+ end
474
+ @storage.should_receive(:ensure_object_connections).and_return { |*args| }
475
+ }
476
+ end
477
+
478
+ def test_delete
479
+ delete_item = ODBAContainerInCache.new
480
+ delete_item.odba_id = 1
481
+ origin_obj = ODBAContainerInCache.new
482
+ origin_obj.odba_id = 2
483
+ origin_obj.odba_connection = delete_item
484
+ @cache.fetched.store(1, delete_item)
485
+ @storage.should_receive(:retrieve_connected_objects).and_return { |id|
486
+ [[2]]
487
+ }
488
+ prepare_fetch(2, origin_obj)
489
+ @storage.should_receive(:restore_collection).and_return { |*args|
490
+ []
491
+ }
492
+ @storage.should_receive(:store).and_return { |id, dump, name, prefetch, klass| }
493
+ @storage.should_receive(:ensure_object_connections).and_return {}
494
+ @storage.should_receive(:delete_persistable).and_return { |id| }
495
+ @marshal.should_receive(:dump).and_return { |ob| "foo" }
496
+ @cache.delete(delete_item)
497
+ assert_equal(1, @cache.fetched.size)
498
+ assert_nil(origin_obj.odba_connection)
499
+ end
500
+
501
+ def prepare_delete(mock, name, id)
502
+ mock.should_receive(:odba_id).and_return { id }
503
+ mock.should_receive(:odba_name).and_return { name }
504
+ mock.should_receive(:odba_notify_observers).and_return { |key, id1, id2|
475
505
  assert_equal(:delete, key)
476
506
  }
477
- @storage.should_receive(:retrieve_connected_objects).and_return { |id|
478
- []
479
- }
480
- mock.should_receive(:origin_class?).and_return { true }
481
- mock.should_receive(:odba_id).and_return { id }
482
- @storage.should_receive(:delete_persistable).and_return { |id_arg|
483
- assert_equal(id, id_arg)
484
- }
485
- @storage.should_receive(:delete_index_element).and_return { }
486
- end
487
- def prepare_bulk_restore(rows)
488
- rows.each { |odba_mock|
507
+ @storage.should_receive(:retrieve_connected_objects).and_return { |id|
508
+ []
509
+ }
510
+ mock.should_receive(:origin_class?).and_return { true }
511
+ mock.should_receive(:odba_id).and_return { id }
512
+ @storage.should_receive(:delete_persistable).and_return { |id_arg|
513
+ assert_equal(id, id_arg)
514
+ }
515
+ @storage.should_receive(:delete_index_element).and_return {}
516
+ end
517
+
518
+ def prepare_bulk_restore(rows)
519
+ rows.each { |odba_mock|
489
520
  ## according to recent changes, objects are extended with
490
521
  # ODBA::Persistable after loading - this enables ad-hoc storing
491
522
  # but messes up loads of tests
492
- @marshal.should_receive(:load).and_return { |dump|
493
- odba_mock.instance_variable_set('@odba_id', 2)
494
- odba_mock.instance_variable_set('@odba_prefetch', true)
523
+ @marshal.should_receive(:load).and_return { |dump|
524
+ odba_mock.instance_variable_set(:@odba_id, 2)
525
+ odba_mock.instance_variable_set(:@odba_prefetch, true)
495
526
  odba_mock
496
- }
497
- @storage.should_receive(:restore_collection).and_return {|*args|
498
- []
499
- }
500
- }
501
- end
502
- def test_retrieve_from_index
503
- foo = flexmock
504
- index = flexmock("bar_index")
505
- @cache.indices["index_name"] = index
506
- index.should_receive(:fetch_ids).and_return { |search_term, meta|
507
- assert_equal('search term', search_term)
508
- assert_nil(meta)
509
- [2]
510
- }
511
- @storage.should_receive(:bulk_restore).and_return {
512
- [[2, 'dump']]
513
- }
514
- prepare_bulk_restore([foo])
515
- @cache.retrieve_from_index("index_name", "search term")
516
- end
517
- def test_update_indices
518
- index = flexmock("index")
519
- bar = flexmock("bar")
520
- bar.should_receive(:odba_indexable?).and_return { true }
521
- @cache.indices = {
522
- "foo" => index
523
- }
524
- index.should_receive(:update).and_return { |obj|
525
- assert_equal(bar, obj)
526
- }
527
- @cache.update_indices(bar)
528
- end
529
- def test_delete_index_element
530
- foo = flexmock("foo")
531
- bar = flexmock("bar")
532
- @cache.indices = {
533
- "foo" => foo
534
- }
535
- foo.should_receive(:delete).with(bar).times(1).and_return {
527
+ }
528
+ @storage.should_receive(:restore_collection).and_return { |*args|
529
+ []
530
+ }
531
+ }
532
+ end
533
+
534
+ def test_retrieve_from_index
535
+ foo = flexmock
536
+ index = flexmock("bar_index")
537
+ @cache.indices["index_name"] = index
538
+ index.should_receive(:fetch_ids).and_return { |search_term, meta|
539
+ assert_equal("search term", search_term)
540
+ assert_nil(meta)
541
+ [2]
542
+ }
543
+ @storage.should_receive(:bulk_restore).and_return {
544
+ [[2, "dump"]]
545
+ }
546
+ prepare_bulk_restore([foo])
547
+ @cache.retrieve_from_index("index_name", "search term")
548
+ end
549
+
550
+ def test_update_indices
551
+ index = flexmock("index")
552
+ bar = flexmock("bar")
553
+ bar.should_receive(:odba_indexable?).and_return { true }
554
+ @cache.indices = {
555
+ "foo" => index
556
+ }
557
+ index.should_receive(:update).and_return { |obj|
558
+ assert_equal(bar, obj)
559
+ }
560
+ @cache.update_indices(bar)
561
+ end
562
+
563
+ def test_delete_index_element
564
+ foo = flexmock("foo")
565
+ bar = flexmock("bar")
566
+ @cache.indices = {
567
+ "foo" => foo
568
+ }
569
+ foo.should_receive(:delete).with(bar).times(1).and_return {
536
570
  assert(true)
537
571
  }
538
- @cache.delete_index_element(bar)
539
- end
540
- def test_drop_index
541
- @storage.should_receive(:transaction).and_return { |block| block.call }
542
- @storage.should_receive(:drop_index).and_return { |index_name|
543
- assert_equal("foo_index", index_name)
544
- }
545
- index = flexmock("index")
572
+ @cache.delete_index_element(bar)
573
+ end
574
+
575
+ def test_drop_index
576
+ @storage.should_receive(:transaction).and_return { |block| block.call }
577
+ @storage.should_receive(:drop_index).and_return { |index_name|
578
+ assert_equal("foo_index", index_name)
579
+ }
580
+ index = flexmock("index")
546
581
  index.should_receive(:delete).with(index)
547
- prepare_delete(index, "foo", 2)
548
- @cache.indices.store("foo_index", index)
549
- @cache.drop_index("foo_index")
550
- end
551
- def test_drop_indices
552
- @storage.should_receive(:transaction).and_return { |block| block.call}
553
- @storage.should_receive(:drop_index).and_return {|index_name|
554
- assert_equal("foo_index", index_name)
555
- }
556
- index = flexmock("index")
582
+ prepare_delete(index, "foo", 2)
583
+ @cache.indices.store("foo_index", index)
584
+ @cache.drop_index("foo_index")
585
+ end
586
+
587
+ def test_drop_indices
588
+ @storage.should_receive(:transaction).and_return { |block| block.call }
589
+ @storage.should_receive(:drop_index).and_return { |index_name|
590
+ assert_equal("foo_index", index_name)
591
+ }
592
+ index = flexmock("index")
557
593
  index.should_receive(:delete).with(index)
558
- prepare_delete(index, "foo", 2)
559
- @cache.indices.store("foo_index", index)
560
- @cache.drop_indices
561
- end
562
- def test_fetch_collection_element
563
- key_dump = Marshal.dump('foo')
564
- @storage.should_receive(:collection_fetch).and_return { |odba_id, key|
565
- assert_equal(12, odba_id)
566
- assert_equal(key_dump, key)
567
- 'val_dump'
568
- }
569
- @marshal.should_receive(:dump).and_return { |key|
570
- assert_equal('foo', key)
571
- key_dump
572
- }
573
- @marshal.should_receive(:load).and_return { |dump|
574
- assert_equal('val_dump', dump)
575
- 'val'
576
- }
577
- res = @cache.fetch_collection_element(12, 'foo')
578
- assert_equal('val', res)
579
- end
594
+ prepare_delete(index, "foo", 2)
595
+ @cache.indices.store("foo_index", index)
596
+ @cache.drop_indices
597
+ end
598
+
599
+ def test_fetch_collection_element
600
+ key_dump = Marshal.dump("foo")
601
+ @storage.should_receive(:collection_fetch).and_return { |odba_id, key|
602
+ assert_equal(12, odba_id)
603
+ assert_equal(key_dump, key)
604
+ "val_dump"
605
+ }
606
+ @marshal.should_receive(:dump).and_return { |key|
607
+ assert_equal("foo", key)
608
+ key_dump
609
+ }
610
+ @marshal.should_receive(:load).and_return { |dump|
611
+ assert_equal("val_dump", dump)
612
+ "val"
613
+ }
614
+ res = @cache.fetch_collection_element(12, "foo")
615
+ assert_equal("val", res)
616
+ end
617
+
580
618
  def test_transaction
581
619
  o1 = Object.new
582
- o1.instance_variable_set('@odba_id', 1)
620
+ o1.instance_variable_set(:@odba_id, 1)
583
621
  o2 = Object.new
584
622
  o3 = Object.new
585
623
  o1.extend(ODBA::Persistable)
586
624
  o2.extend(ODBA::Persistable)
587
- o2.odba_name = 'name2'
625
+ o2.odba_name = "name2"
588
626
  o3.extend(ODBA::Persistable)
589
627
  o4 = o1.odba_dup
590
- @storage.should_receive(:transaction).and_return { |block| block.call }
628
+ @storage.should_receive(:transaction).and_return { |block| block.call }
591
629
 
592
630
  ## store o1
593
631
  @marshal.should_receive(:dump).times(3).and_return { |obj|
@@ -595,159 +633,165 @@ module ODBA
595
633
  }
596
634
  next_id = 1
597
635
  @storage.should_receive(:next_id).and_return { next_id += 1 }
598
- @storage.should_receive(:store).with(1,'dump1',nil,nil,Object)\
636
+ @storage.should_receive(:store).with(1, "dump1", nil, nil, Object)
599
637
  .times(1).and_return { assert(true) }
600
- @storage.should_receive(:ensure_object_connections)\
601
- .with(Integer,Array).times(1).and_return { assert(true) }
638
+ @storage.should_receive(:ensure_object_connections)
639
+ .with(Integer, Array).times(1).and_return { assert(true) }
602
640
 
603
641
  ## store o2
604
- @storage.should_receive(:restore_collection).with(Integer)\
642
+ @storage.should_receive(:restore_collection).with(Integer)
605
643
  .times(1).and_return([])
606
- @storage.should_receive(:store)\
607
- .with(Integer,String,'name2',nil,Object)\
644
+ @storage.should_receive(:store)
645
+ .with(Integer, String, "name2", nil, Object)
608
646
  .times(1).and_return { assert(true) }
609
- @storage.should_receive(:ensure_object_connections)\
610
- .with(Integer,Array).times(1).and_return { assert(true) }
647
+ @storage.should_receive(:ensure_object_connections)
648
+ .with(Integer, Array).times(1).and_return { assert(true) }
611
649
 
612
650
  ## store o3 and raise
613
- @storage.should_receive(:restore_collection).with(Integer)\
651
+ @storage.should_receive(:restore_collection).with(Integer)
614
652
  .times(1).and_return([])
615
653
  ## at this stage 1 and 2 (and 'name2') are stored:
616
- @storage.should_receive(:store)\
617
- .with(Integer,String,nil,nil,Object)\
654
+ @storage.should_receive(:store)
655
+ .with(Integer, String, nil, nil, Object)
618
656
  .times(1).and_return { raise "trigger rollback" }
619
657
 
620
658
  ## rollback
621
- @storage.should_receive(:restore).with(Integer)\
659
+ @storage.should_receive(:restore).with(Integer)
622
660
  .times(1).and_return(nil)
623
- @storage.should_receive(:restore).with(1)\
624
- .times(1).and_return('dump1')
625
- @storage.should_receive(:restore_collection).with(Integer)\
661
+ @storage.should_receive(:restore).with(1)
662
+ .times(1).and_return("dump1")
663
+ @storage.should_receive(:restore_collection).with(Integer)
626
664
  .times(2).and_return([])
627
- dbi = flexmock('dbi', :dbi_args => ['dbi_args'])
665
+ dbi = flexmock("dbi", dbi_args: ["dbi_args"])
628
666
  @storage.should_receive(:instance_variable_get).and_return(dbi)
629
667
  @storage.should_receive(:update_max_id)
630
- flexmock(@storage, :max_id => 'max_id')
631
- @marshal.should_receive(:load).with('dump1')\
668
+ flexmock(@storage, max_id: "max_id")
669
+ @marshal.should_receive(:load).with("dump1")
632
670
  .times(1).and_return(o4)
633
671
  @cache.fetched.store(1, ODBA::CacheEntry.new(o1))
634
672
  assert_raises(RuntimeError) {
635
673
  ODBA.transaction {
636
- o2.instance_variable_set('@other', o3)
637
- o1.instance_variable_set('@other', o2)
674
+ o2.instance_variable_set(:@other, o3)
675
+ o1.instance_variable_set(:@other, o2)
638
676
  o1.odba_store
639
677
  }
640
678
  }
641
679
  assert_equal(1, @cache.size)
642
- assert_nil(o1.instance_variable_get('@other'))
680
+ assert_nil(o1.instance_variable_get(:@other))
643
681
  end
682
+
644
683
  def test_extent
645
- o1 = flexmock('O1')
684
+ o1 = flexmock("O1")
646
685
  o1.should_receive(:odba_id).and_return(1)
647
- o2 = flexmock('O2')
686
+ o2 = flexmock("O2")
648
687
  o2.should_receive(:odba_id).and_return(2)
649
- clr = flexmock('Caller')
650
- @storage.should_receive(:extent_ids).and_return([1,2])
688
+ clr = flexmock("Caller")
689
+ @storage.should_receive(:extent_ids).and_return([1, 2])
651
690
  @storage.should_receive(:restore_collection).and_return([])
652
- @storage.should_receive(:bulk_restore).with([1,2])\
653
- .and_return([[1, 'dump1'],[2,'dump2']])
654
- @marshal.should_receive(:load).with('dump1')\
691
+ @storage.should_receive(:bulk_restore).with([1, 2])
692
+ .and_return([[1, "dump1"], [2, "dump2"]])
693
+ @marshal.should_receive(:load).with("dump1")
655
694
  .times(1).and_return(o1)
656
- @marshal.should_receive(:load).with('dump2')\
695
+ @marshal.should_receive(:load).with("dump2")
657
696
  .times(1).and_return(o2)
658
697
  assert_equal([o1, o2], @cache.extent(ODBAContainerInCache, clr))
659
698
  end
699
+
660
700
  def test_fetch_collection
661
- obj = flexmock('Object')
701
+ obj = flexmock("Object")
662
702
  obj.should_receive(:odba_id).and_return(1)
663
- restored = flexmock('Restored')
703
+ restored = flexmock("Restored")
664
704
  restored.should_receive(:odba_id).and_return(7)
665
- i1 = flexmock('Item1')
666
- i2 = flexmock('Item2')
705
+ i1 = flexmock("Item1")
706
+ i2 = flexmock("Item2")
667
707
  i1.should_receive(:is_a?).with(Stub).and_return(false)
668
708
  i2.should_receive(:is_a?).with(Stub).and_return(true)
669
709
  i2.should_receive(:odba_id).and_return(7)
670
710
  i2.should_receive(:odba_container=).with(obj).times(1)
671
- @storage.should_receive(:restore_collection).with(1)\
672
- .times(1).and_return([['keydump1','dump1'],['keydump2','dump2']])
711
+ @storage.should_receive(:restore_collection).with(1)
712
+ .times(1).and_return([["keydump1", "dump1"], ["keydump2", "dump2"]])
673
713
  @storage.should_receive(:restore_collection).with(7).times(1).and_return([])
674
- @storage.should_receive(:bulk_restore).with([7]).and_return([[7,'inst']])
675
- @marshal.should_receive(:load).with('keydump1').and_return(0)
676
- @marshal.should_receive(:load).with('keydump2').and_return(1)
677
- @marshal.should_receive(:load).with('dump1').and_return(i1)
678
- @marshal.should_receive(:load).with('dump2').and_return(i2)
679
- @marshal.should_receive(:load).with('inst').and_return(restored)
680
- assert_equal([[0,i1],[1,i2]], @cache.fetch_collection(obj))
714
+ @storage.should_receive(:bulk_restore).with([7]).and_return([[7, "inst"]])
715
+ @marshal.should_receive(:load).with("keydump1").and_return(0)
716
+ @marshal.should_receive(:load).with("keydump2").and_return(1)
717
+ @marshal.should_receive(:load).with("dump1").and_return(i1)
718
+ @marshal.should_receive(:load).with("dump2").and_return(i2)
719
+ @marshal.should_receive(:load).with("inst").and_return(restored)
720
+ assert_equal([[0, i1], [1, i2]], @cache.fetch_collection(obj))
681
721
  end
722
+
682
723
  def test_include
683
724
  assert(!@cache.include?(1))
684
- @cache.fetched.store(1, 'foo')
725
+ @cache.fetched.store(1, "foo")
685
726
  assert(@cache.include?(1))
686
727
  assert(!@cache.include?(2))
687
- @cache.prefetched.store(2, 'bar')
728
+ @cache.prefetched.store(2, "bar")
688
729
  assert(@cache.include?(2))
689
730
  end
731
+
690
732
  def test_index_keys
691
- index = flexmock('Index')
692
- @cache.indices.store('index', index)
693
- index.should_receive(:keys).with(nil).times(1).and_return(['ABC'])
694
- index.should_receive(:keys).with(2).times(1).and_return(['AB'])
695
- assert_equal(['ABC'], @cache.index_keys('index'))
696
- assert_equal(['AB'], @cache.index_keys('index', 2))
733
+ index = flexmock("Index")
734
+ @cache.indices.store("index", index)
735
+ index.should_receive(:keys).with(nil).times(1).and_return(["ABC"])
736
+ index.should_receive(:keys).with(2).times(1).and_return(["AB"])
737
+ assert_equal(["ABC"], @cache.index_keys("index"))
738
+ assert_equal(["AB"], @cache.index_keys("index", 2))
697
739
  end
740
+
698
741
  def test_setup
699
- @storage.should_receive(:setup).times(1).and_return { assert(true)}
700
- @storage.should_receive(:ensure_target_id_index)\
701
- .with('index').times(1).and_return { assert(true)}
742
+ @storage.should_receive(:setup).times(1).and_return { assert(true) }
743
+ @storage.should_receive(:ensure_target_id_index)
744
+ .with("index").times(1).and_return { assert(true) }
702
745
  df1 = IndexDefinition.new
703
- df1.index_name = 'deferred'
746
+ df1.index_name = "deferred"
704
747
  df2 = IndexDefinition.new
705
- df2.index_name = 'index'
706
- indices = flexmock('indices')
748
+ df2.index_name = "index"
749
+ indices = flexmock("indices")
707
750
  indices.should_receive(:each_key).and_return { |block|
708
- block.call('index')
751
+ block.call("index")
709
752
  }
710
- indices.should_receive(:include?).with('index')\
753
+ indices.should_receive(:include?).with("index")
711
754
  .times(1).and_return(true)
712
- indices.should_receive(:include?).with('deferred')\
755
+ indices.should_receive(:include?).with("deferred")
713
756
  .times(1).and_return(false)
714
- indices.should_receive(:store).and_return { |key, val|
715
- assert_equal('deferred', key)
716
- assert_instance_of(Index, val)
717
- }
718
- indices.should_receive(:odba_store_unsaved).times(1)
719
- @cache.instance_variable_set('@indices', indices)
757
+ indices.should_receive(:store).and_return { |key, val|
758
+ assert_equal("deferred", key)
759
+ assert_instance_of(Index, val)
760
+ }
761
+ indices.should_receive(:odba_store_unsaved).times(1)
762
+ @cache.instance_variable_set(:@indices, indices)
720
763
  @storage.should_receive(:transaction).and_return { |block|
721
764
  block.call
722
765
  }
723
- @storage.should_receive(:create_index).with('deferred')\
766
+ @storage.should_receive(:create_index).with("deferred")
724
767
  .times(1).and_return { assert(true) }
725
- @cache.instance_variable_set('@deferred_indices', [df1, df2])
768
+ @cache.instance_variable_set(:@deferred_indices, [df1, df2])
726
769
  @cache.setup
727
- @cache.instance_variable_set('@indices', {})
770
+ @cache.instance_variable_set(:@indices, {})
728
771
  end
772
+
729
773
  def test_lock
730
774
  result = ""
731
775
  th = Thread.new do
732
776
  sleep 1
733
- @cache.lock('testcase') do
734
- result << '123'
777
+ @cache.lock("testcase") do
778
+ result << "123"
735
779
  end
736
780
  end
737
- @cache.lock('testcase') do
738
- result << '456'
781
+ @cache.lock("testcase") do
782
+ result << "456"
739
783
  end
740
784
  th.join
741
- expected = '456123'
785
+ expected = "456123"
742
786
  assert_equal(expected, result)
743
787
  end
788
+
744
789
  def test_new_id
745
- flexmock(File, :exist? => false)
746
- storage = flexmock('storage',
747
- :max_id => 123,
748
- :update_max_id => nil
749
- )
750
- assert_equal(124, @cache.new_id('testcase', storage))
751
- end
752
- end
790
+ flexmock(File, exist?: false)
791
+ storage = flexmock("storage",
792
+ max_id: 123,
793
+ update_max_id: nil)
794
+ assert_equal(124, @cache.new_id("testcase", storage))
795
+ end
796
+ end
753
797
  end