ros-apartment 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/.rubocop-linter.yml +22 -0
- data/.pryrc +5 -3
- data/.rubocop.yml +17 -0
- data/.rubocop_todo.yml +29 -0
- data/.story_branch.yml +4 -0
- data/.travis.yml +12 -11
- data/Appraisals +18 -18
- data/Gemfile +4 -1
- data/README.md +33 -5
- data/Rakefile +2 -0
- data/apartment.gemspec +6 -6
- data/gemfiles/rails_4_2.gemfile +12 -10
- data/gemfiles/rails_5_0.gemfile +11 -9
- data/gemfiles/rails_5_1.gemfile +11 -9
- data/gemfiles/rails_5_2.gemfile +10 -8
- data/gemfiles/rails_6_0.gemfile +11 -9
- data/gemfiles/rails_master.gemfile +11 -9
- data/lib/apartment/adapters/abstract_adapter.rb +34 -34
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +4 -3
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +13 -11
- data/lib/apartment/adapters/mysql2_adapter.rb +10 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +28 -25
- data/lib/apartment/adapters/sqlite3_adapter.rb +16 -8
- data/lib/apartment/console.rb +28 -3
- data/lib/apartment/custom_console.rb +26 -0
- data/lib/apartment/deprecation.rb +2 -1
- data/lib/apartment/elevators/domain.rb +4 -3
- data/lib/apartment/elevators/first_subdomain.rb +3 -2
- data/lib/apartment/elevators/generic.rb +4 -3
- data/lib/apartment/elevators/host.rb +6 -1
- data/lib/apartment/elevators/host_hash.rb +6 -2
- data/lib/apartment/elevators/subdomain.rb +9 -5
- data/lib/apartment/migrator.rb +4 -3
- data/lib/apartment/railtie.rb +13 -5
- data/lib/apartment/reloader.rb +2 -1
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tenant.rb +3 -4
- data/lib/apartment/version.rb +3 -1
- data/lib/apartment.rb +15 -9
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +3 -2
- data/lib/tasks/apartment.rake +19 -18
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +5 -4
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +10 -12
- data/spec/adapters/mysql2_adapter_spec.rb +15 -13
- data/spec/adapters/postgresql_adapter_spec.rb +22 -20
- data/spec/adapters/sqlite3_adapter_spec.rb +41 -23
- data/spec/apartment_spec.rb +4 -2
- data/spec/dummy/app/controllers/application_controller.rb +4 -3
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/application_record.rb +6 -0
- data/spec/dummy/app/models/company.rb +4 -2
- data/spec/dummy/app/models/user.rb +4 -2
- data/spec/dummy/config/application.rb +11 -9
- data/spec/dummy/config/boot.rb +4 -2
- data/spec/dummy/config/environment.rb +3 -1
- data/spec/dummy/config/environments/development.rb +2 -1
- data/spec/dummy/config/environments/production.rb +3 -1
- data/spec/dummy/config/environments/test.rb +3 -1
- data/spec/dummy/config/initializers/apartment.rb +4 -2
- data/spec/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy/config/initializers/secret_token.rb +2 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -1
- data/spec/dummy/config/routes.rb +3 -1
- data/spec/dummy/config.ru +3 -1
- data/spec/dummy/db/seeds/import.rb +3 -1
- data/spec/dummy/script/rails +4 -2
- data/spec/dummy_engine/config/initializers/apartment.rb +4 -3
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +2 -0
- data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -1
- data/spec/dummy_engine/lib/dummy_engine.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/application.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/boot.rb +4 -2
- data/spec/dummy_engine/test/dummy/config/environment.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -1
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +2 -0
- data/spec/dummy_engine/test/dummy/config/routes.rb +2 -0
- data/spec/dummy_engine/test/dummy/config.ru +3 -1
- data/spec/examples/connection_adapter_examples.rb +15 -13
- data/spec/examples/generic_adapter_custom_configuration_example.rb +21 -23
- data/spec/examples/generic_adapter_examples.rb +40 -39
- data/spec/examples/schema_adapter_examples.rb +93 -88
- data/spec/integration/apartment_rake_integration_spec.rb +24 -24
- data/spec/integration/query_caching_spec.rb +10 -8
- data/spec/integration/use_within_an_engine_spec.rb +5 -5
- data/spec/schemas/v1.rb +2 -3
- data/spec/schemas/v2.rb +22 -24
- data/spec/schemas/v3.rb +26 -28
- data/spec/spec_helper.rb +8 -6
- data/spec/support/apartment_helpers.rb +9 -5
- data/spec/support/capybara_sessions.rb +5 -5
- data/spec/support/config.rb +5 -2
- data/spec/support/contexts.rb +10 -8
- data/spec/support/requirements.rb +19 -6
- data/spec/support/setup.rb +5 -5
- data/spec/tasks/apartment_rake_spec.rb +30 -35
- data/spec/tenant_spec.rb +48 -44
- data/spec/unit/config_spec.rb +26 -27
- data/spec/unit/elevators/domain_spec.rb +9 -8
- data/spec/unit/elevators/first_subdomain_spec.rb +14 -12
- data/spec/unit/elevators/generic_spec.rb +16 -15
- data/spec/unit/elevators/host_hash_spec.rb +9 -8
- data/spec/unit/elevators/host_spec.rb +26 -26
- data/spec/unit/elevators/subdomain_spec.rb +24 -23
- data/spec/unit/migrator_spec.rb +18 -17
- data/spec/unit/reloader_spec.rb +8 -8
- metadata +40 -22
@@ -1,8 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'rake'
|
3
5
|
|
4
|
-
describe
|
5
|
-
|
6
|
+
describe 'apartment rake tasks', database: :postgresql do
|
6
7
|
before do
|
7
8
|
@rake = Rake::Application.new
|
8
9
|
Rake.application = @rake
|
@@ -18,8 +19,8 @@ describe "apartment rake tasks", database: :postgresql do
|
|
18
19
|
|
19
20
|
Apartment.configure do |config|
|
20
21
|
config.use_schemas = true
|
21
|
-
config.excluded_models = [
|
22
|
-
config.tenant_names =
|
22
|
+
config.excluded_models = ['Company']
|
23
|
+
config.tenant_names = -> { Company.pluck(:database) }
|
23
24
|
end
|
24
25
|
Apartment::Tenant.reload!(config)
|
25
26
|
|
@@ -29,40 +30,39 @@ describe "apartment rake tasks", database: :postgresql do
|
|
29
30
|
|
30
31
|
after { Rake.application = nil }
|
31
32
|
|
32
|
-
context
|
33
|
-
|
34
|
-
let(:
|
35
|
-
let(:
|
36
|
-
let!(:company_count){ db_names.length }
|
33
|
+
context 'with x number of databases' do
|
34
|
+
let(:x) { rand(1..5) } # random number of dbs to create
|
35
|
+
let(:db_names) { x.times.map { Apartment::Test.next_db } }
|
36
|
+
let!(:company_count) { db_names.length }
|
37
37
|
|
38
38
|
before do
|
39
39
|
db_names.collect do |db_name|
|
40
40
|
Apartment::Tenant.create(db_name)
|
41
|
-
Company.create :
|
41
|
+
Company.create database: db_name
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
after do
|
46
|
-
db_names.each{ |db| Apartment::Tenant.drop(db) }
|
46
|
+
db_names.each { |db| Apartment::Tenant.drop(db) }
|
47
47
|
Company.delete_all
|
48
48
|
end
|
49
49
|
|
50
|
-
context
|
50
|
+
context 'with ActiveRecord below 5.2.0' do
|
51
51
|
before do
|
52
|
-
allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w
|
52
|
+
allow(ActiveRecord::Migrator).to receive(:migrations_paths) { %w[spec/dummy/db/migrate] }
|
53
53
|
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { true }
|
54
54
|
end
|
55
55
|
|
56
|
-
describe
|
57
|
-
it
|
56
|
+
describe '#migrate' do
|
57
|
+
it 'should migrate all databases' do
|
58
58
|
expect(ActiveRecord::Migrator).to receive(:migrate).exactly(company_count).times
|
59
59
|
|
60
60
|
@rake['apartment:migrate'].invoke
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
describe
|
65
|
-
it
|
64
|
+
describe '#rollback' do
|
65
|
+
it 'should rollback all dbs' do
|
66
66
|
expect(ActiveRecord::Migrator).to receive(:rollback).exactly(company_count).times
|
67
67
|
|
68
68
|
@rake['apartment:rollback'].invoke
|
@@ -70,15 +70,15 @@ describe "apartment rake tasks", database: :postgresql do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
-
context
|
73
|
+
context 'with ActiveRecord above or equal to 5.2.0' do
|
74
74
|
let(:migration_context_double) { double(:migration_context) }
|
75
75
|
|
76
76
|
before do
|
77
77
|
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
|
78
78
|
end
|
79
79
|
|
80
|
-
describe
|
81
|
-
it
|
80
|
+
describe '#migrate' do
|
81
|
+
it 'should migrate all databases' do
|
82
82
|
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
83
83
|
expect(migration_context_double).to receive(:migrate).exactly(company_count).times
|
84
84
|
|
@@ -86,8 +86,8 @@ describe "apartment rake tasks", database: :postgresql do
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
-
describe
|
90
|
-
it
|
89
|
+
describe '#rollback' do
|
90
|
+
it 'should rollback all dbs' do
|
91
91
|
allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double }
|
92
92
|
expect(migration_context_double).to receive(:rollback).exactly(company_count).times
|
93
93
|
|
@@ -96,8 +96,8 @@ describe "apartment rake tasks", database: :postgresql do
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
-
describe
|
100
|
-
it
|
99
|
+
describe 'apartment:seed' do
|
100
|
+
it 'should seed all databases' do
|
101
101
|
expect(Apartment::Tenant).to receive(:seed).exactly(company_count).times
|
102
102
|
|
103
103
|
@rake['apartment:seed'].invoke
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe 'query caching' do
|
@@ -6,8 +8,8 @@ describe 'query caching' do
|
|
6
8
|
|
7
9
|
before do
|
8
10
|
Apartment.configure do |config|
|
9
|
-
config.excluded_models = [
|
10
|
-
config.tenant_names =
|
11
|
+
config.excluded_models = ['Company']
|
12
|
+
config.tenant_names = -> { Company.pluck(:database) }
|
11
13
|
config.use_schemas = true
|
12
14
|
end
|
13
15
|
|
@@ -20,7 +22,7 @@ describe 'query caching' do
|
|
20
22
|
end
|
21
23
|
|
22
24
|
after do
|
23
|
-
db_names.each{ |db| Apartment::Tenant.drop(db) }
|
25
|
+
db_names.each { |db| Apartment::Tenant.drop(db) }
|
24
26
|
Apartment::Tenant.reset
|
25
27
|
Company.delete_all
|
26
28
|
end
|
@@ -34,10 +36,10 @@ describe 'query caching' do
|
|
34
36
|
ActiveRecord::Base.connection.enable_query_cache!
|
35
37
|
|
36
38
|
Apartment::Tenant.switch! db_names.first
|
37
|
-
expect(User.
|
39
|
+
expect(User.find_by(name: db_names.first).name).to eq(db_names.first)
|
38
40
|
|
39
41
|
Apartment::Tenant.switch! db_names.last
|
40
|
-
expect(User.
|
42
|
+
expect(User.find_by(name: db_names.first)).to be_nil
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
@@ -46,8 +48,8 @@ describe 'query caching' do
|
|
46
48
|
|
47
49
|
before do
|
48
50
|
Apartment.configure do |config|
|
49
|
-
config.excluded_models = [
|
50
|
-
config.tenant_names =
|
51
|
+
config.excluded_models = ['Company']
|
52
|
+
config.tenant_names = -> { Company.pluck(:database) }
|
51
53
|
config.use_schemas = false
|
52
54
|
end
|
53
55
|
|
@@ -66,7 +68,7 @@ describe 'query caching' do
|
|
66
68
|
Company.delete_all
|
67
69
|
end
|
68
70
|
|
69
|
-
it
|
71
|
+
it 'configuration value is kept after switching databases' do
|
70
72
|
ActiveRecord::Base.connection.enable_query_cache!
|
71
73
|
|
72
74
|
Apartment::Tenant.switch! db_name
|
@@ -1,7 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
describe 'using apartment within an engine' do
|
3
4
|
before do
|
4
|
-
engine_path = Pathname.new(File.expand_path('
|
5
|
+
engine_path = Pathname.new(File.expand_path('../dummy_engine', __dir__))
|
5
6
|
require engine_path.join('test/dummy/config/application')
|
6
7
|
@rake = Rake::Application.new
|
7
8
|
Rake.application = @rake
|
@@ -10,11 +11,11 @@ describe 'using apartment within an engine' do
|
|
10
11
|
end
|
11
12
|
|
12
13
|
it 'sucessfully runs rake db:migrate in the engine root' do
|
13
|
-
expect{ Rake::Task['db:migrate'].invoke }.to_not raise_error
|
14
|
+
expect { Rake::Task['db:migrate'].invoke }.to_not raise_error
|
14
15
|
end
|
15
16
|
|
16
17
|
it 'sucessfully runs rake app:db:migrate in the engine root' do
|
17
|
-
expect{ Rake::Task['app:db:migrate'].invoke }.to_not raise_error
|
18
|
+
expect { Rake::Task['app:db:migrate'].invoke }.to_not raise_error
|
18
19
|
end
|
19
20
|
|
20
21
|
context 'when Apartment.db_migrate_tenants is false' do
|
@@ -24,5 +25,4 @@ describe 'using apartment within an engine' do
|
|
24
25
|
Rake::Task['db:migrate'].invoke
|
25
26
|
end
|
26
27
|
end
|
27
|
-
|
28
28
|
end
|
data/spec/schemas/v1.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
# This file is auto-generated from the current state of the database. Instead
|
3
3
|
# of editing this file, please use the migrations feature of Active Record to
|
4
4
|
# incrementally modify your database, and then regenerate this schema definition.
|
@@ -11,6 +11,5 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:
|
15
|
-
|
14
|
+
ActiveRecord::Schema.define(version: 0) do
|
16
15
|
end
|
data/spec/schemas/v2.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
# This file is auto-generated from the current state of the database. Instead
|
3
3
|
# of editing this file, please use the migrations feature of Active Record to
|
4
4
|
# incrementally modify your database, and then regenerate this schema definition.
|
@@ -11,33 +11,31 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:
|
15
|
-
|
16
|
-
|
17
|
-
t.
|
18
|
-
t.string "database"
|
14
|
+
ActiveRecord::Schema.define(version: 20110613152810) do
|
15
|
+
create_table 'companies', force: true do |t|
|
16
|
+
t.boolean 'dummy'
|
17
|
+
t.string 'database'
|
19
18
|
end
|
20
19
|
|
21
|
-
create_table
|
22
|
-
t.integer
|
23
|
-
t.integer
|
24
|
-
t.text
|
25
|
-
t.text
|
26
|
-
t.datetime
|
27
|
-
t.datetime
|
28
|
-
t.datetime
|
29
|
-
t.string
|
30
|
-
t.datetime
|
31
|
-
t.datetime
|
32
|
-
t.string
|
20
|
+
create_table 'delayed_jobs', force: true do |t|
|
21
|
+
t.integer 'priority', default: 0
|
22
|
+
t.integer 'attempts', default: 0
|
23
|
+
t.text 'handler'
|
24
|
+
t.text 'last_error'
|
25
|
+
t.datetime 'run_at'
|
26
|
+
t.datetime 'locked_at'
|
27
|
+
t.datetime 'failed_at'
|
28
|
+
t.string 'locked_by'
|
29
|
+
t.datetime 'created_at'
|
30
|
+
t.datetime 'updated_at'
|
31
|
+
t.string 'queue'
|
33
32
|
end
|
34
33
|
|
35
|
-
add_index
|
34
|
+
add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority'
|
36
35
|
|
37
|
-
create_table
|
38
|
-
t.string
|
39
|
-
t.datetime
|
40
|
-
t.string
|
36
|
+
create_table 'users', force: true do |t|
|
37
|
+
t.string 'name'
|
38
|
+
t.datetime 'birthdate'
|
39
|
+
t.string 'sex'
|
41
40
|
end
|
42
|
-
|
43
41
|
end
|
data/spec/schemas/v3.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
# This file is auto-generated from the current state of the database. Instead
|
3
3
|
# of editing this file, please use the migrations feature of Active Record to
|
4
4
|
# incrementally modify your database, and then regenerate this schema definition.
|
@@ -11,39 +11,37 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:
|
15
|
-
|
16
|
-
|
17
|
-
t.
|
18
|
-
t.
|
19
|
-
t.datetime "published"
|
14
|
+
ActiveRecord::Schema.define(version: 20111202022214) do
|
15
|
+
create_table 'books', force: true do |t|
|
16
|
+
t.string 'name'
|
17
|
+
t.integer 'pages'
|
18
|
+
t.datetime 'published'
|
20
19
|
end
|
21
20
|
|
22
|
-
create_table
|
23
|
-
t.boolean
|
24
|
-
t.string
|
21
|
+
create_table 'companies', force: true do |t|
|
22
|
+
t.boolean 'dummy'
|
23
|
+
t.string 'database'
|
25
24
|
end
|
26
25
|
|
27
|
-
create_table
|
28
|
-
t.integer
|
29
|
-
t.integer
|
30
|
-
t.text
|
31
|
-
t.text
|
32
|
-
t.datetime
|
33
|
-
t.datetime
|
34
|
-
t.datetime
|
35
|
-
t.string
|
36
|
-
t.datetime
|
37
|
-
t.datetime
|
38
|
-
t.string
|
26
|
+
create_table 'delayed_jobs', force: true do |t|
|
27
|
+
t.integer 'priority', default: 0
|
28
|
+
t.integer 'attempts', default: 0
|
29
|
+
t.text 'handler'
|
30
|
+
t.text 'last_error'
|
31
|
+
t.datetime 'run_at'
|
32
|
+
t.datetime 'locked_at'
|
33
|
+
t.datetime 'failed_at'
|
34
|
+
t.string 'locked_by'
|
35
|
+
t.datetime 'created_at'
|
36
|
+
t.datetime 'updated_at'
|
37
|
+
t.string 'queue'
|
39
38
|
end
|
40
39
|
|
41
|
-
add_index
|
40
|
+
add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority'
|
42
41
|
|
43
|
-
create_table
|
44
|
-
t.string
|
45
|
-
t.datetime
|
46
|
-
t.string
|
42
|
+
create_table 'users', force: true do |t|
|
43
|
+
t.string 'name'
|
44
|
+
t.datetime 'birthdate'
|
45
|
+
t.string 'sex'
|
47
46
|
end
|
48
|
-
|
49
47
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
4
|
|
3
5
|
# Configure Rails Environment
|
4
|
-
ENV[
|
6
|
+
ENV['RAILS_ENV'] = 'test'
|
5
7
|
|
6
|
-
require File.expand_path(
|
8
|
+
require File.expand_path('dummy/config/environment.rb', __dir__)
|
7
9
|
|
8
10
|
# Loading dummy applications affects table_name of each excluded models
|
9
11
|
# defined in `spec/dummy/config/initializers/apartment.rb`.
|
@@ -16,19 +18,20 @@ Apartment.excluded_models.each do |model|
|
|
16
18
|
klass.reset_table_name
|
17
19
|
end
|
18
20
|
|
19
|
-
require
|
21
|
+
require 'rspec/rails'
|
20
22
|
require 'capybara/rspec'
|
21
23
|
require 'capybara/rails'
|
22
24
|
|
23
25
|
begin
|
24
26
|
require 'pry'
|
25
|
-
silence_warnings{ IRB = Pry }
|
27
|
+
silence_warnings { IRB = Pry }
|
26
28
|
rescue LoadError
|
29
|
+
nil
|
27
30
|
end
|
28
31
|
|
29
32
|
ActionMailer::Base.delivery_method = :test
|
30
33
|
ActionMailer::Base.perform_deliveries = true
|
31
|
-
ActionMailer::Base.default_url_options[:host] =
|
34
|
+
ActionMailer::Base.default_url_options[:host] = 'test.com'
|
32
35
|
|
33
36
|
Rails.backtrace_cleaner.remove_silencers!
|
34
37
|
|
@@ -36,7 +39,6 @@ Rails.backtrace_cleaner.remove_silencers!
|
|
36
39
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
37
40
|
|
38
41
|
RSpec.configure do |config|
|
39
|
-
|
40
42
|
config.include RSpec::Integration::CapybaraSessions, type: :request
|
41
43
|
config.include Apartment::Spec::Setup
|
42
44
|
|
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
module Test
|
3
|
-
|
5
|
+
# rubocop:disable Style/ModuleFunction
|
4
6
|
extend self
|
7
|
+
# rubocop:enable Style/ModuleFunction
|
5
8
|
|
6
9
|
def reset
|
7
10
|
Apartment.excluded_models = nil
|
@@ -12,11 +15,13 @@ module Apartment
|
|
12
15
|
|
13
16
|
def next_db
|
14
17
|
@x ||= 0
|
15
|
-
|
18
|
+
format('db%<db_idx>d', db_idx: @x += 1)
|
16
19
|
end
|
17
20
|
|
18
21
|
def drop_schema(schema)
|
19
|
-
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE")
|
22
|
+
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE")
|
23
|
+
rescue StandardError => _e
|
24
|
+
true
|
20
25
|
end
|
21
26
|
|
22
27
|
# Use this if you don't want to import schema.rb etc... but need the postgres schema to exist
|
@@ -28,7 +33,7 @@ module Apartment
|
|
28
33
|
def load_schema(version = 3)
|
29
34
|
file = File.expand_path("../../schemas/v#{version}.rb", __FILE__)
|
30
35
|
|
31
|
-
silence_warnings{ load(file) }
|
36
|
+
silence_warnings { load(file) }
|
32
37
|
end
|
33
38
|
|
34
39
|
def migrate
|
@@ -38,6 +43,5 @@ module Apartment
|
|
38
43
|
def rollback
|
39
44
|
ActiveRecord::Migrator.rollback(Rails.root + ActiveRecord::Migrator.migrations_path)
|
40
45
|
end
|
41
|
-
|
42
46
|
end
|
43
47
|
end
|
@@ -1,15 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RSpec
|
2
4
|
module Integration
|
3
5
|
module CapybaraSessions
|
4
|
-
|
5
|
-
def in_new_session(&block)
|
6
|
+
def in_new_session(&_block)
|
6
7
|
yield new_session
|
7
8
|
end
|
8
|
-
|
9
|
+
|
9
10
|
def new_session
|
10
11
|
Capybara::Session.new(Capybara.current_driver, Capybara.app)
|
11
12
|
end
|
12
|
-
|
13
13
|
end
|
14
14
|
end
|
15
|
-
end
|
15
|
+
end
|
data/spec/support/config.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'yaml'
|
2
4
|
|
3
5
|
module Apartment
|
4
6
|
module Test
|
5
|
-
|
6
7
|
def self.config
|
8
|
+
# rubocop:disable Security/YAMLLoad
|
7
9
|
@config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result)
|
10
|
+
# rubocop:enable Security/YAMLLoad
|
8
11
|
end
|
9
12
|
end
|
10
|
-
end
|
13
|
+
end
|
data/spec/support/contexts.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Some shared contexts for specs
|
2
4
|
|
3
|
-
shared_context
|
4
|
-
let(:default_schema){ Apartment::Test.next_db }
|
5
|
+
shared_context 'with default schema', default_schema: true do
|
6
|
+
let(:default_schema) { Apartment::Test.next_db }
|
5
7
|
|
6
8
|
before do
|
7
9
|
Apartment::Test.create_schema(default_schema)
|
@@ -16,7 +18,7 @@ shared_context "with default schema", :default_schema => true do
|
|
16
18
|
end
|
17
19
|
|
18
20
|
# Some default setup for elevator specs
|
19
|
-
shared_context
|
21
|
+
shared_context 'elevators', elevator: true do
|
20
22
|
let(:company1) { mock_model(Company, database: db1).as_null_object }
|
21
23
|
let(:company2) { mock_model(Company, database: db2).as_null_object }
|
22
24
|
|
@@ -37,16 +39,16 @@ shared_context "elevators", elevator: true do
|
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
shared_context
|
41
|
-
let(:persistent_schemas){ [
|
42
|
+
shared_context 'persistent_schemas', persistent_schemas: true do
|
43
|
+
let(:persistent_schemas) { %w[hstore postgis] }
|
42
44
|
|
43
45
|
before do
|
44
|
-
persistent_schemas.map{|schema| subject.create(schema) }
|
46
|
+
persistent_schemas.map { |schema| subject.create(schema) }
|
45
47
|
Apartment.persistent_schemas = persistent_schemas
|
46
48
|
end
|
47
49
|
|
48
50
|
after do
|
49
51
|
Apartment.persistent_schemas = []
|
50
|
-
persistent_schemas.map{|schema| subject.drop(schema) }
|
52
|
+
persistent_schemas.map { |schema| subject.drop(schema) }
|
51
53
|
end
|
52
|
-
end
|
54
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
module Spec
|
3
|
-
|
4
5
|
#
|
5
6
|
# Define the interface methods required to
|
6
7
|
# use an adapter shared example
|
@@ -19,16 +20,28 @@ module Apartment
|
|
19
20
|
# Reset before dropping (can't drop a db you're connected to)
|
20
21
|
subject.reset
|
21
22
|
|
22
|
-
# sometimes we manually drop these schemas in testing, don't care if
|
23
|
-
|
24
|
-
|
23
|
+
# sometimes we manually drop these schemas in testing, don't care if
|
24
|
+
# we can't drop, hence rescue
|
25
|
+
begin
|
26
|
+
subject.drop(db1)
|
27
|
+
rescue StandardError => _e
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
subject.drop(db2)
|
33
|
+
rescue StandardError => _e
|
34
|
+
true
|
35
|
+
end
|
25
36
|
end
|
26
37
|
end
|
27
38
|
|
28
|
-
%w
|
39
|
+
%w[subject tenant_names default_tenant].each do |method|
|
40
|
+
next if defined?(method)
|
41
|
+
|
29
42
|
define_method method do
|
30
43
|
raise "You must define a `#{method}` method in your host group"
|
31
|
-
end
|
44
|
+
end
|
32
45
|
end
|
33
46
|
end
|
34
47
|
end
|
data/spec/support/setup.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Apartment
|
2
4
|
module Spec
|
3
5
|
module Setup
|
4
|
-
|
5
6
|
def self.included(base)
|
6
7
|
base.instance_eval do
|
7
|
-
let(:db1){ Apartment::Test.next_db }
|
8
|
-
let(:db2){ Apartment::Test.next_db }
|
9
|
-
let(:connection){ ActiveRecord::Base.connection }
|
8
|
+
let(:db1) { Apartment::Test.next_db }
|
9
|
+
let(:db2) { Apartment::Test.next_db }
|
10
|
+
let(:connection) { ActiveRecord::Base.connection }
|
10
11
|
|
11
12
|
# This around ensures that we run these hooks before and after
|
12
13
|
# any before/after hooks defined in individual tests
|
13
14
|
# Otherwise these actually get run after test defined hooks
|
14
15
|
around(:each) do |example|
|
15
|
-
|
16
16
|
def config
|
17
17
|
db = RSpec.current_example.metadata.fetch(:database, :postgresql)
|
18
18
|
|