Merge pull request #5031 from tgxworld/introduce_multisite_tests

Introduce multisite tests for better coverage.
This commit is contained in:
Guo Xiang Tan 2017-08-08 14:13:30 +09:00 committed by GitHub
commit 1f07050d12
6 changed files with 79 additions and 2 deletions

1
.gitignore vendored
View File

@ -36,6 +36,7 @@ config/discourse.conf
*.sql.gz
/db/*.sqlite3
/db/structure.sql
/db/schema.rb
# Ignore all logfiles and tempfiles.
/log/*.log

View File

@ -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

View File

@ -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")

View File

@ -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

5
spec/fixtures/multisite/two_dbs.yml vendored Normal file
View File

@ -0,0 +1,5 @@
second:
adapter: postgresql
database: discourse_test_multisite
host_names:
- test2.localhost

View File

@ -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