From a7558d83be5fa1f10402173dd1bab1ddd27ce29e Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 6 May 2019 16:14:50 +0200 Subject: [PATCH] Add valid factory check for Project, MyModules, notification, protocol --- app/models/project.rb | 2 +- spec/factories/my_module_repository_rows.rb | 6 +++-- spec/factories/notifications.rb | 2 ++ spec/models/my_module_group_spec.rb | 10 ++++++- spec/models/my_module_repository_row_spec.rb | 10 ++++++- spec/models/my_module_spec.rb | 8 ++++++ spec/models/my_module_tag_spec.rb | 10 ++++++- spec/models/notification_spec.rb | 8 ++++++ spec/models/project_comment_spec.rb | 10 ++++++- spec/models/project_spec.rb | 28 ++++++++++---------- spec/models/protocol_spec.rb | 9 ++++++- 11 files changed, 81 insertions(+), 22 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index d0dc5c557..1ffd8983e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -9,7 +9,7 @@ class Project < ApplicationRecord validates :name, length: { minimum: Constants::NAME_MIN_LENGTH, maximum: Constants::NAME_MAX_LENGTH }, - uniqueness: { scope: :team, case_sensitive: false } + uniqueness: { scope: :team_id, case_sensitive: false } validates :visibility, presence: true validates :team, presence: true diff --git a/spec/factories/my_module_repository_rows.rb b/spec/factories/my_module_repository_rows.rb index 99ff9f237..7f9bac2af 100644 --- a/spec/factories/my_module_repository_rows.rb +++ b/spec/factories/my_module_repository_rows.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + FactoryBot.define do factory :mm_repository_row, class: MyModuleRepositoryRow do - association :repository_row, factory: :repository_row - association :my_module, factory: :my_module + repository_row + my_module end end diff --git a/spec/factories/notifications.rb b/spec/factories/notifications.rb index fc702321d..bfefcfcf8 100644 --- a/spec/factories/notifications.rb +++ b/spec/factories/notifications.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :notification do title 'Admin was added as Owner to project ' \ diff --git a/spec/models/my_module_group_spec.rb b/spec/models/my_module_group_spec.rb index 0ac325bc3..1732536e1 100644 --- a/spec/models/my_module_group_spec.rb +++ b/spec/models/my_module_group_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe MyModuleGroup, type: :model do + let(:my_module_group) { build :my_module_group } + + it 'is valid' do + expect(my_module_group).to be_valid + end + it 'should be of class MyModuleGroup' do expect(subject.class).to eq MyModuleGroup end @@ -19,7 +27,7 @@ describe MyModuleGroup, type: :model do it { should have_many(:my_modules) } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :experiment } end end diff --git a/spec/models/my_module_repository_row_spec.rb b/spec/models/my_module_repository_row_spec.rb index 677692314..7f2409483 100644 --- a/spec/models/my_module_repository_row_spec.rb +++ b/spec/models/my_module_repository_row_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe MyModuleRepositoryRow, type: :model do + let(:my_module_repository_row) { build :mm_repository_row } + + it 'is valid' do + expect(my_module_repository_row).to be_valid + end + it 'should be of class MyModuleRepositoryRow' do expect(subject.class).to eq MyModuleRepositoryRow end @@ -20,7 +28,7 @@ describe MyModuleRepositoryRow, type: :model do it { should belong_to :repository_row } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :repository_row } it { should validate_presence_of :my_module } end diff --git a/spec/models/my_module_spec.rb b/spec/models/my_module_spec.rb index 40222d710..418937151 100644 --- a/spec/models/my_module_spec.rb +++ b/spec/models/my_module_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe MyModule, type: :model do + let(:my_module) { build :my_module } + + it 'is valid' do + expect(my_module).to be_valid + end + it 'should be of class MyModule' do expect(subject.class).to eq MyModule end diff --git a/spec/models/my_module_tag_spec.rb b/spec/models/my_module_tag_spec.rb index 0045e8d57..bb4ef9590 100644 --- a/spec/models/my_module_tag_spec.rb +++ b/spec/models/my_module_tag_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe MyModuleTag, type: :model do + let(:my_module_tag) { build :my_module_tag } + + it 'is valid' do + expect(my_module_tag).to be_valid + end + it 'should be of class MyModuleTag' do expect(subject.class).to eq MyModuleTag end @@ -17,7 +25,7 @@ describe MyModuleTag, type: :model do it { should belong_to :tag } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :my_module } it { should validate_presence_of :tag } end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index e54539b00..5c6394415 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe Notification, type: :model do + let(:notification) { build :notification } + + it 'is valid' do + expect(notification).to be_valid + end + it 'should be of class Notification' do expect(subject.class).to eq Notification end diff --git a/spec/models/project_comment_spec.rb b/spec/models/project_comment_spec.rb index a66cdd06a..3607b293a 100644 --- a/spec/models/project_comment_spec.rb +++ b/spec/models/project_comment_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe ProjectComment, type: :model do + let(:project_comment) { build :project_comment } + + it 'is valid' do + expect(project_comment).to be_valid + end + it 'should be of class MyModuleTag' do expect(subject.class).to eq ProjectComment end @@ -20,7 +28,7 @@ describe ProjectComment, type: :model do it { should belong_to(:project).with_foreign_key('associated_id') } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :project } end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 22ff0ceda..13b05d061 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3,6 +3,12 @@ require 'rails_helper' describe Project, type: :model do + let(:project) { build :project } + + it 'is valid' do + expect(project).to be_valid + end + it 'should be of class Project' do expect(subject.class).to eq Project end @@ -44,27 +50,21 @@ describe Project, type: :model do describe 'Validations' do describe '#visibility' do - it { should validate_presence_of :visibility } + it { is_expected.to validate_presence_of :visibility } end describe '#team' do - it { should validate_presence_of :team } + it { is_expected.to validate_presence_of :team } end describe '#name' do - it 'should be at least 2 long and max 255 long' do - should validate_length_of(:name) - .is_at_least(Constants::NAME_MIN_LENGTH) - .is_at_most(Constants::NAME_MAX_LENGTH) + it do + is_expected.to(validate_length_of(:name) + .is_at_least(Constants::NAME_MIN_LENGTH) + .is_at_most(Constants::NAME_MAX_LENGTH)) end - - it 'should be uniq per project and team' do - first_project = create :project - second_project = build :project, - name: first_project.name, - team: first_project.team - - expect(second_project).to_not be_valid + it do + expect(project).to validate_uniqueness_of(:name).scoped_to(:team_id).case_insensitive end end end diff --git a/spec/models/protocol_spec.rb b/spec/models/protocol_spec.rb index 99d184941..5882f67dd 100644 --- a/spec/models/protocol_spec.rb +++ b/spec/models/protocol_spec.rb @@ -3,6 +3,12 @@ require 'rails_helper' describe Protocol, type: :model do + let(:protocol) { build :protocol } + + it 'is valid' do + expect(protocol).to be_valid + end + it 'should be of class Protocol' do expect(subject.class).to eq Protocol end @@ -40,7 +46,7 @@ describe Protocol, type: :model do it { should have_many :steps } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :team } it { should validate_presence_of :protocol_type } it do @@ -51,6 +57,7 @@ describe Protocol, type: :model do .is_at_most(Constants::RICH_TEXT_MAX_LENGTH) end end + describe '.archive(user)' do let(:protocol) { create :protocol, :in_public_repository, added_by: user } let(:user) { create :user }