Add tests for Site Settings import/export

- extracted out site settings rake task to a class
- added tests for import and export of site settings
This commit is contained in:
Blake Erickson 2018-03-23 14:55:17 -06:00
parent 58ec4d04c0
commit 9fdf139235
3 changed files with 70 additions and 37 deletions

View File

@ -0,0 +1,38 @@
class SiteSettingsTask
def self.export_to_hash
site_settings = SiteSetting.all_settings
h = {}
site_settings.each do |site_setting|
h.store(site_setting[:setting].to_s, site_setting[:value])
end
h
end
def self.import(yml)
h = SiteSettingsTask.export_to_hash
counts = { updated: 0, not_found: 0, errors: 0 }
log = []
site_settings = YAML::load(yml)
site_settings.each do |site_setting|
key = site_setting[0]
val = site_setting[1]
if h.has_key?(key)
if val != h[key] #only update if different
begin
result = SiteSetting.set_and_log(key, val)
log << "Changed #{key} FROM: #{result.previous_value} TO: #{result.new_value}"
counts[:updated] += 1
rescue => e
log << "ERROR: #{e.message}"
counts[:errors] += 1
end
end
else
log << "NOT FOUND: existing site setting not found for #{key}"
counts[:not_found] += 1
end
end
return log, counts
end
end

View File

@ -1,16 +1,5 @@
require 'yaml'
class SiteSettingsTask
def self.export_to_hash
site_settings = SiteSetting.all_settings
h = {}
site_settings.each do |site_setting|
h.store(site_setting[:setting].to_s, site_setting[:value])
end
h
end
end
desc "Exports site settings"
task "site_settings:export" => :environment do
h = SiteSettingsTask.export_to_hash
@ -21,40 +10,21 @@ desc "Imports site settings"
task "site_settings:import" => :environment do
yml = (STDIN.tty?) ? '' : STDIN.read
if yml == ''
puts ""
puts
puts "Please specify a settings yml file"
puts "Example: rake site_settings:import < settings.yml"
exit 1
end
puts ""
puts
puts "starting import..."
puts ""
puts
h = SiteSettingsTask.export_to_hash
counts = { updated: 0, not_found: 0, errors: 0 }
log, counts = SiteSettingsTask.import(yml)
site_settings = YAML::load(yml)
site_settings.each do |site_setting|
key = site_setting[0]
val = site_setting[1]
if h.has_key?(key)
if val != h[key] #only update if different
begin
result = SiteSetting.set_and_log(key, val)
puts "Changed #{key} FROM: #{result.previous_value} TO: #{result.new_value}"
counts[:updated] += 1
rescue => e
puts "ERROR: #{e.message}"
counts[:errors] += 1
end
end
else
puts "NOT FOUND: existing site setting not found for #{key}"
counts[:not_found] += 1
end
end
puts ""
puts log
puts
puts "Results:"
puts " Updated: #{counts[:updated]}"
puts " Not Found: #{counts[:not_found]}"

View File

@ -0,0 +1,25 @@
require 'rails_helper'
describe SiteSettingsTask do
before do
Discourse::Application.load_tasks
end
describe 'export' do
it 'creates a hash of all site settings' do
h = SiteSettingsTask.export_to_hash
expect(h.count).to be > 0
end
end
describe 'import' do
it 'updates site settings' do
yml = "title: Test"
log, counts = SiteSettingsTask.import(yml)
expect(log[0]).to eq "Changed title FROM: Discourse TO: Test"
expect(counts[:updated]).to eq 1
expect(SiteSetting.title).to eq "Test"
end
end
end