af-cache-money 0.2.10

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.
@@ -0,0 +1,211 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ module Cash
4
+ describe Finders do
5
+ describe 'when the cache is populated' do
6
+ describe "#find" do
7
+ describe '#find(id...)' do
8
+ describe '#find(id)' do
9
+ it "returns an active record" do
10
+ story = Story.create!(:title => 'a story')
11
+ Story.find(story.id).should == story
12
+ end
13
+ end
14
+
15
+ describe 'when the object is destroyed' do
16
+ describe '#find(id)' do
17
+ it "raises an error" do
18
+ story = Story.create!(:title => "I am delicious")
19
+ story.destroy
20
+ lambda { Story.find(story.id) }.should raise_error(ActiveRecord::RecordNotFound)
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '#find(id1, id2, ...)' do
26
+ it "returns an array" do
27
+ story1, story2 = Story.create!, Story.create!
28
+ Story.find(story1.id, story2.id).should == [story1, story2]
29
+ end
30
+
31
+ describe "#find(id, nil)" do
32
+ it "ignores the nils" do
33
+ story = Story.create!
34
+ Story.find(story.id, nil).should == story
35
+ end
36
+ end
37
+ end
38
+
39
+ describe 'when given nonexistent ids' do
40
+ describe 'when given one nonexistent id' do
41
+ it 'raises an error' do
42
+ lambda { Story.find(1) }.should raise_error(ActiveRecord::RecordNotFound)
43
+ end
44
+ end
45
+
46
+ describe 'when given multiple nonexistent ids' do
47
+ it "raises an error" do
48
+ lambda { Story.find(1, 2, 3) }.should raise_error(ActiveRecord::RecordNotFound)
49
+ end
50
+ end
51
+
52
+
53
+ describe '#find(nil)' do
54
+ it 'raises an error' do
55
+ lambda { Story.find(nil) }.should raise_error(ActiveRecord::RecordNotFound)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#find(object)' do
62
+ it "coerces arguments to integers" do
63
+ story = Story.create!
64
+ Story.find(story.id.to_s).should == story
65
+ end
66
+ end
67
+
68
+ describe '#find([...])' do
69
+ describe 'when given an array with valid ids' do
70
+ it "#finds the object with that id" do
71
+ story = Story.create!
72
+ Story.find([story.id]).should == [story]
73
+ end
74
+ end
75
+
76
+ describe '#find([])' do
77
+ it 'returns the empty array' do
78
+ Story.find([]).should == []
79
+ end
80
+ end
81
+
82
+ describe 'when given nonexistent ids' do
83
+ it 'raises an error' do
84
+ lambda { Story.find([1, 2, 3]) }.should raise_error(ActiveRecord::RecordNotFound)
85
+ end
86
+ end
87
+
88
+ describe 'when given limits and offsets' do
89
+ describe '#find([1, 2, ...], :limit => ..., :offset => ...)' do
90
+ it "returns the correct slice of objects" do
91
+ character1 = Character.create!(:name => "Sam", :story_id => 1)
92
+ character2 = Character.create!(:name => "Sam", :story_id => 1)
93
+ character3 = Character.create!(:name => "Sam", :story_id => 1)
94
+ Character.find(
95
+ [character1.id, character2.id, character3.id],
96
+ :conditions => { :name => "Sam", :story_id => 1 }, :limit => 2
97
+ ).should == [character1, character2]
98
+ end
99
+ end
100
+
101
+ describe '#find([1], :limit => 0)' do
102
+ it "raises an error" do
103
+ character = Character.create!(:name => "Sam", :story_id => 1)
104
+ lambda do
105
+ Character.find([character.id], :conditions => { :name => "Sam", :story_id => 1 }, :limit => 0)
106
+ end.should raise_error(ActiveRecord::RecordNotFound)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ describe '#find(:first, ...)' do
113
+ describe '#find(:first, ..., :offset => ...)' do
114
+ it "#finds the object in the correct order" do
115
+ story1 = Story.create!(:title => 'title1')
116
+ story2 = Story.create!(:title => story1.title)
117
+ Story.find(:first, :conditions => { :title => story1.title }, :offset => 1).should == story2
118
+ end
119
+ end
120
+
121
+ describe '#find(:first, :conditions => [])' do
122
+ it 'finds the object in the correct order' do
123
+ story = Story.create!
124
+ Story.find(:first, :conditions => []).should == story
125
+ end
126
+ end
127
+
128
+ describe "#find(:first, :conditions => '...')" do
129
+ it "coerces ruby values to the appropriate database values" do
130
+ story1 = Story.create! :title => 'a story', :published => true
131
+ story2 = Story.create! :title => 'another story', :published => false
132
+ Story.find(:first, :conditions => 'published = 0').should == story2
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ describe '#find_by_attr' do
139
+ describe '#find_by_attr(nil)' do
140
+ it 'returns nil' do
141
+ Story.find_by_id(nil).should == nil
142
+ end
143
+ end
144
+
145
+ describe 'when given non-existent ids' do
146
+ it 'returns nil' do
147
+ Story.find_by_id(-1).should == nil
148
+ end
149
+ end
150
+ end
151
+
152
+ describe '#find_all_by_attr' do
153
+ describe 'when given non-existent ids' do
154
+ it "does not raise an error" do
155
+ lambda { Story.find_all_by_id([-1, -2, -3]) }.should_not raise_error
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ describe 'when the cache is partially populated' do
162
+ describe '#find(:all, :conditions => ...)' do
163
+ it "returns the correct records" do
164
+ story1 = Story.create!(:title => title = 'once upon a time...')
165
+ $memcache.flush_all
166
+ story2 = Story.create!(:title => title)
167
+ Story.find(:all, :conditions => { :title => story1.title }).should == [story1, story2]
168
+ end
169
+ end
170
+
171
+ describe '#find(id1, id2, ...)' do
172
+ it "returns the correct records" do
173
+ story1 = Story.create!(:title => 'story 1')
174
+ $memcache.flush_all
175
+ story2 = Story.create!(:title => 'story 2')
176
+ Story.find(story1.id, story2.id).should == [story1, story2]
177
+ end
178
+ end
179
+ end
180
+
181
+ describe 'when the cache is not populated' do
182
+ describe '#find(id)' do
183
+ it "returns the correct records" do
184
+ story = Story.create!(:title => 'a story')
185
+ $memcache.flush_all
186
+ Story.find(story.id).should == story
187
+ end
188
+
189
+ it "handles after_find on model" do
190
+ class AfterFindStory < Story
191
+ def after_find
192
+ self.title
193
+ end
194
+ end
195
+ lambda do
196
+ AfterFindStory.create!(:title => 'a story')
197
+ end.should_not raise_error(ActiveRecord::MissingAttributeError)
198
+ end
199
+ end
200
+
201
+ describe '#find(id1, id2, ...)' do
202
+ it "handles finds with multiple ids correctly" do
203
+ story1 = Story.create!
204
+ story2 = Story.create!
205
+ $memcache.flush_all
206
+ Story.find(story1.id, story2.id).should == [story1, story2]
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,67 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ module Cash
4
+ describe Finders do
5
+ describe 'Calculations' do
6
+ describe 'when the cache is populated' do
7
+ before do
8
+ @stories = [Story.create!(:title => @title = 'asdf'), Story.create!(:title => @title)]
9
+ end
10
+
11
+ describe '#count(:all, :conditions => ...)' do
12
+ it "does not use the database" do
13
+ Story.count(:all, :conditions => { :title => @title }).should == @stories.size
14
+ end
15
+ end
16
+
17
+ describe '#count(:column, :conditions => ...)' do
18
+ it "uses the database, not the cache" do
19
+ mock(Story).get.never
20
+ Story.count(:title, :conditions => { :title => @title }).should == @stories.size
21
+ end
22
+ end
23
+
24
+ describe '#count(:all, :distinct => ..., :select => ...)' do
25
+ it 'uses the database, not the cache' do
26
+ mock(Story).get.never
27
+ Story.count(:all, :distinct => true, :select => :title, :conditions => { :title => @title }).should == @stories.collect(&:title).uniq.size
28
+ end
29
+ end
30
+
31
+ describe 'association proxies' do
32
+ describe '#count(:all, :conditions => ...)' do
33
+ it 'does not use the database' do
34
+ story = Story.create!
35
+ characters = [story.characters.create!(:name => name = 'name'), story.characters.create!(:name => name)]
36
+ mock(Story.connection).execute.never
37
+ story.characters.count(:all, :conditions => { :name => name }).should == characters.size
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe 'when the cache is not populated' do
44
+ describe '#count(:all, ...)' do
45
+ describe '#count(:all)' do
46
+ it 'uses the database, not the cache' do
47
+ mock(Story).get.never
48
+ Story.count
49
+ end
50
+ end
51
+
52
+ describe '#count(:all, :conditions => ...)' do
53
+ before do
54
+ Story.create!(:title => @title = 'title')
55
+ $memcache.flush_all
56
+ end
57
+
58
+ it "populates the count correctly" do
59
+ Story.count(:all, :conditions => { :title => @title }).should == 1
60
+ Story.fetch("title/#{@title}/count", :raw => true).should =~ /\s*1\s*/
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,372 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ module Cash
4
+ describe Finders do
5
+ describe 'Cache Usage' do
6
+ describe 'when the cache is populated' do
7
+ describe '#find' do
8
+ describe '#find(1)' do
9
+ it 'does not use the database' do
10
+ story = Story.create!
11
+ mock(Story.connection).execute.never
12
+ Story.find(story.id).should == story
13
+ end
14
+ end
15
+
16
+ describe '#find(object)' do
17
+ it 'uses the objects quoted id' do
18
+ story = Story.create!
19
+ mock(Story.connection).execute.never
20
+ Story.find(story).should == story
21
+ end
22
+ end
23
+
24
+ describe '#find(:first, ...)' do
25
+ describe '#find(:first, :conditions => { :id => ? })' do
26
+ it "does not use the database" do
27
+ story = Story.create!
28
+ mock(Story.connection).execute.never
29
+ Story.find(:first, :conditions => { :id => story.id }).should == story
30
+ end
31
+ end
32
+
33
+ describe '#find(:first, :conditions => [ "id = :id", { :id => story.id } ])' do
34
+ it "does not use the database" do
35
+ story = Story.create!
36
+ mock(Story.connection).execute.never
37
+ Story.find(:first, :conditions => [ "id = :id", { :id => story.id } ]).should == story
38
+ end
39
+ end
40
+
41
+ describe "#find(:first, :conditions => 'id = ?')" do
42
+ it "does not use the database" do
43
+ story = Story.create!
44
+ mock(Story.connection).execute.never
45
+ Story.find(:first, :conditions => "id = #{story.id}").should == story
46
+ Story.find(:first, :conditions => "`stories`.id = #{story.id}").should == story
47
+ Story.find(:first, :conditions => "`stories`.`id` = #{story.id}").should == story
48
+ end
49
+ end
50
+
51
+ describe '#find(:first, :readonly => false) and any other options other than conditions are nil' do
52
+ it "does not use the database" do
53
+ story = Story.create!
54
+ mock(Story.connection).execute.never
55
+ Story.find(:first, :conditions => { :id => story.id }, :readonly => false, :limit => nil, :offset => nil, :joins => nil, :include => nil).should == story
56
+ end
57
+ end
58
+
59
+ describe '#find(:first, :readonly => true)' do
60
+ it "uses the database, not the cache" do
61
+ story = Story.create!
62
+ mock(Story).get.never
63
+ Story.find(:first, :conditions => { :id => story.id }, :readonly => true).should == story
64
+ end
65
+ end
66
+
67
+ describe '#find(:first, :join => ...) or find(..., :include => ...)' do
68
+ it "uses the database, not the cache" do
69
+ story = Story.create!
70
+ mock(Story).get.never
71
+ Story.find(:first, :conditions => { :id => story.id }, :joins => 'AS stories').should == story
72
+ Story.find(:first, :conditions => { :id => story.id }, :include => :characters).should == story
73
+ end
74
+ end
75
+
76
+ describe '#find(:first)' do
77
+ it 'uses the database, not the cache' do
78
+ mock(Story).get.never
79
+ Story.find(:first)
80
+ end
81
+ end
82
+
83
+ describe '#find(:first, :conditions => "...")' do
84
+ describe 'on unindexed attributes' do
85
+ it 'uses the database, not the cache' do
86
+ story = Story.create!
87
+ mock(Story).get.never
88
+ Story.find(:first, :conditions => "type IS NULL")
89
+ end
90
+ end
91
+
92
+ describe 'on indexed attributes' do
93
+ describe 'when the attributes are integers' do
94
+ it 'does not use the database' do
95
+ story = Story.create!
96
+ mock(Story.connection).execute.never
97
+ Story.find(:first, :conditions => "`stories`.id = #{story.id}") \
98
+ .should == story
99
+ end
100
+ end
101
+
102
+ describe 'when the attributes are non-integers' do
103
+ it 'uses the database, not the cache' do
104
+ story = Story.create!(:title => "title")
105
+ mock(Story.connection).execute.never
106
+ Story.find(:first, :conditions => "`stories`.title = '#{story.title }'") \
107
+ .should == story
108
+ end
109
+ end
110
+
111
+ describe 'when the attributes must be coerced to sql values' do
112
+ it 'does not use the database' do
113
+ story1 = Story.create!(:published => true)
114
+ story2 = Story.create!(:published => false)
115
+ mock(Story.connection).execute.never
116
+ Story.find(:first, :conditions => 'published = 0').should == story2
117
+ end
118
+ end
119
+ end
120
+
121
+ describe '#find(:first, :conditions => [...])' do
122
+ describe 'with one indexed attribute' do
123
+ it 'does not use the database' do
124
+ story = Story.create!
125
+ mock(Story.connection).execute.never
126
+ Story.find(:first, :conditions => ['id = ?', story.id]).should == story
127
+ end
128
+ end
129
+
130
+ describe 'with two attributes that match a combo-index' do
131
+ it 'does not use the database' do
132
+ story = Story.create!(:title => 'title')
133
+ mock(Story.connection).execute.never
134
+ Story.find(:first, :conditions => ['id = ? AND title = ?', story.id, story.title]).should == story
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ describe '#find(:first, :conditions => {...})' do
141
+ it "does not use the database" do
142
+ story = Story.create!(:title => "Sam")
143
+ mock(Story.connection).execute.never
144
+ Story.find(:first, :conditions => { :id => story.id, :title => story.title }).should == story
145
+ end
146
+
147
+ describe 'regardless of hash order' do
148
+ it 'does not use the database' do
149
+ story = Story.create!(:title => "Sam")
150
+ mock(Story.connection).execute.never
151
+ Story.find(:first, :conditions => { :id => story.id, :title => story.title }).should == story
152
+ Story.find(:first, :conditions => { :title => story.title, :id => story.id }).should == story
153
+ end
154
+ end
155
+
156
+ describe 'on unindexed attribtes' do
157
+ it 'uses the database, not the cache' do
158
+ story = Story.create!
159
+ mock(Story).get.never
160
+ Story.find(:first, :conditions => { :id => story.id, :type => story.type }).should == story
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ describe 'when there is a with_scope' do
167
+ describe 'when the with_scope has conditions' do
168
+ describe 'when the scope conditions is a string' do
169
+ it "uses the database, not the cache" do
170
+ story = Story.create!(:title => title = 'title')
171
+ mock(Story.connection).execute.never
172
+ Story.send :with_scope, :find => { :conditions => "title = '#{title}'"} do
173
+ Story.find(:first, :conditions => { :id => story.id }).should == story
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'when the find conditions is a string' do
179
+ it "does not use the database" do
180
+ story = Story.create!(:title => title = 'title')
181
+ mock(Story.connection).execute.never
182
+ Story.send :with_scope, :find => { :conditions => { :id => story.id }} do
183
+ Story.find(:first, :conditions => "title = '#{title}'").should == story
184
+ end
185
+ end
186
+ end
187
+
188
+ describe '#find(1, :conditions => ...)' do
189
+ it "does not use the database" do
190
+ story = Story.create!
191
+ character = Character.create!(:name => name = 'barbara', :story_id => story)
192
+ mock(Character.connection).execute.never
193
+ Character.send :with_scope, :find => { :conditions => { :story_id => story.id } } do
194
+ Character.find(character.id, :conditions => { :name => name }).should == character
195
+ end
196
+ end
197
+ end
198
+ end
199
+
200
+ describe 'has_many associations' do
201
+ describe '#find(1)' do
202
+ it "does not use the database" do
203
+ story = Story.create!
204
+ character = story.characters.create!
205
+ mock(Character.connection).execute.never
206
+ story.characters.find(character.id).should == character
207
+ end
208
+ end
209
+
210
+ describe '#find(1, 2, ...)' do
211
+ it "does not use the database" do
212
+ story = Story.create!
213
+ character1 = story.characters.create!
214
+ character2 = story.characters.create!
215
+ mock(Character.connection).execute.never
216
+ story.characters.find(character1.id, character2.id).should == [character1, character2]
217
+ end
218
+ end
219
+
220
+ describe '#find_by_attr' do
221
+ it "does not use the database" do
222
+ story = Story.create!
223
+ character = story.characters.create!
224
+ mock(Character.connection).execute.never
225
+ story.characters.find_by_id(character.id).should == character
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ describe '#find(:all)' do
232
+ it "uses the database, not the cache" do
233
+ character = Character.create!
234
+ mock(Character).get.never
235
+ Character.find(:all).should == [character]
236
+ end
237
+
238
+ describe '#find(:all, :conditions => {...})' do
239
+ describe 'when the index is not empty' do
240
+ it 'does not use the database' do
241
+ story1 = Story.create!(:title => title = "title")
242
+ story2 = Story.create!(:title => title)
243
+ mock(Story.connection).execute.never
244
+ Story.find(:all, :conditions => { :title => story1.title }).should == [story1, story2]
245
+ end
246
+ end
247
+ end
248
+
249
+ describe '#find(:all, :limit => ..., :offset => ...)' do
250
+ it "cached attributes should support limits and offsets" do
251
+ character1 = Character.create!(:name => "Sam", :story_id => 1)
252
+ character2 = Character.create!(:name => "Sam", :story_id => 1)
253
+ character3 = Character.create!(:name => "Sam", :story_id => 1)
254
+ mock(Character.connection).execute.never
255
+
256
+ Character.find(:all, :conditions => { :name => character1.name, :story_id => character1.story_id }, :limit => 1).should == [character1]
257
+ Character.find(:all, :conditions => { :name => character1.name, :story_id => character1.story_id }, :offset => 1).should == [character2, character3]
258
+ Character.find(:all, :conditions => { :name => character1.name, :story_id => character1.story_id }, :limit => 1, :offset => 1).should == [character2]
259
+ end
260
+ end
261
+ end
262
+
263
+ describe '#find([...])' do
264
+ describe '#find([1, 2, ...], :conditions => ...)' do
265
+ it "uses the database, not the cache" do
266
+ story1, story2 = Story.create!, Story.create!
267
+ mock(Story).get.never
268
+ Story.find([story1.id, story2.id], :conditions => "type IS NULL").should == [story1, story2]
269
+ end
270
+ end
271
+
272
+ describe '#find([1], :conditions => ...)' do
273
+ it "uses the database, not the cache" do
274
+ story1, story2 = Story.create!, Story.create!
275
+ mock(Story).get.never
276
+ Story.find([story1.id], :conditions => "type IS NULL").should == [story1]
277
+ end
278
+ end
279
+ end
280
+
281
+ describe '#find_by_attr' do
282
+ describe 'on indexed attributes' do
283
+ describe '#find_by_id(id)' do
284
+ it "does not use the database" do
285
+ story = Story.create!
286
+ mock(Story.connection).execute.never
287
+ Story.find_by_id(story.id).should == story
288
+ end
289
+ end
290
+
291
+ describe '#find_by_title(title)' do
292
+ it "does not use the database" do
293
+ story1 = Story.create!(:title => 'title1')
294
+ story2 = Story.create!(:title => 'title2')
295
+ mock(Story.connection).execute.never
296
+ Story.find_by_title('title1').should == story1
297
+ end
298
+ end
299
+ end
300
+ end
301
+
302
+ describe "Single Table Inheritence" do
303
+ describe '#find(:all, ...)' do
304
+ it "does not use the database" do
305
+ story, epic, oral = Story.create!(:title => title = 'foo'), Epic.create!(:title => title), Oral.create!(:title => title)
306
+ mock(Story.connection).execute.never
307
+ Story.find(:all, :conditions => { :title => title }).should == [story, epic, oral]
308
+ Epic.find(:all, :conditions => { :title => title }).should == [epic, oral]
309
+ Oral.find(:all, :conditions => { :title => title }).should == [oral]
310
+ end
311
+ end
312
+ end
313
+ end
314
+
315
+ describe '#without_cache' do
316
+ describe 'when finders are called within the provided block' do
317
+ it 'uses the database not the cache' do
318
+ story = Story.create!
319
+ mock(Story).get.never
320
+ Story.without_cache do
321
+ Story.find(story.id).should == story
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ describe 'when the cache is not populated' do
329
+ before do
330
+ @story = Story.create!(:title => 'title')
331
+ $memcache.flush_all
332
+ end
333
+
334
+ describe '#find(:first, ...)' do
335
+ it 'populates the cache' do
336
+ Story.find(:first, :conditions => { :title => @story.title })
337
+ Story.fetch("title/#{@story.title}").should == [@story.id]
338
+ end
339
+ end
340
+
341
+ describe '#find_by_attr' do
342
+ it 'populates the cache' do
343
+ Story.find_by_title(@story.title)
344
+ Story.fetch("title/#{@story.title}").should == [@story.id]
345
+ end
346
+ end
347
+
348
+ describe '#find(:all, :conditions => ...)' do
349
+ it 'populates the cache' do
350
+ Story.find(:all, :conditions => { :title => @story.title })
351
+ Story.fetch("title/#{@story.title}").should == [@story.id]
352
+ end
353
+ end
354
+
355
+ describe '#find(1)' do
356
+ it 'populates the cache' do
357
+ Story.find(@story.id)
358
+ Story.fetch("id/#{@story.id}").should == [@story]
359
+ end
360
+ end
361
+
362
+ describe 'when there is a with_scope' do
363
+ it "uses the database, not the cache" do
364
+ Story.send :with_scope, :find => { :conditions => { :title => @story.title }} do
365
+ Story.find(:first, :conditions => { :id => @story.id }).should == @story
366
+ end
367
+ end
368
+ end
369
+ end
370
+ end
371
+ end
372
+ end