mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Add repository table state column update service spec
Include both service methods. There remains some additional tests that should be written.
This commit is contained in:
parent
63b7b937bb
commit
b29cde4414
|
@ -1,5 +1,296 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe RepositoryTableStateColumnUpdateService do
|
||||
# TODO
|
||||
let!(:team) { create :team }
|
||||
let!(:user_1) { create :user, email: 'user_one@asdf.com' }
|
||||
let!(:user_2) { create :user, email: 'user_two@asdf.com' }
|
||||
let!(:repository) do
|
||||
create :repository, name: 'my repo',
|
||||
created_by: user_1,
|
||||
team: team
|
||||
end
|
||||
let!(:repository_column_1) do
|
||||
create :repository_column, name: 'My column 1',
|
||||
repository: repository,
|
||||
data_type: :RepositoryTextValue
|
||||
end
|
||||
let!(:repository_column_2) do
|
||||
create :repository_column, name: 'My column 2',
|
||||
repository: repository,
|
||||
data_type: :RepositoryAssetValue
|
||||
end
|
||||
let!(:repository_row) do
|
||||
create :repository_row, name: 'A row',
|
||||
repository: repository,
|
||||
created_by: user_1,
|
||||
last_modified_by: user_1
|
||||
end
|
||||
let!(:repository_row_two) do
|
||||
create :repository_row, name: 'B row',
|
||||
repository: repository,
|
||||
created_by: user_2,
|
||||
last_modified_by: user_2
|
||||
end
|
||||
let!(:default_order) do
|
||||
{ '0' => ['2', 'asc'] }
|
||||
end
|
||||
let!(:default_column_def) do
|
||||
{ 'visible' => 'true',
|
||||
'searchable' => 'true',
|
||||
'search' => { 'search' => '',
|
||||
'smart' => 'true',
|
||||
'regex' => 'false',
|
||||
'caseInsensitive' => 'true' } }
|
||||
end
|
||||
let!(:service) do
|
||||
subject
|
||||
end
|
||||
|
||||
describe '#update_states_with_new_column' do
|
||||
let!(:initial_state_1) do
|
||||
RepositoryTableStateService.new(user_1, repository).create_default_state
|
||||
end
|
||||
let!(:initial_state_2) do
|
||||
RepositoryTableStateService.new(user_2, repository).create_default_state
|
||||
end
|
||||
|
||||
it 'should keep default repository states valid' do
|
||||
expect(initial_state_1).to be_valid_default_repository_table_state(2)
|
||||
expect(initial_state_2).to be_valid_default_repository_table_state(2)
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
[user_1, user_2].each do |user|
|
||||
state = RepositoryTableStateService.new(user, repository).load_state
|
||||
expect(state).to be_valid_default_repository_table_state(4)
|
||||
end
|
||||
end
|
||||
|
||||
it 'should calculate correct length' do
|
||||
expect(initial_state_1.state['length']).to eq '8'
|
||||
expect(initial_state_2.state['length']).to eq '8'
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
[user_1, user_2].each do |user|
|
||||
state = RepositoryTableStateService.new(user, repository).load_state
|
||||
expect(state.state['length']).to eq '10'
|
||||
end
|
||||
end
|
||||
|
||||
it 'should keep order as it was' do
|
||||
initial_state_1.state['order'] = { '0' => ['3', 'desc'] }
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
initial_state_2.state['order'] = { '0' => ['4', 'asc'] }
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['order']).to eq({ '0' => ['3', 'desc'] })
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['order']).to eq({ '0' => ['4', 'asc'] })
|
||||
end
|
||||
|
||||
it 'should keep search as it was' do
|
||||
search_1 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false',
|
||||
'caseInsensitive' => 'true' }
|
||||
initial_state_1.state['search'] = search_1
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
search_2 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false',
|
||||
'caseInsensitive' => 'true' }
|
||||
initial_state_2.state['search'] = search_2
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['search']).to eq search_1
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['search']).to eq search_2
|
||||
end
|
||||
|
||||
it 'should keep columns as they were' do
|
||||
cols_1 = initial_state_1.state['columns'].deep_dup
|
||||
cols_2 = initial_state_2.state['columns'].deep_dup
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['columns']).to eq(
|
||||
cols_1.merge({'8' => default_column_def, '9' => default_column_def})
|
||||
)
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['columns']).to eq(
|
||||
cols_2.merge('8' => default_column_def, '9' => default_column_def)
|
||||
)
|
||||
end
|
||||
|
||||
it 'should keep column order as it was' do
|
||||
initial_state_1.state['ColReorder'] =
|
||||
['5', '3', '2', '0', '1', '4', '6', '7']
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
initial_state_2.state['ColReorder'] =
|
||||
['0', '6', '1', '4', '5', '7', '2', '3']
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_new_column(repository)
|
||||
service.update_states_with_new_column(repository)
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['ColReorder']).to eq(
|
||||
['5', '3', '2', '0', '1', '4', '6', '7', '8', '9']
|
||||
)
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['ColReorder']).to eq(
|
||||
['0', '6', '1', '4', '5', '7', '2', '3', '8', '9']
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update_states_with_removed_column' do
|
||||
let!(:initial_state_1) do
|
||||
RepositoryTableStateService.new(user_1, repository).create_default_state
|
||||
end
|
||||
let!(:initial_state_2) do
|
||||
RepositoryTableStateService.new(user_2, repository).create_default_state
|
||||
end
|
||||
|
||||
# For column removal, we often use the index '6' twice - first, to
|
||||
# remove 6th (out of 7) column, and afterwards, to remove the last,
|
||||
# 6th (out of 6) column
|
||||
|
||||
it 'should keep default repository states valid' do
|
||||
expect(initial_state_1).to be_valid_default_repository_table_state(2)
|
||||
expect(initial_state_2).to be_valid_default_repository_table_state(2)
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
[user_1, user_2].each do |user|
|
||||
state = RepositoryTableStateService.new(user, repository).load_state
|
||||
expect(state).to be_valid_default_repository_table_state(0)
|
||||
end
|
||||
end
|
||||
|
||||
it 'should calculate correct length' do
|
||||
expect(initial_state_1.state['length']).to eq '8'
|
||||
expect(initial_state_2.state['length']).to eq '8'
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
[user_1, user_2].each do |user|
|
||||
state = RepositoryTableStateService.new(user, repository).load_state
|
||||
expect(state.state['length']).to eq '6'
|
||||
end
|
||||
end
|
||||
|
||||
it 'should keep order as it was' do
|
||||
initial_state_1.state['order'] = { '0' => ['3', 'desc'] }
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
initial_state_2.state['order'] = { '0' => ['7', 'asc'] }
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['order']).to eq({ '0' => ['3', 'desc'] })
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['order']).to eq(default_order)
|
||||
end
|
||||
|
||||
it 'should keep search as it was' do
|
||||
search_1 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false',
|
||||
'caseInsensitive' => 'true' }
|
||||
initial_state_1.state['search'] = search_1
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
search_2 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false',
|
||||
'caseInsensitive' => 'true' }
|
||||
initial_state_2.state['search'] = search_2
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['search']).to eq search_1
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['search']).to eq search_2
|
||||
end
|
||||
|
||||
it 'should keep columns as they were' do
|
||||
cols_1 = initial_state_1.state['columns'].deep_dup
|
||||
cols_1['3']['visible'] = 'false'
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state.merge('columns' => cols_1)
|
||||
)
|
||||
cols_1.except!('6', '7')
|
||||
cols_2 = initial_state_2.state['columns'].deep_dup
|
||||
cols_2['4']['searchable'] = 'false'
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state.merge('columns' => cols_2)
|
||||
)
|
||||
cols_2.except!('6', '7')
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['columns']).to eq cols_1
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['columns']).to eq cols_2
|
||||
end
|
||||
|
||||
it 'should keep column order as it was' do
|
||||
initial_state_1.state['ColReorder'] =
|
||||
['5', '3', '2', '0', '1', '4', '6', '7']
|
||||
RepositoryTableStateService.new(user_1, repository).update_state(
|
||||
initial_state_1.state
|
||||
)
|
||||
initial_state_2.state['ColReorder'] =
|
||||
['0', '6', '1', '4', '5', '7', '2', '3']
|
||||
RepositoryTableStateService.new(user_2, repository).update_state(
|
||||
initial_state_2.state
|
||||
)
|
||||
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
service.update_states_with_removed_column(repository, '6')
|
||||
|
||||
state_1 = RepositoryTableStateService.new(user_1, repository).load_state
|
||||
expect(state_1.state['ColReorder']).to eq(
|
||||
['5', '3', '2', '0', '1', '4']
|
||||
)
|
||||
state_2 = RepositoryTableStateService.new(user_2, repository).load_state
|
||||
expect(state_2.state['ColReorder']).to eq(
|
||||
['0', '1', '4', '5', '2', '3']
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
96
spec/support/matchers/be_valid_repository_table_state.rb
Normal file
96
spec/support/matchers/be_valid_repository_table_state.rb
Normal file
|
@ -0,0 +1,96 @@
|
|||
require 'rspec/expectations'
|
||||
|
||||
RSpec::Matchers.define :be_valid_repository_table_state do
|
||||
match do |subject|
|
||||
expect(subject).to be_truthy
|
||||
expect(subject).to be_an_instance_of RepositoryTableState
|
||||
|
||||
state = subject.state
|
||||
|
||||
expect(state).to be_an_instance_of Hash
|
||||
expect(state).to include(
|
||||
'time', 'order', 'start', 'length', 'search', 'columns', 'ColReorder'
|
||||
)
|
||||
|
||||
expect(state['time']).to be_an_instance_of String
|
||||
expect { Integer(state['time']) }.to_not raise_error
|
||||
expect { Time.at(state['time'].to_i) }.to_not raise_error
|
||||
|
||||
expect(state['start']).to eq "0"
|
||||
|
||||
expect { Integer(state['length']) }.to_not raise_error
|
||||
cols_length = Integer(state['length'])
|
||||
|
||||
# There should always be at least 6 columns
|
||||
expect(cols_length).to be >= 6
|
||||
|
||||
col_indexes = []
|
||||
expect(state['columns']).to be_an_instance_of Hash
|
||||
expect(state['columns'].length).to eq cols_length
|
||||
state['columns'].each do |key, val|
|
||||
expect(key).to be_an_instance_of String
|
||||
expect { Integer(key) }.to_not raise_error
|
||||
|
||||
expect(val).to be_an_instance_of Hash
|
||||
expect(val).to include(
|
||||
'search', 'visible', 'searchable'
|
||||
)
|
||||
expect(val['visible']).to eq('true').or eq('false')
|
||||
expect(val['searchable']).to eq('true').or eq('false')
|
||||
expect(val['search']).to be_an_instance_of Hash
|
||||
expect(val['search']).to include(
|
||||
'regex', 'smart', 'search', 'caseInsensitive'
|
||||
)
|
||||
expect(val['search']['regex']).to eq('true').or eq('false')
|
||||
expect(val['search']['smart']).to eq('true').or eq('false')
|
||||
expect(val['search']['caseInsensitive']).to eq('true').or eq('false')
|
||||
expect(val['search']['search']).to be_an_instance_of String
|
||||
|
||||
col_indexes << Integer(key)
|
||||
end
|
||||
|
||||
col_indexes.sort!
|
||||
|
||||
# Check if column indexes are 0..N
|
||||
expect(col_indexes).to eq [*0..(cols_length - 1)]
|
||||
|
||||
expect(state['order']).to be_an_instance_of Hash
|
||||
expect(state['order'].length).to be > 0
|
||||
state['order'].each do |key, val|
|
||||
expect(key).to be_an_instance_of String
|
||||
expect { Integer(key) }.to_not raise_error
|
||||
|
||||
expect(val).to be_an_instance_of Array
|
||||
expect(val.length).to eq 2
|
||||
expect(val[0]).to be_an_instance_of String
|
||||
expect { Integer(val[0]) }.to_not raise_error
|
||||
expect(val[1]).to be_an_instance_of String
|
||||
expect(val[1]).to eq('asc').or eq('desc')
|
||||
|
||||
# Check that the ordering column exists in columns
|
||||
expect(col_indexes).to include(Integer(val[0]))
|
||||
end
|
||||
|
||||
expect(state['search']).to be_an_instance_of Hash
|
||||
expect(state['search']).to include(
|
||||
'regex', 'smart', 'search', 'caseInsensitive'
|
||||
)
|
||||
expect(state['search']['regex']).to eq('true').or eq('false')
|
||||
expect(state['search']['smart']).to eq('true').or eq('false')
|
||||
expect(state['search']['caseInsensitive']).to eq('true').or eq('false')
|
||||
expect(state['search']['search']).to be_an_instance_of String
|
||||
|
||||
expect(state['ColReorder']).to be_an_instance_of Array
|
||||
expect(state['ColReorder'].length).to eq cols_length
|
||||
state['ColReorder'].each do |col|
|
||||
expect { Integer(col) }.to_not raise_error
|
||||
|
||||
# Column should be in the columns indexes
|
||||
expect(col_indexes).to include (Integer(col))
|
||||
end
|
||||
end
|
||||
|
||||
failure_message do
|
||||
'expected to be valid repository table state, but was not'
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue