Add factory valid check for User's associations

This commit is contained in:
Urban Rotnik 2019-05-08 15:28:07 +02:00
parent ba340f7bee
commit f19eeba8dc
17 changed files with 112 additions and 28 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class User < ApplicationRecord
include SearchableModel
include SettingsModel

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UserIdentity < ActiveRecord::Base
belongs_to :user
validates :provider, presence: true, uniqueness: { scope: :user_id }

View file

@ -1,13 +1,10 @@
# frozen_string_literal: true
class UserMyModule < ApplicationRecord
validates :user, presence: true, uniqueness: { scope: :my_module }
validates :my_module, presence: true
belongs_to :user, inverse_of: :user_my_modules, touch: true, optional: true
belongs_to :assigned_by,
foreign_key: 'assigned_by_id',
class_name: 'User',
optional: true
belongs_to :my_module, inverse_of: :user_my_modules,
touch: true,
optional: true
belongs_to :user, inverse_of: :user_my_modules, touch: true
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true
belongs_to :my_module, inverse_of: :user_my_modules, touch: true
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UserNotification < ApplicationRecord
include NotificationsHelper

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UserProject < ApplicationRecord
enum role: { owner: 0, normal_user: 1, technician: 2, viewer: 3 }
@ -5,12 +7,9 @@ class UserProject < ApplicationRecord
validates :user, presence: true, uniqueness: { scope: :project }
validates :project, presence: true
belongs_to :user, inverse_of: :user_projects, touch: true, optional: true
belongs_to :assigned_by,
foreign_key: 'assigned_by_id',
class_name: 'User',
optional: true
belongs_to :project, inverse_of: :user_projects, touch: true, optional: true
belongs_to :user, inverse_of: :user_projects, touch: true
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true
belongs_to :project, inverse_of: :user_projects, touch: true
before_destroy :destroy_associations
validates_uniqueness_of :user_id, scope: :project_id

View file

@ -1,16 +1,13 @@
# frozen_string_literal: true
class UserTeam < ApplicationRecord
enum role: { guest: 0, normal_user: 1, admin: 2 }
validates :role, presence: true
validates :user, presence: true
validates :team, presence: true
validates :role, :user, :team, presence: true
belongs_to :user, inverse_of: :user_teams, touch: true, optional: true
belongs_to :assigned_by,
foreign_key: 'assigned_by_id',
class_name: 'User',
optional: true
belongs_to :team, inverse_of: :user_teams, optional: true
belongs_to :user, inverse_of: :user_teams, touch: true
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true
belongs_to :team, inverse_of: :user_teams
before_destroy :destroy_associations
after_create :create_samples_table_state

View file

@ -1,5 +1,8 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user_identity do
user
uid Faker::Crypto.unique.sha1
provider Faker::App.unique.name
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryBot.define do
factory :user_notification do
checked false

View file

@ -2,6 +2,8 @@
FactoryBot.define do
factory :user_team do
user
team
trait :admin do
role 'admin'
end

View file

@ -3,7 +3,7 @@
FactoryBot.define do
factory :view_state do
state {}
user { User.first || create(:user) }
viewable { Team.first || create(:team) }
user
viewable { create :team }
end
end

View file

@ -0,0 +1,31 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserIdentity, type: :model do
let(:user_identity) { build :user_identity }
it 'is valid' do
expect(user_identity).to be_valid
end
it 'should be of class UserIdentity' do
expect(subject.class).to eq UserIdentity
end
describe 'Relations' do
it { is_expected.to belong_to :user }
end
describe 'Validations' do
describe '#uid' do
it { is_expected.to validate_presence_of :uid }
it { expect(user_identity).to validate_uniqueness_of(:uid).scoped_to(:provider) }
end
describe '#provider' do
it { is_expected.to validate_presence_of :provider }
it { expect(user_identity).to validate_uniqueness_of(:provider).scoped_to(:user_id) }
end
end
end

View file

@ -1,6 +1,14 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserMyModule, type: :model do
let(:user_my_module) { build :user_my_module }
it 'is valid' do
expect(user_my_module).to be_valid
end
it 'should be of class UserMyModule' do
expect(subject.class).to eq UserMyModule
end
@ -19,7 +27,7 @@ describe UserMyModule, type: :model do
it { should belong_to(:assigned_by).class_name('User') }
end
describe 'Should be a valid object' do
describe 'Validations' do
it { should validate_presence_of :user }
it { should validate_presence_of :my_module }
end

View file

@ -1,7 +1,14 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserNotification, type: :model do
let(:user) { create :user }
let(:user_notification) { build :user_notification }
it 'is valid' do
expect(user_notification).to be_valid
end
it 'should be of class UserNotification' do
expect(subject.class).to eq UserNotification

View file

@ -1,6 +1,19 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserProject, type: :model do
let(:user_project_without_role) { build :user_project }
let(:user_project) { build :user_project, :owner }
it 'is invalid without role' do
expect(user_project_without_role).not_to be_valid
end
it 'is valid with role' do
expect(user_project).to be_valid
end
it 'should be of class UserProject' do
expect(subject.class).to eq UserProject
end

View file

@ -3,6 +3,11 @@
require 'rails_helper'
describe User, type: :model do
let(:user) { build :user }
it 'is valid' do
expect(user).to be_valid
end
it 'should be of class User' do
expect(subject.class).to eq User
end
@ -120,7 +125,7 @@ describe User, type: :model do
end
end
describe 'Should be a valid object' do
describe 'Validations' do
it { should validate_presence_of :full_name }
it { should validate_presence_of :initials }
it { should validate_presence_of :email }

View file

@ -1,6 +1,14 @@
# frozen_string_literal: true
require 'rails_helper'
describe UserTeam, type: :model do
let(:user_team) { build :user_team }
it 'is valid' do
expect(user_team).to be_valid
end
it 'should be of class UserTeam' do
expect(subject.class).to eq UserTeam
end
@ -20,7 +28,7 @@ describe UserTeam, type: :model do
it { should belong_to(:assigned_by).class_name('User') }
end
describe 'Should be a valid object' do
describe 'Validations' do
it { should validate_presence_of :role }
it { should validate_presence_of :user }
it { should validate_presence_of :team }

View file

@ -3,6 +3,12 @@
require 'rails_helper'
RSpec.describe ViewState, type: :model do
let(:view_state) { build :view_state }
it 'is valid' do
expect(view_state).to be_valid
end
it 'should be of class ViewState' do
expect(subject.class).to eq ViewState
end