Commit 75804df3ea577fa505756460399247ba98998449

Authored by Victor Costa
1 parent 46d91e43

Improve test coverage

lib/gamification_plugin/api.rb
... ... @@ -57,7 +57,7 @@ class GamificationPlugin::API < Grape::API
57 57 get ':id/points_by_profile' do
58 58 person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
59 59 return not_found! if person.blank?
60   - {points: person.points_by_type(params[:profile]) }
  60 + {points: person.points_by_profile(params[:profile]) }
61 61 end
62 62  
63 63 get ':id/points_out_of_profiles' do
... ...
test/test_helper.rb
... ... @@ -15,6 +15,10 @@ def create_all_point_rules
15 15 end
16 16 end
17 17  
  18 +def default_point_weight(rule_name)
  19 + Merit::PointRules::AVAILABLE_RULES[rule_name][:default_weight]
  20 +end
  21 +
18 22 def load_point_rule(rule_name, config)
19 23 rule_config = Merit::PointRules::AVAILABLE_RULES[rule_name.to_sym]
20 24 raise "Point rule '#{rule_name}' is not available" if rule_config.nil?
... ... @@ -22,7 +26,7 @@ def load_point_rule(rule_name, config)
22 26 rule_config
23 27 end
24 28  
25   -#person_points_debug(person)
  29 +#person_points_debug(person)
26 30 def person_points_debug(person)
27 31 person.score_points.map do |sp|
28 32 puts 'Ponto:'
... ...
test/unit/api_test.rb
... ... @@ -8,6 +8,7 @@ class APITest < ActiveSupport::TestCase
8 8 environment = Environment.default
9 9 environment.enable_plugin(GamificationPlugin)
10 10 GamificationPlugin.gamification_set_rules(@environment)
  11 + create_all_point_rules
11 12 end
12 13  
13 14 should 'get my own badges' do
... ... @@ -27,14 +28,6 @@ class APITest < ActiveSupport::TestCase
27 28 assert_not_nil json['percent']
28 29 end
29 30  
30   - should 'get my total pontuation' do
31   - badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
32   - person.add_badge(badge.id)
33   - get "/api/v1/gamification_plugin/my/points?#{params.to_query}"
34   - json = JSON.parse(last_response.body)
35   - assert_not_nil json['points']
36   - end
37   -
38 31 should 'get badges of the public person' do
39 32 badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
40 33 another_person = create(User, :environment => environment).person
... ... @@ -76,4 +69,95 @@ class APITest < ActiveSupport::TestCase
76 69 assert_equal 404, last_response.status
77 70 end
78 71  
  72 + should 'get amount of environment badges grouped by name' do
  73 + 3.times { GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') }
  74 + get "/api/v1/gamification_plugin/badges"
  75 + json = JSON.parse(last_response.body)
  76 + assert_equal 3, json['test_badge']
  77 + end
  78 +
  79 + should 'get my points' do
  80 + article = create(TextArticle, :profile_id => @person.id, :author => @person)
  81 + create(Comment, :source_id => article.id, :author => fast_create(Person))
  82 +
  83 + get "/api/v1/gamification_plugin/my/points?#{params.to_query}"
  84 + json = JSON.parse(last_response.body)
  85 + assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points']
  86 + end
  87 +
  88 + should 'get my points filtered by type' do
  89 + article = create(TextArticle, :profile_id => @person.id, :author => @person)
  90 + create(Comment, :source_id => article.id, :author => fast_create(Person))
  91 + params[:type] = 'article_author'
  92 +
  93 + get "/api/v1/gamification_plugin/my/points_by_type?#{params.to_query}"
  94 + json = JSON.parse(last_response.body)
  95 + assert_equal default_point_weight(:article_author), json['points']
  96 + end
  97 +
  98 + should 'get my points filtered by profile' do
  99 + community = fast_create(Community)
  100 + create_point_rule_definition('article_author', community)
  101 + create(TextArticle, :profile_id => @person.id, :author => @person)
  102 + create(TextArticle, :profile_id => community.id, :author => @person)
  103 + params[:profile] = community.identifier
  104 +
  105 + get "/api/v1/gamification_plugin/my/points_by_profile?#{params.to_query}"
  106 + json = JSON.parse(last_response.body)
  107 + assert_equal default_point_weight(:article_author), json['points']
  108 + end
  109 +
  110 + should 'get my points excluding points earned in profiles' do
  111 + community = fast_create(Community)
  112 + create_point_rule_definition('article_author', community)
  113 + create(TextArticle, :profile_id => @person.id, :author => @person)
  114 + create(TextArticle, :profile_id => community.id, :author => @person)
  115 +
  116 + get "/api/v1/gamification_plugin/my/points_out_of_profiles?#{params.to_query}"
  117 + json = JSON.parse(last_response.body)
  118 + assert_equal 2*default_point_weight(:article_author), json['points']
  119 + end
  120 +
  121 + should 'get points of a person' do
  122 + article = create(TextArticle, :profile_id => @person.id, :author => @person)
  123 + create(Comment, :source_id => article.id, :author => fast_create(Person))
  124 +
  125 + get "/api/v1/gamification_plugin/people/#{person.id}/points?#{params.to_query}"
  126 + json = JSON.parse(last_response.body)
  127 + assert_equal default_point_weight(:article_author) + default_point_weight(:comment_article_author), json['points']
  128 + end
  129 +
  130 + should 'get points of a person filtered by type' do
  131 + article = create(TextArticle, :profile_id => @person.id, :author => @person)
  132 + create(Comment, :source_id => article.id, :author => fast_create(Person))
  133 + params[:type] = 'article_author'
  134 +
  135 + get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_type?#{params.to_query}"
  136 + json = JSON.parse(last_response.body)
  137 + assert_equal default_point_weight(:article_author), json['points']
  138 + end
  139 +
  140 + should 'get points of a person filtered by profile' do
  141 + community = fast_create(Community)
  142 + create_point_rule_definition('article_author', community)
  143 + create(TextArticle, :profile_id => @person.id, :author => @person)
  144 + create(TextArticle, :profile_id => community.id, :author => @person)
  145 + params[:profile] = community.identifier
  146 +
  147 + get "/api/v1/gamification_plugin/people/#{@person.id}/points_by_profile?#{params.to_query}"
  148 + json = JSON.parse(last_response.body)
  149 + assert_equal default_point_weight(:article_author), json['points']
  150 + end
  151 +
  152 + should 'get points of a person excluding points earned in profiles' do
  153 + community = fast_create(Community)
  154 + create_point_rule_definition('article_author', community)
  155 + create(TextArticle, :profile_id => @person.id, :author => @person)
  156 + create(TextArticle, :profile_id => community.id, :author => @person)
  157 +
  158 + get "/api/v1/gamification_plugin/people/#{@person.id}/points_out_of_profiles?#{params.to_query}"
  159 + json = JSON.parse(last_response.body)
  160 + assert_equal 2*default_point_weight(:article_author), json['points']
  161 + end
  162 +
79 163 end
... ...
test/unit/gamification_plugin_test.rb
1   -require_relative '../../../../test/test_helper'
  1 +require_relative '../test_helper'
2 2  
3 3 class GamificationPluginTest < ActiveSupport::TestCase
4 4  
... ...
test/unit/point_rules_test.rb
... ... @@ -19,4 +19,21 @@ class PointRulesTest &lt; ActiveSupport::TestCase
19 19 assert point_rules.defined_rules.present?
20 20 end
21 21  
  22 + should 'return target url for a point related to article creation' do
  23 + person = create_user('testuser').person
  24 + create_point_rule_definition('article_author')
  25 + article = create(TextArticle, :profile_id => person.id, :author => person)
  26 + url = Merit::PointRules.target_url(person.score_points.last)
  27 + assert_equal article.url, url
  28 + end
  29 +
  30 + should 'return target url for a point related to comment creation' do
  31 + person = create_user('testuser').person
  32 + create_point_rule_definition('comment_author')
  33 + article = create(Article, :profile_id => person.id, :author => person)
  34 + comment = create(Comment, :source_id => article.id, :author => person)
  35 + url = Merit::PointRules.target_url(person.score_points.last)
  36 + assert_equal comment.url, url
  37 + end
  38 +
22 39 end
... ...