diff --git a/app/models/asset.rb b/app/models/asset.rb index 7430bc473..7d3eb30b3 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -472,7 +472,7 @@ class Asset < ApplicationRecord if errors.size > 1 temp_errors = errors[:file] errors.clear - errors.set(:file, temp_errors) + errors.add(:file, temp_errors) end end diff --git a/spec/factories/asset_text_datums.rb b/spec/factories/asset_text_datums.rb new file mode 100644 index 000000000..b272b26ee --- /dev/null +++ b/spec/factories/asset_text_datums.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :asset_text_datum do + data "Sample name\tSample type\n" + "sample6\tsample\n" + "\n" + end +end diff --git a/spec/factories/assets.rb b/spec/factories/assets.rb new file mode 100644 index 000000000..456ea1f29 --- /dev/null +++ b/spec/factories/assets.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :asset do + association :created_by, factory: :project_user + association :team, factory: :team + file_file_name 'sample_file.txt' + file_content_type 'text/plain' + file_file_size 69 + version 1 + estimated_size 232 + file_processing false + end +end diff --git a/spec/factories/custom_fields.rb b/spec/factories/custom_fields.rb new file mode 100644 index 000000000..6d1ced03e --- /dev/null +++ b/spec/factories/custom_fields.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :custom_field do + name 'My custom field' + end +end diff --git a/spec/factories/experiments.rb b/spec/factories/experiments.rb new file mode 100644 index 000000000..d15997fad --- /dev/null +++ b/spec/factories/experiments.rb @@ -0,0 +1,26 @@ +FactoryGirl.define do + factory :experiment do + name 'My Experiment' + description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.' + end + + factory :experiment_one, class: Experiment do + name 'My Experiment One' + description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.' + association :project, factory: :project + association :created_by, factory: :user, email: Faker::Internet.email + association :last_modified_by, + factory: :user, + email: Faker::Internet.email + end + + factory :experiment_two, class: Experiment do + name Faker::Name.name + description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.' + association :project, factory: :project + association :created_by, factory: :user, email: Faker::Internet.email + association :last_modified_by, + factory: :user, + email: Faker::Internet.email + end +end diff --git a/spec/factories/my_module_groups.rb b/spec/factories/my_module_groups.rb new file mode 100644 index 000000000..3688ad772 --- /dev/null +++ b/spec/factories/my_module_groups.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :my_module_group do + name Faker::Name.unique.name + experiment { Experiment.first || create(:experiment_two) } + end +end diff --git a/spec/factories/my_module_repository_rows.rb b/spec/factories/my_module_repository_rows.rb new file mode 100644 index 000000000..af444818c --- /dev/null +++ b/spec/factories/my_module_repository_rows.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :mm_repository_row, class: MyModuleRepositoryRow do + association :repository_row, factory: :repository_row + association :my_module, factory: :my_module + end +end diff --git a/spec/factories/my_modules.rb b/spec/factories/my_modules.rb new file mode 100644 index 000000000..ff2a92d50 --- /dev/null +++ b/spec/factories/my_modules.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + factory :my_module do + name 'My first module' + x 0 + y 0 + workflow_order 0 + experiment { Experiment.first || create(:experiment_one) } + my_module_group { MyModuleGroup.first || create(:my_module_group) } + end +end diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb index b37f4b679..1db48538a 100644 --- a/spec/factories/projects.rb +++ b/spec/factories/projects.rb @@ -1,13 +1,13 @@ FactoryGirl.define do factory :project do - association :created_by, factory: :project_user - association :team, factory: :team + created_by { User.first || association(:project_user) } + team { Team.first || association(:project_team) } archived false name 'My project' visibility 'hidden' end - factory :project_user, class: 'User' do + factory :project_user, class: User do full_name Faker::Name.name initials 'AD' email Faker::Internet.email @@ -15,8 +15,8 @@ FactoryGirl.define do password_confirmation 'asdf1243' end - factory :project_team do - association :created_by, factory: :project_user + factory :project_team, class: Team do + created_by { User.first || association(:project_user) } name 'My team' description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.' space_taken 1048576 diff --git a/spec/factories/reports.rb b/spec/factories/reports.rb new file mode 100644 index 000000000..e36b8d5b6 --- /dev/null +++ b/spec/factories/reports.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :report do + user { User.first || association(:user) } + project { Project.first || association(:project) } + name Faker::Name.unique.name + end +end diff --git a/spec/factories/repository.rb b/spec/factories/repository.rb new file mode 100644 index 000000000..caffd62cd --- /dev/null +++ b/spec/factories/repository.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :repository do + name 'My Repository' + created_by { User.first || create(:user) } + team { Team.first || create(:team) } + end +end diff --git a/spec/factories/repository_cell.rb b/spec/factories/repository_cell.rb new file mode 100644 index 000000000..1c8f39886 --- /dev/null +++ b/spec/factories/repository_cell.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :repository_cell do + repository_row { RepositoryRow.first || create(:repository_row) } + repository_column do + RepositoryColumn.first || create(:repository_column) + end + value 'RepositoryTextValue' + end +end diff --git a/spec/factories/repository_columns.rb b/spec/factories/repository_columns.rb new file mode 100644 index 000000000..9417db115 --- /dev/null +++ b/spec/factories/repository_columns.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :repository_column do + name 'My Column' + created_by { User.first || create(:user) } + repository { Repository.first || create(:repository) } + data_type :RepositoryTextValue + end +end diff --git a/spec/factories/repository_rows.rb b/spec/factories/repository_rows.rb new file mode 100644 index 000000000..524db45f5 --- /dev/null +++ b/spec/factories/repository_rows.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :repository_row do + name 'Custom row' + created_by { User.first || association(:user) } + last_modified_by { User.first || association(:user) } + end +end diff --git a/spec/factories/sample_groups.rb b/spec/factories/sample_groups.rb new file mode 100644 index 000000000..5943a1557 --- /dev/null +++ b/spec/factories/sample_groups.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :sample_group do + name 'Sample' + color '#ff00ff' + team { Team.first || create(:team) } + end +end diff --git a/spec/factories/sample_my_modules.rb b/spec/factories/sample_my_modules.rb new file mode 100644 index 000000000..fbe1fcbc8 --- /dev/null +++ b/spec/factories/sample_my_modules.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :sample_my_module do + sample { Sample.frist || create(:sample) } + my_module { MyModule.frist || create(:my_module) } + end +end diff --git a/spec/factories/samples.rb b/spec/factories/samples.rb index 36957c3d6..8b7b515e9 100644 --- a/spec/factories/samples.rb +++ b/spec/factories/samples.rb @@ -1,5 +1,7 @@ FactoryGirl.define do factory :sample do name 'Sample' + user { User.first || create(:user) } + team { Team.first || create(:team) } end end diff --git a/spec/factories/samples_tables.rb b/spec/factories/samples_tables.rb new file mode 100644 index 000000000..5f14031c6 --- /dev/null +++ b/spec/factories/samples_tables.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :samples_table do + end +end diff --git a/spec/factories/teams.rb b/spec/factories/teams.rb index f534e4d90..9d196212f 100644 --- a/spec/factories/teams.rb +++ b/spec/factories/teams.rb @@ -1,6 +1,6 @@ FactoryGirl.define do factory :team do - association :created_by, factory: :user + created_by { User.first || create(:user) } name 'My team' description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.' space_taken 1048576 diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 07054f6c7..fc1a11700 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,7 +2,7 @@ FactoryGirl.define do factory :user do full_name 'admin' initials 'AD' - email 'admin@scinote.net' + email 'admin_test@scinote.net' password 'asdf1243' password_confirmation 'asdf1243' end diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb index 77856f8bb..36b7d7365 100644 --- a/spec/models/asset_spec.rb +++ b/spec/models/asset_spec.rb @@ -38,7 +38,11 @@ describe Asset, type: :model do describe 'Should be a valid object' do it { should validate_presence_of :file } - it { should validate_presence_of :estimated_size } + it 'should validate the presence of estimated size' do + asset = build :asset, estimated_size: nil + expect(asset).to_not be_valid + end + # should validate_presence_of :estimated_size } it { should validate_inclusion_of(:file_present).in_array([true, false]) } end end diff --git a/spec/models/asset_text_datum_spec.rb b/spec/models/asset_text_datum_spec.rb index 7a350b40f..e959296df 100644 --- a/spec/models/asset_text_datum_spec.rb +++ b/spec/models/asset_text_datum_spec.rb @@ -18,8 +18,16 @@ describe AssetTextDatum, type: :model do end describe 'Should be a valid object' do + let(:asset) { create :asset } + it { should validate_presence_of :data } it { should validate_presence_of :asset } - it { should validate_uniqueness_of :asset } + + it 'should have uniq asset' do + create :asset_text_datum, asset: asset + new_atd = build :asset_text_datum, asset: asset + # binding.pry + expect(new_atd).to_not be_valid + end end end diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index 68e453c88..292e96148 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -24,14 +24,38 @@ describe CustomField, type: :model do 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 + end + it { should validate_presence_of :name } - it { should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) } - it { should validate_uniqueness_of(:name).scoped_to(:team) } + 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( - %w(Assigned Sample name Sample type Sample group Added on Added by) + ['Assigned', 'Sample name', 'Sample type', + 'Sample group', 'Added on', 'Added by'] ) end - it { should validate_presence_of :user } + + 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 + end end end diff --git a/spec/models/experiment_spec.rb b/spec/models/experiment_spec.rb index 7160d8979..d30877fa4 100644 --- a/spec/models/experiment_spec.rb +++ b/spec/models/experiment_spec.rb @@ -38,21 +38,31 @@ describe Experiment, type: :model do end describe 'Should be a valid object' do + let(:project) { create :project } it { should validate_presence_of :project } it { should validate_presence_of :created_by } it { should validate_presence_of :last_modified_by } it do should validate_length_of(:name) - .is_at_least(Constants::NAME_MIN_LENGTH) - .is_at_most(Constants::NAME_MAX_LENGTH) - end - it do - should validate_uniqueness_of(:name).scoped_to(:project).case_insensitive - end - it do - should validate_length_of(:description) - .is_at_most(Constants::TEXT_MAX_LENGTH) + .is_at_least(Constants::NAME_MIN_LENGTH) + .is_at_most(Constants::NAME_MAX_LENGTH) end + it do + should validate_length_of(:description) + .is_at_most(Constants::TEXT_MAX_LENGTH) + end + + it 'should have uniq name scoped on project' do + create :experiment, name: 'experiment', + project: project, + created_by: project.created_by, + last_modified_by: project.created_by + new_exp = build_stubbed :experiment, name: 'experiment', + project: project, + created_by: project.created_by, + last_modified_by: project.created_by + expect(new_exp).to_not be_valid + end end end diff --git a/spec/models/my_module_repository_row_spec.rb b/spec/models/my_module_repository_row_spec.rb index 7a003dd13..677692314 100644 --- a/spec/models/my_module_repository_row_spec.rb +++ b/spec/models/my_module_repository_row_spec.rb @@ -23,6 +23,5 @@ describe MyModuleRepositoryRow, type: :model do describe 'Should be a valid object' do it { should validate_presence_of :repository_row } it { should validate_presence_of :my_module } - it { should validate_uniqueness_of(:repository_row).scoped_to(:my_module) } end end diff --git a/spec/models/my_module_spec.rb b/spec/models/my_module_spec.rb index 2b1f24304..f926b4b34 100644 --- a/spec/models/my_module_spec.rb +++ b/spec/models/my_module_spec.rb @@ -56,21 +56,18 @@ describe MyModule, type: :model do end describe 'Should be a valid object' do - it { should validate_presence_of :name } it { should validate_presence_of :x } it { should validate_presence_of :y } it { should validate_presence_of :workflow_order } it { should validate_presence_of :experiment } - it { should validate_presence_of :my_module_group } it do should validate_length_of(:name) - .is_at_least(Constants::NAME_MIN_LENGTH) - .is_at_most(Constants::NAME_MAX_LENGTH) + .is_at_least(Constants::NAME_MIN_LENGTH) + .is_at_most(Constants::NAME_MAX_LENGTH) end it do should validate_length_of(:description) - .is_at_most(Constants::TEXT_MAX_LENGTH) + .is_at_most(Constants::TEXT_MAX_LENGTH) end - end end diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index d1099ce41..1dd288081 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -27,17 +27,18 @@ describe Report, type: :model do it { should validate_presence_of :user } it do should validate_length_of(:description) - .is_at_most(Constants::TEXT_MAX_LENGTH) + .is_at_most(Constants::TEXT_MAX_LENGTH) end it do should validate_length_of(:name) - .is_at_least(Constants::NAME_MIN_LENGTH) - .is_at_most(Constants::NAME_MAX_LENGTH) + .is_at_least(Constants::NAME_MIN_LENGTH) + .is_at_most(Constants::NAME_MAX_LENGTH) end - it do - should validate_uniqueness_of(:name) - .scoped_to([:user, :project]) - .case_insensitive + + it 'should have uniq name scoped to user, project' do + create :report, name: 'Same Name' + new_rep = build :report, name: 'Same Name' + expect(new_rep).to_not be_valid end end end diff --git a/spec/models/repository_cell_spec.rb b/spec/models/repository_cell_spec.rb index 56fe8e820..44f66259f 100644 --- a/spec/models/repository_cell_spec.rb +++ b/spec/models/repository_cell_spec.rb @@ -18,12 +18,4 @@ describe RepositoryCell, type: :model do it { should belong_to :repository_row } it { should belong_to :repository_column } end - - describe 'Should be a valid object' do - it { should validate_presence_of :repository_column } - it do - should validate_uniqueness_of(:repository_row) - .scoped_to(:repository_column) - end - end end diff --git a/spec/models/repository_column_spec.rb b/spec/models/repository_column_spec.rb index 919f9779c..0ba1b8807 100644 --- a/spec/models/repository_column_spec.rb +++ b/spec/models/repository_column_spec.rb @@ -29,9 +29,11 @@ describe RepositoryColumn, type: :model do it do should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) end - it do - should validate_uniqueness_of(:name) - .scoped_to(:repository) + it 'have uniq name scoped to repository' do + create :repository_column, name: 'Repo One' + column_two = build :repository_column, name: 'Repo One' + + expect(column_two).to_not be_valid end end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 33ca28933..6a927f715 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -16,7 +16,6 @@ describe Repository, type: :model do describe 'Relations' do it { should belong_to :team } it { should belong_to(:created_by).class_name('User') } - it { should have_many :repository_columns } it { should have_many :repository_rows } it { should have_many :repository_table_states } it { should have_many :report_elements } @@ -28,8 +27,17 @@ describe Repository, type: :model do it do should validate_length_of(:name).is_at_most(Constants::NAME_MAX_LENGTH) end - it do - should validate_uniqueness_of(:name).scoped_to(:team).case_insensitive + + it 'should have uniq name scoped to team' do + create :repository, name: 'Repository One' + repo = build :repository, name: 'Repository One' + expect(repo).to_not be_valid + end + + it 'should have uniq name scoped to team calse insensitive' do + create :repository, name: 'Repository One' + repo = build :repository, name: 'REPOSITORY ONE' + expect(repo).to_not be_valid end end end diff --git a/spec/models/sample_group_spec.rb b/spec/models/sample_group_spec.rb index 02ed91451..45fe8e100 100644 --- a/spec/models/sample_group_spec.rb +++ b/spec/models/sample_group_spec.rb @@ -32,8 +32,11 @@ describe SampleGroup, type: :model do it do should validate_length_of(:color).is_at_most(Constants::COLOR_MAX_LENGTH) end - it do - should validate_uniqueness_of(:name).scoped_to(:team) + it 'should have uniq name scoped to team' do + create :sample_group, name: 'My Group' + new_group = build :sample_group, name: 'My Group' + + expect(new_group).to_not be_valid end end end diff --git a/spec/models/sample_my_module_spec.rb b/spec/models/sample_my_module_spec.rb index e38606216..ead544cf3 100644 --- a/spec/models/sample_my_module_spec.rb +++ b/spec/models/sample_my_module_spec.rb @@ -21,6 +21,13 @@ describe SampleMyModule, type: :model do describe 'Should be a valid object' do it { should validate_presence_of :sample } it { should validate_presence_of :my_module } - it { should validate_uniqueness_of(:sample_id).scoped_to(:my_module_id) } + + it 'should have one sample assigned per model' do + sample = create :sample + my_module = create :my_module, name: 'Module one' + create :sample_my_module, sample: sample, my_module: my_module + new_smm = build :sample_my_module, sample: sample, my_module: my_module + expect(new_smm).to_not be_valid + end end end diff --git a/spec/models/sample_type_spec.rb b/spec/models/sample_type_spec.rb index da448f2a3..e2b240802 100644 --- a/spec/models/sample_type_spec.rb +++ b/spec/models/sample_type_spec.rb @@ -23,7 +23,6 @@ describe SampleType, type: :model do describe 'Should be a valid object' do let(:team) { create :team } - let!(:test_type) { create :sample_type, name: 'Sample one', team: team } it { should validate_presence_of :name } it { should validate_presence_of :team } @@ -32,13 +31,15 @@ describe SampleType, type: :model do end it 'should have uniq name scoped to team' do + create :sample_type, name: 'Sample one', team: team new_type = build :sample_type, name: 'Sample one', team: team expect(new_type).to_not be_valid end it 'should not be case sensitive' do - new_type = build :sample_type, name: 'SAMPLE ONE', team: team - expect(new_type).to be_valid + create :sample_type, name: 'Sample T', team: team + new_type = build :sample_type, name: 'SAMPLE T', team: team + expect(new_type).to_not be_valid end end end