From f2328e5a05f9c630aa9cdfe8583c466ba441b13a Mon Sep 17 00:00:00 2001 From: Hugo Melo Date: Wed, 27 Jan 2016 14:35:52 -0200 Subject: [PATCH] Move merit create callback to delayed_job --- lib/merit_ext.rb | 2 +- test/unit/api_test.rb | 22 +++++++++++++++++++++- test/unit/article_follower_test.rb | 25 +++++++++++++++++++++++++ test/unit/article_test.rb | 41 ++++++++++++++++++++++++++++++++++++++--- test/unit/comment_test.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- test/unit/person_test.rb | 4 ++++ test/unit/point_rules_test.rb | 2 ++ test/unit/profile_test.rb | 3 +++ 8 files changed, 142 insertions(+), 8 deletions(-) diff --git a/lib/merit_ext.rb b/lib/merit_ext.rb index 8e9a447..aedbbfa 100644 --- a/lib/merit_ext.rb +++ b/lib/merit_ext.rb @@ -43,7 +43,7 @@ module Merit module ClassMethods def has_merit_actions(options = {}) - after_create { |obj| obj.new_merit_action(:create, options) } + after_create { |obj| obj.delay.new_merit_action(:create, options) } before_destroy { |obj| obj.new_merit_action(:destroy, options) } end diff --git a/test/unit/api_test.rb b/test/unit/api_test.rb index 35421da..23ef305 100644 --- a/test/unit/api_test.rb +++ b/test/unit/api_test.rb @@ -56,7 +56,7 @@ class APITest < ActiveSupport::TestCase another_person.save another_person.add_badge(badge.id) get "/api/v1/gamification_plugin/people/#{another_person.id}/badges?#{params.to_query}" - json = JSON.parse(last_response.body) + JSON.parse(last_response.body) assert_equal 404, last_response.status end @@ -80,6 +80,8 @@ class APITest < ActiveSupport::TestCase article = create(TextArticle, :profile_id => @person.id, :author => @person) create(Comment, :source_id => article.id, :author => fast_create(Person)) + process_delayed_job_queue + get "/api/v1/gamification_plugin/my/points?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points'] @@ -88,6 +90,9 @@ class APITest < ActiveSupport::TestCase should 'get my points filtered by type' do article = create(TextArticle, :profile_id => @person.id, :author => @person) create(Comment, :source_id => article.id, :author => fast_create(Person)) + + process_delayed_job_queue + params[:type] = 'article_author' get "/api/v1/gamification_plugin/my/points_by_type?#{params.to_query}" @@ -100,6 +105,9 @@ class APITest < ActiveSupport::TestCase create_point_rule_definition('article_author', community) create(TextArticle, :profile_id => @person.id, :author => @person) create(TextArticle, :profile_id => community.id, :author => @person) + + process_delayed_job_queue + params[:profile] = community.identifier get "/api/v1/gamification_plugin/my/points_by_profile?#{params.to_query}" @@ -113,6 +121,8 @@ class APITest < ActiveSupport::TestCase create(TextArticle, :profile_id => @person.id, :author => @person) create(TextArticle, :profile_id => community.id, :author => @person) + process_delayed_job_queue + get "/api/v1/gamification_plugin/my/points_out_of_profiles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 2*default_point_weight(:article_author), json['points'] @@ -122,6 +132,8 @@ class APITest < ActiveSupport::TestCase article = create(TextArticle, :profile_id => @person.id, :author => @person) create(Comment, :source_id => article.id, :author => fast_create(Person)) + process_delayed_job_queue + get "/api/v1/gamification_plugin/people/#{person.id}/points?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points'] @@ -130,6 +142,9 @@ class APITest < ActiveSupport::TestCase should 'get points of a person filtered by type' do article = create(TextArticle, :profile_id => @person.id, :author => @person) create(Comment, :source_id => article.id, :author => fast_create(Person)) + + process_delayed_job_queue + params[:type] = 'article_author' get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_type?#{params.to_query}" @@ -142,6 +157,9 @@ class APITest < ActiveSupport::TestCase create_point_rule_definition('article_author', community) create(TextArticle, :profile_id => @person.id, :author => @person) create(TextArticle, :profile_id => community.id, :author => @person) + + process_delayed_job_queue + params[:profile] = community.identifier get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_profile?#{params.to_query}" @@ -155,6 +173,8 @@ class APITest < ActiveSupport::TestCase create(TextArticle, :profile_id => @person.id, :author => @person) create(TextArticle, :profile_id => community.id, :author => @person) + process_delayed_job_queue + get "/api/v1/gamification_plugin/people/#{@person.id}/points_out_of_profiles?#{params.to_query}" json = JSON.parse(last_response.body) assert_equal 2*default_point_weight(:article_author), json['points'] diff --git a/test/unit/article_follower_test.rb b/test/unit/article_follower_test.rb index d0fc416..3ae9dd0 100644 --- a/test/unit/article_follower_test.rb +++ b/test/unit/article_follower_test.rb @@ -14,9 +14,12 @@ class ArticleFollowerTest < ActiveSupport::TestCase create_point_rule_definition('followed_article') article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue + c = GamificationPlugin::PointsCategorization.for_type(:followed_article).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do article.person_followers << person + process_delayed_job_queue end end @@ -25,9 +28,12 @@ class ArticleFollowerTest < ActiveSupport::TestCase article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue + c = GamificationPlugin::PointsCategorization.for_type(:follower).first assert_difference 'person.points(:category => c.id.to_s)', c.weight do article.person_followers << person + process_delayed_job_queue end end @@ -36,9 +42,12 @@ class ArticleFollowerTest < ActiveSupport::TestCase article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue + c = GamificationPlugin::PointsCategorization.for_type(:followed_article_author).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do article.person_followers << person + process_delayed_job_queue end end @@ -46,11 +55,18 @@ class ArticleFollowerTest < ActiveSupport::TestCase create_point_rule_definition('follower') follower = create_user('someuser').person article = create(TextArticle, :profile_id => community.id, :author => person) + + process_delayed_job_queue + score_points = follower.score_points.count points = follower.points article.person_followers << follower + process_delayed_job_queue + assert_equal score_points + 1, follower.score_points.count ArticleFollower.last.destroy + process_delayed_job_queue + assert_equal score_points + 2, follower.score_points.count assert_equal points, follower.points end @@ -58,9 +74,13 @@ class ArticleFollowerTest < ActiveSupport::TestCase should 'subtract merit points to article author when a user unfollow an article' do create_point_rule_definition('follower') article = create(TextArticle, :profile_id => community.id, :author => person) + + process_delayed_job_queue + assert_no_difference 'person.points' do assert_difference 'person.score_points.count' do article.person_followers << fast_create(Person) + process_delayed_job_queue end assert_difference 'person.score_points.count' do ArticleFollower.last.destroy @@ -71,11 +91,16 @@ class ArticleFollowerTest < ActiveSupport::TestCase should 'subtract merit points to article when a user unfollow an article' do create_point_rule_definition('followed_article') article = create(TextArticle, :profile_id => community.id, :author => person) + process_delayed_job_queue score_points = article.score_points.count points = article.points article.person_followers << person + process_delayed_job_queue + assert_equal score_points + 1, article.score_points.count ArticleFollower.last.destroy + process_delayed_job_queue + assert_equal score_points + 2, article.score_points.count assert_equal points, article.points end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index cb7aea7..10d5fff 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -12,6 +12,7 @@ class ArticleTest < ActiveSupport::TestCase should 'add merit points to author when create a new article' do create_point_rule_definition('article_author') create(TextArticle, :profile_id => person.id, :author => person) + process_delayed_job_queue assert_equal 1, person.score_points.count assert person.score_points.first.action.present? end @@ -19,8 +20,10 @@ class ArticleTest < ActiveSupport::TestCase should 'subtract merit points to author when destroy an article' do create_point_rule_definition('article_author') article = create(TextArticle, :profile_id => person.id, :author => person) + process_delayed_job_queue assert_equal 1, person.score_points.count article.destroy + process_delayed_job_queue assert_equal 2, person.score_points.count assert_equal 0, person.points end @@ -30,6 +33,7 @@ class ArticleTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { create(TextArticle, :profile_id => person.id, :author => person) } + process_delayed_job_queue assert_equal 'article_author', person.badges.first.name assert_equal 1, person.badges.first.level end @@ -40,6 +44,8 @@ class ArticleTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 10.times { create(TextArticle, :profile_id => person.id, :author => person) } + process_delayed_job_queue + sleep 4 assert_equal ['article_author'], person.badges.map(&:name).uniq assert_equal [1, 2], person.badges.map(&:level) end @@ -48,49 +54,59 @@ class ArticleTest < ActiveSupport::TestCase create_point_rule_definition('vote_voteable_author') community = fast_create(Community) article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first - assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do - Vote.create!(:voter => person, :voteable => article, :vote => 1) - end + points = article.author.points(:category => c.id.to_s) + Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue + assert_equal article.author.points(:category => c.id.to_s), points + c.weight end should 'add merit points to article when an user like it' do create_point_rule_definition('vote_voteable') article = create(TextArticle, :name => 'Test', :profile => person, :author => person) + process_delayed_job_queue article = article.reload c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue end end should 'add merit points to community when create a new article' do create_point_rule_definition('article_community') community = fast_create(Community) + process_delayed_job_queue assert_difference 'community.score_points.count' do create(TextArticle, :profile_id => community.id, :author => person) + process_delayed_job_queue end end should 'add merit points to voter when he likes an article' do create_point_rule_definition('vote_voter') article = create(TextArticle, :name => 'Test', :profile => person, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue end end should 'add merit points to voter when he dislikes an article' do create_point_rule_definition('vote_voter') article = create(TextArticle, :name => 'Test', :profile => person, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => -1) + process_delayed_job_queue end end @@ -101,8 +117,10 @@ class ArticleTest < ActiveSupport::TestCase article = create(TextArticle, :name => 'Test', :profile => person, :author => person) 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) } Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) + process_delayed_job_queue assert_equal [], person.badges Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) + process_delayed_job_queue assert_equal 'positive_votes_received', person.reload.badges.first.name end @@ -113,8 +131,10 @@ class ArticleTest < ActiveSupport::TestCase article = create(TextArticle, :name => 'Test', :profile => person, :author => person) 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) } Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) + process_delayed_job_queue assert_equal [], person.badges Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) + process_delayed_job_queue assert_equal 'negative_votes_received', person.reload.badges.first.name end @@ -123,6 +143,7 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) rule = create_point_rule_definition('article_author', community) create(TextArticle, profile_id: community.id, author_id: person.id) + process_delayed_job_queue assert_equal rule.weight, person.points_by_profile(community.identifier) assert person.score_points.first.action.present? end @@ -131,8 +152,10 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) rule = create_point_rule_definition('article_author', community) article = create(TextArticle, profile_id: community.id, author_id: person.id) + process_delayed_job_queue assert_equal rule.weight, person.points_by_profile(community.identifier) article.destroy + process_delayed_job_queue assert_equal 0, person.points end @@ -140,10 +163,12 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) create_point_rule_definition('vote_voteable_author', community) article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue end end @@ -151,19 +176,23 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) create_point_rule_definition('vote_voteable', community) article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue article = article.reload c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue end end should 'add merit points to community when create a new article on community' do community = fast_create(Community) create_point_rule_definition('article_community') + process_delayed_job_queue assert_difference 'community.score_points.count' do create(TextArticle, :profile_id => community.id, :author => person) + process_delayed_job_queue end end @@ -171,10 +200,12 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) create_point_rule_definition('vote_voter', community) article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => 1) + process_delayed_job_queue end end @@ -182,10 +213,12 @@ class ArticleTest < ActiveSupport::TestCase community = fast_create(Community) create_point_rule_definition('vote_voter', community) article = create(TextArticle, :name => 'Test', :profile => community, :author => person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => article, :vote => -1) + process_delayed_job_queue end end @@ -195,6 +228,7 @@ class ArticleTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { create(TextArticle, :profile_id => organization.id, :author => person) } + process_delayed_job_queue assert_equal 'article_author', person.badges.first.name assert_equal 1, person.badges.first.level end @@ -206,6 +240,7 @@ class ArticleTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { create(TextArticle, :profile_id => other_organization.id, :author => person) } + process_delayed_job_queue assert_equal [], person.badges end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index b86cfa1..01ee217 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -13,14 +13,17 @@ class CommentTest < ActiveSupport::TestCase should 'add merit points to author when create a new comment' do create_point_rule_definition('comment_author') create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue assert_equal 1, person.score_points.count end should 'subtract merit points from author when destroy a comment' do create_point_rule_definition('comment_author') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue assert_equal 1, person.score_points.count comment.destroy + process_delayed_job_queue assert_equal 2, person.score_points.count assert_equal 0, person.points end @@ -30,6 +33,7 @@ class CommentTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { create(Comment, :source => article, :author_id => person.id) } + process_delayed_job_queue assert_equal 'comment_author', person.badges.first.name end @@ -38,6 +42,7 @@ class CommentTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { create(Comment, :source => article, :author_id => person.id) } + process_delayed_job_queue assert_equal 'comment_received', author.badges.first.name end @@ -48,8 +53,10 @@ class CommentTest < ActiveSupport::TestCase comment = create(Comment, :source => article, :author_id => person.id) 4.times { Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) } Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) + process_delayed_job_queue assert_equal [], person.badges Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) + process_delayed_job_queue assert_equal 'positive_votes_received', person.reload.badges.first.name end @@ -60,18 +67,22 @@ class CommentTest < ActiveSupport::TestCase comment = create(Comment, :source => article, :author_id => person.id) 4.times { Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) } Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => 1) + process_delayed_job_queue assert_equal [], person.badges Vote.create!(:voter => fast_create(Person), :voteable => comment, :vote => -1) + process_delayed_job_queue assert_equal 'negative_votes_received', person.reload.badges.first.name end should 'add merit points to comment owner when an user like his comment' do create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue end end @@ -79,20 +90,24 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => author.id) Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points', -1*c.weight do Vote.where(:voteable_id => comment.id).destroy_all + process_delayed_job_queue end end should 'subtract merit points from comment owner when an user dislike his comment' do create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue end end @@ -100,37 +115,44 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('vote_voteable_author') comment = create(Comment, :source => article, :author_id => author.id) Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points', c.weight do Vote.where(:voteable_id => comment.id).destroy_all + process_delayed_job_queue end end should 'add merit points to article author when create a new comment' do create_point_rule_definition('comment_article_author') - assert_difference 'author.score_points.count' do - create(Comment, :source => article, :author_id => person.id) - end + scores = author.score_points.count + create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue + assert_not_equal author.reload.score_points.count, scores end should 'add merit points to voter when he likes a comment' do create_point_rule_definition('vote_voter') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue end end should 'add merit points to voter when he dislikes a comment' do create_point_rule_definition('vote_voter') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue end end @@ -139,6 +161,7 @@ class CommentTest < ActiveSupport::TestCase c = GamificationPlugin::PointsCategorization.for_type(:comment_article).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue end end @@ -146,10 +169,12 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('comment_community') community = fast_create(Community) article = create(TextileArticle, :profile_id => community.id, :author_id => author.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:comment_community).first assert_difference 'community.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue end end @@ -157,8 +182,10 @@ class CommentTest < ActiveSupport::TestCase should 'add merit community points to author when create a new comment in a community' do community = fast_create(Community) article = create(TextArticle, profile_id: community.id, author_id: person.id) + process_delayed_job_queue rule = create_point_rule_definition('comment_author', article.profile) create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue assert_equal rule.weight, person.points_by_profile(article.profile.identifier) end @@ -167,6 +194,7 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: fast_create(Person).id) rule = create_point_rule_definition('comment_author', article.profile) comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue assert_equal rule.weight, person.points_by_profile(article.profile.identifier) comment.destroy assert_equal 0, person.points_by_profile(article.profile.identifier) @@ -177,10 +205,12 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: person.id) create_point_rule_definition('vote_voteable_author', community) comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue end end @@ -190,6 +220,7 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('vote_voteable_author', community) comment = create(Comment, :source => article, :author_id => author.id) Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points_by_profile(community.identifier)', -1*c.weight do @@ -202,10 +233,12 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: person.id) create_point_rule_definition('vote_voteable_author', community) comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: article.profile.id).first assert_difference 'comment.author.points(:category => c.id.to_s)', -1*c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue end end @@ -215,6 +248,7 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('vote_voteable_author', community) comment = create(Comment, :source => article, :author_id => author.id) Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).first assert_difference 'comment.author.points_by_profile(community.identifier)', c.weight do @@ -225,9 +259,11 @@ class CommentTest < ActiveSupport::TestCase should 'add merit community points to article author when create a new comment inside a community' do community = fast_create(Community) article = create(TextArticle, profile_id: community.id, author_id: author.id) + process_delayed_job_queue rule = create_point_rule_definition('comment_article_author', community) assert_difference 'author.points_by_profile(community.identifier)', rule.weight do create(Comment, :source => article, :author_id => author.id) + process_delayed_job_queue end end @@ -236,10 +272,12 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: person.id) create_point_rule_definition('vote_voter') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => 1) + process_delayed_job_queue end end @@ -248,20 +286,24 @@ class CommentTest < ActiveSupport::TestCase article = create(TextArticle, profile_id: community.id, author_id: person.id) create_point_rule_definition('vote_voter') comment = create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).first assert_difference 'comment.author.points(:category => c.id.to_s)', c.weight do Vote.create!(:voter => person, :voteable => comment, :vote => -1) + process_delayed_job_queue end end should 'add merit community points to source article when create a comment inside a community' do community = fast_create(Community) article = create(TextArticle, profile_id: community.id, author_id: person.id) + process_delayed_job_queue create_point_rule_definition('comment_article') c = GamificationPlugin::PointsCategorization.for_type(:comment_article).first assert_difference 'article.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue end end @@ -269,10 +311,12 @@ class CommentTest < ActiveSupport::TestCase create_point_rule_definition('comment_community') community = fast_create(Community) article = create(TextileArticle, :profile_id => community.id, :author_id => author.id) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:comment_community).first assert_difference 'community.points(:category => c.id.to_s)', c.weight do create(Comment, :source => article, :author_id => person.id) + process_delayed_job_queue end end @@ -283,6 +327,7 @@ class CommentTest < ActiveSupport::TestCase article.profile = organization 5.times { create(Comment, :source => article, :author_id => person.id) } + process_delayed_job_queue assert_equal 'comment_author', person.badges.first.name end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 9bac5bc..38e5c63 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -13,8 +13,10 @@ class PersonTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 4.times { Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1) } + process_delayed_job_queue assert_equal [], person.badges Vote.create!(:voter => person, :voteable => fast_create(Comment), :vote => 1) + process_delayed_job_queue assert_equal 'votes_performed', person.reload.badges.first.name end @@ -23,6 +25,7 @@ class PersonTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) 5.times { |i| person.add_friend(create_user("testuser#{i}").person) } + process_delayed_job_queue assert_equal 'friendly', person.reload.badges.first.name end @@ -30,6 +33,7 @@ class PersonTest < ActiveSupport::TestCase create_point_rule_definition('friends') other_person = create_user("testuserfriend").person person.add_friend(other_person) + process_delayed_job_queue c = GamificationPlugin::PointsCategorization.for_type(:friends).first assert_equal 5, person.score_points(:category => c.id.to_s).sum(:num_points) end diff --git a/test/unit/point_rules_test.rb b/test/unit/point_rules_test.rb index 1c31167..3378a41 100644 --- a/test/unit/point_rules_test.rb +++ b/test/unit/point_rules_test.rb @@ -23,6 +23,7 @@ class PointRulesTest < ActiveSupport::TestCase person = create_user('testuser').person create_point_rule_definition('article_author') article = create(TextArticle, :profile_id => person.id, :author => person) + process_delayed_job_queue url = Merit::PointRules.target_url(person.score_points.last) assert_equal article.url, url end @@ -32,6 +33,7 @@ class PointRulesTest < ActiveSupport::TestCase create_point_rule_definition('comment_author') article = create(Article, :profile_id => person.id, :author => person) comment = create(Comment, :source_id => article.id, :author => person) + process_delayed_job_queue url = Merit::PointRules.target_url(person.score_points.last) assert_equal comment.url, url end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index b27fc77..f55053f 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -50,6 +50,7 @@ class ProfileTest < ActiveSupport::TestCase Person.any_instance.stubs(:is_profile_complete?).returns(true) create_point_rule_definition('profile_completion', nil, {value: 0}) GamificationPlugin.gamification_set_rules(environment) + process_delayed_job_queue assert_equal 0, person.level assert_nothing_raised do person.save @@ -62,8 +63,10 @@ class ProfileTest < ActiveSupport::TestCase GamificationPlugin.gamification_set_rules(environment) person = create_user('testuser').person + process_delayed_job_queue assert_equal 0, person.level create(TextArticle, :profile_id => community.id, :author => person) + process_delayed_job_queue assert_equal 3, person.reload.level end -- libgit2 0.21.2