Refactor FactoryBot factories

This commit is contained in:
Urban Rotnik 2019-01-09 18:30:33 +01:00
parent 47cc6a9d10
commit d738169120
7 changed files with 64 additions and 58 deletions

View file

@ -1,26 +1,19 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :experiment do factory :experiment, class: Experiment do
name { Faker::Name.unique.name } transient do
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.' user { create :user }
end end
sequence(:name) { |n| "Experiment-#{n}" }
factory :experiment_one, class: Experiment do description { Faker::Lorem.sentence }
name 'My Experiment One' created_by { user }
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.' last_modified_by { user }
association :project, factory: :project project { create :project, created_by: user }
association :created_by, factory: :user, email: Faker::Internet.email factory :experiment_with_tasks do
association :last_modified_by, after(:create) do |e|
factory: :user, create_list :my_module, 3, experiment: e
email: Faker::Internet.email end
end 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
end end

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :my_module_group do factory :my_module_group do
experiment { Experiment.first || create(:experiment_two) } experiment
end end
end end

View file

@ -1,10 +1,12 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :my_module do factory :my_module do
name 'My first module' sequence(:name) { |n| "Task-#{n}" }
x 0 x { Faker::Number.between(1, 100) }
y 0 y { Faker::Number.between(1, 100) }
workflow_order 0 workflow_order { 0 }
experiment { Experiment.first || create(:experiment_one) } experiment
my_module_group { MyModuleGroup.first || create(:my_module_group) } my_module_group { create :my_module_group, experiment: experiment }
end end
end end

View file

@ -1,10 +1,12 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :project do factory :project do
created_by { User.first || association(:project_user) } sequence(:name) { |n| "My project-#{n}" }
team { Team.first || association(:project_team) } association :created_by, factory: :user
archived false team { create :team, created_by: created_by }
name 'My project' archived { false }
visibility 'hidden' visibility { 'hidden' }
end end
factory :project_user, class: User do factory :project_user, class: User do
@ -14,11 +16,4 @@ FactoryBot.define do
password 'asdf1243' password 'asdf1243'
password_confirmation 'asdf1243' password_confirmation 'asdf1243'
end end
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
end
end end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :team do factory :team do
created_by { User.first || create(:user) } association :created_by, factory: :user
name 'My team' name 'My team'
description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.' description 'Lorem ipsum dolor sit amet, consectetuer adipiscing eli.'
space_taken 1048576 space_taken 1048576

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :user do factory :user do
full_name 'admin' full_name 'admin'
initials 'AD' initials 'AD'
email Faker::Internet.unique.email sequence(:email) { |n| "user-#{n}@example.com" }
password 'asdf1243' password 'asdf1243'
password_confirmation 'asdf1243' password_confirmation 'asdf1243'
current_sign_in_at DateTime.now current_sign_in_at DateTime.now

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
describe Project, type: :model do describe Project, type: :model do
@ -39,22 +41,30 @@ describe Project, type: :model do
it { should have_many :report_elements } it { should have_many :report_elements }
end end
describe 'Should be a valid object' do describe 'Validations' do
let(:user) { create :user } describe '#visibility' do
let(:team) { create :team, created_by: user } it { should validate_presence_of :visibility }
it { should validate_presence_of :visibility }
it { should validate_presence_of :team }
it do
should validate_length_of(:name).is_at_least(Constants::NAME_MIN_LENGTH)
.is_at_most(Constants::NAME_MAX_LENGTH)
end end
it 'should have a unique name scoped to team' do describe '#team' do
create :project, created_by: user, last_modified_by: user, team: team it { should validate_presence_of :team }
project_two = build :project, created_by: user, end
last_modified_by: user,
team: team describe '#name' do
expect(project_two).to_not be_valid 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)
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
end
end end
end end
end end