socialization 0.5.0.beta3 → 0.5.0.beta4
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.
- data/lib/socialization/actors/follower.rb +2 -0
- data/lib/socialization/actors/liker.rb +2 -0
- data/lib/socialization/actors/mentioner.rb +2 -0
- data/lib/socialization/stores/active_record/follow.rb +12 -0
- data/lib/socialization/stores/active_record/like.rb +12 -0
- data/lib/socialization/stores/active_record/mention.rb +12 -0
- data/lib/socialization/stores/redis/base.rb +120 -3
- data/lib/socialization/stores/redis/follow.rb +10 -87
- data/lib/socialization/stores/redis/like.rb +10 -118
- data/lib/socialization/stores/redis/mention.rb +10 -87
- data/lib/socialization/version.rb +1 -1
- data/lib/socialization/victims/followable.rb +2 -0
- data/lib/socialization/victims/likeable.rb +2 -0
- data/lib/socialization/victims/mentionable.rb +2 -0
- data/test/actors/follower_test.rb +12 -0
- data/test/actors/liker_test.rb +4 -0
- data/test/actors/mentioner_test.rb +5 -0
- data/test/stores/active_record/follow_store_test.rb +17 -0
- data/test/stores/active_record/like_store_test.rb +18 -0
- data/test/stores/active_record/mention_store_test.rb +17 -0
- data/test/stores/redis/base_test.rb +203 -0
- data/test/stores/redis/follow_store_test.rb +16 -142
- data/test/stores/redis/like_store_test.rb +14 -142
- data/test/stores/redis/mention_store_test.rb +14 -142
- data/test/test_helper.rb +18 -0
- data/test/victims/followable_test.rb +12 -0
- data/test/victims/likeable_test.rb +13 -0
- data/test/victims/mentionable_test.rb +13 -0
- metadata +6 -4
@@ -108,6 +108,24 @@ class ActiveRecordLikeStoreTest < Test::Unit::TestCase
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
+
context "#remove_likers" do
|
112
|
+
should "delete all likers relationships for a likeable" do
|
113
|
+
@liker.like!(@likeable)
|
114
|
+
assert_equal 1, @likeable.likers(@liker.class).count
|
115
|
+
@klass.remove_likers(@likeable)
|
116
|
+
assert_equal 0, @likeable.likers(@liker.class).count
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "#remove_likeables" do
|
121
|
+
should "delete all likeables relationships for a liker" do
|
122
|
+
@liker.like!(@likeable)
|
123
|
+
assert_equal 1, @liker.likeables(@likeable.class).count
|
124
|
+
@klass.remove_likeables(@liker)
|
125
|
+
assert_equal 0, @liker.likeables(@likeable.class).count
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
111
129
|
end
|
112
130
|
|
113
131
|
# Helpers
|
@@ -108,6 +108,23 @@ class ActiveRecordMentionStoreTest < Test::Unit::TestCase
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
+
context "#remove_mentioners" do
|
112
|
+
should "delete all mentioners relationships for a mentionable" do
|
113
|
+
@mentioner.mention!(@mentionable)
|
114
|
+
assert_equal 1, @mentionable.mentioners(@mentioner.class).count
|
115
|
+
@klass.remove_mentioners(@mentionable)
|
116
|
+
assert_equal 0, @mentionable.mentioners(@mentioner.class).count
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "#remove_mentionables" do
|
121
|
+
should "delete all mentionables relationships for a mentioner" do
|
122
|
+
@mentioner.mention!(@mentionable)
|
123
|
+
assert_equal 1, @mentioner.mentionables(@mentionable.class).count
|
124
|
+
@klass.remove_mentionables(@mentioner)
|
125
|
+
assert_equal 0, @mentioner.mentionables(@mentionable.class).count
|
126
|
+
end
|
127
|
+
end
|
111
128
|
end
|
112
129
|
|
113
130
|
# Helpers
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
|
+
|
3
|
+
class RedisBaseStoreTest < Test::Unit::TestCase
|
4
|
+
# Testing through RedisStores::Follow for easy testing
|
5
|
+
context "RedisStores::Base through RedisStores::Follow" do
|
6
|
+
setup do
|
7
|
+
use_redis_store
|
8
|
+
@klass = Socialization::RedisStores::Follow
|
9
|
+
@klass.touch nil
|
10
|
+
@klass.after_follow nil
|
11
|
+
@klass.after_unfollow nil
|
12
|
+
@follower1 = ImAFollower.create
|
13
|
+
@follower2 = ImAFollower.create
|
14
|
+
@followable1 = ImAFollowable.create
|
15
|
+
@followable2 = ImAFollowable.create
|
16
|
+
end
|
17
|
+
|
18
|
+
context "Stores" do
|
19
|
+
should "inherit Socialization::RedisStores::Follow" do
|
20
|
+
assert_equal Socialization::RedisStores::Follow, Socialization.follow_model
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#follow!" do
|
25
|
+
should "create follow records" do
|
26
|
+
@klass.follow!(@follower1, @followable1)
|
27
|
+
assert_array_similarity ["#{@follower1.class}:#{@follower1.id}"], Socialization.redis.smembers(forward_key(@followable1))
|
28
|
+
assert_array_similarity ["#{@followable1.class}:#{@followable1.id}"], Socialization.redis.smembers(backward_key(@follower1))
|
29
|
+
|
30
|
+
@klass.follow!(@follower2, @followable1)
|
31
|
+
assert_array_similarity ["#{@follower1.class}:#{@follower1.id}", "#{@follower2.class}:#{@follower2.id}"], Socialization.redis.smembers(forward_key(@followable1))
|
32
|
+
assert_array_similarity ["#{@followable1.class}:#{@followable1.id}"], Socialization.redis.smembers(backward_key(@follower1))
|
33
|
+
assert_array_similarity ["#{@followable1.class}:#{@followable1.id}"], Socialization.redis.smembers(backward_key(@follower2))
|
34
|
+
end
|
35
|
+
|
36
|
+
should "touch follower when instructed" do
|
37
|
+
@klass.touch :follower
|
38
|
+
@follower1.expects(:touch).once
|
39
|
+
@followable1.expects(:touch).never
|
40
|
+
@klass.follow!(@follower1, @followable1)
|
41
|
+
end
|
42
|
+
|
43
|
+
should "touch followable when instructed" do
|
44
|
+
@klass.touch :followable
|
45
|
+
@follower1.expects(:touch).never
|
46
|
+
@followable1.expects(:touch).once
|
47
|
+
@klass.follow!(@follower1, @followable1)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "touch all when instructed" do
|
51
|
+
@klass.touch :all
|
52
|
+
@follower1.expects(:touch).once
|
53
|
+
@followable1.expects(:touch).once
|
54
|
+
@klass.follow!(@follower1, @followable1)
|
55
|
+
end
|
56
|
+
|
57
|
+
should "call after follow hook" do
|
58
|
+
@klass.after_follow :after_follow
|
59
|
+
@klass.expects(:after_follow).once
|
60
|
+
@klass.follow!(@follower1, @followable1)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "call after unfollow hook" do
|
64
|
+
@klass.after_follow :after_unfollow
|
65
|
+
@klass.expects(:after_unfollow).once
|
66
|
+
@klass.follow!(@follower1, @followable1)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "#unfollow!" do
|
71
|
+
setup do
|
72
|
+
@klass.follow!(@follower1, @followable1)
|
73
|
+
end
|
74
|
+
|
75
|
+
should "remove follow records" do
|
76
|
+
@klass.unfollow!(@follower1, @followable1)
|
77
|
+
assert_empty Socialization.redis.smembers forward_key(@followable1)
|
78
|
+
assert_empty Socialization.redis.smembers backward_key(@follower1)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "#follows?" do
|
83
|
+
should "return true when follow exists" do
|
84
|
+
@klass.follow!(@follower1, @followable1)
|
85
|
+
assert_true @klass.follows?(@follower1, @followable1)
|
86
|
+
end
|
87
|
+
|
88
|
+
should "return false when follow doesn't exist" do
|
89
|
+
assert_false @klass.follows?(@follower1, @followable1)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "#followers" do
|
94
|
+
should "return an array of followers" do
|
95
|
+
follower1 = ImAFollower.create
|
96
|
+
follower2 = ImAFollower.create
|
97
|
+
follower1.follow!(@followable1)
|
98
|
+
follower2.follow!(@followable1)
|
99
|
+
assert_array_similarity [follower1, follower2], @klass.followers(@followable1, follower1.class)
|
100
|
+
end
|
101
|
+
|
102
|
+
should "return an array of follower ids when plucking" do
|
103
|
+
follower1 = ImAFollower.create
|
104
|
+
follower2 = ImAFollower.create
|
105
|
+
follower1.follow!(@followable1)
|
106
|
+
follower2.follow!(@followable1)
|
107
|
+
assert_array_similarity ["#{follower1.id}", "#{follower2.id}"], @klass.followers(@followable1, follower1.class, :pluck => :id)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "#followables" do
|
112
|
+
should "return an array of followables" do
|
113
|
+
followable1 = ImAFollowable.create
|
114
|
+
followable2 = ImAFollowable.create
|
115
|
+
@follower1.follow!(followable1)
|
116
|
+
@follower1.follow!(followable2)
|
117
|
+
|
118
|
+
assert_array_similarity [followable1, followable2], @klass.followables(@follower1, followable1.class)
|
119
|
+
end
|
120
|
+
|
121
|
+
should "return an array of followables ids when plucking" do
|
122
|
+
followable1 = ImAFollowable.create
|
123
|
+
followable2 = ImAFollowable.create
|
124
|
+
@follower1.follow!(followable1)
|
125
|
+
@follower1.follow!(followable2)
|
126
|
+
assert_array_similarity ["#{followable1.id}", "#{followable2.id}"], @klass.followables(@follower1, followable1.class, :pluck => :id)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "#generate_forward_key" do
|
131
|
+
should "return valid key when passed an object" do
|
132
|
+
assert_equal "Followers:#{@followable1.class.name}:#{@followable1.id}", forward_key(@followable1)
|
133
|
+
end
|
134
|
+
|
135
|
+
should "return valid key when passed a String" do
|
136
|
+
assert_equal "Followers:Followable:1", forward_key("Followable:1")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "#generate_backward_key" do
|
141
|
+
should "return valid key when passed an object" do
|
142
|
+
assert_equal "Followables:#{@follower1.class.name}:#{@follower1.id}", backward_key(@follower1)
|
143
|
+
end
|
144
|
+
|
145
|
+
should "return valid key when passed a String" do
|
146
|
+
assert_equal "Followables:Follower:1", backward_key("Follower:1")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "#remove_followers" do
|
151
|
+
should "delete all followers relationships for a followable" do
|
152
|
+
@follower1.follow!(@followable1)
|
153
|
+
@follower2.follow!(@followable1)
|
154
|
+
assert_equal 2, @followable1.followers(@follower1.class).count
|
155
|
+
|
156
|
+
@klass.remove_followers(@followable1)
|
157
|
+
assert_equal 0, @followable1.followers(@follower1.class).count
|
158
|
+
assert_empty Socialization.redis.smembers forward_key(@followable1)
|
159
|
+
assert_empty Socialization.redis.smembers backward_key(@follower1)
|
160
|
+
assert_empty Socialization.redis.smembers backward_key(@follower2)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "#remove_followables" do
|
165
|
+
should "delete all followables relationships for a follower" do
|
166
|
+
@follower1.follow!(@followable1)
|
167
|
+
@follower1.follow!(@followable2)
|
168
|
+
assert_equal 2, @follower1.followables(@followable1.class).count
|
169
|
+
|
170
|
+
@klass.remove_followables(@follower1)
|
171
|
+
assert_equal 0, @follower1.followables(@followable1.class).count
|
172
|
+
assert_empty Socialization.redis.smembers backward_key(@followable1)
|
173
|
+
assert_empty Socialization.redis.smembers backward_key(@follower2)
|
174
|
+
assert_empty Socialization.redis.smembers forward_key(@follower1)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context "#key_type_to_type_names" do
|
179
|
+
should "return the proper arrays" do
|
180
|
+
assert_equal ['follower', 'followable'], @klass.send(:key_type_to_type_names, Socialization::RedisStores::Follow)
|
181
|
+
assert_equal ['mentioner', 'mentionable'], @klass.send(:key_type_to_type_names, Socialization::RedisStores::Mention)
|
182
|
+
assert_equal ['liker', 'likeable'], @klass.send(:key_type_to_type_names, Socialization::RedisStores::Like)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Helpers
|
188
|
+
def assert_match_follower(follow_record, follower)
|
189
|
+
assert follow_record.follower_type == follower.class.to_s && follow_record.follower_id == follower.id
|
190
|
+
end
|
191
|
+
|
192
|
+
def assert_match_followable(follow_record, followable)
|
193
|
+
assert follow_record.followable_type == followable.class.to_s && follow_record.followable_id == followable.id
|
194
|
+
end
|
195
|
+
|
196
|
+
def forward_key(followable)
|
197
|
+
Socialization::RedisStores::Follow.send(:generate_forward_key, followable)
|
198
|
+
end
|
199
|
+
|
200
|
+
def backward_key(follower)
|
201
|
+
Socialization::RedisStores::Follow.send(:generate_backward_key, follower)
|
202
|
+
end
|
203
|
+
end
|
@@ -1,153 +1,27 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
2
|
|
3
3
|
class RedisFollowStoreTest < Test::Unit::TestCase
|
4
|
-
context "RedisStores::
|
4
|
+
context "RedisStores::Follow" do
|
5
5
|
setup do
|
6
6
|
use_redis_store
|
7
7
|
@klass = Socialization::RedisStores::Follow
|
8
|
-
@
|
9
|
-
@klass.after_follow nil
|
10
|
-
@klass.after_unfollow nil
|
11
|
-
@follower = ImAFollower.create
|
12
|
-
@followable = ImAFollowable.create
|
8
|
+
@base = Socialization::RedisStores::Base
|
13
9
|
end
|
14
10
|
|
15
|
-
context "
|
16
|
-
should "
|
17
|
-
|
11
|
+
context "method aliases" do
|
12
|
+
should "be set properly and made public" do
|
13
|
+
# TODO: Can't figure out how to test method aliases properly. The following doesn't work:
|
14
|
+
# assert @klass.method(:follow!) == @base.method(:relation!)
|
15
|
+
assert_method_public @klass, :follow!
|
16
|
+
assert_method_public @klass, :unfollow!
|
17
|
+
assert_method_public @klass, :follows?
|
18
|
+
assert_method_public @klass, :followers_relation
|
19
|
+
assert_method_public @klass, :followers
|
20
|
+
assert_method_public @klass, :followables_relation
|
21
|
+
assert_method_public @klass, :followables
|
22
|
+
assert_method_public @klass, :remove_followers
|
23
|
+
assert_method_public @klass, :remove_followables
|
18
24
|
end
|
19
25
|
end
|
20
|
-
|
21
|
-
context "#follow!" do
|
22
|
-
should "create a Follow record" do
|
23
|
-
@klass.follow!(@follower, @followable)
|
24
|
-
assert_equal ["#{@follower.id}"], Socialization.redis.smembers(followers_key)
|
25
|
-
assert_equal ["#{@followable.id}"], Socialization.redis.smembers(followables_key)
|
26
|
-
end
|
27
|
-
|
28
|
-
should "touch follower when instructed" do
|
29
|
-
@klass.touch :follower
|
30
|
-
@follower.expects(:touch).once
|
31
|
-
@followable.expects(:touch).never
|
32
|
-
@klass.follow!(@follower, @followable)
|
33
|
-
end
|
34
|
-
|
35
|
-
should "touch followable when instructed" do
|
36
|
-
@klass.touch :followable
|
37
|
-
@follower.expects(:touch).never
|
38
|
-
@followable.expects(:touch).once
|
39
|
-
@klass.follow!(@follower, @followable)
|
40
|
-
end
|
41
|
-
|
42
|
-
should "touch all when instructed" do
|
43
|
-
@klass.touch :all
|
44
|
-
@follower.expects(:touch).once
|
45
|
-
@followable.expects(:touch).once
|
46
|
-
@klass.follow!(@follower, @followable)
|
47
|
-
end
|
48
|
-
|
49
|
-
should "call after follow hook" do
|
50
|
-
@klass.after_follow :after_follow
|
51
|
-
@klass.expects(:after_follow).once
|
52
|
-
@klass.follow!(@follower, @followable)
|
53
|
-
end
|
54
|
-
|
55
|
-
should "call after unfollow hook" do
|
56
|
-
@klass.after_follow :after_unfollow
|
57
|
-
@klass.expects(:after_unfollow).once
|
58
|
-
@klass.follow!(@follower, @followable)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "#follows?" do
|
63
|
-
should "return true when follow exists" do
|
64
|
-
Socialization.redis.sadd followers_key, @follower.id
|
65
|
-
Socialization.redis.sadd followables_key, @followable.id
|
66
|
-
assert_true @klass.follows?(@follower, @followable)
|
67
|
-
end
|
68
|
-
|
69
|
-
should "return false when follow doesn't exist" do
|
70
|
-
assert_false @klass.follows?(@follower, @followable)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context "#followers" do
|
75
|
-
should "return an array of followers" do
|
76
|
-
follower1 = ImAFollower.create
|
77
|
-
follower2 = ImAFollower.create
|
78
|
-
follower1.follow!(@followable)
|
79
|
-
follower2.follow!(@followable)
|
80
|
-
assert_array_similarity [follower1, follower2], @klass.followers(@followable, follower1.class)
|
81
|
-
end
|
82
|
-
|
83
|
-
should "return an array of follower ids when plucking" do
|
84
|
-
follower1 = ImAFollower.create
|
85
|
-
follower2 = ImAFollower.create
|
86
|
-
follower1.follow!(@followable)
|
87
|
-
follower2.follow!(@followable)
|
88
|
-
assert_array_similarity [follower1.id, follower2.id], @klass.followers(@followable, follower1.class, :pluck => :id)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "#followables" do
|
93
|
-
should "return an array of followables" do
|
94
|
-
followable1 = ImAFollowable.create
|
95
|
-
followable2 = ImAFollowable.create
|
96
|
-
@follower.follow!(followable1)
|
97
|
-
@follower.follow!(followable2)
|
98
|
-
|
99
|
-
assert_array_similarity [followable1, followable2], @klass.followables(@follower, followable1.class)
|
100
|
-
end
|
101
|
-
|
102
|
-
should "return an array of followables ids when plucking" do
|
103
|
-
followable1 = ImAFollowable.create
|
104
|
-
followable2 = ImAFollowable.create
|
105
|
-
@follower.follow!(followable1)
|
106
|
-
@follower.follow!(followable2)
|
107
|
-
assert_array_similarity [followable1.id, followable2.id], @klass.followables(@follower, followable1.class, :pluck => :id)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
context "#generate_followers_key" do
|
112
|
-
should "return valid key when passed objects" do
|
113
|
-
assert_equal "Followers:ImAFollowable:#{@followable.id}:ImAFollower", followers_key(@follower, @followable)
|
114
|
-
end
|
115
|
-
|
116
|
-
should "return valid key when follower is a class" do
|
117
|
-
assert_equal "Followers:ImAFollowable:#{@followable.id}:ImAFollower", followers_key(@follower.class, @followable)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context "#generate_followables_key" do
|
122
|
-
should "return valid key when passed objects" do
|
123
|
-
assert_equal "Followables:ImAFollower:#{@follower.id}:ImAFollowable", followables_key(@follower, @followable)
|
124
|
-
end
|
125
|
-
|
126
|
-
should "return valid key when followable is a class" do
|
127
|
-
assert_equal "Followables:ImAFollower:#{@follower.id}:ImAFollowable", followables_key(@follower, @followable.class)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
# Helpers
|
134
|
-
def assert_match_follower(follow_record, follower)
|
135
|
-
assert follow_record.follower_type == follower.class.to_s && follow_record.follower_id == follower.id
|
136
|
-
end
|
137
|
-
|
138
|
-
def assert_match_followable(follow_record, followable)
|
139
|
-
assert follow_record.followable_type == followable.class.to_s && follow_record.followable_id == followable.id
|
140
|
-
end
|
141
|
-
|
142
|
-
def followers_key(follower = nil, followable = nil)
|
143
|
-
follower ||= @follower
|
144
|
-
followable ||= @followable
|
145
|
-
@klass.send(:generate_followers_key, follower, followable)
|
146
|
-
end
|
147
|
-
|
148
|
-
def followables_key(follower = nil, followable = nil)
|
149
|
-
follower ||= @follower
|
150
|
-
followable ||= @followable
|
151
|
-
@klass.send(:generate_followables_key, follower, followable)
|
152
26
|
end
|
153
|
-
end
|
27
|
+
end
|
@@ -1,153 +1,25 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
2
|
|
3
3
|
class RedisLikeStoreTest < Test::Unit::TestCase
|
4
|
-
context "RedisStores::
|
4
|
+
context "RedisStores::Like" do
|
5
5
|
setup do
|
6
6
|
use_redis_store
|
7
7
|
@klass = Socialization::RedisStores::Like
|
8
|
-
@
|
9
|
-
@klass.after_like nil
|
10
|
-
@klass.after_unlike nil
|
11
|
-
@liker = ImALiker.create
|
12
|
-
@likeable = ImALikeable.create
|
8
|
+
@base = Socialization::RedisStores::Base
|
13
9
|
end
|
14
10
|
|
15
|
-
context "
|
16
|
-
should "
|
17
|
-
|
11
|
+
context "method aliases" do
|
12
|
+
should "be set properly and made public" do
|
13
|
+
assert_method_public @klass, :like!
|
14
|
+
assert_method_public @klass, :unlike!
|
15
|
+
assert_method_public @klass, :likes?
|
16
|
+
assert_method_public @klass, :likers_relation
|
17
|
+
assert_method_public @klass, :likers
|
18
|
+
assert_method_public @klass, :likeables_relation
|
19
|
+
assert_method_public @klass, :likeables
|
20
|
+
assert_method_public @klass, :remove_likers
|
21
|
+
assert_method_public @klass, :remove_likeables
|
18
22
|
end
|
19
23
|
end
|
20
|
-
|
21
|
-
context "#like!" do
|
22
|
-
should "create a Like record" do
|
23
|
-
@klass.like!(@liker, @likeable)
|
24
|
-
assert_equal ["#{@liker.id}"], Socialization.redis.smembers(likers_key)
|
25
|
-
assert_equal ["#{@likeable.id}"], Socialization.redis.smembers(likeables_key)
|
26
|
-
end
|
27
|
-
|
28
|
-
should "touch liker when instructed" do
|
29
|
-
@klass.touch :liker
|
30
|
-
@liker.expects(:touch).once
|
31
|
-
@likeable.expects(:touch).never
|
32
|
-
@klass.like!(@liker, @likeable)
|
33
|
-
end
|
34
|
-
|
35
|
-
should "touch likeable when instructed" do
|
36
|
-
@klass.touch :likeable
|
37
|
-
@liker.expects(:touch).never
|
38
|
-
@likeable.expects(:touch).once
|
39
|
-
@klass.like!(@liker, @likeable)
|
40
|
-
end
|
41
|
-
|
42
|
-
should "touch all when instructed" do
|
43
|
-
@klass.touch :all
|
44
|
-
@liker.expects(:touch).once
|
45
|
-
@likeable.expects(:touch).once
|
46
|
-
@klass.like!(@liker, @likeable)
|
47
|
-
end
|
48
|
-
|
49
|
-
should "call after like hook" do
|
50
|
-
@klass.after_like :after_like
|
51
|
-
@klass.expects(:after_like).once
|
52
|
-
@klass.like!(@liker, @likeable)
|
53
|
-
end
|
54
|
-
|
55
|
-
should "call after unlike hook" do
|
56
|
-
@klass.after_like :after_unlike
|
57
|
-
@klass.expects(:after_unlike).once
|
58
|
-
@klass.like!(@liker, @likeable)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "#likes?" do
|
63
|
-
should "return true when like exists" do
|
64
|
-
Socialization.redis.sadd likers_key, @liker.id
|
65
|
-
Socialization.redis.sadd likeables_key, @likeable.id
|
66
|
-
assert_true @klass.likes?(@liker, @likeable)
|
67
|
-
end
|
68
|
-
|
69
|
-
should "return false when like doesn't exist" do
|
70
|
-
assert_false @klass.likes?(@liker, @likeable)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context "#likers" do
|
75
|
-
should "return an array of likers" do
|
76
|
-
liker1 = ImALiker.create
|
77
|
-
liker2 = ImALiker.create
|
78
|
-
liker1.like!(@likeable)
|
79
|
-
liker2.like!(@likeable)
|
80
|
-
assert_array_similarity [liker1, liker2], @klass.likers(@likeable, liker1.class)
|
81
|
-
end
|
82
|
-
|
83
|
-
should "return an array of liker ids when plucking" do
|
84
|
-
liker1 = ImALiker.create
|
85
|
-
liker2 = ImALiker.create
|
86
|
-
liker1.like!(@likeable)
|
87
|
-
liker2.like!(@likeable)
|
88
|
-
assert_array_similarity [liker1.id, liker2.id], @klass.likers(@likeable, liker1.class, :pluck => :id)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "#likeables" do
|
93
|
-
should "return an array of likeables" do
|
94
|
-
likeable1 = ImALikeable.create
|
95
|
-
likeable2 = ImALikeable.create
|
96
|
-
@liker.like!(likeable1)
|
97
|
-
@liker.like!(likeable2)
|
98
|
-
|
99
|
-
assert_array_similarity [likeable1, likeable2], @klass.likeables(@liker, likeable1.class)
|
100
|
-
end
|
101
|
-
|
102
|
-
should "return an array of likeables ids when plucking" do
|
103
|
-
likeable1 = ImALikeable.create
|
104
|
-
likeable2 = ImALikeable.create
|
105
|
-
@liker.like!(likeable1)
|
106
|
-
@liker.like!(likeable2)
|
107
|
-
assert_array_similarity [likeable1.id, likeable2.id], @klass.likeables(@liker, likeable1.class, :pluck => :id)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
context "#generate_likers_key" do
|
112
|
-
should "return valid key when passed objects" do
|
113
|
-
assert_equal "Likers:ImALikeable:#{@likeable.id}:ImALiker", likers_key(@liker, @likeable)
|
114
|
-
end
|
115
|
-
|
116
|
-
should "return valid key when liker is a class" do
|
117
|
-
assert_equal "Likers:ImALikeable:#{@likeable.id}:ImALiker", likers_key(@liker.class, @likeable)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context "#generate_likeables_key" do
|
122
|
-
should "return valid key when passed objects" do
|
123
|
-
assert_equal "Likeables:ImALiker:#{@liker.id}:ImALikeable", likeables_key(@liker, @likeable)
|
124
|
-
end
|
125
|
-
|
126
|
-
should "return valid key when likeable is a class" do
|
127
|
-
assert_equal "Likeables:ImALiker:#{@liker.id}:ImALikeable", likeables_key(@liker, @likeable.class)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
|
133
|
-
# Helpers
|
134
|
-
def assert_match_liker(like_record, liker)
|
135
|
-
assert like_record.liker_type == liker.class.to_s && like_record.liker_id == liker.id
|
136
|
-
end
|
137
|
-
|
138
|
-
def assert_match_likeable(like_record, likeable)
|
139
|
-
assert like_record.likeable_type == likeable.class.to_s && like_record.likeable_id == likeable.id
|
140
|
-
end
|
141
|
-
|
142
|
-
def likers_key(liker = nil, likeable = nil)
|
143
|
-
liker ||= @liker
|
144
|
-
likeable ||= @likeable
|
145
|
-
@klass.send(:generate_likers_key, liker, likeable)
|
146
|
-
end
|
147
|
-
|
148
|
-
def likeables_key(liker = nil, likeable = nil)
|
149
|
-
liker ||= @liker
|
150
|
-
likeable ||= @likeable
|
151
|
-
@klass.send(:generate_likeables_key, liker, likeable)
|
152
24
|
end
|
153
|
-
end
|
25
|
+
end
|