Fix failing tests for repository states [SCI-4070]

This commit is contained in:
Oleksii Kriuchykhin 2019-11-19 10:33:51 +01:00 committed by Urban Rotnik
parent 338b710d81
commit 73be1181ac
8 changed files with 156 additions and 196 deletions

View file

@ -41,12 +41,12 @@ class RepositoryColumn < ApplicationRecord
# Calculate old_column_index - this can only be done before # Calculate old_column_index - this can only be done before
# record is deleted when we still have its index # record is deleted when we still have its index
old_column_index = ( old_column_index = (
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:length] + Constants::REPOSITORY_TABLE_DEFAULT_STATE['length'] +
repository.repository_columns repository.repository_columns
.order(id: :asc) .order(id: :asc)
.pluck(:id) .pluck(:id)
.index(id) .index(id)
).to_s )
# Perform the destroy itself # Perform the destroy itself
yield yield

View file

@ -16,13 +16,10 @@ class RepositoryTableStateColumnUpdateService
index = state['columns'].count index = state['columns'].count
# Add new columns, ColReorder, length entries # Add new columns, ColReorder, length entries
state['columns'][index.to_s] = state['columns'][index] = Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE
HashUtil.deep_stringify_keys_and_values( state['ColReorder'] << index
Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE state['length'] = (index + 1)
) state['time'] = Time.new.to_i
state['ColReorder'] << index.to_s
state['length'] = (index + 1).to_s
state['time'] = Time.new.to_i.to_s
table_state.save table_state.save
end end
end end
@ -36,40 +33,27 @@ class RepositoryTableStateColumnUpdateService
).find_each do |table_state| ).find_each do |table_state|
state = table_state.state state = table_state.state
# old_column_index is a String!
# Remove column from ColReorder, columns, length entries # Remove column from ColReorder, columns, length entries
state['columns'].delete(old_column_index) state['columns'].delete_at(old_column_index)
state['columns'].keys.each do |index|
if index.to_i > old_column_index.to_i
state['columns'][(index.to_i - 1).to_s] =
state['columns'].delete(index.to_s)
end
end
state['ColReorder'].delete(old_column_index) state['ColReorder'].delete(old_column_index)
state['ColReorder'].map! do |index| state['ColReorder'].map! do |index|
if index.to_i > old_column_index.to_i if index > old_column_index
(index.to_i - 1).to_s index - 1
else else
index index
end end
end end
state['order'].reject! { |_, v| v[0] == old_column_index } if state.dig('order', 0, 0) == old_column_index
state['order'].each do |k, v|
state['order'][k] = [(v[0].to_i - 1).to_s, v[1]] if v[0].to_i > old_column_index.to_i
end
if state['order'].empty?
# Fallback to default order if user had table ordered by # Fallback to default order if user had table ordered by
# the deleted column # the deleted column
state['order'] = HashUtil.deep_stringify_keys_and_values( state['order'] = Constants::REPOSITORY_TABLE_DEFAULT_STATE['order']
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:order] elsif state.dig('order', 0, 0) > old_column_index
) state['order'][0][0] -= 1
end end
state['length'] = (state['length'].to_i - 1).to_s state['length'] = (state['length'] - 1)
state['time'] = Time.new.to_i.to_s state['time'] = Time.new.to_i
table_state.save table_state.save
end end
end end

View file

@ -25,7 +25,7 @@ class RepositoryTableStateService
def update_state(state) def update_state(state)
saved_state = load_state saved_state = load_state
state[:order][0] = [3, 'asc'] if state.dig(:order, 0, 0).to_i < 2 state['order'][0] = [3, 'asc'] if state.dig('order', 0, 0).to_i < 2
return if saved_state.state.except('time') == state.except('time') return if saved_state.state.except('time') == state.except('time')
@ -46,21 +46,17 @@ class RepositoryTableStateService
private private
def generate_default_state def generate_default_state
default_columns_num = default_columns_num = Constants::REPOSITORY_TABLE_DEFAULT_STATE['length']
Constants::REPOSITORY_TABLE_DEFAULT_STATE[:length]
# This state should be strings-only # This state should be strings-only
state = Constants::REPOSITORY_TABLE_DEFAULT_STATE.with_indifferent_access state = Constants::REPOSITORY_TABLE_DEFAULT_STATE.deep_dup
repository.repository_columns.each_with_index do |_, index| repository.repository_columns.each_with_index do |_, index|
real_index = default_columns_num + index real_index = default_columns_num + index
state['columns'][real_index.to_s] = state['columns'][real_index] = Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE
HashUtil.deep_stringify_keys_and_values( state['ColReorder'] << real_index
Constants::REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE
)
state['ColReorder'] << real_index.to_s
end end
state['length'] = state['columns'].length.to_s state['length'] = state['columns'].length
state['time'] = Time.new.to_i.to_s state['time'] = Time.new.to_i
state state
end end
end end

View file

@ -944,34 +944,34 @@ class Constants
# Repository default table state # Repository default table state
REPOSITORY_TABLE_DEFAULT_STATE = { REPOSITORY_TABLE_DEFAULT_STATE = {
time: 0, 'time' => 0,
start: 0, 'start' => 0,
length: 6, 'length' => 6,
order: [[2, 'asc']], # Default sorting by 'ID' column 'order' => [[2, 'asc']], # Default sorting by 'ID' column
search: { search: '', 'search' => { 'search' => '',
smart: true, 'smart' => true,
regex: false, 'regex' => false,
caseInsensitive: true }, 'caseInsensitive' => true },
columns: {}, 'columns' => [],
assigned: 'assigned', 'assigned' => 'assigned',
ColReorder: [*0..5] 'ColReorder' => [*0..5]
} }
6.times do |i| 6.times do |i|
REPOSITORY_TABLE_DEFAULT_STATE[:columns][i] = { REPOSITORY_TABLE_DEFAULT_STATE['columns'] << {
visible: true, 'visible' => true,
searchable: i >= 1, # Checkboxes column is not searchable 'searchable' => (i >= 1), # Checkboxes column is not searchable
search: { search: '', 'search' => { 'search' => '',
smart: true, 'smart' => true,
regex: false, 'regex' => false,
caseInsensitive: true } 'caseInsensitive' => true }
} }
end end
REPOSITORY_TABLE_DEFAULT_STATE.freeze REPOSITORY_TABLE_DEFAULT_STATE.freeze
# For default custom column template, any searchable default # For default custom column template, any searchable default
# column can be reused # column can be reused
REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE = REPOSITORY_TABLE_STATE_CUSTOM_COLUMN_TEMPLATE =
REPOSITORY_TABLE_DEFAULT_STATE[:columns][1].deep_dup REPOSITORY_TABLE_DEFAULT_STATE['columns'][1].deep_dup
.freeze .freeze
EXPORTABLE_ZIP_EXPIRATION_DAYS = 7 EXPORTABLE_ZIP_EXPIRATION_DAYS = 7

View file

@ -34,15 +34,15 @@ describe RepositoryTableStateColumnUpdateService do
last_modified_by: user_2 last_modified_by: user_2
end end
let!(:default_order) do let!(:default_order) do
{ '0' => %w(2 asc) } [[2, 'asc']]
end end
let!(:default_column_def) do let!(:default_column_def) do
{ 'visible' => 'true', { 'visible' => true,
'searchable' => 'true', 'searchable' => true,
'search' => { 'search' => '', 'search' => { 'search' => '',
'smart' => 'true', 'smart' => true,
'regex' => 'false', 'regex' => false,
'caseInsensitive' => 'true' } } 'caseInsensitive' => true } }
end end
let!(:service) do let!(:service) do
subject subject
@ -70,24 +70,24 @@ describe RepositoryTableStateColumnUpdateService do
end end
it 'should calculate correct length' do it 'should calculate correct length' do
expect(initial_state_1.state['length']).to eq '8' expect(initial_state_1.state['length']).to eq 8
expect(initial_state_2.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)
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
expect(state.state['length']).to eq '10' expect(state.state['length']).to eq 10
end end
end end
it 'should keep order as it was' do it 'should keep order as it was' do
initial_state_1.state['order'] = { '0' => %w(3 desc) } initial_state_1.state['order'] = [[3, 'desc']]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
initial_state_2.state['order'] = { '0' => %w(4 asc) } initial_state_2.state['order'] = [[4, 'asc']]
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
@ -96,20 +96,20 @@ describe RepositoryTableStateColumnUpdateService do
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
state_1 = RepositoryTableStateService.new(user_1, repository).load_state state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['order']).to eq('0' => %w(3 desc)) expect(state_1.state['order']).to eq([[3, 'desc']])
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['order']).to eq('0' => %w(4 asc)) expect(state_2.state['order']).to eq([[4, 'asc']])
end end
it 'should keep search as it was' do it 'should keep search as it was' do
search_1 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false', search_1 = { 'search' => 'lala1', 'smart' => true, 'regex' => false,
'caseInsensitive' => 'true' } 'caseInsensitive' => true }
initial_state_1.state['search'] = search_1 initial_state_1.state['search'] = search_1
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
search_2 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false', search_2 = { 'search' => 'lala1', 'smart' => true, 'regex' => false,
'caseInsensitive' => 'true' } 'caseInsensitive' => true }
initial_state_2.state['search'] = search_2 initial_state_2.state['search'] = search_2
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
@ -133,22 +133,20 @@ describe RepositoryTableStateColumnUpdateService do
state_1 = RepositoryTableStateService.new(user_1, repository).load_state state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['columns']).to eq( expect(state_1.state['columns']).to eq(
cols_1.merge('8' => default_column_def, '9' => default_column_def) cols_1.push(default_column_def, default_column_def)
) )
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['columns']).to eq( expect(state_2.state['columns']).to eq(
cols_2.merge('8' => default_column_def, '9' => default_column_def) cols_2.push(default_column_def, default_column_def)
) )
end end
it 'should keep column order as it was' do it 'should keep column order as it was' do
initial_state_1.state['ColReorder'] = initial_state_1.state['ColReorder'] = [5, 3, 2, 0, 1, 4, 6, 7]
%w(5 3 2 0 1 4 6 7)
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
initial_state_2.state['ColReorder'] = initial_state_2.state['ColReorder'] = [0, 6, 1, 4, 5, 7, 2, 3]
%w(0 6 1 4 5 7 2 3)
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
@ -157,13 +155,9 @@ describe RepositoryTableStateColumnUpdateService do
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
state_1 = RepositoryTableStateService.new(user_1, repository).load_state state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['ColReorder']).to eq( expect(state_1.state['ColReorder']).to eq([5, 3, 2, 0, 1, 4, 6, 7, 8, 9])
%w(5 3 2 0 1 4 6 7 8 9)
)
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['ColReorder']).to eq( expect(state_2.state['ColReorder']).to eq([0, 6, 1, 4, 5, 7, 2, 3, 8, 9])
%w(0 6 1 4 5 7 2 3 8 9)
)
end end
end end
@ -183,8 +177,8 @@ describe RepositoryTableStateColumnUpdateService do
expect(initial_state_1).to be_valid_default_repository_table_state(2) expect(initial_state_1).to be_valid_default_repository_table_state(2)
expect(initial_state_2).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)
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
@ -193,53 +187,53 @@ describe RepositoryTableStateColumnUpdateService do
end end
it 'should calculate correct length' do it 'should calculate correct length' do
expect(initial_state_1.state['length']).to eq '8' expect(initial_state_1.state['length']).to eq 8
expect(initial_state_2.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)
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
[user_1, user_2].each do |user| [user_1, user_2].each do |user|
state = RepositoryTableStateService.new(user, repository).load_state state = RepositoryTableStateService.new(user, repository).load_state
expect(state.state['length']).to eq '6' expect(state.state['length']).to eq 6
end end
end end
it 'should keep order as it was' do it 'should keep order as it was' do
initial_state_1.state['order'] = { '0' => %w(3 desc) } initial_state_1.state['order'] = [[3, 'desc']]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
initial_state_2.state['order'] = { '0' => %w(7 asc) } initial_state_2.state['order'] = [[7, 'asc']]
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
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 state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['order']).to eq('0' => %w(3 desc)) expect(state_1.state['order']).to eq([[3, 'desc']])
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['order']).to eq(default_order) expect(state_2.state['order']).to eq(default_order)
end end
it 'should keep search as it was' do it 'should keep search as it was' do
search_1 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false', search_1 = { 'search' => 'lala1', 'smart' => true, 'regex' => false,
'caseInsensitive' => 'true' } 'caseInsensitive' => true }
initial_state_1.state['search'] = search_1 initial_state_1.state['search'] = search_1
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
search_2 = { 'search' => 'lala1', 'smart' => 'true', 'regex' => 'false', search_2 = { 'search' => 'lala1', 'smart' => true, 'regex' => false,
'caseInsensitive' => 'true' } 'caseInsensitive' => true }
initial_state_2.state['search'] = search_2 initial_state_2.state['search'] = search_2
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
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 state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['search']).to eq search_1 expect(state_1.state['search']).to eq search_1
@ -249,20 +243,23 @@ describe RepositoryTableStateColumnUpdateService do
it 'should keep columns as they were' do it 'should keep columns as they were' do
cols_1 = initial_state_1.state['columns'].deep_dup cols_1 = initial_state_1.state['columns'].deep_dup
cols_1['4']['visible'] = 'false' cols_1[4]['visible'] = false
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state.merge('columns' => cols_1) initial_state_1.state.merge('columns' => cols_1)
) )
cols_1.except!('6', '7') cols_1.delete_at(6)
cols_1.delete_at(6)
cols_2 = initial_state_2.state['columns'].deep_dup cols_2 = initial_state_2.state['columns'].deep_dup
cols_2['4']['searchable'] = 'false' cols_2[4]['searchable'] = false
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state.merge('columns' => cols_2) initial_state_2.state.merge('columns' => cols_2)
) )
cols_2.except!('6', '7') cols_2.delete_at(6)
cols_2.delete_at(6)
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
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 state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['columns']).to eq cols_1 expect(state_1.state['columns']).to eq cols_1
@ -272,26 +269,26 @@ describe RepositoryTableStateColumnUpdateService do
it 'should keep column order as it was' do it 'should keep column order as it was' do
initial_state_1.state['ColReorder'] = initial_state_1.state['ColReorder'] =
%w(5 3 2 0 1 4 6 7) [5, 3, 2, 0, 1, 4, 6, 7]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
initial_state_1.state initial_state_1.state
) )
initial_state_2.state['ColReorder'] = initial_state_2.state['ColReorder'] =
%w(0 6 1 4 5 7 2 3) [0, 6, 1, 4, 5, 7, 2, 3]
RepositoryTableStateService.new(user_2, repository).update_state( RepositoryTableStateService.new(user_2, repository).update_state(
initial_state_2.state initial_state_2.state
) )
service.update_states_with_removed_column(repository, '6') service.update_states_with_removed_column(repository, 6)
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 state_1 = RepositoryTableStateService.new(user_1, repository).load_state
expect(state_1.state['ColReorder']).to eq( expect(state_1.state['ColReorder']).to eq(
%w(5 3 2 0 1 4) [5, 3, 2, 0, 1, 4]
) )
state_2 = RepositoryTableStateService.new(user_2, repository).load_state state_2 = RepositoryTableStateService.new(user_2, repository).load_state
expect(state_2.state['ColReorder']).to eq( expect(state_2.state['ColReorder']).to eq(
%w(0 1 4 5 2 3) [0, 1, 4, 5, 2, 3]
) )
end end
end end
@ -310,12 +307,12 @@ describe RepositoryTableStateColumnUpdateService do
let!(:initial_state) do let!(:initial_state) do
state = RepositoryTableStateService.new(user_1, repository) state = RepositoryTableStateService.new(user_1, repository)
.create_default_state .create_default_state
state.state['order'] = { '0' => %w(8 desc) } state.state['order'] = [[8, 'desc']]
(0..9).each do |idx| (0..9).each do |idx|
state.state['columns'][idx.to_s]['search']['search'] = "search_#{idx}" state.state['columns'][idx]['search']['search'] = "search_#{idx}"
end end
state.state['ColReorder'] = state.state['ColReorder'] =
%w(0 1 2 9 8 4 7 3 5 6) [0, 1, 2, 9, 8, 4, 7, 3, 5, 6]
RepositoryTableStateService.new(user_1, repository).update_state( RepositoryTableStateService.new(user_1, repository).update_state(
state.state state.state
) )
@ -330,33 +327,33 @@ describe RepositoryTableStateColumnUpdateService do
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(5) expect(state).to be_valid_repository_table_state(5)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
%w(0 1 2 9 8 4 7 3 5 6 10) [0, 1, 2, 9, 8, 4, 7, 3, 5, 6, 10]
) )
service.update_states_with_removed_column(repository, '7') service.update_states_with_removed_column(repository, 7)
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(4) expect(state).to be_valid_repository_table_state(4)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
%w(0 1 2 8 7 4 3 5 6 9) [0, 1, 2, 8, 7, 4, 3, 5, 6, 9]
) )
expect(state.state['order']).to eq('0' => %w(7 desc)) expect(state.state['order']).to eq([[7, 'desc']])
service.update_states_with_removed_column(repository, '7') service.update_states_with_removed_column(repository, 7)
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(3) expect(state).to be_valid_repository_table_state(3)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
%w(0 1 2 7 4 3 5 6 8) [0, 1, 2, 7, 4, 3, 5, 6, 8]
) )
expect(state.state['order']).to eq('0' => %w(2 asc)) expect(state.state['order']).to eq([[2, 'asc']])
service.update_states_with_removed_column(repository, '7') service.update_states_with_removed_column(repository, 7)
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(2) expect(state).to be_valid_repository_table_state(2)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
%w(0 1 2 4 3 5 6 7) [0, 1, 2, 4, 3, 5, 6, 7]
) )
service.update_states_with_new_column(repository) service.update_states_with_new_column(repository)
@ -365,7 +362,7 @@ describe RepositoryTableStateColumnUpdateService do
state = RepositoryTableStateService.new(user_1, repository).load_state state = RepositoryTableStateService.new(user_1, repository).load_state
expect(state).to be_valid_repository_table_state(4) expect(state).to be_valid_repository_table_state(4)
expect(state.state['ColReorder']).to eq( expect(state.state['ColReorder']).to eq(
%w(0 1 2 4 3 5 6 7 8 9) [0, 1, 2, 4, 3, 5, 6, 7, 8, 9]
) )
end end
end end

View file

@ -88,7 +88,9 @@ describe RepositoryTableStateService do
describe '#update_state' do describe '#update_state' do
let!(:new_state) do let!(:new_state) do
{ 'my' => 'state' } initial_state = RepositoryTableStateService.new(user, repository).create_default_state
initial_state.state['columns'][3]['visible'] = false
initial_state.state
end end
it 'should update the state' do it 'should update the state' do

View file

@ -8,43 +8,41 @@ RSpec::Matchers.define :be_valid_default_repository_table_state do |nr_of_cols|
state = subject.state state = subject.state
cols_length = 6 + nr_of_cols cols_length = 6 + nr_of_cols
cols_str_array = [*0..(5 + nr_of_cols)].collect(&:to_s) cols_array = [*0..(5 + nr_of_cols)]
expect(state).to be_an_instance_of Hash expect(state).to be_an_instance_of Hash
expect(state).to include( expect(state).to include(
'time', 'time',
'columns', 'columns',
'start' => '0', 'start' => 0,
'length' => cols_length.to_s, # 6 default columns + parameter 'length' => cols_length, # 6 default columns + parameter
'order' => { '0'=> ['2', 'asc'] }, 'order' => [[2, 'asc']],
'search' => { 'search' => {
'search' => '', 'search' => '',
'smart' => 'true', 'smart' => true,
'regex' => 'false', 'regex' => false,
'caseInsensitive' => 'true' 'caseInsensitive' => true
}, },
'ColReorder' => cols_str_array 'assigned' => 'assigned',
'ColReorder' => cols_array
) )
expect(state['columns']).to be_an_instance_of Hash expect(state['columns']).to be_an_instance_of Array
expect(state['columns'].length).to eq (6 + nr_of_cols) expect(state['columns'].length).to eq(6 + nr_of_cols)
expect(state['columns'].keys.sort).to eq cols_str_array state['columns'].each_with_index do |val, i|
state['columns'].each do |key, val|
expect(val).to include( expect(val).to include(
'visible' => 'true', 'visible' => true,
'searchable' => (key == '0' ? 'false' : 'true'), 'searchable' => (i != 0),
'search' => { 'search' => {
'search' => '', 'smart' => 'true', 'regex' => 'false', 'caseInsensitive' => 'true' 'search' => '', 'smart' => true, 'regex' => false, 'caseInsensitive' => true
} }
) )
end end
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 { Time.at(state['time'].to_i) }.to_not raise_error
end end
failure_message do failure_message do
'expected to be valid default repository table state, but was not' 'expected to be valid default repository table state, but was not'
end end
end end

View file

@ -12,85 +12,68 @@ RSpec::Matchers.define :be_valid_repository_table_state do
'time', 'order', 'start', 'length', 'search', 'columns', 'ColReorder' 'time', 'order', 'start', 'length', 'search', 'columns', 'ColReorder'
) )
expect(state['time']).to be_an_instance_of String expect(state['time']).to be_an_instance_of Integer
expect { Integer(state['time']) }.to_not raise_error expect { Time.at(state['time']) }.to_not raise_error
expect { Time.at(state['time'].to_i) }.to_not raise_error
expect(state['start']).to eq "0" expect(state['start']).to eq 0
expect { Integer(state['length']) }.to_not raise_error cols_length = state['length']
cols_length = Integer(state['length'])
# There should always be at least 6 columns # There should always be at least 6 columns
expect(cols_length).to be >= 6 expect(cols_length).to be >= 6
col_indexes = [] col_indexes = []
expect(state['columns']).to be_an_instance_of Hash expect(state['columns']).to be_an_instance_of Array
expect(state['columns'].length).to eq cols_length expect(state['columns'].length).to eq cols_length
state['columns'].each do |key, val| state['columns'].each_with_index do |val, i|
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 be_an_instance_of Hash
expect(val).to include( expect(val).to include(
'search', 'visible', 'searchable' 'search', 'visible', 'searchable'
) )
expect(val['visible']).to eq('true').or eq('false') expect(val['visible']).to eq(true).or eq(false)
expect(val['searchable']).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 be_an_instance_of Hash
expect(val['search']).to include( expect(val['search']).to include(
'regex', 'smart', 'search', 'caseInsensitive' 'regex', 'smart', 'search', 'caseInsensitive'
) )
expect(val['search']['regex']).to eq('true').or eq('false') expect(val['search']['regex']).to eq(true).or eq(false)
expect(val['search']['smart']).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']['caseInsensitive']).to eq(true).or eq(false)
expect(val['search']['search']).to be_an_instance_of String expect(val['search']['search']).to be_an_instance_of String
col_indexes << Integer(key) col_indexes << i
end end
col_indexes.sort! expect(state['order']).to be_an_instance_of Array
# 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 expect(state['order'].length).to be > 0
state['order'].each do |key, val| expect(state['order'][0]).to be_an_instance_of Array
expect(key).to be_an_instance_of String expect(state['order'][0].length).to eq 2
expect { Integer(key) }.to_not raise_error expect(state['order'][0][0]).to be_an_instance_of Integer
expect(state['order'][0][1]).to be_an_instance_of String
expect(state['order'][0][1]).to eq('asc').or eq('desc')
expect(val).to be_an_instance_of Array # Check that the ordering column exists in columns
expect(val.length).to eq 2 expect(col_indexes).to include(state['order'][0][0])
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 be_an_instance_of Hash
expect(state['search']).to include( expect(state['search']).to include(
'regex', 'smart', 'search', 'caseInsensitive' 'regex', 'smart', 'search', 'caseInsensitive'
) )
expect(state['search']['regex']).to eq('true').or eq('false') expect(state['search']['regex']).to eq(true).or eq(false)
expect(state['search']['smart']).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']['caseInsensitive']).to eq(true).or eq(false)
expect(state['search']['search']).to be_an_instance_of String expect(state['search']['search']).to be_an_instance_of String
expect(state['ColReorder']).to be_an_instance_of Array expect(state['ColReorder']).to be_an_instance_of Array
expect(state['ColReorder'].length).to eq cols_length expect(state['ColReorder'].length).to eq cols_length
state['ColReorder'].each do |col|
expect { Integer(col) }.to_not raise_error
state['ColReorder'].each do |col|
# Column should be in the columns indexes # Column should be in the columns indexes
expect(col_indexes).to include (Integer(col)) expect(col_indexes).to include(col)
end end
end end
failure_message do failure_message do
'expected to be valid repository table state, but was not' 'expected to be valid repository table state, but was not'
end end
end end