mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 08:04:03 +08:00
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:
parent
58ec4d04c0
commit
9fdf139235
38
app/services/site_settings_task.rb
Normal file
38
app/services/site_settings_task.rb
Normal 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
|
|
@ -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]}"
|
||||
|
|
25
spec/services/site_settings_spec.rb
Normal file
25
spec/services/site_settings_spec.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user