forked from rails-on-services/apartment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenant_spec.rb
176 lines (144 loc) · 4.61 KB
/
tenant_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true
require 'spec_helper'
describe Apartment::Tenant do
context 'using mysql', database: :mysql do
before { subject.reload!(config) }
describe '#adapter' do
it 'should load mysql adapter' do
subject.adapter
expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
end
end
# TODO: re-organize these tests
context 'with prefix and schemas' do
describe '#create' do
before do
Apartment.configure do |config|
config.prepend_environment = true
config.use_schemas = true
end
subject.reload!(config)
end
after do
subject.drop 'db_with_prefix'
rescue StandardError => _e
nil
end
it 'should create a new database' do
subject.create 'db_with_prefix'
end
end
end
end
context 'using postgresql', database: :postgresql do
before do
Apartment.use_schemas = true
subject.reload!(config)
end
describe '#adapter' do
it 'should load postgresql adapter' do
expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
end
it 'raises exception with invalid adapter specified' do
subject.reload!(config.merge(adapter: 'unknown'))
expect do
Apartment::Tenant.adapter
end.to raise_error(RuntimeError)
end
context 'threadsafety' do
before { subject.create db1 }
after { subject.drop db1 }
it 'has a threadsafe adapter' do
subject.switch!(db1)
thread = Thread.new { expect(subject.current).to eq(subject.adapter.default_tenant) }
thread.join
expect(subject.current).to eq(db1)
end
end
end
# TODO: above spec are also with use_schemas=true
context 'with schemas' do
before do
Apartment.configure do |config|
config.excluded_models = []
config.use_schemas = true
config.seed_after_create = true
end
subject.create db1
end
after { subject.drop db1 }
describe '#create' do
it 'should seed data' do
subject.switch! db1
expect(User.count).to be > 0
end
end
describe '#switch!' do
let(:x) { rand(3) }
context 'creating models' do
before { subject.create db2 }
after { subject.drop db2 }
it 'should create a model instance in the current schema' do
subject.switch! db2
db2_count = User.count + x.times { User.create }
subject.switch! db1
db_count = User.count + x.times { User.create }
subject.switch! db2
expect(User.count).to eq(db2_count)
subject.switch! db1
expect(User.count).to eq(db_count)
end
end
context 'with excluded models' do
before do
Apartment.configure do |config|
config.excluded_models = ['Company']
end
subject.init
end
after do
# Apartment::Tenant.init creates per model connection.
# Remove the connection after testing not to unintentionally keep the connection across tests.
Apartment.excluded_models.each do |excluded_model|
excluded_model.constantize.remove_connection
end
end
it 'should create excluded models in public schema' do
subject.reset # ensure we're on public schema
count = Company.count + x.times { Company.create }
subject.switch! db1
x.times { Company.create }
expect(Company.count).to eq(count + x)
subject.reset
expect(Company.count).to eq(count + x)
end
end
end
end
context 'seed paths' do
before do
Apartment.configure do |config|
config.excluded_models = []
config.use_schemas = true
config.seed_after_create = true
end
end
after { subject.drop db1 }
it 'should seed from default path' do
subject.create db1
subject.switch! db1
expect(User.count).to eq(3)
expect(User.first.name).to eq('Some User 0')
end
it 'should seed from custom path' do
Apartment.configure do |config|
config.seed_data_file = Rails.root.join('db/seeds/import.rb')
end
subject.create db1
subject.switch! db1
expect(User.count).to eq(6)
expect(User.first.name).to eq('Different User 0')
end
end
end
end