From 2c39743d5d0d13c6cbf744b337370e37361176fd Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 7 Aug 2017 22:05:13 +0900 Subject: [PATCH] Introduce multisite tests for better coverage. --- .gitignore | 1 + config/database.yml | 2 +- lib/site_settings/defaults_provider.rb | 1 + lib/tasks/db.rake | 20 +++++++++- spec/fixtures/multisite/two_dbs.yml | 5 +++ spec/multisite/site_settings_spec.rb | 52 ++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/multisite/two_dbs.yml create mode 100644 spec/multisite/site_settings_spec.rb diff --git a/.gitignore b/.gitignore index 2b372eaa07d..f096ebc684e 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ config/discourse.conf *.sql.gz /db/*.sqlite3 /db/structure.sql +/db/schema.rb # Ignore all logfiles and tempfiles. /log/*.log diff --git a/config/database.yml b/config/database.yml index e9a2a50369b..ff68ec1a859 100644 --- a/config/database.yml +++ b/config/database.yml @@ -18,7 +18,7 @@ development: test: prepared_statements: false adapter: postgresql - database: discourse_test + database: discourse_test<%= ENV['MULTISITE'] ? "_#{ENV['MULTISITE']}" : '' %> min_messages: warning pool: 5 timeout: 5000 diff --git a/lib/site_settings/defaults_provider.rb b/lib/site_settings/defaults_provider.rb index c155a01ef1f..078a62dc043 100644 --- a/lib/site_settings/defaults_provider.rb +++ b/lib/site_settings/defaults_provider.rb @@ -53,6 +53,7 @@ class SiteSettings::DefaultsProvider @defaults.each { |_, hash| hash.delete(name) } @defaults[DEFAULT_LOCALE.to_sym][name] = value value, type = @site_setting.type_supervisor.to_db_value(name, value) + @defaults[self.site_locale.to_sym] ||= {} @defaults[self.site_locale.to_sym][name] = @site_setting.type_supervisor.to_rb_value(name, value, type) else raise ArgumentError.new("No setting named '#{name}' exists") diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 0c88c62c55c..d0f67702c66 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -3,10 +3,28 @@ task 'set_locale' do I18n.locale = (SiteSetting.default_locale || :en) rescue :en end +task 'db:create', [:multisite] => [:load_config] do |_, args| + if Rails.env.test? && !args[:multisite] + system("MULTISITE=multisite rake db:create['true']") + end +end + +task 'db:drop', [:multisite] => [:load_config] do |_, args| + if Rails.env.test? && !args[:multisite] + system("MULTISITE=multisite rake db:drop['true']") + end +end + # we need to run seed_fu every time we run rake db:migrate -task 'db:migrate' => ['environment', 'set_locale'] do +task 'db:migrate', [:multisite] => ['environment', 'set_locale'] do |_, args| SeedFu.seed Jobs::Onceoff.enqueue_all + + if Rails.env.test? && !args[:multisite] + system("rake db:schema:dump") + system("MULTISITE=multisite rake db:schema:load") + system("MULTISITE=multisite rake db:migrate['multisite']") + end end task 'test:prepare' => 'environment' do diff --git a/spec/fixtures/multisite/two_dbs.yml b/spec/fixtures/multisite/two_dbs.yml new file mode 100644 index 00000000000..ae9a5ab5691 --- /dev/null +++ b/spec/fixtures/multisite/two_dbs.yml @@ -0,0 +1,5 @@ +second: + adapter: postgresql + database: discourse_test_multisite + host_names: + - test2.localhost diff --git a/spec/multisite/site_settings_spec.rb b/spec/multisite/site_settings_spec.rb new file mode 100644 index 00000000000..94a2e3c421d --- /dev/null +++ b/spec/multisite/site_settings_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +RSpec.describe 'Multisite SiteSettings' do + let(:conn) { RailsMultisite::ConnectionManagement } + + before do + @original_provider = SiteSetting.provider + SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting) + conn.config_filename = "spec/fixtures/multisite/two_dbs.yml" + conn.load_settings! + end + + after do + ['default', 'second'].each do |db| + conn.with_connection(db) { SiteSetting.destroy_all } + end + + conn.clear_settings! + + [:@@db_spec_cache, :@@host_spec_cache, :@@default_spec].each do |class_variable| + conn.remove_class_variable(class_variable) + end + + SiteSetting.provider = @original_provider + end + + describe '#default_locale' do + it 'should return the right locale' do + conn.with_connection('default') do + expect(SiteSetting.default_locale).to eq('en') + end + + conn.with_connection('second') do + SiteSetting.default_locale = 'zh_TW' + + expect(SiteSetting.default_locale).to eq('zh_TW') + end + + conn.with_connection('default') do + expect(SiteSetting.default_locale).to eq('en') + + SiteSetting.default_locale = 'ja' + + expect(SiteSetting.default_locale).to eq('ja') + end + + conn.with_connection('second') do + expect(SiteSetting.default_locale).to eq('zh_TW') + end + end + end +end