forked from mongomapper/mongomapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_caching.rb
77 lines (64 loc) · 1.94 KB
/
test_caching.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
require 'test_helper'
class CachingTest < Test::Unit::TestCase
context "Caching" do
setup do
@klass = Class.new do
extend MongoMapper::Plugins
plugin MongoMapper::Plugins::Caching
end
@klass.stubs(:name).returns('Post')
@klass.any_instance.stubs(:persisted?).returns(true)
@klass.any_instance.stubs(:[]).returns(nil)
@klass.any_instance.stubs(:[]=).returns(nil)
end
context "new" do
setup do
@doc = @klass.new
@doc.stubs(:persisted?).returns(false)
end
should "be class/new" do
@doc.cache_key.should == 'Post/new'
end
should "work with suffix" do
@doc.cache_key(:foo).
should == 'Post/new/foo'
@doc.cache_key(:foo, :bar).
should == 'Post/new/foo/bar'
end
end
context "not new" do
setup do
@object_id = BSON::ObjectId.new
@doc = @klass.new
@doc.stubs(:persisted).returns(true)
@doc.stubs(:id).returns(@object_id)
end
context "with updated_at" do
setup do
time = Time.utc(2010, 6, 20, 8, 10, 7)
@doc.stubs(:[]).with(:updated_at).returns(time)
end
should "be class/id-timestamp" do
@doc.cache_key.should == "Post/#{@object_id}-20100620081007"
end
should "work with suffix" do
@doc.cache_key(:foo).
should == "Post/#{@object_id}-20100620081007/foo"
@doc.cache_key(:foo, :bar).
should == "Post/#{@object_id}-20100620081007/foo/bar"
end
end
context "without updated_at" do
should "be class/id" do
@doc.cache_key.should == "Post/#{@object_id}"
end
should "work with suffix" do
@doc.cache_key(:foo).
should == "Post/#{@object_id}/foo"
@doc.cache_key(:foo, :bar, :baz).
should == "Post/#{@object_id}/foo/bar/baz"
end
end
end
end
end