Commit 1d41510416d7399e24d19cea64754eba82f7815d
1 parent
27f9186b
Exists in
master
and in
1 other branch
adding uni tests for points type and points categorization
Showing
4 changed files
with
77 additions
and
8 deletions
Show diff stats
models/gamification_plugin/points_categorization.rb
... | ... | @@ -3,7 +3,9 @@ class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord |
3 | 3 | belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id |
4 | 4 | attr_accessible :profile_id, :profile, :point_type_id, :weight |
5 | 5 | |
6 | - validates_presence_of :point_type_id, :weight | |
6 | + validates_presence_of :weight | |
7 | + validates :point_type_id, presence: true, uniqueness: { scope: :profile_id, message: _("should have only one point type per profile") } | |
8 | +# validates :point_type_id, presence: true, uniqueness: true | |
7 | 9 | |
8 | 10 | scope :for_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } |
9 | 11 | scope :for_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } | ... | ... |
models/gamification_plugin/points_type.rb
test/unit/gamification_plugin_points_categorizations_test.rb
1 | 1 | require 'test_helper' |
2 | 2 | |
3 | 3 | class GamificationPluginPointsCategorizationsTest < ActiveSupport::TestCase |
4 | - # test "the truth" do | |
5 | - # assert true | |
6 | - # end | |
4 | + | |
5 | + should 'require value for weight' do | |
6 | + p = GamificationPlugin::PointsCategorization.new | |
7 | + p.valid? | |
8 | + assert p.errors[:weight].present? | |
9 | + | |
10 | + p.weight = 12 | |
11 | + p.valid? | |
12 | + refute p.errors[:weight].present? | |
13 | + end | |
14 | + | |
15 | + should 'require value for point type' do | |
16 | + p = GamificationPlugin::PointsCategorization.new | |
17 | + p.valid? | |
18 | + assert p.errors[:point_type_id].present? | |
19 | + | |
20 | + p.point_type = GamificationPlugin::PointsType.create!(:name => 'some') | |
21 | + p.valid? | |
22 | + refute p.errors[:point_type_id].present? | |
23 | + end | |
24 | + | |
25 | + should 'the point type be unique in profile scope' do | |
26 | + point_type = GamificationPlugin::PointsType.create!(:name => 'some') | |
27 | + profile = fast_create(Community) | |
28 | + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => profile.id).save! | |
29 | + | |
30 | + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => profile.id) | |
31 | + p.valid? | |
32 | + assert p.errors[:point_type_id].present? | |
33 | + end | |
34 | + | |
35 | + | |
36 | + should 'the point type be unique in profile scope when profile is nil' do | |
37 | + point_type = GamificationPlugin::PointsType.create!(:name => 'some') | |
38 | + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id).save! | |
39 | + | |
40 | + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id) | |
41 | + p.valid? | |
42 | + assert p.errors[:point_type_id].present? | |
43 | + end | |
44 | + | |
45 | + should 'the point type be used in differente profile scopes' do | |
46 | + point_type = GamificationPlugin::PointsType.create!(:name => 'some') | |
47 | + p1 = fast_create(Community) | |
48 | + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => p1.id).save! | |
49 | + | |
50 | + p2 = fast_create(Community) | |
51 | + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => p2.id) | |
52 | + p.valid? | |
53 | + refute p.errors[:point_type_id].present? | |
54 | + end | |
55 | + | |
7 | 56 | end | ... | ... |
test/unit/gamification_plugin_points_types_test.rb
1 | 1 | require 'test_helper' |
2 | 2 | |
3 | 3 | class GamificationPluginPointsTypesTest < ActiveSupport::TestCase |
4 | - # test "the truth" do | |
5 | - # assert true | |
6 | - # end | |
4 | + | |
5 | + should 'require value for name' do | |
6 | + p = GamificationPlugin::PointsType.new | |
7 | + p.valid? | |
8 | + assert p.errors[:name].present? | |
9 | + | |
10 | + p.name = 'some' | |
11 | + p.valid? | |
12 | + refute p.errors[:name].present? | |
13 | + end | |
14 | + | |
15 | + should 'the name be unique' do | |
16 | + GamificationPlugin::PointsType.new(:name => 'some').save! | |
17 | + | |
18 | + p = GamificationPlugin::PointsType.new(:name => 'some') | |
19 | + p.valid? | |
20 | + assert p.errors[:name].present? | |
21 | + end | |
22 | + | |
23 | + | |
7 | 24 | end | ... | ... |