simple_cacheable 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,18 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
- let(:user) { User.create(:login => 'flyerhzm') }
6
- let(:descendant) { Descendant.create(:login => "scotterc")}
7
5
 
8
6
  before :all do
9
- @post1 = user.posts.create(:title => 'post1')
10
- @post2 = user.posts.create(:title => 'post2')
11
- @post3 = descendant.posts.create(:title => 'post3')
7
+ @user = User.create(:login => 'flyerhzm')
8
+ @descendant = Descendant.create(:login => "scotterc")
9
+ @post1 = @user.posts.create(:title => 'post1')
10
+ @post2 = @user.posts.create(:title => 'post2')
11
+ @post3 = @descendant.posts.create(:title => 'post3')
12
12
  end
13
13
 
14
14
  before :each do
15
- cache.clear
16
- user.reload
15
+ @user.reload
17
16
  end
18
17
 
19
18
  context "with_attribute" do
@@ -22,13 +21,13 @@ describe Cacheable do
22
21
  end
23
22
 
24
23
  it "should cache by User.find_by_login" do
25
- User.find_cached_by_login("flyerhzm").should == user
26
- Rails.cache.read("users/attribute/login/flyerhzm").should == user
24
+ User.find_cached_by_login("flyerhzm").should == @user
25
+ Rails.cache.read("users/attribute/login/flyerhzm").should =={:class => @user.class, 'attributes' => @user.attributes}
27
26
  end
28
27
 
29
28
  it "should get cached by User.find_by_login multiple times" do
30
29
  User.find_cached_by_login("flyerhzm")
31
- User.find_cached_by_login("flyerhzm").should == user
30
+ User.find_cached_by_login("flyerhzm").should == @user
32
31
  end
33
32
 
34
33
  it "should escape whitespace" do
@@ -37,23 +36,31 @@ describe Cacheable do
37
36
  end
38
37
 
39
38
  it "should handle fixed numbers" do
40
- Post.find_cached_by_user_id(user.id).should == @post1
41
- Rails.cache.read("posts/attribute/user_id/#{user.id}").should == @post1
39
+ Post.find_cached_by_user_id(@user.id).should == @post1
40
+ Rails.cache.read("posts/attribute/user_id/#{@user.id}").should == {:class => @post1.class, 'attributes' => @post1.attributes}
41
+ end
42
+
43
+ it "should return correct nil values" do
44
+ User.find_cached_by_login("ducksauce").should be_nil
45
+ User.find_cached_by_login("ducksauce").should be_nil
46
+ User.find_cached_all_by_login("ducksauce").should == []
47
+ User.find_cached_all_by_login("ducksauce").should == []
42
48
  end
43
49
 
44
50
  context "find_all" do
45
51
  it "should not cache Post.find_all_by_user_id" do
46
- Rails.cache.read("posts/attribute/user_id/all/#{user.id}").should be_nil
52
+ Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should be_nil
47
53
  end
48
54
 
49
55
  it "should cache by Post.find_cached_all_by_user_id" do
50
- Post.find_cached_all_by_user_id(user.id).should == [@post1, @post2]
51
- Rails.cache.read("posts/attribute/user_id/all/#{user.id}").should == [@post1, @post2]
56
+ Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
57
+ Rails.cache.read("posts/attribute/user_id/all/#{@user.id}").should == [{:class => @post1.class, 'attributes' => @post1.attributes},
58
+ {:class => @post2.class, 'attributes' => @post2.attributes}]
52
59
  end
53
60
 
54
61
  it "should get cached by Post.find_cached_all_by_user_id multiple times" do
55
- Post.find_cached_all_by_user_id(user.id)
56
- Post.find_cached_all_by_user_id(user.id).should == [@post1, @post2]
62
+ Post.find_cached_all_by_user_id(@user.id)
63
+ Post.find_cached_all_by_user_id(@user.id).should == [@post1, @post2]
57
64
  end
58
65
 
59
66
  end
@@ -61,17 +68,17 @@ describe Cacheable do
61
68
 
62
69
  context "descendant" do
63
70
  it "should not cache Descendant.find_by_login" do
64
- Rails.cache.read("descendants/attribute/login/scotterc").should be_nil
71
+ Rails.cache.read("users/attribute/login/scotterc").should be_nil
65
72
  end
66
73
 
67
74
  it "should cache by Descendant.find_by_login" do
68
- Descendant.find_cached_by_login("scotterc").should == descendant
69
- Rails.cache.read("descendants/attribute/login/scotterc").should == descendant
75
+ Descendant.find_cached_by_login("scotterc").should == @descendant
76
+ Rails.cache.read("users/attribute/login/scotterc").should == {:class => @descendant.class, 'attributes' => @descendant.attributes }
70
77
  end
71
78
 
72
79
  it "should get cached by Descendant.find_by_login multiple times" do
73
80
  Descendant.find_cached_by_login("scotterc")
74
- Descendant.find_cached_by_login("scotterc").should == descendant
81
+ Descendant.find_cached_by_login("scotterc").should == @descendant
75
82
  end
76
83
 
77
84
  it "should escape whitespace" do
@@ -80,9 +87,9 @@ describe Cacheable do
80
87
  end
81
88
 
82
89
  it "maintains cached methods" do
83
- Rails.cache.read("descendants/#{descendant.id}/method/name").should be_nil
84
- descendant.cached_name.should == descendant.name
85
- Rails.cache.read("descendants/#{descendant.id}/method/name").should == descendant.name
90
+ Rails.cache.read("users/#{@descendant.id}/method/name").should be_nil
91
+ @descendant.cached_name.should == @descendant.name
92
+ Rails.cache.read("users/#{@descendant.id}/method/name").should == @descendant.name
86
93
  end
87
94
  end
88
95
 
@@ -2,42 +2,70 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
- let(:user) { User.create(:login => 'flyerhzm') }
6
5
 
7
6
  before :all do
8
- @post1 = user.posts.create(:title => 'post1')
9
- @post2 = user.posts.create(:title => 'post2')
7
+ @user = User.create(:login => 'flyerhzm', :email => 'flyerhzm@mail.com')
8
+ @post1 = @user.posts.create(:title => 'post1')
9
+ @post2 = @user.posts.create(:title => 'post2')
10
10
  end
11
11
 
12
12
  before :each do
13
- cache.clear
14
- user.reload
13
+ @user.reload
15
14
  end
16
15
 
17
- context "with_class_method" do
18
- it "should not cache Post.default_post" do
19
- Rails.cache.read("posts/class_method/default_post").should be_nil
16
+ it "should not cache Post.default_post" do
17
+ Rails.cache.read("posts/class_method/default_post").should be_nil
18
+ end
19
+
20
+ it "should cache Post.default_post" do
21
+ Post.cached_default_post.should == @post1
22
+ Rails.cache.read("posts/class_method/default_post").should == coder(@post1)
23
+ end
24
+
25
+ it "should cache Post.default_post multiple times" do
26
+ Post.cached_default_post
27
+ Post.cached_default_post.should == @post1
28
+ end
29
+
30
+ it "should cache Post.retrieve_with_user_id" do
31
+ Post.cached_retrieve_with_user_id(1).should == @post1
32
+ Rails.cache.read("posts/class_method/retrieve_with_user_id/1").should == coder(@post1)
33
+ end
34
+
35
+ it "should cache Post.retrieve_with_both with multiple arguments" do
36
+ Post.cached_retrieve_with_both(1, 1).should be_true
37
+ Rails.cache.read("posts/class_method/retrieve_with_both/1+1").should be_true
38
+ end
39
+
40
+ describe "argument types" do
41
+ it "should handle methods with a number argument" do
42
+ result = User.cached_user_with_id(1)
43
+ key = User.class_method_cache_key("user_with_id", 1)
44
+ Rails.cache.read(key).should == coder(result)
20
45
  end
21
46
 
22
- it "should cache Post.default_post" do
23
- Post.cached_default_post.should == @post1
24
- Rails.cache.read("posts/class_method/default_post").should == @post1
47
+ it "should handle methods with a string argument" do
48
+ result = User.cached_user_with_email("flyerhzm@mail.com")
49
+ key = User.class_method_cache_key("user_with_email", "flyerhzm@mail.com")
50
+ Rails.cache.read(key).should == coder(result)
25
51
  end
26
52
 
27
- it "should cache Post.default_post multiple times" do
28
- Post.cached_default_post
29
- Post.cached_default_post.should == @post1
53
+ it "should handle methods with an array argument" do
54
+ result = User.cached_users_with_ids([ 1 ])
55
+ key = User.class_method_cache_key("users_with_ids", [ 1 ])
56
+ Rails.cache.read(key).should == coder(result)
30
57
  end
31
58
 
32
- it "should cache Post.retrieve_with_user_id" do
33
- Post.cached_retrieve_with_user_id(1).should == @post1
34
- Rails.cache.read("posts/class_method/retrieve_with_user_id/1").should == @post1
59
+ it "should handle methods with a range argument" do
60
+ result = User.cached_users_with_ids_in( (1...3) )
61
+ key = User.class_method_cache_key("users_with_ids_in", (1...3))
62
+ Rails.cache.read(key).should == coder(result)
35
63
  end
36
64
 
37
- it "should cache Post.retrieve_with_both with multiple arguments" do
38
- Post.cached_retrieve_with_both(1, 1).should be_true
39
- Rails.cache.read("posts/class_method/retrieve_with_both/1+1").should be_true
65
+ it "should handle methods with a hash argument" do
66
+ result = Post.cached_where_options_are(:title => 'post1')
67
+ key = Post.class_method_cache_key("where_options_are", {:title => 'post1'})
68
+ Rails.cache.read(key).should == coder(result)
40
69
  end
41
70
  end
42
-
43
71
  end
@@ -2,26 +2,65 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
- let(:user) { User.create(:login => 'flyerhzm') }
5
+
6
+ before :all do
7
+ @user = User.create(:login => 'flyerhzm')
8
+ @post1 = @user.posts.create(:title => 'post1')
9
+ end
6
10
 
7
11
  before :each do
8
- cache.clear
9
- user.reload
12
+ @user.reload
13
+ end
14
+
15
+ it "should not cache key" do
16
+ Rails.cache.read("users/#{@user.id}").should be_nil
10
17
  end
11
18
 
12
- context "with_key" do
13
- it "should not cache key" do
14
- Rails.cache.read("users/#{user.id}").should be_nil
19
+ it "should cache by User#id" do
20
+ User.find_cached(@user.id).should == @user
21
+ Rails.cache.read("users/#{@user.id}").should == coder(@user)
22
+ end
23
+
24
+ it "should get cached by User#id multiple times" do
25
+ User.find_cached(@user.id)
26
+ User.find_cached(@user.id).should == @user
27
+ end
28
+
29
+ describe "it should handle slugs as keys" do
30
+ it "should have a slug" do
31
+ @post1.slug.should == @post1.title
32
+ end
33
+
34
+ it "should be accessed by slug" do
35
+ Post.find(@post1.slug).should == @post1
15
36
  end
16
37
 
17
- it "should cache by User#id" do
18
- User.find_cached(user.id).should == user
19
- Rails.cache.read("users/#{user.id}").should == user
38
+ it "should cache it with id" do
39
+ Rails.cache.read("posts/#{@post1.id}").should == nil
40
+ Post.find_cached(@post1.id)
41
+ Rails.cache.read("posts/#{@post1.id}").should == coder(@post1)
20
42
  end
21
43
 
22
- it "should get cached by User#id multiple times" do
23
- User.find_cached(user.id)
24
- User.find_cached(user.id).should == user
44
+ it "should cache it with slug" do
45
+ Rails.cache.read("posts/#{@post1.slug}").should == nil
46
+ Post.find_cached(@post1.slug)
47
+ Rails.cache.read("posts/#{@post1.slug}").should == coder(@post1)
48
+ end
49
+
50
+ describe "it should expire both" do
51
+ it "should expire it with id" do
52
+ Post.find_cached(@post1.id)
53
+ Rails.cache.read("posts/#{@post1.id}").should == coder(@post1)
54
+ @post1.save
55
+ Rails.cache.read("posts/#{@post1.id}").should == nil
56
+ end
57
+
58
+ it "should expire it with slug" do
59
+ Post.find_cached(@post1.slug)
60
+ Rails.cache.read("posts/#{@post1.slug}").should == coder(@post1)
61
+ @post1.save
62
+ Rails.cache.read("posts/#{@post1.slug}").should == nil
63
+ end
25
64
  end
26
65
  end
27
66
 
@@ -2,66 +2,105 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
- let(:user) { User.create(:login => 'flyerhzm') }
6
- let(:descendant) { Descendant.create(:login => "scotterc")}
7
5
 
8
6
  before :all do
9
- @post1 = user.posts.create(:title => 'post1')
10
- @post2 = user.posts.create(:title => 'post2')
11
- @post3 = descendant.posts.create(:title => 'post3')
7
+ @user = User.create(:login => 'flyerhzm')
8
+ @descendant = Descendant.create(:login => "scotterc")
9
+ @post1 = @user.posts.create(:title => 'post1')
10
+ @post2 = @user.posts.create(:title => 'post2')
11
+ @post3 = @descendant.posts.create(:title => 'post3')
12
12
  end
13
13
 
14
14
  before :each do
15
- cache.clear
16
- user.reload
15
+ @user.reload
17
16
  end
18
17
 
19
- context "with_method" do
20
- it "should not cache User.last_post" do
21
- Rails.cache.read("users/#{user.id}/method/last_post").should be_nil
18
+ it "should not cache User.last_post" do
19
+ Rails.cache.read("users/#{@user.id}/method/last_post").should be_nil
20
+ end
21
+
22
+ it "should cache User#last_post" do
23
+ @user.cached_last_post.should == @user.last_post
24
+ Rails.cache.read("users/#{@user.id}/method/last_post").should == coder(@user.last_post)
25
+ end
26
+
27
+ it "should cache User#last_post multiple times" do
28
+ @user.cached_last_post
29
+ @user.cached_last_post.should == @user.last_post
30
+ end
31
+
32
+ context "descendant should inherit methods" do
33
+ it "should not cache Descendant.last_post" do
34
+ Rails.cache.read("users/#{@descendant.id}/method/last_post").should be_nil
22
35
  end
23
36
 
24
- it "should cache User#last_post" do
25
- user.cached_last_post.should == user.last_post
26
- Rails.cache.read("users/#{user.id}/method/last_post").should == user.last_post
37
+ it "should cache Descendant#last_post" do
38
+ @descendant.cached_last_post.should == @descendant.last_post
39
+ Rails.cache.read("users/#{@descendant.id}/method/last_post").should == coder(@descendant.last_post)
27
40
  end
28
41
 
29
- it "should cache User#last_post multiple times" do
30
- user.cached_last_post
31
- user.cached_last_post.should == user.last_post
42
+ it "should cache Descendant#last_post multiple times" do
43
+ @descendant.cached_last_post
44
+ @descendant.cached_last_post.should == @descendant.last_post
32
45
  end
33
46
 
34
- context "descendant should inherit methods" do
35
- it "should not cache Descendant.last_post" do
36
- Rails.cache.read("descendants/#{descendant.id}/method/last_post").should be_nil
47
+ context "as well as new methods" do
48
+ it "should not cache Descendant.name" do
49
+ Rails.cache.read("users/#{@descendant.id}/method/name").should be_nil
37
50
  end
38
51
 
39
- it "should cache Descendant#last_post" do
40
- descendant.cached_last_post.should == descendant.last_post
41
- Rails.cache.read("descendants/#{descendant.id}/method/last_post").should == descendant.last_post
52
+ it "should cache Descendant#name" do
53
+ @descendant.cached_name.should == @descendant.name
54
+ Rails.cache.read("users/#{@descendant.id}/method/name").should == @descendant.name
42
55
  end
43
56
 
44
- it "should cache Descendant#last_post multiple times" do
45
- descendant.cached_last_post
46
- descendant.cached_last_post.should == descendant.last_post
57
+ it "should cache Descendant#name multiple times" do
58
+ @descendant.cached_name
59
+ @descendant.cached_name.should == @descendant.name
47
60
  end
61
+ end
62
+ end
63
+
64
+ describe "memoization" do
65
+ before :each do
66
+ @user.instance_variable_set("@cached_last_post", nil)
67
+ @user.expire_model_cache
68
+ end
48
69
 
49
- context "as well as new methods" do
50
- it "should not cache Descendant.name" do
51
- Rails.cache.read("descendants/#{descendant.id}/method/name").should be_nil
52
- end
70
+ it "memoizes cache calls" do
71
+ @user.instance_variable_get("@cached_last_post").should be_nil
72
+ @user.cached_last_post.should == @user.last_post
73
+ @user.instance_variable_get("@cached_last_post").should == @user.last_post
74
+ end
53
75
 
54
- it "should cache Descendant#name" do
55
- descendant.cached_name.should == descendant.name
56
- Rails.cache.read("descendants/#{descendant.id}/method/name").should == descendant.name
57
- end
76
+ it "hits the cache only once" do
77
+ Cacheable.expects(:fetch).returns(@user.last_post).once
78
+ @user.cached_last_post.should == @user.last_post
79
+ @user.cached_last_post.should == @user.last_post
80
+ end
81
+
82
+ describe "bad iv names stripped" do
83
+ it "should deal with queries" do
84
+ @user.instance_variable_get("@cached_bad_iv_name_bang").should be_nil
85
+ @user.cached_bad_iv_name!.should == 42
86
+ @user.instance_variable_get("@cached_bad_iv_name_bang").should == 42
87
+ end
58
88
 
59
- it "should cache Descendant#name multiple times" do
60
- descendant.cached_name
61
- descendant.cached_name.should == descendant.name
62
- end
89
+ it "should deal with bangs" do
90
+ @user.instance_variable_get("@cached_bad_iv_name_query").should be_nil
91
+ @user.cached_bad_iv_name?.should == 44
92
+ @user.instance_variable_get("@cached_bad_iv_name_query").should == 44
63
93
  end
64
94
  end
65
95
  end
66
96
 
97
+ describe "data types" do
98
+
99
+ it "handles boolean values" do
100
+ Rails.cache.read("users/#{@user.id}/method/admin?").should be_nil
101
+ @user.cached_admin?.should == false
102
+ Rails.cache.read("users/#{@user.id}/method/admin?").should be_false
103
+ end
104
+ end
105
+
67
106
  end
@@ -2,28 +2,27 @@ require 'spec_helper'
2
2
 
3
3
  describe Cacheable do
4
4
  let(:cache) { Rails.cache }
5
- let(:user) { User.create(:login => 'flyerhzm') }
6
5
 
7
6
  before :all do
8
- @group1 = Group.create(name: "Ruby On Rails")
9
- @account = user.create_account(group: @group1)
10
- @post1 = user.posts.create(:title => 'post1')
11
- @image1 = @post1.images.create
7
+ @user = User.create(:login => 'flyerhzm')
8
+ @group1 = Group.create(name: "Ruby On Rails")
9
+ @account = @user.create_account(group: @group1)
10
+ @post1 = @user.posts.create(:title => 'post1')
11
+ @image1 = @post1.images.create
12
12
  @comment1 = @post1.comments.create
13
- @tag1 = @post1.tags.create(title: "Rails")
13
+ @tag1 = @post1.tags.create(title: "Rails")
14
14
  end
15
15
 
16
16
  before :each do
17
- cache.clear
18
- user.reload
17
+ @user.reload
19
18
  end
20
19
 
21
20
  context "Association Expires on Save" do
22
21
  it "should delete has_many with_association cache" do
23
- user.cached_posts
24
- Rails.cache.read("users/#{user.id}/association/posts").should_not be_nil
22
+ @user.cached_posts
23
+ Rails.cache.read("users/#{@user.id}/association/posts").should_not be_nil
25
24
  @post1.save
26
- Rails.cache.read("users/#{user.id}/association/posts").should be_nil
25
+ Rails.cache.read("users/#{@user.id}/association/posts").should be_nil
27
26
  end
28
27
 
29
28
  it "should delete has_many with polymorphic with_association cache" do
@@ -34,17 +33,17 @@ describe Cacheable do
34
33
  end
35
34
 
36
35
  it "should delete has_many through with_association cache" do
37
- user.cached_images
38
- Rails.cache.read("users/#{user.id}/association/images").should_not be_nil
36
+ @user.cached_images
37
+ Rails.cache.read("users/#{@user.id}/association/images").should_not be_nil
39
38
  @image1.save
40
- Rails.cache.read("users/#{user.id}/association/images").should be_nil
39
+ Rails.cache.read("users/#{@user.id}/association/images").should be_nil
41
40
  end
42
41
 
43
42
  it "should delete has_one with_association cache" do
44
- user.cached_account
45
- Rails.cache.read("users/#{user.id}/association/account").should_not be_nil
43
+ @user.cached_account
44
+ Rails.cache.read("users/#{@user.id}/association/account").should_not be_nil
46
45
  @account.save
47
- Rails.cache.read("users/#{user.id}/association/account").should be_nil
46
+ Rails.cache.read("users/#{@user.id}/association/account").should be_nil
48
47
  end
49
48
 
50
49
  it "should delete has_and_belongs_to_many with_association cache" do
@@ -56,9 +55,9 @@ describe Cacheable do
56
55
 
57
56
  it "should delete has_one through belongs_to with_association cache" do
58
57
  @group1.save
59
- Rails.cache.read("users/#{user.id}/association/group").should be_nil
60
- user.cached_group.should == @group1
61
- Rails.cache.read("users/#{user.id}/association/group").should == @group1
58
+ Rails.cache.read("users/#{@user.id}/association/group").should be_nil
59
+ @user.cached_group.should == @group1
60
+ Rails.cache.read("users/#{@user.id}/association/group").should == coder(@group1)
62
61
  end
63
62
  end
64
63
  end