From 1d41510416d7399e24d19cea64754eba82f7815d Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Thu, 15 Oct 2015 19:33:37 -0300 Subject: [PATCH] adding uni tests for points type and points categorization --- models/gamification_plugin/points_categorization.rb | 4 +++- models/gamification_plugin/points_type.rb | 3 ++- test/unit/gamification_plugin_points_categorizations_test.rb | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- test/unit/gamification_plugin_points_types_test.rb | 23 ++++++++++++++++++++--- 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/models/gamification_plugin/points_categorization.rb b/models/gamification_plugin/points_categorization.rb index a97add2..275ef30 100644 --- a/models/gamification_plugin/points_categorization.rb +++ b/models/gamification_plugin/points_categorization.rb @@ -3,7 +3,9 @@ class GamificationPlugin::PointsCategorization < Noosfero::Plugin::ActiveRecord belongs_to :point_type, class_name: 'GamificationPlugin::PointsType', foreign_key: :point_type_id attr_accessible :profile_id, :profile, :point_type_id, :weight - validates_presence_of :point_type_id, :weight + validates_presence_of :weight + validates :point_type_id, presence: true, uniqueness: { scope: :profile_id, message: _("should have only one point type per profile") } +# validates :point_type_id, presence: true, uniqueness: true scope :for_type, lambda { |p_type| joins(:point_type).where(gamification_plugin_points_types: {name: p_type}) } scope :for_profile, lambda { |p_profile| joins(:profile).where(profiles: {identifier: p_profile}) } diff --git a/models/gamification_plugin/points_type.rb b/models/gamification_plugin/points_type.rb index 26832c7..ad5a1d7 100644 --- a/models/gamification_plugin/points_type.rb +++ b/models/gamification_plugin/points_type.rb @@ -1,5 +1,6 @@ class GamificationPlugin::PointsType < Noosfero::Plugin::ActiveRecord attr_accessible :description, :name - validates_presence_of :name + validates :name, presence: true, uniqueness: true + end diff --git a/test/unit/gamification_plugin_points_categorizations_test.rb b/test/unit/gamification_plugin_points_categorizations_test.rb index fd7bd68..36373f0 100644 --- a/test/unit/gamification_plugin_points_categorizations_test.rb +++ b/test/unit/gamification_plugin_points_categorizations_test.rb @@ -1,7 +1,56 @@ require 'test_helper' class GamificationPluginPointsCategorizationsTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + should 'require value for weight' do + p = GamificationPlugin::PointsCategorization.new + p.valid? + assert p.errors[:weight].present? + + p.weight = 12 + p.valid? + refute p.errors[:weight].present? + end + + should 'require value for point type' do + p = GamificationPlugin::PointsCategorization.new + p.valid? + assert p.errors[:point_type_id].present? + + p.point_type = GamificationPlugin::PointsType.create!(:name => 'some') + p.valid? + refute p.errors[:point_type_id].present? + end + + should 'the point type be unique in profile scope' do + point_type = GamificationPlugin::PointsType.create!(:name => 'some') + profile = fast_create(Community) + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => profile.id).save! + + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => profile.id) + p.valid? + assert p.errors[:point_type_id].present? + end + + + should 'the point type be unique in profile scope when profile is nil' do + point_type = GamificationPlugin::PointsType.create!(:name => 'some') + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id).save! + + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id) + p.valid? + assert p.errors[:point_type_id].present? + end + + should 'the point type be used in differente profile scopes' do + point_type = GamificationPlugin::PointsType.create!(:name => 'some') + p1 = fast_create(Community) + GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => p1.id).save! + + p2 = fast_create(Community) + p = GamificationPlugin::PointsCategorization.new(:weight => 10, :point_type_id => point_type.id, :profile_id => p2.id) + p.valid? + refute p.errors[:point_type_id].present? + end + end diff --git a/test/unit/gamification_plugin_points_types_test.rb b/test/unit/gamification_plugin_points_types_test.rb index f1a7d96..6620eaf 100644 --- a/test/unit/gamification_plugin_points_types_test.rb +++ b/test/unit/gamification_plugin_points_types_test.rb @@ -1,7 +1,24 @@ require 'test_helper' class GamificationPluginPointsTypesTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + should 'require value for name' do + p = GamificationPlugin::PointsType.new + p.valid? + assert p.errors[:name].present? + + p.name = 'some' + p.valid? + refute p.errors[:name].present? + end + + should 'the name be unique' do + GamificationPlugin::PointsType.new(:name => 'some').save! + + p = GamificationPlugin::PointsType.new(:name => 'some') + p.valid? + assert p.errors[:name].present? + end + + end -- libgit2 0.21.2