Skip to content

Commit

Permalink
Added unit tests for Settings and UserSettingsStore
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Mar 22, 2024
1 parent 9c6a456 commit 1ea485c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
4 changes: 0 additions & 4 deletions apps/dashboard/app/models/batch_connect/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def app
@app ||= BatchConnect::App.from_token token
end

def to_h
values.clone
end

def outdated?
outdated = false
# CHECK IF THERE ARE NEW ATTRIBUTES NOT IN THE VALUES HASH
Expand Down
85 changes: 78 additions & 7 deletions apps/dashboard/test/models/concerns/user_setting_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ def setup
end

test 'update_user_settings should create data directory when is not available' do
Dir.mktmpdir do |temp_data_dir|
data_root = Pathname.new(temp_data_dir).join('update_test')
with_user_settings_file('update_test') do
data_root = Pathname.new(File.dirname(Configuration.user_settings_file))
assert_equal false, data_root.exist?

Configuration.stubs(:user_settings_file).returns("#{data_root.to_s}/settings.yml")

update_user_settings({})

assert_equal true, data_root.exist?
end
end

test 'update_user_settings should update internal data and user settings file' do
Dir.mktmpdir do |temp_data_dir|
Configuration.stubs(:user_settings_file).returns("#{temp_data_dir}/settings.yml")

with_user_settings_file do
settings = user_settings
settings[:profile] = 'profile_value'
update_user_settings(settings)
Expand All @@ -58,4 +54,79 @@ def setup
end
end

test 'all_bc_templates returns empty hash when there is no data' do
with_user_settings_file do
assert_equal({}, all_bc_templates)
end
end

test 'bc_templates returns empty hash when there is no data' do
with_user_settings_file do
assert_equal({}, bc_templates('app/token'))
end
end

test 'bc_templates returns application saved settings' do
with_user_settings_file do
app_token = 'app/token'
values = { name1: 'value1', name2: 'value2' }

save_bc_template(app_token, 'settings1', values)
save_bc_template(app_token, 'settings2', values)
assert_equal({ settings1: values, settings2: values }, bc_templates(app_token))
end
end

test 'save_bc_template save settings with expected structure' do
with_user_settings_file do
app_token = 'app/token'
values = { name1: 'value1', name2: 'value2' }

save_bc_template(app_token, 'settings1', values)
save_bc_template(app_token, 'settings2', values)
assert_equal({ app_token.to_sym => { settings1: values, settings2: values } }, all_bc_templates)
end
end

test 'delete_bc_template deletes settings data' do
with_user_settings_file do
assert_equal({}, all_bc_templates)
app_token = 'app/token'
values = { name1: 'value1', name2: 'value2' }

save_bc_template(app_token, 'settings1', values)
save_bc_template(app_token, 'settings2', values)
assert_equal({ app_token.to_sym => { settings1: values, settings2: values } }, all_bc_templates)

delete_bc_template(app_token, 'settings1')
assert_equal({ app_token.to_sym => { settings2: values } }, all_bc_templates)
end
end

test 'delete_bc_template deletes app entry when empty' do
with_user_settings_file do
assert_equal({}, all_bc_templates)
app_token = 'app/token'
values = { name1: 'value1', name2: 'value2' }

save_bc_template('app/token', 'settings name', values)
assert_equal({ app_token.to_sym => { :'settings name' => values } }, all_bc_templates)

delete_bc_template('app/token', 'settings name')
assert_equal({}, all_bc_templates)
end
end

private

def with_user_settings_file(sub_folder = nil)
Dir.mktmpdir do |temp_data_dir|
path_parts = [temp_data_dir, sub_folder, 'settings.yml'].compact
Configuration.stubs(:user_settings_file).returns(File.join(path_parts))

yield if block_given?

end
end

end

0 comments on commit 1ea485c

Please sign in to comment.