socialization 0.4.0 → 0.5.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -1
- data/CHANGELOG.md +41 -0
- data/README.md +246 -0
- data/demo/{demo_app/.gitignore → .gitignore} +0 -0
- data/demo/{demo_app/Gemfile → Gemfile} +1 -1
- data/demo/{demo_app/README.rdoc → README.rdoc} +0 -0
- data/demo/{demo_app/Rakefile → Rakefile} +0 -0
- data/demo/{demo_app/app → app}/assets/images/rails.png +0 -0
- data/demo/{demo_app/app → app}/assets/javascripts/application.js +0 -0
- data/demo/{demo_app/app → app}/assets/stylesheets/application.css +0 -0
- data/demo/{demo_app/app → app}/controllers/application_controller.rb +0 -0
- data/demo/{demo_app/app → app}/helpers/application_helper.rb +0 -0
- data/demo/{demo_app/app → app}/mailers/.gitkeep +0 -0
- data/demo/{demo_app/app → app}/models/.gitkeep +0 -0
- data/demo/{demo_app/app → app}/models/celebrity.rb +0 -0
- data/demo/{demo_app/app → app}/models/comment.rb +0 -0
- data/demo/app/models/follow.rb +2 -0
- data/demo/app/models/like.rb +2 -0
- data/demo/app/models/mention.rb +2 -0
- data/demo/{demo_app/app → app}/models/movie.rb +0 -0
- data/demo/{demo_app/app → app}/models/user.rb +0 -0
- data/demo/{demo_app/app → app}/views/layouts/application.html.erb +0 -0
- data/demo/{demo_app/config.ru → config.ru} +0 -0
- data/demo/{demo_app/config → config}/application.rb +0 -0
- data/demo/{demo_app/config → config}/boot.rb +0 -0
- data/demo/{demo_app/config → config}/database.yml +0 -0
- data/demo/{demo_app/config → config}/environment.rb +0 -0
- data/demo/{demo_app/config → config}/environments/development.rb +0 -0
- data/demo/{demo_app/config → config}/environments/production.rb +0 -0
- data/demo/{demo_app/config → config}/environments/test.rb +0 -0
- data/demo/{demo_app/config → config}/initializers/backtrace_silencers.rb +0 -0
- data/demo/{demo_app/config → config}/initializers/inflections.rb +0 -0
- data/demo/{demo_app/config → config}/initializers/mime_types.rb +0 -0
- data/demo/{demo_app/config → config}/initializers/secret_token.rb +0 -0
- data/demo/{demo_app/config → config}/initializers/session_store.rb +1 -1
- data/demo/{demo_app/config → config}/initializers/wrap_parameters.rb +1 -1
- data/demo/{demo_app/config → config}/locales/en.yml +0 -0
- data/demo/{demo_app/config → config}/routes.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120115051222_create_users.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120115051234_create_movies.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120115051255_create_celebrities.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120115054646_create_follows.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120115054647_create_likes.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120221200644_create_mentions.rb +0 -0
- data/demo/{demo_app/db → db}/migrate/20120221202703_create_comments.rb +0 -0
- data/demo/{demo_app/db → db}/schema.rb +0 -1
- data/demo/{demo_app/db → db}/seeds.rb +0 -0
- data/demo/{demo_app/lib → lib}/assets/.gitkeep +0 -0
- data/demo/{demo_app/lib → lib}/tasks/.gitkeep +0 -0
- data/demo/{demo_app/public → public}/404.html +0 -0
- data/demo/{demo_app/public → public}/422.html +0 -0
- data/demo/{demo_app/public → public}/500.html +0 -0
- data/demo/{demo_app/public → public}/favicon.ico +0 -0
- data/demo/{demo_app/public → public}/index.html +0 -0
- data/demo/{demo_app/public → public}/robots.txt +0 -0
- data/demo/{demo_app/script → script}/rails +0 -0
- data/demo/{demo_app/test → test}/fixtures/.gitkeep +0 -0
- data/demo/{demo_app/test → test}/fixtures/celebrities.yml +0 -0
- data/demo/{demo_app/test → test}/fixtures/movies.yml +0 -0
- data/demo/{demo_app/test → test}/fixtures/users.yml +0 -0
- data/demo/{demo_app/test → test}/functional/.gitkeep +0 -0
- data/demo/{demo_app/test → test}/integration/.gitkeep +0 -0
- data/demo/{demo_app/test → test}/performance/browsing_test.rb +0 -0
- data/demo/{demo_app/test → test}/test_helper.rb +0 -0
- data/demo/{demo_app/test → test}/unit/.gitkeep +0 -0
- data/demo/{demo_app/test → test}/unit/celebrity_test.rb +0 -0
- data/demo/{demo_app/test → test}/unit/movie_test.rb +0 -0
- data/demo/{demo_app/test → test}/unit/user_test.rb +0 -0
- data/demo/{demo_app/vendor → vendor}/assets/javascripts/.gitkeep +0 -0
- data/demo/{demo_app/vendor → vendor}/assets/stylesheets/.gitkeep +0 -0
- data/demo/{demo_app/vendor → vendor}/plugins/.gitkeep +0 -0
- data/lib/generators/socialization/socialization_generator.rb +20 -9
- data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_follows.rb +0 -0
- data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_likes.rb +0 -0
- data/{generators/socialization/templates → lib/generators/socialization/templates/active_record}/migration_mentions.rb +0 -0
- data/lib/generators/socialization/templates/active_record/model_follow.rb +2 -0
- data/lib/generators/socialization/templates/active_record/model_like.rb +2 -0
- data/lib/generators/socialization/templates/active_record/model_mention.rb +2 -0
- data/lib/socialization.rb +3 -5
- data/lib/socialization/actors/follower.rb +74 -0
- data/lib/socialization/actors/liker.rb +74 -0
- data/lib/socialization/actors/mentioner.rb +74 -0
- data/lib/socialization/acts_as_helpers.rb +43 -0
- data/lib/socialization/config.rb +45 -0
- data/lib/socialization/helpers/string.rb +17 -0
- data/lib/socialization/stores/active_record/follow_store.rb +143 -0
- data/lib/socialization/stores/active_record/like_store.rb +143 -0
- data/lib/socialization/stores/active_record/mention_store.rb +143 -0
- data/lib/socialization/stores/redis/base.rb +8 -0
- data/lib/socialization/stores/redis/config.rb +11 -0
- data/lib/socialization/stores/redis/follow_store.rb +126 -0
- data/lib/socialization/stores/redis/like_store.rb +126 -0
- data/lib/socialization/stores/redis/mention_store.rb +126 -0
- data/lib/socialization/version.rb +1 -1
- data/lib/socialization/victims/followable.rb +38 -0
- data/lib/socialization/victims/likeable.rb +38 -0
- data/lib/socialization/victims/mentionable.rb +38 -0
- data/socialization.gemspec +3 -3
- data/test/actors/follower_test.rb +74 -0
- data/test/actors/liker_test.rb +74 -0
- data/test/actors/mentioner_test.rb +74 -0
- data/test/stores/active_record/follow_store_test.rb +121 -0
- data/test/stores/active_record/like_store_test.rb +121 -0
- data/test/stores/active_record/mention_store_test.rb +121 -0
- data/test/stores/redis/config_test.rb +30 -0
- data/test/stores/redis/follow_store_test.rb +153 -0
- data/test/stores/redis/like_store_test.rb +153 -0
- data/test/stores/redis/mention_store_test.rb +153 -0
- data/test/string_test.rb +13 -0
- data/test/test_helper.rb +70 -12
- data/test/victims/followable_test.rb +34 -0
- data/test/victims/likeable_test.rb +34 -0
- data/test/victims/mentionable_test.rb +34 -0
- data/test/world_test.rb +80 -69
- metadata +261 -176
- data/README.rdoc +0 -186
- data/demo/demo_app/app/models/follow.rb +0 -3
- data/demo/demo_app/app/models/like.rb +0 -3
- data/demo/demo_app/app/models/mention.rb +0 -3
- data/demo/demo_app/doc/README_FOR_APP +0 -2
- data/gemfiles/activerecord30.gemfile +0 -6
- data/gemfiles/activerecord30.gemfile.lock +0 -23
- data/gemfiles/activerecord31.gemfile +0 -6
- data/gemfiles/activerecord31.gemfile.lock +0 -25
- data/gemfiles/activerecord32.gemfile +0 -6
- data/gemfiles/activerecord32.gemfile.lock +0 -25
- data/generators/socialization/USAGE +0 -1
- data/generators/socialization/socialization_generator.rb +0 -15
- data/generators/socialization/templates/model_follow.rb +0 -3
- data/generators/socialization/templates/model_like.rb +0 -3
- data/generators/socialization/templates/model_mention.rb +0 -3
- data/lib/socialization/follow_store.rb +0 -14
- data/lib/socialization/followable.rb +0 -34
- data/lib/socialization/follower.rb +0 -50
- data/lib/socialization/hello.rb +0 -53
- data/lib/socialization/like_store.rb +0 -14
- data/lib/socialization/likeable.rb +0 -34
- data/lib/socialization/liker.rb +0 -54
- data/lib/socialization/mention_store.rb +0 -14
- data/lib/socialization/mentionable.rb +0 -36
- data/lib/socialization/mentioner.rb +0 -54
- data/test/follow_test.rb +0 -125
- data/test/like_test.rb +0 -125
- data/test/mention_test.rb +0 -116
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
|
+
|
3
|
+
class ActiveRecordMentionStoreTest < Test::Unit::TestCase
|
4
|
+
context "ActiveRecordStores::MentionStoreTest" do
|
5
|
+
setup do
|
6
|
+
@klass = Socialization::ActiveRecordStores::MentionStore
|
7
|
+
@klass.touch nil
|
8
|
+
@klass.after_mention nil
|
9
|
+
@klass.after_unmention nil
|
10
|
+
@mentioner = ImAMentioner.create
|
11
|
+
@mentionable = ImAMentionable.create
|
12
|
+
end
|
13
|
+
|
14
|
+
context "data store" do
|
15
|
+
should "inherit Socialization::ActiveRecordStores::MentionStore" do
|
16
|
+
assert_equal Socialization::ActiveRecordStores::MentionStore, Socialization.mention_model
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "#mention!" do
|
21
|
+
should "create a Mention record" do
|
22
|
+
@klass.mention!(@mentioner, @mentionable)
|
23
|
+
assert_match_mentioner @klass.last, @mentioner
|
24
|
+
assert_match_mentionable @klass.last, @mentionable
|
25
|
+
end
|
26
|
+
|
27
|
+
should "touch mentioner when instructed" do
|
28
|
+
@klass.touch :mentioner
|
29
|
+
@mentioner.expects(:touch).once
|
30
|
+
@mentionable.expects(:touch).never
|
31
|
+
@klass.mention!(@mentioner, @mentionable)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "touch mentionable when instructed" do
|
35
|
+
@klass.touch :mentionable
|
36
|
+
@mentioner.expects(:touch).never
|
37
|
+
@mentionable.expects(:touch).once
|
38
|
+
@klass.mention!(@mentioner, @mentionable)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "touch all when instructed" do
|
42
|
+
@klass.touch :all
|
43
|
+
@mentioner.expects(:touch).once
|
44
|
+
@mentionable.expects(:touch).once
|
45
|
+
@klass.mention!(@mentioner, @mentionable)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "call after mention hook" do
|
49
|
+
@klass.after_mention :after_mention
|
50
|
+
@klass.expects(:after_mention).once
|
51
|
+
@klass.mention!(@mentioner, @mentionable)
|
52
|
+
end
|
53
|
+
|
54
|
+
should "call after unmention hook" do
|
55
|
+
@klass.after_mention :after_unmention
|
56
|
+
@klass.expects(:after_unmention).once
|
57
|
+
@klass.mention!(@mentioner, @mentionable)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "#mentions?" do
|
62
|
+
should "return true when mention exists" do
|
63
|
+
@klass.create! do |f|
|
64
|
+
f.mentioner = @mentioner
|
65
|
+
f.mentionable = @mentionable
|
66
|
+
end
|
67
|
+
assert_true @klass.mentions?(@mentioner, @mentionable)
|
68
|
+
end
|
69
|
+
|
70
|
+
should "return false when mention doesn't exist" do
|
71
|
+
assert_false @klass.mentions?(@mentioner, @mentionable)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "#mentioners" do
|
76
|
+
should "return an array of mentioners" do
|
77
|
+
mentioner1 = ImAMentioner.create
|
78
|
+
mentioner2 = ImAMentioner.create
|
79
|
+
mentioner1.mention!(@mentionable)
|
80
|
+
mentioner2.mention!(@mentionable)
|
81
|
+
assert_equal [mentioner1, mentioner2], @klass.mentioners(@mentionable, mentioner1.class)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "return an array of mentioner ids when plucking" do
|
85
|
+
mentioner1 = ImAMentioner.create
|
86
|
+
mentioner2 = ImAMentioner.create
|
87
|
+
mentioner1.mention!(@mentionable)
|
88
|
+
mentioner2.mention!(@mentionable)
|
89
|
+
assert_equal [mentioner1.id, mentioner2.id], @klass.mentioners(@mentionable, mentioner1.class, :pluck => :id)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "#mentionables" do
|
94
|
+
should "return an array of mentioners" do
|
95
|
+
mentionable1 = ImAMentionable.create
|
96
|
+
mentionable2 = ImAMentionable.create
|
97
|
+
@mentioner.mention!(mentionable1)
|
98
|
+
@mentioner.mention!(mentionable2)
|
99
|
+
assert_equal [mentionable1, mentionable2], @klass.mentionables(@mentioner, mentionable1.class)
|
100
|
+
end
|
101
|
+
|
102
|
+
should "return an array of mentioner ids when plucking" do
|
103
|
+
mentionable1 = ImAMentionable.create
|
104
|
+
mentionable2 = ImAMentionable.create
|
105
|
+
@mentioner.mention!(mentionable1)
|
106
|
+
@mentioner.mention!(mentionable2)
|
107
|
+
assert_equal [mentionable1.id, mentionable2.id], @klass.mentionables(@mentioner, mentionable1.class, :pluck => :id)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
# Helpers
|
114
|
+
def assert_match_mentioner(mention_record, mentioner)
|
115
|
+
assert mention_record.mentioner_type == mentioner.class.to_s && mention_record.mentioner_id == mentioner.id
|
116
|
+
end
|
117
|
+
|
118
|
+
def assert_match_mentionable(mention_record, mentionable)
|
119
|
+
assert mention_record.mentionable_type == mentionable.class.to_s && mention_record.mentionable_id == mentionable.id
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
|
+
|
3
|
+
class RedisStoreConfigTest < Test::Unit::TestCase
|
4
|
+
context "redis" do
|
5
|
+
setup do
|
6
|
+
Socialization.instance_eval { @redis = nil }
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return a new Redis object when none were specified" do
|
10
|
+
assert_instance_of Redis, Socialization.redis
|
11
|
+
end
|
12
|
+
|
13
|
+
should "always return the same Redis object when none were specified" do
|
14
|
+
redis = Socialization.redis
|
15
|
+
assert_same redis, Socialization.redis
|
16
|
+
end
|
17
|
+
|
18
|
+
should "be able to set and get a redis instance" do
|
19
|
+
redis = Redis.new
|
20
|
+
Socialization.redis = redis
|
21
|
+
assert_same redis, Socialization.redis
|
22
|
+
end
|
23
|
+
|
24
|
+
should "always return the same Redis object when it was specified" do
|
25
|
+
redis = Redis.new
|
26
|
+
Socialization.redis = redis
|
27
|
+
assert_same redis, Socialization.redis
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
|
+
|
3
|
+
class RedisFollowStoreTest < Test::Unit::TestCase
|
4
|
+
context "RedisStores::FollowStoreTest" do
|
5
|
+
setup do
|
6
|
+
use_redis_store
|
7
|
+
@klass = Socialization::RedisStores::FollowStore
|
8
|
+
@klass.touch nil
|
9
|
+
@klass.after_follow nil
|
10
|
+
@klass.after_unfollow nil
|
11
|
+
@follower = ImAFollower.create
|
12
|
+
@followable = ImAFollowable.create
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Stores" do
|
16
|
+
should "inherit Socialization::RedisStores::FollowStore" do
|
17
|
+
assert_equal Socialization::RedisStores::FollowStore, Socialization.follow_model
|
18
|
+
end
|
19
|
+
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
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
|
2
|
+
|
3
|
+
class RedisLikeStoreTest < Test::Unit::TestCase
|
4
|
+
context "RedisStores::LikeStoreTest" do
|
5
|
+
setup do
|
6
|
+
use_redis_store
|
7
|
+
@klass = Socialization::RedisStores::LikeStore
|
8
|
+
@klass.touch nil
|
9
|
+
@klass.after_like nil
|
10
|
+
@klass.after_unlike nil
|
11
|
+
@liker = ImALiker.create
|
12
|
+
@likeable = ImALikeable.create
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Stores" do
|
16
|
+
should "inherit Socialization::RedisStores::LikeStore" do
|
17
|
+
assert_equal Socialization::RedisStores::LikeStore, Socialization.like_model
|
18
|
+
end
|
19
|
+
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
|
+
end
|
153
|
+
end
|