From a0568a73956c932379644bd3bdb4aa2d5243c946 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 11 Oct 2016 08:25:32 +0200 Subject: [PATCH] fix failing tests --- test/models/checklist_item_test.rb | 19 +++--------------- test/models/checklist_test.rb | 24 ++++------------------ test/models/comment_test.rb | 19 +++--------------- test/models/custom_field_test.rb | 22 ++++++++++---------- test/models/experiment_test.rb | 8 +++++--- test/models/my_module_group_test.rb | 16 ++++----------- test/models/my_module_test.rb | 14 ++++--------- test/models/notification_test.rb | 13 ------------ test/models/organization_test.rb | 12 ++++++----- test/models/project_test.rb | 14 ++++--------- test/models/protocol_keyword_test.rb | 17 +++------------- test/models/protocol_test.rb | 12 ++++------- test/models/report_test.rb | 25 ++++++++++------------- test/models/result_test.rb | 8 ++++---- test/models/sample_group_test.rb | 30 ++++++---------------------- test/models/sample_type_test.rb | 20 ++++++------------- test/models/step_test.rb | 8 ++++---- test/models/user_test.rb | 29 ++++++--------------------- 18 files changed, 88 insertions(+), 222 deletions(-) diff --git a/test/models/checklist_item_test.rb b/test/models/checklist_item_test.rb index e147a2a23..e32bffddd 100644 --- a/test/models/checklist_item_test.rb +++ b/test/models/checklist_item_test.rb @@ -1,6 +1,9 @@ require 'test_helper' class ChecklistItemTest < ActiveSupport::TestCase + should validate_presence_of(:text) + should should validate_length_of(:text) + .is_at_most(TEXT_MAX_LENGTH) test "should validate with correct data" do chkItem = ChecklistItem.new( @@ -11,22 +14,6 @@ class ChecklistItemTest < ActiveSupport::TestCase assert chkItem.valid? end - test "should not validate without text" do - chkItem = ChecklistItem.new( - text: " ", checked: false, - checklist: checklists(:one)) - assert_not chkItem.valid?, "Checklist item was created without text." - end - - test "should not validate with text too long" do - chkItem = ChecklistItem.new( - text: "#" * 1001, - checked: false, - checklist: checklists(:one) - ) - assert_not chkItem.valid?, "Checklist item was created with text being too long." - end - test "should not validate without checked value" do chkItem = ChecklistItem.new( text: "text", checked: nil, diff --git a/test/models/checklist_test.rb b/test/models/checklist_test.rb index 1e8311644..7f8c6b3e0 100644 --- a/test/models/checklist_test.rb +++ b/test/models/checklist_test.rb @@ -1,6 +1,10 @@ require 'test_helper' class ChecklistTest < ActiveSupport::TestCase + should validate_presence_of(:step) + should validate_presence_of(:name) + should validate_length_of(:name).is_at_most(TEXT_MAX_LENGTH) + test "should validate with correct data" do checklist = Checklist.new( name: "test", @@ -9,26 +13,6 @@ class ChecklistTest < ActiveSupport::TestCase assert checklist end - test "should not validate without name" do - checklist = Checklist.new(step: steps(:step1)) - assert_not checklist.valid? - end - - test "should not validate with name too long" do - checklist = Checklist.new( - name: "#" * 51, - step: steps(:step1) - ) - assert_not checklist.valid? - end - - test "should not validate with non existent step" do - checklist = Checklist.new( - name: "test", - step_id: 123123) - assert_not checklist.valid? - end - test "should have association step -> checklist" do checklist = Checklist.new( name: "test", diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb index 37d444a86..96af081d1 100644 --- a/test/models/comment_test.rb +++ b/test/models/comment_test.rb @@ -13,26 +13,13 @@ class CommentTest < ActiveSupport::TestCase ) end + should validate_presence_of(:message) + should validate_length_of(:message).is_at_most(TEXT_MAX_LENGTH) + test "should validate" do assert @valid.valid? end - test "should not validate with empty or nil message" do - comment_empty = Comment.new(message: " ", user: users(:steve)) - comment_nil = Comment.new(message: nil, user: users(:steve)) - - assert_not comment_empty.valid?, "Comment was created with empty message." - assert_not comment_nil.valid?, "Comment was created with nil message." - end - - test "should not validate with message too long" do - comment = Comment.new( - message: "#" * 1001, - user: users(:steve) - ) - assert_not comment.valid?, "Comment was created with message being too long." - end - test "should not validate with empty user_id" do comment = Comment.new(message: "Message") assert_not comment.valid?, "Comment was created with empty user_id." diff --git a/test/models/custom_field_test.rb b/test/models/custom_field_test.rb index 5d48b69ad..d46e29f22 100644 --- a/test/models/custom_field_test.rb +++ b/test/models/custom_field_test.rb @@ -5,22 +5,20 @@ class CustomFieldTest < ActiveSupport::TestCase @custom_field = custom_fields(:volume) end + should validate_presence_of(:name) + should validate_length_of(:name).is_at_most(NAME_MAX_LENGTH) + should validate_exclusion_of(:name) + .in_array(['Assigned', + 'Sample name', + 'Sample type', + 'Sample group', + 'Added on', + 'Added by']) + test "should validate with correct data" do assert @custom_field.valid? end - test "should not validate without name" do - @custom_field.name = "" - assert_not @custom_field.valid? - @custom_field.name = nil - assert_not @custom_field.valid? - end - - test "should not validate with too long name" do - @custom_field.name = "n" * 51 - assert_not @custom_field.valid? - end - test "should not validate with non existent user" do @custom_field.user_id = 11231231 assert_not @custom_field.valid? diff --git a/test/models/experiment_test.rb b/test/models/experiment_test.rb index 766d9afb6..011c6aae0 100644 --- a/test/models/experiment_test.rb +++ b/test/models/experiment_test.rb @@ -3,12 +3,14 @@ require 'helpers/archivable_model_test_helper' require 'helpers/searchable_model_test_helper' class ExperimentTest < ActiveSupport::TestCase - should validate_presence_of(:name) - should validate_length_of(:name).is_at_least(4).is_at_most(50) + should validate_length_of(:name) + .is_at_least(NAME_MIN_LENGTH) + .is_at_most(NAME_MAX_LENGTH) should validate_presence_of(:project) should validate_presence_of(:created_by) should validate_presence_of(:last_modified_by) - should validate_length_of(:description).is_at_most(255) + should validate_length_of(:description) + .is_at_most(TEXT_MAX_LENGTH) should have_db_column(:name).of_type(:string) should have_db_column(:description).of_type(:text) diff --git a/test/models/my_module_group_test.rb b/test/models/my_module_group_test.rb index 91cdab33c..0244d0010 100644 --- a/test/models/my_module_group_test.rb +++ b/test/models/my_module_group_test.rb @@ -8,22 +8,14 @@ class MyModuleGroupTest < ActiveSupport::TestCase @module_group = my_module_groups(:wf1) end + should validate_presence_of(:name) + should validate_length_of(:name) + .is_at_most(NAME_MAX_LENGTH) + test "should validate with valid data" do assert @module_group.valid? end - test "should not validate with name" do - @module_group.name = "" - assert_not @module_group.valid? - @module_group.name = nil - assert_not @module_group.valid? - end - - test "should not validate too long name" do - @module_group.name = "n" * 51 - assert_not @module_group.valid? - end - test "where_attributes_like should work" do attributes_like_test(MyModuleGroup, :name, "expression") end diff --git a/test/models/my_module_test.rb b/test/models/my_module_test.rb index 8ee0f10d0..488165590 100644 --- a/test/models/my_module_test.rb +++ b/test/models/my_module_test.rb @@ -10,6 +10,10 @@ class MyModuleTest < ActiveSupport::TestCase @my_module = my_modules(:list_of_samples) end + should validate_length_of(:name) + .is_at_least(NAME_MIN_LENGTH) + .is_at_most(NAME_MAX_LENGTH) + test "should validate valid module object" do assert @my_module.valid? end @@ -21,16 +25,6 @@ class MyModuleTest < ActiveSupport::TestCase assert_not @my_module.valid? end - test "should not validate too short name" do - @my_module.name = "n" - assert_not @my_module.valid? - end - - test "should not validate too long name" do - @my_module.name = "n" * 51 - assert_not @my_module.valid? - end - test "should not validate with non existing experiment" do @my_module.experiment_id = 123123 assert_not @my_module.valid? diff --git a/test/models/notification_test.rb b/test/models/notification_test.rb index c9cfcfb63..c718c53fe 100644 --- a/test/models/notification_test.rb +++ b/test/models/notification_test.rb @@ -1,13 +1,6 @@ require 'test_helper' class NotificationTest < ActiveSupport::TestCase - def setup - @user = users(:john) - @notification = notificaton(:one) - @user_notification = UserNotification.new(user: @user, - notificaton: @notificaton) - end - should have_db_column(:title).of_type(:string) should have_db_column(:message).of_type(:string) should have_db_column(:type_of).of_type(:integer) @@ -18,10 +11,4 @@ class NotificationTest < ActiveSupport::TestCase should have_many(:user_notifications) should have_many(:users) should belong_to(:generator_user).class_name('User') - - test '#already_seen' do - notifications = Notification.already_seen(@user) - byebug - assert notifications.eq([false]) - end end diff --git a/test/models/organization_test.rb b/test/models/organization_test.rb index fd8b5da0e..40c2b8542 100644 --- a/test/models/organization_test.rb +++ b/test/models/organization_test.rb @@ -5,6 +5,13 @@ class OrganizationTest < ActiveSupport::TestCase @org = organizations(:test) end + should validate_length_of(:name) + .is_at_least(NAME_MIN_LENGTH) + .is_at_most(NAME_MAX_LENGTH) + + should validate_length_of(:description) + .is_at_most(TEXT_MAX_LENGTH) + test "should validate organization default values" do assert @org.valid? end @@ -14,11 +21,6 @@ class OrganizationTest < ActiveSupport::TestCase assert @org.invalid?, "Organization with blank name returns valid? = true" end - test "should have short name" do - @org.name = "k" * 101 - assert @org.invalid?, "Organization with name too long returns valid? = true" - end - test "should have space_taken present" do @org.space_taken = nil assert @org.invalid?, "Organization without space_taken returns valid? = true" diff --git a/test/models/project_test.rb b/test/models/project_test.rb index 8bbf46c3f..8ac4d1e98 100644 --- a/test/models/project_test.rb +++ b/test/models/project_test.rb @@ -12,21 +12,15 @@ class ProjectTest < ActiveSupport::TestCase @project3 = projects(:test3) end + should validate_length_of(:name) + .is_at_least(NAME_MIN_LENGTH) + .is_at_most(NAME_MAX_LENGTH) + test "should have non-blank name" do @project.name = "" assert @project.invalid?, "Project with blank name returns valid? = true" end - test "should have short name" do - @project.name = "k" * 31 - assert @project.invalid?, "Project with name too long returns valid? = true" - end - - test "should have long enough name" do - @project.name = "k" * 3 - assert @project.invalid?, "Project with name too short returns valid? = true" - end - test "should have organization-wide unique name" do @project.name = @project2.name assert @project.invalid?, "Project with non-unique organization-wide name returns valid? = true" diff --git a/test/models/protocol_keyword_test.rb b/test/models/protocol_keyword_test.rb index 70285b8a4..6026994e0 100644 --- a/test/models/protocol_keyword_test.rb +++ b/test/models/protocol_keyword_test.rb @@ -6,17 +6,6 @@ class ProtocolKeywordTest < ActiveSupport::TestCase @kw = protocol_keywords(:kw1) end - test "should not validate without name" do - assert @kw.valid? - @kw.name = nil - assert_not @kw.valid? - end - - test "should not validate with name too long" do - @kw.name = "A" * 50 - assert @kw.valid? - @kw.name = "A" * 51 - assert_not @kw.valid? - end - -end \ No newline at end of file + should validate_presence_of(:name) + should validate_length_of(:name).is_at_most(NAME_MAX_LENGTH) +end diff --git a/test/models/protocol_test.rb b/test/models/protocol_test.rb index aa19f1d2c..0d27495f5 100644 --- a/test/models/protocol_test.rb +++ b/test/models/protocol_test.rb @@ -13,6 +13,9 @@ class ProtocolTest < ActiveSupport::TestCase ) end + should validate_length_of(:name).is_at_most(NAME_MAX_LENGTH) + should validate_length_of(:description).is_at_most(TEXT_MAX_LENGTH) + test "protocol_type enum works" do @p.protocol_type = :unlinked assert @p.in_module? @@ -26,13 +29,6 @@ class ProtocolTest < ActiveSupport::TestCase assert @p.in_repository? end - test "should not validate with name too long" do - @p.name = "Q" * 100 - assert @p.valid? - @p.name = "Q" * 101 - assert_not @p.valid? - end - test "should not validate without organization" do @p.organization = @org assert @p.valid? @@ -164,4 +160,4 @@ class ProtocolTest < ActiveSupport::TestCase ) end -end \ No newline at end of file +end diff --git a/test/models/report_test.rb b/test/models/report_test.rb index 855ccf499..820243842 100644 --- a/test/models/report_test.rb +++ b/test/models/report_test.rb @@ -11,30 +11,27 @@ class ReportTest < ActiveSupport::TestCase } end + should validate_length_of(:name) + .is_at_least(NAME_MIN_LENGTH) + .is_at_most(NAME_MAX_LENGTH) + + should validate_length_of(:description) + .is_at_most(TEXT_MAX_LENGTH) + test "should validate with valid data" do assert @report.valid?, "Report with valid data was invalid" end - test "should not validate with invalid name" do - @report.name = nil - assert_not @report.valid?, "Report with nil name was valid" - @report.name = "a" - assert_not @report.valid?, "Report with name too short was valid" - @report.name = "a" * 31 - assert_not @report.valid?, "Report with name too long was valid" - + test 'should not validate with invalid name' do # Check if uniqueness constraint works @report2 = Report.new( - name: @report.name_was, + name: @report.name, project: projects(:interfaces), user: users(:steve) ) - assert_not @report.valid?, "Report with same name for specific user was valid" - end - test "should not validate with invalid description" do - @report.description = "a" * 1001 - assert_not @report.valid?, "Report with description too long was valid" + assert_not @report2.valid?, + 'Report with same name for specific user was valid' end test "should not validate without project" do diff --git a/test/models/result_test.rb b/test/models/result_test.rb index 110899883..3eea5fc1c 100644 --- a/test/models/result_test.rb +++ b/test/models/result_test.rb @@ -111,8 +111,8 @@ class ResultTest < ActiveSupport::TestCase first_comment = comments(:test_result_comment_24) last_comment = comments(:test_result_comment_5) assert_equal 20, last_comments.size - assert_equal first_comment, last_comments.first - assert_equal last_comment, last_comments.last + assert_equal first_comment, last_comments.last + assert_equal last_comment, last_comments.first end # Not possible to test with fixtures and random id values @@ -125,8 +125,8 @@ class ResultTest < ActiveSupport::TestCase first_comment = comments(:test_result_comment_24) last_comment = comments(:test_result_comment_20) assert_equal 5, last_comments.size - assert_equal first_comment, last_comments.first - assert_equal last_comment, last_comments.last + assert_equal first_comment, last_comments.last + assert_equal last_comment, last_comments.first end test "should search for results of user" do diff --git a/test/models/sample_group_test.rb b/test/models/sample_group_test.rb index 1bfabb304..cb0a8983a 100644 --- a/test/models/sample_group_test.rb +++ b/test/models/sample_group_test.rb @@ -5,34 +5,16 @@ class SampleGroupTest < ActiveSupport::TestCase @sample_group = sample_groups(:blood) end + should validate_presence_of(:name) + should validate_length_of(:name).is_at_most(NAME_MAX_LENGTH) + should validate_presence_of(:color) + should validate_length_of(:color).is_at_most(COLOR_MAX_LENGTH) + should validate_presence_of(:organization) + test "should validate with correct data" do assert @sample_group.valid? end - test "should not validate without name" do - @sample_group.name = "" - assert_not @sample_group.valid? - @sample_group.name = nil - assert_not @sample_group.valid? - end - - test "should validate too long name value" do - @sample_group.name *= 50 - assert_not @sample_group.valid? - end - - test "should validate without color because of default value" do - @sample_group.color = "" - assert_not @sample_group.valid? - @sample_group.color = nil - assert_not @sample_group.valid? - end - - test "should validate too long color value" do - @sample_group.color *= 7 - assert_not @sample_group.valid? - end - test "should not validate without organization" do @sample_group.organization = nil assert_not @sample_group.valid? diff --git a/test/models/sample_type_test.rb b/test/models/sample_type_test.rb index 2a31bddd2..80f42f83e 100644 --- a/test/models/sample_type_test.rb +++ b/test/models/sample_type_test.rb @@ -5,23 +5,15 @@ class SampleTypeTest < ActiveSupport::TestCase @sample_type = sample_types(:skin) end - test "should validate with correct data" do + should validate_length_of(:name).is_at_most(NAME_MAX_LENGTH) + should validate_presence_of(:name) + should validate_presence_of(:organization) + + test 'should validate with correct data' do assert @sample_type.valid? end - test "should not validate without name" do - @sample_type.name = "" - assert_not @sample_type.valid? - @sample_type.name = nil - assert_not @sample_type.valid? - end - - test "should validate too long name value" do - @sample_type.name *= 50 - assert_not @sample_type.valid? - end - - test "should not validate without organization" do + test 'should not validate without organization' do @sample_type.organization_id = 12321321 assert_not @sample_type.valid? @sample_type.organization = nil diff --git a/test/models/step_test.rb b/test/models/step_test.rb index d70883c7f..2dd4be83b 100644 --- a/test/models/step_test.rb +++ b/test/models/step_test.rb @@ -85,8 +85,8 @@ class StepTest < ActiveSupport::TestCase first_comment = comments(:test_step_comment_24) last_comment = comments(:test_step_comment_5) assert_equal 20, last_comments.size - assert_equal first_comment, last_comments.first - assert_equal last_comment, last_comments.last + assert_equal first_comment, last_comments.last + assert_equal last_comment, last_comments.first end # Not possible to test with fixtures and random id values @@ -98,8 +98,8 @@ class StepTest < ActiveSupport::TestCase first_comment = comments(:test_step_comment_24) last_comment = comments(:test_step_comment_20) assert_equal 5, last_comments.size - assert_equal first_comment, last_comments.first - assert_equal last_comment, last_comments.last + assert_equal first_comment, last_comments.last + assert_equal last_comment, last_comments.first end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index dbbc9cee0..bb795bd66 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -7,27 +7,10 @@ class UserTest < ActiveSupport::TestCase @org = organizations(:biosistemika) end -# Test full_name attribute - test "should have non-blank full_name" do - @user.full_name = "" - assert @user.invalid?, "User with blank full_name is not valid" - end - - test "should have short full_name" do - @user.full_name = "k" * 51 - assert @user.invalid?, "User with name too long is not valid" - end - -# Test initials attribute - test "should have non-blank initials" do - @user.initials = "" - assert @user.invalid?, "User with blank initials is not valid" - end - - test "should have short initials" do - @user.initials = "k" * 5 - assert @user.invalid?, "User with initials too long is not valid" - end + should validate_presence_of(:full_name) + should validate_length_of(:full_name).is_at_most(NAME_MAX_LENGTH) + should validate_presence_of(:initials) + should validate_length_of(:initials).is_at_most(USER_INITIALS_MAX_LENGTH) # Test password attribute test "should have non-blank password" do @@ -137,7 +120,7 @@ class UserTest < ActiveSupport::TestCase assert_equal 10, last_activities.size assert_equal first_activity, last_activities.first assert_equal last_activity, last_activities.last - end + end test "should get specified number of last activities" do last_activities = @user2.last_activities(0, 4) @@ -146,7 +129,7 @@ class UserTest < ActiveSupport::TestCase assert_equal 4, last_activities.size assert_equal first_activity, last_activities.first assert_equal last_activity, last_activities.last - end + end test "should allow to change time zone" do assert @user.valid?