forked from mhgbrown/cached_resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caching_spec.rb
220 lines (187 loc) · 7.2 KB
/
caching_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require 'spec_helper'
describe CachedResource do
before(:all) do
class Thing < ActiveResource::Base
self.site = "http://api.thing.com"
cached_resource
end
@thing = {:thing => {:id => 1, :name => "Ada"}}
@other_thing = {:thing => {:id => 1, :name => "Ari"}}
@thing_json = @thing.to_json
@other_thing_json = @other_thing.to_json
end
after(:all) do
Thing.cached_resource.cache.clear
Object.send(:remove_const, :Thing)
end
describe "when enabled" do
before(:each) do
# it's on by default, but lets call the method
# to make sure it works
Thing.cached_resource.cache.clear
Thing.cached_resource.on!
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @thing_json
end
end
it "should cache a response" do
result = Thing.find(1)
Thing.cached_resource.cache.read("thing/1").should == result
end
it "should read a response when the request is made again" do
# make a request
Thing.find(1)
# make the same request
Thing.find(1)
# only one request should have happened
ActiveResource::HttpMock.requests.length.should == 1
end
it "should remake a request when reloaded" do
# make a request
Thing.find(1)
# make the same request, but reload it
Thing.find(1, :reload => true)
# we should get two requests
ActiveResource::HttpMock.requests.length.should == 2
end
it "should rewrite the cache when the request is reloaded" do
# make a request
Thing.find(1)
# get the cached result of the request
old_result = Thing.cached_resource.cache.read("thing/1")
# change the response
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @other_thing_json
end
Thing.find(1, :reload => true)
new_result = Thing.cached_resource.cache.read("thing/1")
# since active resources are equal if and only if they
# are the same object or an instance of the same class,
# not new?, and have the same id.
new_result.name.should_not == old_result.name
end
it "should remake the request when the ttl expires" do
# set cache time to live to 1 second
Thing.cached_resource.ttl = 1
# make a request
Thing.find(1)
# wait for the cache to expire
sleep(1.5)
# make the same request
Thing.find(1)
ActiveResource::HttpMock.requests.length.should == 2
end
describe "when collection synchronize is enabled" do
before(:each) do
Thing.cached_resource.cache.clear
Thing.cached_resource.collection_synchronize = true
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @thing_json
mock.get "/things.json", {}, [@thing[:thing]].to_json(:root => :thing)
end
# make a request for all things
Thing.all
end
it "should write cache entries for its members" do
result = Thing.find(1)
# only the all request should have been made
ActiveResource::HttpMock.requests.length.should == 1
# the result should be cached with the appropriate key
Thing.cached_resource.cache.read("thing/1").should == result
end
it "should rewrite cache entries for its members when reloaded" do
# get the soon to be stale result so that we have a cache entry
old_result = Thing.find(1)
# change the server
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @other_thing_json
mock.get "/things.json", {}, [@other_thing[:thing]].to_json(:root => :thing)
end
# reload the collection
Thing.all(:reload => true)
# get the updated result, read from the cache
result = Thing.find(1)
Thing.cached_resource.cache.read("thing/all")[0].should == result
Thing.cached_resource.cache.read("thing/all")[0].name.should == result.name
end
it "should update the collection when an individual request is reloaded" do
# change the server
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @other_thing_json
mock.get "/things.json", {}, [@other_thing[:thing]].to_json(:root => :thing)
end
# reload the individual
result = Thing.find(1, :reload => true)
Thing.cached_resource.cache.read("thing/all")[0].should == result
Thing.cached_resource.cache.read("thing/all")[0].name.should == result.name
end
end
describe "when collection synchronize is disabled" do
before(:each) do
Thing.cached_resource.cache.clear
Thing.cached_resource.collection_synchronize = false
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @thing_json
mock.get "/things.json", {}, [@thing[:thing]].to_json(:root => :thing)
end
# make a request for all things
Thing.all
end
it "should not write cache entries for its members" do
result = Thing.find(1)
# both the all in the before each and this request should have been made
ActiveResource::HttpMock.requests.length.should == 2
end
it "should not update the collection when an individual request is reloaded" do
# change the server
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @other_thing_json
mock.get "/things.json", {}, [@other_thing[:thing]].to_json(:root => :thing)
end
# reload the individual
result = Thing.find(1, :reload => true)
# the ids are the same, but the names should be different
Thing.cached_resource.cache.read("thing/all")[0].name.should_not == result.name
end
end
end
describe "when disabled" do
before(:each) do
Thing.cached_resource.cache.clear
Thing.cached_resource.off!
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @thing_json
end
end
it "should cache a response" do
result = Thing.find(1)
Thing.cached_resource.cache.read("thing/1").should == result
end
it "should always remake the request" do
Thing.find(1)
ActiveResource::HttpMock.requests.length.should == 1
Thing.find(1)
ActiveResource::HttpMock.requests.length.should == 2
end
it "should rewrite the cache for each request" do
Thing.find(1)
old_result = Thing.cached_resource.cache.read("thing/1")
# change the response
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/1.json", {}, @other_thing_json
end
Thing.find(1)
new_result = Thing.cached_resource.cache.read("thing/1")
# since active resources are equal if and only if they
# are the same object or an instance of the same class,
# not new?, and have the same id.
new_result.name.should_not == old_result.name
end
end
end