From 3348b81b142d2efcc8c7b2360ceee4fe71984d62 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 6 May 2019 15:33:33 +0200 Subject: [PATCH] Checklist, checklist_item, activity, experiment spec and factories updated --- app/models/custom_field.rb | 2 +- spec/factories/custom_fields.rb | 4 +++ spec/models/activity_spec.rb | 19 +++++++---- spec/models/checklist_item_spec.rb | 10 +++++- spec/models/checklist_spec.rb | 8 +++++ spec/models/custom_field_spec.rb | 55 +++++++++++++----------------- spec/models/experiment_spec.rb | 6 ++++ 7 files changed, 64 insertions(+), 40 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 184863bb6..1189021cf 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -3,7 +3,7 @@ class CustomField < ApplicationRecord validates :name, presence: true, length: { maximum: Constants::NAME_MAX_LENGTH }, - uniqueness: { scope: :team, case_sensitive: true }, + uniqueness: { scope: :team_id, case_sensitive: true }, exclusion: { in: ['Assigned', 'Sample name', 'Sample type', 'Sample group', 'Added on', 'Added by'] } validates :user, :team, presence: true diff --git a/spec/factories/custom_fields.rb b/spec/factories/custom_fields.rb index 407a2e92f..20f9a0acc 100644 --- a/spec/factories/custom_fields.rb +++ b/spec/factories/custom_fields.rb @@ -1,5 +1,9 @@ +# frozen_string_literal: true + FactoryBot.define do factory :custom_field do name 'My custom field' + user + team end end diff --git a/spec/models/activity_spec.rb b/spec/models/activity_spec.rb index 78e8af52d..cec6f0e2b 100644 --- a/spec/models/activity_spec.rb +++ b/spec/models/activity_spec.rb @@ -3,8 +3,8 @@ require 'rails_helper' describe Activity, type: :model do - subject(:activity) { create :activity } - let(:old_activity) { create :activity, :old } + let(:activity) { build :activity } + let(:old_activity) { build :activity, :old } it 'should be of class Activity' do expect(subject.class).to eq Activity @@ -39,11 +39,16 @@ describe Activity, type: :model do end describe 'Validations' do - it { should validate_presence_of :type_of } - it { should validate_presence_of :owner } + describe '#type_of' do + it { is_expected.to validate_presence_of :type_of } + end - Extends::ACTIVITY_SUBJECT_TYPES.each do |value| - it { is_expected.to allow_values(value).for(:subject_type) } + describe '#owner' do + it { is_expected.to validate_presence_of :owner } + end + + describe '#subject_type' do + it { is_expected.to validate_inclusion_of(:subject_type).in_array(Extends::ACTIVITY_SUBJECT_TYPES) } end end @@ -58,7 +63,7 @@ describe Activity, type: :model do describe '.save' do it 'adds user to message items' do - create :activity + activity.save expect(activity.message_items).to include(user: be_an(Hash)) end diff --git a/spec/models/checklist_item_spec.rb b/spec/models/checklist_item_spec.rb index 62dd392e6..afe132b5b 100644 --- a/spec/models/checklist_item_spec.rb +++ b/spec/models/checklist_item_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe ChecklistItem, type: :model do + let(:checklist_item) { build :checklist_item } + + it 'is valid' do + expect(checklist_item).to be_valid + end + it 'should be of class ChecklistItem' do expect(subject.class).to eq ChecklistItem end @@ -23,7 +31,7 @@ describe ChecklistItem, type: :model do it { should belong_to(:last_modified_by).class_name('User') } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :text } it { should validate_length_of(:text).is_at_most(Constants::TEXT_MAX_LENGTH) } it { should validate_presence_of :checklist } diff --git a/spec/models/checklist_spec.rb b/spec/models/checklist_spec.rb index 61e765e29..dc4c8905e 100644 --- a/spec/models/checklist_spec.rb +++ b/spec/models/checklist_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe Checklist, type: :model do + let(:checklist) { build :checklist } + + it 'is valid' do + expect(checklist).to be_valid + end + it 'should be of class Checklist' do expect(subject.class).to eq Checklist end diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index 292e96148..eb9df1628 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -1,6 +1,13 @@ +# frozen_string_literal: true + require 'rails_helper' describe CustomField, type: :model do + let(:custom_field) { build :custom_field } + + it 'is valid' do + expect(custom_field).to be_valid + end it 'should be of class CustomField' do expect(subject.class).to eq CustomField end @@ -23,39 +30,25 @@ describe CustomField, type: :model do it { should have_many :sample_custom_fields } end - describe 'Should be a valid object' do - before do - @user = create :user, email: 'example_one@adsf.com' - @team = create :team - @samples_table = create :samples_table, user: @user, team: @team + describe 'Validations' do + describe '#name' do + it { is_expected.to validate_presence_of :name } + it { is_expected.to validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) } + it do + is_expected.to validate_exclusion_of(:name).in_array( + ['Assigned', 'Sample name', 'Sample type', 'Sample group', 'Added on', 'Added by'] + ) + end + it do + allow_any_instance_of(CustomField).to receive(:update_samples_table_state) + + expect(custom_field).to validate_uniqueness_of(:name).scoped_to(:team_id) + end + end - it { should validate_presence_of :name } - it { should validate_presence_of :user } - - it do - should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) - end - - it do - should validate_exclusion_of(:name).in_array( - ['Assigned', 'Sample name', 'Sample type', - 'Sample group', 'Added on', 'Added by'] - ) - end - - it 'should have uniq name scoped on team' do - create :custom_field, user: @user, team: @team - custom_field_two = build :custom_field, user: @user, team: @team - - expect(custom_field_two).to_not be_valid - end - - it 'should have uniq case sensitive name' do - build_stubbed :custom_field, name: 'custom one', user: @user, team: @team - cf = build :custom_field, name: 'CUSTOM ONE', user: @user, team: @team - - expect(cf).to be_valid + describe '#user' do + it { is_expected.to validate_presence_of :user } end end end diff --git a/spec/models/experiment_spec.rb b/spec/models/experiment_spec.rb index 8f2c32359..6010c1fe5 100644 --- a/spec/models/experiment_spec.rb +++ b/spec/models/experiment_spec.rb @@ -3,6 +3,12 @@ require 'rails_helper' describe Experiment, type: :model do + let(:experiment) { build :experiment } + + it 'is valid' do + expect(experiment).to be_valid + end + it 'should be of class Experiment' do expect(subject.class).to eq Experiment end