odba 1.1.8 → 1.2.0

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