-
Notifications
You must be signed in to change notification settings - Fork 382
/
counter_cache_spec.rb
149 lines (110 loc) · 3.66 KB
/
counter_cache_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
require 'spec_helper'
module CounterCacheFixtureModels
class User
include MongoMapper::Document
key :posts_count, Integer, :default => 0
has_many :posts,
:class_name => "CounterCacheFixtureModels::Post"
end
class Post
include MongoMapper::Document
key :comments_count, Integer, :default => 0
key :some_custom_comments_count, Integer, :default => 0
has_many :comments,
:class_name => "CounterCacheFixtureModels::Comment"
belongs_to :user,
:class_name => "CounterCacheFixtureModels::User"
counter_cache :user
end
class Comment
include MongoMapper::Document
belongs_to :post,
:class_name => "CounterCacheFixtureModels::Post"
counter_cache :post
end
class CustomComment
include MongoMapper::Document
belongs_to :post,
:class_name => "CounterCacheFixtureModels::Post"
counter_cache :post, :field => :some_custom_comments_count
end
end
describe MongoMapper::Plugins::CounterCache do
before do
@post_class = CounterCacheFixtureModels::Post
@comment_class = CounterCacheFixtureModels::Comment
@user_class = CounterCacheFixtureModels::User
@custom_comment_class = CounterCacheFixtureModels::CustomComment
end
it "should have a key with posts_count defaulting to 0" do
@post_class.new.comments_count.should == 0
end
it "should update the count when a new object is created" do
post = @post_class.new
comment = @comment_class.new
post.save!
comment.post = post
comment.save!
post.reload
post.comments_count.should == 1
second_comment = @comment_class.new
second_comment.post = post
second_comment.save!
post.reload
post.comments_count.should == 2
end
it "should decrease the count by one when an object is destroyed" do
post = @post_class.new
comment = @comment_class.new
post.save!
comment.post = post
comment.save!
post.reload
post.comments_count.should == 1
comment.destroy
post.reload
post.comments_count.should == 0
end
it "should use the correct association name" do
@user = @user_class.new
@post = @post_class.new
@user.save!
@post.user = @user
@post.save!
@user.reload
@user.posts_count.should == 1
end
it "should be able to use a custom field name" do
@post = @post_class.new
@custom_comment = @custom_comment_class.new
@post.save!
@custom_comment.post = @post
@custom_comment.save!
@post.reload
@post.some_custom_comments_count.should == 1
end
it "should thrown an error if there is no association" do
lambda {
CounterCacheFixtureModels.module_eval do
class CommentWithInvalidAssociation
include MongoMapper::Document
belongs_to :post,
:class_name => "CounterCacheFixtureModels::Post"
counter_cache :foo
end
end
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError, "You must define an association with name `foo' on model CommentWithInvalidAssociation")
end
it "should thown a sensible error if the field is not defined on the target object" do
lambda {
CounterCacheFixtureModels.module_eval do
class CommentWithBadRefenceField
include MongoMapper::Document
belongs_to :post,
:class_name => "CounterCacheFixtureModels::Post"
counter_cache :post, :field => :invalid_field
end
end
}.should raise_error(MongoMapper::Plugins::CounterCache::InvalidCounterCacheError, "Missing `key :invalid_field, Integer, :default => 0' on model CounterCacheFixtureModels::Post")
end
end