cached_record 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.rdoc +5 -0
- data/Gemfile +13 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +391 -0
- data/Rakefile +90 -0
- data/VERSION +1 -0
- data/benchmark/setup.rb +77 -0
- data/cached_record.gemspec +26 -0
- data/config/database.yml +17 -0
- data/db/cached_record.sql +71 -0
- data/lib/cached_record/cache.rb +125 -0
- data/lib/cached_record/orm/active_record.rb +76 -0
- data/lib/cached_record/orm/data_mapper.rb +87 -0
- data/lib/cached_record/orm.rb +188 -0
- data/lib/cached_record/version.rb +7 -0
- data/lib/cached_record.rb +22 -0
- data/lib/gem_ext/redis.rb +25 -0
- data/log/.gitkeep +0 -0
- data/script/console +8 -0
- data/script/setup.rb +37 -0
- data/test/gem_ext/test_redis.rb +35 -0
- data/test/test_helper/coverage.rb +8 -0
- data/test/test_helper/db.rb +14 -0
- data/test/test_helper/minitest.rb +24 -0
- data/test/test_helper/setup.rb +16 -0
- data/test/test_helper.rb +17 -0
- data/test/unit/orm/test_active_record.rb +628 -0
- data/test/unit/orm/test_data_mapper.rb +616 -0
- data/test/unit/test_cache.rb +351 -0
- data/test/unit/test_cached_record.rb +34 -0
- data/test/unit/test_orm.rb +193 -0
- metadata +201 -0
@@ -0,0 +1,628 @@
|
|
1
|
+
require File.expand_path("../../../test_helper", __FILE__)
|
2
|
+
|
3
|
+
module Unit
|
4
|
+
module ORM
|
5
|
+
class TestActiveRecord < MiniTest::Test
|
6
|
+
|
7
|
+
class Article < ActiveRecord::Base
|
8
|
+
as_cache :memcached
|
9
|
+
end
|
10
|
+
|
11
|
+
class Barticle < ActiveRecord::Base
|
12
|
+
self.table_name = "articles"
|
13
|
+
as_cache :memcached, :only => [:title, :content]
|
14
|
+
end
|
15
|
+
|
16
|
+
class Carticle < ActiveRecord::Base
|
17
|
+
self.table_name = "articles"
|
18
|
+
as_cache :memcached, :only => [], :memoize => [:random_array]
|
19
|
+
|
20
|
+
def random_array
|
21
|
+
@random_array ||= [rand(10)]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Darticle < ActiveRecord::Base
|
26
|
+
self.table_name = "articles"
|
27
|
+
as_cache :memcached, :only => [:title, :content], :memoize => {:random_array => :@array}
|
28
|
+
|
29
|
+
def random_array
|
30
|
+
@array ||= [rand(10)]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Earticle < ActiveRecord::Base
|
35
|
+
self.table_name = "articles"
|
36
|
+
as_cache :memcached, :only => [:title, :content], :memoize => {:random_array => :@array}, :include_root => true
|
37
|
+
|
38
|
+
def random_array
|
39
|
+
@array ||= [rand(10)]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Farticle < ActiveRecord::Base
|
44
|
+
self.table_name = "articles"
|
45
|
+
as_memoized_cache :memcached, :only => [:title, :content], :memoize => {:random_array => :@array}, :include_root => true
|
46
|
+
|
47
|
+
def random_array
|
48
|
+
@array ||= [rand(10)]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Garticle < ActiveRecord::Base
|
53
|
+
self.table_name = "articles"
|
54
|
+
belongs_to :author, :class_name => "Unit::ORM::TestActiveRecord::User"
|
55
|
+
has_many :comments, :class_name => "Unit::ORM::TestActiveRecord::Comment", :foreign_key => "article_id"
|
56
|
+
has_and_belongs_to_many :tags, :class_name => "Unit::ORM::TestActiveRecord::Tag", :join_table => "articles_tags", :foreign_key => "article_id"
|
57
|
+
as_memoized_cache :memcached, :only => [:title], :include => [:author, :comments, :tags]
|
58
|
+
end
|
59
|
+
|
60
|
+
class User < ActiveRecord::Base
|
61
|
+
has_one :foo, :class_name => "Unit::ORM::TestActiveRecord::Article", :foreign_key => "foo_id"
|
62
|
+
as_memoized_cache :redis, :only => [:name], :include => [:foo]
|
63
|
+
end
|
64
|
+
|
65
|
+
class Comment < ActiveRecord::Base
|
66
|
+
belongs_to :article, :class_name => "Unit::ORM::TestActiveRecord::Garticle"
|
67
|
+
belongs_to :poster, :class_name => "Unit::ORM::TestActiveRecord::User"
|
68
|
+
as_memoized_cache :memcached, :only => [:content], :include => [:poster]
|
69
|
+
end
|
70
|
+
|
71
|
+
class Tag < ActiveRecord::Base
|
72
|
+
has_and_belongs_to_many :articles, :class_name => "Unit::ORM::TestActiveRecord::Garticle", :join_table => "articles_tags", :foreign_key => "tag_id"
|
73
|
+
as_memoized_cache :memcached, :only => [:name]
|
74
|
+
end
|
75
|
+
|
76
|
+
class Harticle < ActiveRecord::Base
|
77
|
+
self.table_name = "articles"
|
78
|
+
as_memoized_cache :memcached, :only => [:title], :expire => 2.seconds
|
79
|
+
end
|
80
|
+
|
81
|
+
class Jarticle < ActiveRecord::Base
|
82
|
+
self.table_name = "articles"
|
83
|
+
as_memoized_cache :memcached, :only => [:title], :retain => 4.seconds
|
84
|
+
end
|
85
|
+
|
86
|
+
describe CachedRecord::ORM::ActiveRecord do
|
87
|
+
describe "when ActiveRecord is not defined" do
|
88
|
+
it "knows not to setup ActiveRecord::Base" do
|
89
|
+
CachedRecord::ORM::ActiveRecord.expects(:setup?).returns false
|
90
|
+
ActiveRecord::Base.expects(:extend).with(CachedRecord::ORM::ActiveRecord::ClassMethods).never
|
91
|
+
ActiveRecord::Base.expects(:send).with(:include, CachedRecord::ORM).never
|
92
|
+
ActiveRecord::Base.expects(:send).with(:include, CachedRecord::ORM::ActiveRecord::InstanceMethods).never
|
93
|
+
CachedRecord::ORM::ActiveRecord.setup
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "when ActiveRecord is defined" do
|
98
|
+
it "knows to setup ActiveRecord::Base" do
|
99
|
+
CachedRecord::ORM::ActiveRecord.expects(:setup?).returns true
|
100
|
+
ActiveRecord::Base.expects(:extend).with(CachedRecord::ORM::ActiveRecord::ClassMethods)
|
101
|
+
ActiveRecord::Base.expects(:send).with(:include, CachedRecord::ORM)
|
102
|
+
ActiveRecord::Base.expects(:send).with(:include, CachedRecord::ORM::ActiveRecord::InstanceMethods)
|
103
|
+
CachedRecord::ORM::ActiveRecord.setup
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "ActiveRecord::Base instances" do
|
108
|
+
before do
|
109
|
+
@memcached = Dalli::Client.new
|
110
|
+
@redis = Redis.new
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "Article" do
|
114
|
+
it "returns its cache JSON hash" do
|
115
|
+
assert_equal_hashes({
|
116
|
+
:id => 1,
|
117
|
+
:title => "Behold! It's CachedRecord!",
|
118
|
+
:content => "Cache ORM instances to avoid database queries",
|
119
|
+
:author_id => 1,
|
120
|
+
:foo_id => 2,
|
121
|
+
:published_at => Time.parse("2013-08-01 12:00:00"),
|
122
|
+
:created_at => Time.parse("2013-08-01 10:00:00"),
|
123
|
+
:updated_at => Time.parse("2013-08-01 11:00:00")
|
124
|
+
}, Article.find(1).as_cache_json)
|
125
|
+
end
|
126
|
+
it "can be stored in the cache store" do
|
127
|
+
Article.cached 1
|
128
|
+
assert_equal_hashes({
|
129
|
+
:id => 1,
|
130
|
+
:title => "Behold! It's CachedRecord!",
|
131
|
+
:content => "Cache ORM instances to avoid database queries",
|
132
|
+
:author_id => 1,
|
133
|
+
:foo_id => 2,
|
134
|
+
:published_at => Time.parse("2013-08-01 12:00:00"),
|
135
|
+
:created_at => Time.parse("2013-08-01 10:00:00"),
|
136
|
+
:updated_at => Time.parse("2013-08-01 11:00:00")
|
137
|
+
}, @memcached.get("unit.orm.test_active_record.article.1"))
|
138
|
+
end
|
139
|
+
it "can be fetched from the cache store" do
|
140
|
+
Article.expects(:find).never
|
141
|
+
@memcached.set(
|
142
|
+
"unit.orm.test_active_record.article.1", {
|
143
|
+
:id => 1,
|
144
|
+
:title => "Behold! It's CachedRecord!",
|
145
|
+
:content => "Cache ORM instances to avoid database queries",
|
146
|
+
:author_id => 1,
|
147
|
+
:foo_id => 2,
|
148
|
+
:published_at => Time.parse("2013-08-01 12:00:00"),
|
149
|
+
:created_at => Time.parse("2013-08-01 10:00:00"),
|
150
|
+
:updated_at => Time.parse("2013-08-01 11:00:00")
|
151
|
+
}.to_json
|
152
|
+
)
|
153
|
+
assert_equal_hashes({
|
154
|
+
"id" => 1,
|
155
|
+
"title" => "Behold! It's CachedRecord!",
|
156
|
+
"content" => "Cache ORM instances to avoid database queries",
|
157
|
+
"author_id" => 1,
|
158
|
+
"foo_id" => 2,
|
159
|
+
"published_at" => Time.parse("2013-08-01 12:00:00"),
|
160
|
+
"created_at" => Time.parse("2013-08-01 10:00:00"),
|
161
|
+
"updated_at" => Time.parse("2013-08-01 11:00:00")
|
162
|
+
}, Article.cached(1).attributes)
|
163
|
+
end
|
164
|
+
it "is not a new record" do
|
165
|
+
assert_equal false, Article.cached(1).new_record?
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "Barticle" do
|
170
|
+
it "returns its cache JSON hash" do
|
171
|
+
assert_equal_hashes({
|
172
|
+
:id => 1,
|
173
|
+
:title => "Behold! It's CachedRecord!",
|
174
|
+
:content => "Cache ORM instances to avoid database queries"
|
175
|
+
}, Barticle.find(1).as_cache_json)
|
176
|
+
end
|
177
|
+
it "can be stored in the cache store" do
|
178
|
+
Barticle.cached(1)
|
179
|
+
assert_equal_hashes({
|
180
|
+
:id => 1,
|
181
|
+
:title => "Behold! It's CachedRecord!",
|
182
|
+
:content => "Cache ORM instances to avoid database queries"
|
183
|
+
}, @memcached.get("unit.orm.test_active_record.barticle.1"))
|
184
|
+
end
|
185
|
+
it "can be fetched from the cache store" do
|
186
|
+
Barticle.expects(:find).never
|
187
|
+
@memcached.set(
|
188
|
+
"unit.orm.test_active_record.barticle.1", {
|
189
|
+
"id" => 1,
|
190
|
+
"title" => "Behold! It's CachedRecord!",
|
191
|
+
"content" => "Cache ORM instances to avoid database queries"
|
192
|
+
}.to_json
|
193
|
+
)
|
194
|
+
assert_equal_hashes({
|
195
|
+
"id" => 1,
|
196
|
+
"title" => "Behold! It's CachedRecord!",
|
197
|
+
"content" => "Cache ORM instances to avoid database queries",
|
198
|
+
"author_id" => nil,
|
199
|
+
"foo_id" => nil,
|
200
|
+
"published_at" => nil,
|
201
|
+
"created_at" => nil,
|
202
|
+
"updated_at" => nil
|
203
|
+
}, Barticle.cached(1).attributes)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
describe "Carticle" do
|
208
|
+
it "returns its cache JSON hash" do
|
209
|
+
c = Carticle.find(1)
|
210
|
+
c.expects(:rand).returns(5)
|
211
|
+
assert_equal_hashes({
|
212
|
+
:id => 1,
|
213
|
+
:@random_array => [5]
|
214
|
+
}, c.as_cache_json)
|
215
|
+
end
|
216
|
+
it "can be stored in the cache store" do
|
217
|
+
Carticle.any_instance.expects(:rand).returns(4)
|
218
|
+
Carticle.cached(1)
|
219
|
+
assert_equal_hashes({
|
220
|
+
:id => 1,
|
221
|
+
:@random_array => [4]
|
222
|
+
}, @memcached.get("unit.orm.test_active_record.carticle.1"))
|
223
|
+
end
|
224
|
+
it "can be fetched from the cache store" do
|
225
|
+
Carticle.expects(:find).never
|
226
|
+
@memcached.set(
|
227
|
+
"unit.orm.test_active_record.carticle.1", {
|
228
|
+
:id => 1,
|
229
|
+
:title => "Behold! It's CachedRecord!",
|
230
|
+
:content => "Cache ORM instances to avoid database queries",
|
231
|
+
:@random_array => [3]
|
232
|
+
}.to_json
|
233
|
+
)
|
234
|
+
assert_equal_hashes({
|
235
|
+
"id" => 1,
|
236
|
+
"title" => "Behold! It's CachedRecord!",
|
237
|
+
"content" => "Cache ORM instances to avoid database queries",
|
238
|
+
"author_id" => nil,
|
239
|
+
"foo_id" => nil,
|
240
|
+
"published_at" => nil,
|
241
|
+
"created_at" => nil,
|
242
|
+
"updated_at" => nil
|
243
|
+
}, Carticle.cached(1).attributes)
|
244
|
+
assert_equal(
|
245
|
+
true, Carticle.cached(1).instance_variables.include?(:@random_array)
|
246
|
+
)
|
247
|
+
assert_equal([
|
248
|
+
3
|
249
|
+
], Carticle.cached(1).instance_variable_get(:@random_array))
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe "Darticle" do
|
254
|
+
it "returns its cache JSON hash" do
|
255
|
+
d = Darticle.find(1)
|
256
|
+
d.expects(:rand).returns(5)
|
257
|
+
assert_equal_hashes({
|
258
|
+
:id => 1,
|
259
|
+
:title => "Behold! It's CachedRecord!",
|
260
|
+
:content => "Cache ORM instances to avoid database queries",
|
261
|
+
:@array => [5]
|
262
|
+
}, d.as_cache_json)
|
263
|
+
end
|
264
|
+
it "can be stored in the cache store" do
|
265
|
+
Darticle.any_instance.expects(:rand).returns(4)
|
266
|
+
Darticle.cached(1)
|
267
|
+
assert_equal_hashes({
|
268
|
+
:id => 1,
|
269
|
+
:title => "Behold! It's CachedRecord!",
|
270
|
+
:content => "Cache ORM instances to avoid database queries",
|
271
|
+
:@array => [4]
|
272
|
+
}, @memcached.get("unit.orm.test_active_record.darticle.1"))
|
273
|
+
end
|
274
|
+
it "can be fetched from the cache store" do
|
275
|
+
Darticle.expects(:find).never
|
276
|
+
@memcached.set(
|
277
|
+
"unit.orm.test_active_record.darticle.1", {
|
278
|
+
:id => 1,
|
279
|
+
:title => "Behold! It's CachedRecord!",
|
280
|
+
:content => "Cache ORM instances to avoid database queries",
|
281
|
+
:@array => [3]
|
282
|
+
}.to_json
|
283
|
+
)
|
284
|
+
assert_equal_hashes({
|
285
|
+
"id" => 1,
|
286
|
+
"title" => "Behold! It's CachedRecord!",
|
287
|
+
"content" => "Cache ORM instances to avoid database queries",
|
288
|
+
"author_id" => nil,
|
289
|
+
"foo_id" => nil,
|
290
|
+
"published_at" => nil,
|
291
|
+
"created_at" => nil,
|
292
|
+
"updated_at" => nil
|
293
|
+
}, Darticle.cached(1).attributes)
|
294
|
+
assert_equal(
|
295
|
+
true, Darticle.cached(1).instance_variables.include?(:@array)
|
296
|
+
)
|
297
|
+
assert_equal([
|
298
|
+
3
|
299
|
+
], Darticle.cached(1).instance_variable_get(:@array))
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
describe "Earticle" do
|
304
|
+
it "returns its cache JSON hash" do
|
305
|
+
e = Earticle.find(1)
|
306
|
+
e.expects(:rand).returns(5)
|
307
|
+
assert_equal_hashes({
|
308
|
+
:earticle => {
|
309
|
+
:id => 1,
|
310
|
+
:title => "Behold! It's CachedRecord!",
|
311
|
+
:content => "Cache ORM instances to avoid database queries"
|
312
|
+
},
|
313
|
+
:array => [5]
|
314
|
+
}, e.as_cache_json)
|
315
|
+
end
|
316
|
+
it "can be stored in the cache store" do
|
317
|
+
Earticle.any_instance.expects(:rand).returns(4)
|
318
|
+
Earticle.cached(1)
|
319
|
+
assert_equal_hashes({
|
320
|
+
:earticle => {
|
321
|
+
:id => 1,
|
322
|
+
:title => "Behold! It's CachedRecord!",
|
323
|
+
:content => "Cache ORM instances to avoid database queries"
|
324
|
+
},
|
325
|
+
:array => [4]
|
326
|
+
}, @memcached.get("unit.orm.test_active_record.earticle.1"))
|
327
|
+
end
|
328
|
+
it "can be fetched from the cache store" do
|
329
|
+
Earticle.expects(:find).never
|
330
|
+
@memcached.set(
|
331
|
+
"unit.orm.test_active_record.earticle.1", {
|
332
|
+
:earticle => {
|
333
|
+
:id => 1,
|
334
|
+
:title => "Behold! It's CachedRecord!",
|
335
|
+
:content => "Cache ORM instances to avoid database queries"
|
336
|
+
},
|
337
|
+
:array => [3]
|
338
|
+
}.to_json
|
339
|
+
)
|
340
|
+
assert_equal_hashes({
|
341
|
+
"id" => 1,
|
342
|
+
"title" => "Behold! It's CachedRecord!",
|
343
|
+
"content" => "Cache ORM instances to avoid database queries",
|
344
|
+
"author_id" => nil,
|
345
|
+
"foo_id" => nil,
|
346
|
+
"published_at" => nil,
|
347
|
+
"created_at" => nil,
|
348
|
+
"updated_at" => nil
|
349
|
+
}, Earticle.cached(1).attributes)
|
350
|
+
assert_equal(
|
351
|
+
true, Earticle.cached(1).instance_variables.include?(:@array)
|
352
|
+
)
|
353
|
+
assert_equal([
|
354
|
+
3
|
355
|
+
], Earticle.cached(1).instance_variable_get(:@array))
|
356
|
+
end
|
357
|
+
it "is not memoized" do
|
358
|
+
assert_equal false, (Earticle.cached(1).object_id == Earticle.cached(1).object_id)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
describe "Farticle" do
|
363
|
+
it "can be fetched from the cache store" do
|
364
|
+
Farticle.expects(:find).never
|
365
|
+
@memcached.set(
|
366
|
+
"unit.orm.test_active_record.farticle.1", {
|
367
|
+
:farticle => {
|
368
|
+
:id => 1,
|
369
|
+
:title => "Behold! It's CachedRecord!",
|
370
|
+
:content => "Cache ORM instances to avoid database queries"
|
371
|
+
},
|
372
|
+
:array => [3]
|
373
|
+
}.to_json
|
374
|
+
)
|
375
|
+
assert_equal_hashes({
|
376
|
+
"id" => 1,
|
377
|
+
"title" => "Behold! It's CachedRecord!",
|
378
|
+
"content" => "Cache ORM instances to avoid database queries",
|
379
|
+
"author_id" => nil,
|
380
|
+
"foo_id" => nil,
|
381
|
+
"published_at" => nil,
|
382
|
+
"created_at" => nil,
|
383
|
+
"updated_at" => nil
|
384
|
+
}, Farticle.cached(1).attributes)
|
385
|
+
assert_equal(
|
386
|
+
true, Farticle.cached(1).instance_variables.include?(:@array)
|
387
|
+
)
|
388
|
+
assert_equal([
|
389
|
+
3
|
390
|
+
], Farticle.cached(1).instance_variable_get(:@array))
|
391
|
+
end
|
392
|
+
it "is memoized" do
|
393
|
+
assert_equal Farticle.cached(1).object_id, Farticle.cached(1).object_id
|
394
|
+
assert_equal Farticle.cached(1).object_id, Farticle.cached(1).object_id
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
describe "Garticle" do
|
399
|
+
it "returns its cache JSON hash" do
|
400
|
+
g = Garticle.find(1)
|
401
|
+
assert_equal_hashes({
|
402
|
+
:id => 1,
|
403
|
+
:title => "Behold! It's CachedRecord!",
|
404
|
+
:author_id => 1,
|
405
|
+
:_comment_ids => [1, 2],
|
406
|
+
:_tag_ids => [1, 2]
|
407
|
+
}, g.as_cache_json)
|
408
|
+
end
|
409
|
+
it "can be stored in the cache store" do
|
410
|
+
Garticle.cached(1)
|
411
|
+
assert_equal_hashes({
|
412
|
+
:id => 1,
|
413
|
+
:title => "Behold! It's CachedRecord!",
|
414
|
+
:author_id => 1,
|
415
|
+
:_comment_ids => [1, 2],
|
416
|
+
:_tag_ids => [1, 2]
|
417
|
+
}, @memcached.get("unit.orm.test_active_record.garticle.1"))
|
418
|
+
assert_equal_hashes({
|
419
|
+
:id => 1,
|
420
|
+
:name => "Paul Engel",
|
421
|
+
:_foo_id => nil
|
422
|
+
}, @redis.get("unit.orm.test_active_record.user.1"))
|
423
|
+
assert_equal_hashes({
|
424
|
+
:id => 2,
|
425
|
+
:name => "Ken Adams",
|
426
|
+
:_foo_id => 1
|
427
|
+
}, @redis.get("unit.orm.test_active_record.user.2"))
|
428
|
+
assert_equal_hashes({
|
429
|
+
:id => 1,
|
430
|
+
:content => "What a great article! :)",
|
431
|
+
:poster_id => 2
|
432
|
+
}, @memcached.get("unit.orm.test_active_record.comment.1"))
|
433
|
+
assert_equal_hashes({
|
434
|
+
:id => 2,
|
435
|
+
:content => "Thanks!",
|
436
|
+
:poster_id => 1
|
437
|
+
}, @memcached.get("unit.orm.test_active_record.comment.2"))
|
438
|
+
end
|
439
|
+
it "can be fetched from the cache store" do
|
440
|
+
Garticle.expects(:find).never
|
441
|
+
User.expects(:find).never
|
442
|
+
Comment.expects(:find).never
|
443
|
+
Tag.expects(:find).never
|
444
|
+
@memcached.set(
|
445
|
+
"unit.orm.test_active_record.garticle.1", {
|
446
|
+
:id => 1,
|
447
|
+
:title => "Behold! It's CachedRecord!",
|
448
|
+
:author_id => 1,
|
449
|
+
:_comment_ids => [1, 2],
|
450
|
+
:_tag_ids => [1, 2]
|
451
|
+
}.to_json
|
452
|
+
)
|
453
|
+
@redis.set(
|
454
|
+
"unit.orm.test_active_record.user.1", {
|
455
|
+
:id => 1,
|
456
|
+
:name => "Paul Engel"
|
457
|
+
}.to_json
|
458
|
+
)
|
459
|
+
@redis.set(
|
460
|
+
"unit.orm.test_active_record.user.2", {
|
461
|
+
:id => 2,
|
462
|
+
:name => "Ken Adams"
|
463
|
+
}.to_json
|
464
|
+
)
|
465
|
+
@memcached.set(
|
466
|
+
"unit.orm.test_active_record.comment.1", {
|
467
|
+
:id => 1,
|
468
|
+
:content => "What a great article! :)",
|
469
|
+
:poster_id => 2
|
470
|
+
}.to_json
|
471
|
+
)
|
472
|
+
@memcached.set(
|
473
|
+
"unit.orm.test_active_record.comment.2", {
|
474
|
+
:id => 2,
|
475
|
+
:content => "Thanks!",
|
476
|
+
:poster_id => 1
|
477
|
+
}.to_json
|
478
|
+
)
|
479
|
+
@memcached.set(
|
480
|
+
"unit.orm.test_active_record.tag.1", {
|
481
|
+
:id => 1,
|
482
|
+
:name => "ruby"
|
483
|
+
}.to_json
|
484
|
+
)
|
485
|
+
@memcached.set(
|
486
|
+
"unit.orm.test_active_record.tag.2", {
|
487
|
+
:id => 2,
|
488
|
+
:name => "gem"
|
489
|
+
}.to_json
|
490
|
+
)
|
491
|
+
g = Garticle.cached(1)
|
492
|
+
assert_equal({
|
493
|
+
"id" => 1,
|
494
|
+
"title" => "Behold! It's CachedRecord!",
|
495
|
+
"content" => nil,
|
496
|
+
"author_id" => 1,
|
497
|
+
"foo_id" => nil,
|
498
|
+
"published_at" => nil,
|
499
|
+
"created_at" => nil,
|
500
|
+
"updated_at" => nil
|
501
|
+
}, g.attributes)
|
502
|
+
assert_equal({
|
503
|
+
"id" => 1,
|
504
|
+
"name" => "Paul Engel",
|
505
|
+
"description" => nil,
|
506
|
+
"active" => nil,
|
507
|
+
"created_at" => nil,
|
508
|
+
"updated_at" => nil
|
509
|
+
}, g.author.attributes)
|
510
|
+
assert_equal([{
|
511
|
+
"id" => 1,
|
512
|
+
"name" => "ruby",
|
513
|
+
"created_at" => nil,
|
514
|
+
"updated_at" => nil
|
515
|
+
}, {
|
516
|
+
"id" => 2,
|
517
|
+
"name" => "gem",
|
518
|
+
"created_at" => nil,
|
519
|
+
"updated_at" => nil
|
520
|
+
}], g.tags.collect(&:attributes))
|
521
|
+
assert_equal([{
|
522
|
+
"id" => 1,
|
523
|
+
"content" => "What a great article! :)",
|
524
|
+
"article_id" => nil,
|
525
|
+
"poster_id" => 2,
|
526
|
+
"created_at" => nil,
|
527
|
+
"updated_at" => nil
|
528
|
+
}, {
|
529
|
+
"id" => 2,
|
530
|
+
"content" => "Thanks!",
|
531
|
+
"article_id" => nil,
|
532
|
+
"poster_id" => 1,
|
533
|
+
"created_at" => nil,
|
534
|
+
"updated_at" => nil
|
535
|
+
}], g.comments.collect(&:attributes))
|
536
|
+
assert_equal([{
|
537
|
+
"id" => 2,
|
538
|
+
"name" => "Ken Adams",
|
539
|
+
"description" => nil,
|
540
|
+
"active" => nil,
|
541
|
+
"created_at" => nil,
|
542
|
+
"updated_at" => nil
|
543
|
+
}, {
|
544
|
+
"id" => 1,
|
545
|
+
"name" => "Paul Engel",
|
546
|
+
"description" => nil,
|
547
|
+
"active" => nil,
|
548
|
+
"created_at" => nil,
|
549
|
+
"updated_at" => nil
|
550
|
+
}], g.comments.collect{|x| x.poster.attributes})
|
551
|
+
assert_equal g.author.object_id, g.comments[1].poster.object_id
|
552
|
+
end
|
553
|
+
end
|
554
|
+
|
555
|
+
describe "Harticle" do
|
556
|
+
it "has the expected as cache options" do
|
557
|
+
assert_equal_hashes({
|
558
|
+
:store => :memcached,
|
559
|
+
:expire => 2.seconds,
|
560
|
+
:as_json => {:only => [:title]},
|
561
|
+
:memoize => true
|
562
|
+
}, Harticle.as_cache)
|
563
|
+
end
|
564
|
+
it "expires after 2 seconds" do
|
565
|
+
object_id = Harticle.cached(1).object_id
|
566
|
+
assert_equal object_id, Harticle.cached(1).object_id
|
567
|
+
assert_equal object_id, Harticle.cached(1).object_id
|
568
|
+
sleep 1
|
569
|
+
assert_equal object_id, Harticle.cached(1).object_id
|
570
|
+
assert_equal object_id, Harticle.cached(1).object_id
|
571
|
+
sleep 1.5
|
572
|
+
assert_equal false, object_id == Harticle.cached(1).object_id
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
describe "Jarticle" do
|
577
|
+
it "has the expected as cache options" do
|
578
|
+
assert_equal_hashes({
|
579
|
+
:store => :memcached,
|
580
|
+
:as_json => {:only => [:title]},
|
581
|
+
:memoize => true,
|
582
|
+
:retain => 4.seconds
|
583
|
+
}, Jarticle.as_cache)
|
584
|
+
end
|
585
|
+
it "does not hit the cache for 4 seconds" do
|
586
|
+
object_id = Jarticle.cached(1).object_id
|
587
|
+
cached = CachedRecord::Cache.send(:stores)[:memcached].get Jarticle.cache_key(1)
|
588
|
+
|
589
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).never
|
590
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
591
|
+
sleep 1
|
592
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
593
|
+
sleep 1
|
594
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
595
|
+
sleep 1
|
596
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
597
|
+
|
598
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).returns(cached)
|
599
|
+
sleep 1.5
|
600
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
601
|
+
|
602
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).never
|
603
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
604
|
+
sleep 1
|
605
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
606
|
+
sleep 1
|
607
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
608
|
+
sleep 1
|
609
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
610
|
+
|
611
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).returns(cached)
|
612
|
+
sleep 1
|
613
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
614
|
+
|
615
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).never
|
616
|
+
assert_equal object_id, Jarticle.cached(1).object_id
|
617
|
+
|
618
|
+
CachedRecord::Cache.expire Jarticle.cached(1)
|
619
|
+
CachedRecord::Cache.send(:stores)[:memcached].expects(:get).returns(cached)
|
620
|
+
assert_equal false, object_id == Jarticle.cached(1).object_id
|
621
|
+
end
|
622
|
+
end
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
end
|
627
|
+
end
|
628
|
+
end
|