scinote-web/spec/models/repository_list_item_spec.rb

68 lines
2.3 KiB
Ruby
Raw Normal View History

require 'rails_helper'
RSpec.describe RepositoryListItem, type: :model do
it 'should be of class RepositoryListItem' do
expect(subject.class).to eq RepositoryListItem
end
describe 'Database table' do
2018-02-09 23:28:21 +08:00
it { should have_db_column :data }
2018-02-09 21:11:56 +08:00
it { should have_db_column :repository_id }
2018-02-09 23:28:21 +08:00
it { should have_db_column :created_by_id }
it { should have_db_column :last_modified_by_id }
2018-02-12 16:05:41 +08:00
it { should have_db_column :repository_column_id }
end
describe 'Relations' do
2018-02-12 16:05:41 +08:00
it { should have_many :repository_list_values }
it { should belong_to :repository }
it { should belong_to :repository_column }
it { should belong_to(:created_by).class_name('User') }
it { should belong_to(:last_modified_by).class_name('User') }
end
describe 'Validations' do
let!(:user) { create :user }
let!(:repository_one) { create :repository }
2018-02-09 23:28:21 +08:00
it { should validate_presence_of(:data) }
it do
2018-02-09 23:28:21 +08:00
should validate_length_of(:data).is_at_most(Constants::TEXT_MAX_LENGTH)
end
2018-02-09 21:11:56 +08:00
2018-02-12 16:09:56 +08:00
context 'has a uniq data scoped on repository column' do
2018-02-12 16:05:41 +08:00
let!(:repository_column) do
create :repository_column, name: 'My column', repository: repository_one
end
2018-02-09 21:11:56 +08:00
let!(:repository_two) { create :repository, name: 'New repo' }
2018-02-12 16:05:41 +08:00
let!(:repository_column_two) do
create :repository_column, name: 'My column', repository: repository_two
end
2018-02-09 21:11:56 +08:00
let!(:repository_list_item) do
2018-02-12 16:05:41 +08:00
create :repository_list_item,
data: 'Test',
repository: repository_one,
repository_column: repository_column
2018-02-09 21:11:56 +08:00
end
it 'creates a repository list item in same repository' do
new_item = build :repository_list_item,
2018-02-09 23:28:21 +08:00
data: 'Test',
2018-02-12 16:05:41 +08:00
repository: repository_one,
repository_column: repository_column
2018-02-09 21:11:56 +08:00
expect(new_item).to_not be_valid
expect(
new_item.errors.full_messages.first
2018-02-09 23:28:21 +08:00
).to eq 'Data has already been taken'
2018-02-09 21:11:56 +08:00
end
it 'create a repository list item in other repository' do
new_item = build :repository_list_item,
2018-02-09 23:28:21 +08:00
data: 'Test',
2018-02-12 16:05:41 +08:00
repository: repository_two,
repository_column: repository_column_two
2018-02-09 21:11:56 +08:00
expect(new_item).to be_valid
end
end
end
end