profile_test.rb
3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require_relative "../test_helper"
class ProfileTest < ActiveSupport::TestCase
def setup
@environment = Environment.default
@profile = fast_create(Profile)
@settings = GamificationPlugin.settings(environment)
@settings.set_setting(:rank_rules, [
{:level => 1, :points => 10},
{:level => 2, :points => 20},
{:level => 3, :points => 30}
])
@settings.save!
end
attr_accessor :profile, :environment
should 'calculate profile level' do
profile.stubs(:points).returns(25)
assert_equal 2, profile.gamification_plugin_calculate_level
end
should 'calculate profile level ignoring blank values' do
@settings.set_setting(:rank_rules, [
{:points => 10},
{:points => 20},
{:points => 30},
{:points => ""},
{:points => nil},
])
@settings.save!
profile.stubs(:points).returns(25)
assert_equal 2, profile.gamification_plugin_calculate_level
end
should 'calculate profile last level' do
profile.stubs(:points).returns(35)
assert_equal 3, profile.gamification_plugin_calculate_level
end
should 'calculate profile first level' do
profile.stubs(:points).returns(10)
assert_equal 1, profile.gamification_plugin_calculate_level
end
should 'update profile level when a profile action makes a score with zero point' do
#avoid loop when the score changes by zero and
person = create_user('testuser').person
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
end
end
should 'update profile level when the score changes' do
create_point_rule_definition('article_author')
community = fast_create(Community)
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
should 'return percentage of points earned in current level with no points' do
profile.stubs(:points).returns(0)
assert_equal 0, profile.gamification_plugin_level_percent
end
should 'return percentage of points earned in current level with full points' do
profile.stubs(:points).returns(10)
assert_equal 100, profile.gamification_plugin_level_percent
end
should 'return percentage of points earned in current level' do
profile.stubs(:points).returns(4)
assert_equal 40, profile.gamification_plugin_level_percent
end
should 'return percentage of points earned in last level' do
profile.stubs(:level).returns(3)
profile.stubs(:points).returns(35)
assert_equal 100, profile.gamification_plugin_level_percent
end
should 'return percentage of points earned in an intermediate level' do
profile.stubs(:level).returns(2)
profile.stubs(:points).returns(25)
assert_equal 50, profile.gamification_plugin_level_percent
end
end