From 75804df3ea577fa505756460399247ba98998449 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 10 Nov 2015 09:38:21 -0300 Subject: [PATCH] Improve test coverage --- lib/gamification_plugin/api.rb | 2 +- test/test_helper.rb | 6 +++++- test/unit/api_test.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- test/unit/gamification_plugin_test.rb | 2 +- test/unit/point_rules_test.rb | 17 +++++++++++++++++ 5 files changed, 116 insertions(+), 11 deletions(-) diff --git a/lib/gamification_plugin/api.rb b/lib/gamification_plugin/api.rb index e3a7887..525826e 100644 --- a/lib/gamification_plugin/api.rb +++ b/lib/gamification_plugin/api.rb @@ -57,7 +57,7 @@ class GamificationPlugin::API < Grape::API get ':id/points_by_profile' do person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) return not_found! if person.blank? - {points: person.points_by_type(params[:profile]) } + {points: person.points_by_profile(params[:profile]) } end get ':id/points_out_of_profiles' do diff --git a/test/test_helper.rb b/test/test_helper.rb index f728fb7..f496641 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -15,6 +15,10 @@ def create_all_point_rules end end +def default_point_weight(rule_name) + Merit::PointRules::AVAILABLE_RULES[rule_name][:default_weight] +end + def load_point_rule(rule_name, config) rule_config = Merit::PointRules::AVAILABLE_RULES[rule_name.to_sym] raise "Point rule '#{rule_name}' is not available" if rule_config.nil? @@ -22,7 +26,7 @@ def load_point_rule(rule_name, config) rule_config end -#person_points_debug(person) +#person_points_debug(person) def person_points_debug(person) person.score_points.map do |sp| puts 'Ponto:' diff --git a/test/unit/api_test.rb b/test/unit/api_test.rb index a9ac478..35421da 100644 --- a/test/unit/api_test.rb +++ b/test/unit/api_test.rb @@ -8,6 +8,7 @@ class APITest < ActiveSupport::TestCase environment = Environment.default environment.enable_plugin(GamificationPlugin) GamificationPlugin.gamification_set_rules(@environment) + create_all_point_rules end should 'get my own badges' do @@ -27,14 +28,6 @@ class APITest < ActiveSupport::TestCase assert_not_nil json['percent'] end - should 'get my total pontuation' do - badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') - person.add_badge(badge.id) - get "/api/v1/gamification_plugin/my/points?#{params.to_query}" - json = JSON.parse(last_response.body) - assert_not_nil json['points'] - end - should 'get badges of the public person' do badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') another_person = create(User, :environment => environment).person @@ -76,4 +69,95 @@ class APITest < ActiveSupport::TestCase assert_equal 404, last_response.status end + should 'get amount of environment badges grouped by name' do + 3.times { GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') } + get "/api/v1/gamification_plugin/badges" + json = JSON.parse(last_response.body) + assert_equal 3, json['test_badge'] + end + + should 'get my points' do + article = create(TextArticle, :profile_id => @person.id, :author => @person) + create(Comment, :source_id => article.id, :author => fast_create(Person)) + + 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'] + end + + 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)) + params[:type] = 'article_author' + + get "/api/v1/gamification_plugin/my/points_by_type?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal default_point_weight(:article_author), json['points'] + end + + should 'get my points filtered by profile' do + community = fast_create(Community) + create_point_rule_definition('article_author', community) + create(TextArticle, :profile_id => @person.id, :author => @person) + create(TextArticle, :profile_id => community.id, :author => @person) + params[:profile] = community.identifier + + get "/api/v1/gamification_plugin/my/points_by_profile?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal default_point_weight(:article_author), json['points'] + end + + should 'get my points excluding points earned in profiles' do + community = fast_create(Community) + create_point_rule_definition('article_author', community) + create(TextArticle, :profile_id => @person.id, :author => @person) + create(TextArticle, :profile_id => community.id, :author => @person) + + 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'] + end + + should 'get points of a person' do + article = create(TextArticle, :profile_id => @person.id, :author => @person) + create(Comment, :source_id => article.id, :author => fast_create(Person)) + + 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'] + end + + 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)) + params[:type] = 'article_author' + + get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_type?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal default_point_weight(:article_author), json['points'] + end + + should 'get points of a person filtered by profile' do + community = fast_create(Community) + create_point_rule_definition('article_author', community) + create(TextArticle, :profile_id => @person.id, :author => @person) + create(TextArticle, :profile_id => community.id, :author => @person) + params[:profile] = community.identifier + + get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_profile?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal default_point_weight(:article_author), json['points'] + end + + should 'get points of a person excluding points earned in profiles' do + community = fast_create(Community) + create_point_rule_definition('article_author', community) + create(TextArticle, :profile_id => @person.id, :author => @person) + create(TextArticle, :profile_id => community.id, :author => @person) + + 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'] + end + end diff --git a/test/unit/gamification_plugin_test.rb b/test/unit/gamification_plugin_test.rb index cb28253..c59fc28 100644 --- a/test/unit/gamification_plugin_test.rb +++ b/test/unit/gamification_plugin_test.rb @@ -1,4 +1,4 @@ -require_relative '../../../../test/test_helper' +require_relative '../test_helper' class GamificationPluginTest < ActiveSupport::TestCase diff --git a/test/unit/point_rules_test.rb b/test/unit/point_rules_test.rb index ed302db..1c31167 100644 --- a/test/unit/point_rules_test.rb +++ b/test/unit/point_rules_test.rb @@ -19,4 +19,21 @@ class PointRulesTest < ActiveSupport::TestCase assert point_rules.defined_rules.present? end + should 'return target url for a point related to article creation' do + person = create_user('testuser').person + create_point_rule_definition('article_author') + article = create(TextArticle, :profile_id => person.id, :author => person) + url = Merit::PointRules.target_url(person.score_points.last) + assert_equal article.url, url + end + + should 'return target url for a point related to comment creation' do + person = create_user('testuser').person + create_point_rule_definition('comment_author') + article = create(Article, :profile_id => person.id, :author => person) + comment = create(Comment, :source_id => article.id, :author => person) + url = Merit::PointRules.target_url(person.score_points.last) + assert_equal comment.url, url + end + end -- libgit2 0.21.2