active_tenant 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,218 +1,250 @@
1
- require 'spec_helper'
2
-
3
- ActiveTenant::Base::ADAPTERS.keys.each do |adapter_name|
4
- adapter = ActiveTenant::Base::ADAPTERS[adapter_name]
5
-
6
- describe adapter do
7
-
8
- before :all do
9
- AdapterTestHelper.before_all adapter_name
10
- end
11
-
12
- after :all do
13
- AdapterTestHelper.after_all adapter_name
14
- end
15
-
16
- before :each do
17
- AdapterTestHelper.before_each adapter_name
18
- end
19
-
20
- after :each do
21
- AdapterTestHelper.after_each adapter_name
22
- end
23
-
24
- context 'Specific adapter' do
25
-
26
- let(:tenant_adapter) { adapter.new }
27
-
28
- it 'List all tenants' do
29
- tenants = tenant_adapter.all
30
-
31
- tenants.should be_a Array
32
- tenants.should have(:no).items
33
- end
34
-
35
- it 'Create a new tenant' do
36
- tenant_adapter.create 'new_tenant'
37
- tenants = tenant_adapter.all
38
-
39
- tenants.should be_a Array
40
- tenants.should have(1).items
41
- tenants.should include 'new_tenant'
42
- end
43
-
44
- it 'Remove an existing tenant' do
45
- tenant_adapter.create 'tenant_to_remove'
46
-
47
- tenant_adapter.all.should include 'tenant_to_remove'
48
-
49
- tenant_adapter.remove 'tenant_to_remove'
50
-
51
- tenant_adapter.all.should_not include 'tenant_to_remove'
52
- end
53
-
54
- it 'Evaluate a block into a tenant' do
55
- tenant_adapter.name.should eq tenant_adapter.global
56
-
57
- tenant_adapter.create 'dummy'
58
-
59
- tenant_adapter.name.should eq tenant_adapter.global
60
-
61
- tenant_adapter.with 'dummy' do
62
- tenant_adapter.name.should eq 'dummy'
63
- end
64
-
65
- tenant_adapter.name.should eq tenant_adapter.global
66
-
67
- tenant_adapter.remove 'dummy'
68
-
69
- tenant_adapter.name.should eq tenant_adapter.global
70
- end
71
-
72
- end
73
-
74
- context 'Global adapter' do
75
-
76
- it 'Adapter operations' do
77
- ActiveTenant.current.create 'dummy'
78
- ActiveTenant.current.all.should include 'dummy'
79
-
80
- ActiveTenant.current.name.should eq ActiveTenant.current.global
81
-
82
- ActiveTenant.current.with 'dummy' do
83
- ActiveTenant.current.name.should eq 'dummy'
84
- end
85
-
86
- ActiveTenant.current.name.should eq ActiveTenant.current.global
87
-
88
- ActiveTenant.current.remove 'dummy'
89
- ActiveTenant.current.all.should_not include 'dummy'
90
- end
91
-
92
- it 'Migrate global' do
93
- ActiveTenant.current.create 'dummy'
94
-
95
- ActiveTenant.current.migrate_global
96
-
97
- ActiveRecord::Base.connection.table_exists?('globals').should be_true
98
- ActiveRecord::Base.connection.table_exists?('tenants').should be_false
99
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
100
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
101
-
102
- ActiveTenant.current.remove 'dummy'
103
- end
104
-
105
- it 'Migrate one tenant' do
106
- ActiveTenant.current.create 'dummy_1'
107
- ActiveTenant.current.create 'dummy_2'
108
-
109
- ActiveTenant.current.migrate 'dummy_1'
110
-
111
- ActiveTenant.current.with 'dummy_1' do
112
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
113
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
114
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
115
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
116
- end
117
-
118
- ActiveTenant.current.with 'dummy_2' do
119
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
120
- ActiveRecord::Base.connection.table_exists?('tenants').should be_false
121
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
122
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
123
- end
124
-
125
- ActiveTenant.current.remove 'dummy_1'
126
- ActiveTenant.current.remove 'dummy_2'
127
- end
128
-
129
- it 'Migrate all tenants' do
130
- ActiveTenant.current.create 'dummy_1'
131
- ActiveTenant.current.create 'dummy_2'
132
-
133
- ActiveTenant.current.migrate_all
134
-
135
- ActiveTenant.current.with 'dummy_1' do
136
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
137
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
138
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
139
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
140
- end
141
-
142
- ActiveTenant.current.with 'dummy_2' do
143
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
144
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
145
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
146
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
147
- end
148
-
149
- ActiveTenant.current.remove 'dummy_1'
150
- ActiveTenant.current.remove 'dummy_2'
151
- end
152
-
153
- it 'Migrate custom tenant' do
154
- ActiveTenant.current.create 'custom'
155
-
156
- ActiveTenant.current.migrate 'custom'
157
-
158
- ActiveTenant.current.with 'custom' do
159
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
160
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
161
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
162
- ActiveRecord::Base.connection.table_exists?('customs').should be_true
163
- end
164
-
165
- ActiveTenant.current.remove 'custom'
166
- end
167
-
168
- it 'Migrate to specific version' do
169
- ActiveTenant.current.create 'dummy'
170
-
171
- ActiveTenant.current.migrate_all 20120823132854
172
-
173
- ActiveTenant.current.with 'dummy' do
174
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
175
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
176
- end
177
-
178
- ActiveTenant.current.remove 'dummy'
179
- end
180
-
181
- end
182
-
183
- context 'ActiveRecord extensions' do
184
-
185
- it 'Create, migrate and remove' do
186
- ActiveRecord::Base.tenant?.should be_false
187
- ActiveRecord::Base.tenant_name.should be_nil
188
-
189
- ActiveRecord::Base.create_tenant 'dummy'
190
-
191
- ActiveRecord::Migration.migrate_all
192
-
193
- ActiveRecord::Base.connection.table_exists?('globals').should be_true
194
- ActiveRecord::Base.connection.table_exists?('tenants').should be_false
195
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
196
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
197
-
198
- ActiveRecord::Base.with_tenant 'dummy' do
199
- ActiveRecord::Base.tenant?.should be_true
200
- ActiveRecord::Base.tenant_name.should eq 'dummy'
201
-
202
- ActiveRecord::Base.connection.table_exists?('globals').should be_false
203
- ActiveRecord::Base.connection.table_exists?('tenants').should be_true
204
- ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
205
- ActiveRecord::Base.connection.table_exists?('customs').should be_false
206
- end
207
-
208
- ActiveRecord::Base.remove_tenant 'dummy'
209
-
210
- ActiveRecord::Base.tenant?.should be_false
211
- ActiveRecord::Base.tenant_name.should be_nil
212
- end
213
-
214
- end
215
-
216
- end
217
-
1
+ require 'spec_helper'
2
+
3
+ ActiveTenant::Base::ADAPTERS.each do |adapter_name, adapter_class|
4
+
5
+ describe adapter_class do
6
+
7
+ before :all do
8
+ AdapterTestHelper.before_all adapter_name
9
+ end
10
+
11
+ after :all do
12
+ AdapterTestHelper.after_all adapter_name
13
+ end
14
+
15
+ before :each do
16
+ AdapterTestHelper.before_each adapter_name
17
+ end
18
+
19
+ after :each do
20
+ AdapterTestHelper.after_each adapter_name
21
+ end
22
+
23
+ context 'Specific adapter' do
24
+
25
+ let(:tenant_adapter) { adapter_class.new }
26
+
27
+ it 'List all tenants' do
28
+ tenants = tenant_adapter.all
29
+
30
+ tenants.should be_a Array
31
+ tenants.should have(:no).items
32
+ end
33
+
34
+ it 'Create a new tenant' do
35
+ tenant_adapter.create 'new_tenant'
36
+ tenants = tenant_adapter.all
37
+
38
+ tenants.should be_a Array
39
+ tenants.should have(1).items
40
+ tenants.should include 'new_tenant'
41
+ end
42
+
43
+ it 'Remove an existing tenant' do
44
+ tenant_adapter.create 'tenant_to_remove'
45
+
46
+ tenant_adapter.all.should include 'tenant_to_remove'
47
+
48
+ tenant_adapter.remove 'tenant_to_remove'
49
+
50
+ tenant_adapter.all.should_not include 'tenant_to_remove'
51
+ end
52
+
53
+ it 'Evaluate a block into a tenant' do
54
+ tenant_adapter.name.should eq tenant_adapter.global
55
+
56
+ tenant_adapter.create 'dummy'
57
+
58
+ tenant_adapter.name.should eq tenant_adapter.global
59
+
60
+ tenant_adapter.with 'dummy' do
61
+ tenant_adapter.name.should eq 'dummy'
62
+ end
63
+
64
+ tenant_adapter.name.should eq tenant_adapter.global
65
+
66
+ tenant_adapter.remove 'dummy'
67
+
68
+ tenant_adapter.name.should eq tenant_adapter.global
69
+ end
70
+
71
+ end
72
+
73
+ context 'Global adapter' do
74
+
75
+ it 'Adapter operations' do
76
+ ActiveTenant.current.create 'dummy'
77
+ ActiveTenant.current.all.should include 'dummy'
78
+
79
+ ActiveTenant.current.name.should eq ActiveTenant.current.global
80
+
81
+ ActiveTenant.current.with 'dummy' do
82
+ ActiveTenant.current.name.should eq 'dummy'
83
+ end
84
+
85
+ ActiveTenant.current.name.should eq ActiveTenant.current.global
86
+
87
+ ActiveTenant.current.remove 'dummy'
88
+ ActiveTenant.current.all.should_not include 'dummy'
89
+ end
90
+
91
+ it 'Migrate global' do
92
+ ActiveTenant.current.create 'dummy'
93
+
94
+ ActiveTenant.current.migrate_global
95
+
96
+ ActiveRecord::Base.connection.table_exists?('globals').should be_true
97
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
98
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
99
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
100
+
101
+ ActiveTenant.current.remove 'dummy'
102
+ end
103
+
104
+ it 'Migrate one tenant' do
105
+ ActiveTenant.current.create 'dummy_1'
106
+ ActiveTenant.current.create 'dummy_2'
107
+
108
+ ActiveTenant.current.migrate 'dummy_1'
109
+
110
+ ActiveTenant.current.with 'dummy_1' do
111
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
112
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
113
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
114
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
115
+ end
116
+
117
+ ActiveTenant.current.with 'dummy_2' do
118
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
119
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
120
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
121
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
122
+ end
123
+
124
+ ActiveTenant.current.remove 'dummy_1'
125
+ ActiveTenant.current.remove 'dummy_2'
126
+ end
127
+
128
+ it 'Migrate all tenants' do
129
+ ActiveTenant.current.create 'dummy_1'
130
+ ActiveTenant.current.create 'dummy_2'
131
+
132
+ ActiveTenant.current.migrate_all
133
+
134
+ ActiveTenant.current.with 'dummy_1' do
135
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
136
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
137
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
138
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
139
+ end
140
+
141
+ ActiveTenant.current.with 'dummy_2' do
142
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
143
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
144
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
145
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
146
+ end
147
+
148
+ ActiveTenant.current.remove 'dummy_1'
149
+ ActiveTenant.current.remove 'dummy_2'
150
+ end
151
+
152
+ it 'Migrate custom tenant' do
153
+ ActiveTenant.current.create 'custom'
154
+
155
+ ActiveTenant.current.migrate 'custom'
156
+
157
+ ActiveTenant.current.with 'custom' do
158
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
159
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
160
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
161
+ ActiveRecord::Base.connection.table_exists?('customs').should be_true
162
+ end
163
+
164
+ ActiveTenant.current.remove 'custom'
165
+ end
166
+
167
+ it 'Migrate to specific version' do
168
+ ActiveTenant.current.create 'dummy'
169
+
170
+ ActiveTenant.current.migrate_all 20120823132854
171
+
172
+ ActiveTenant.current.with 'dummy' do
173
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
174
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
175
+ end
176
+
177
+ ActiveTenant.current.remove 'dummy'
178
+ end
179
+
180
+ end
181
+
182
+ context 'ActiveRecord extensions' do
183
+
184
+ it 'Create, migrate and remove' do
185
+ ActiveRecord::Base.tenant?.should be_false
186
+ ActiveRecord::Base.tenant_name.should be_nil
187
+
188
+ ActiveRecord::Base.create_tenant 'dummy'
189
+
190
+ ActiveRecord::Migration.migrate_all
191
+
192
+ ActiveRecord::Base.connection.table_exists?('globals').should be_true
193
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_false
194
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
195
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
196
+
197
+ ActiveRecord::Base.with_tenant 'dummy' do
198
+ ActiveRecord::Base.tenant?.should be_true
199
+ ActiveRecord::Base.tenant_name.should eq 'dummy'
200
+
201
+ ActiveRecord::Base.connection.table_exists?('globals').should be_false
202
+ ActiveRecord::Base.connection.table_exists?('tenants').should be_true
203
+ ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
204
+ ActiveRecord::Base.connection.table_exists?('customs').should be_false
205
+ end
206
+
207
+ ActiveRecord::Base.remove_tenant 'dummy'
208
+
209
+ ActiveRecord::Base.tenant?.should be_false
210
+ ActiveRecord::Base.tenant_name.should be_nil
211
+ end
212
+
213
+ end
214
+
215
+ context 'Models' do
216
+
217
+ it 'Globals always visible' do
218
+ ActiveTenant.current.create 'dummy_1'
219
+ ActiveTenant.current.create 'dummy_2'
220
+
221
+ ActiveTenant.current.migrate_global
222
+ ActiveTenant.current.migrate_all
223
+
224
+ Global.count.should eq 0
225
+
226
+ ActiveTenant.current.with('dummy_1') do
227
+ Tenant.count.should eq 0
228
+ end
229
+
230
+ ActiveTenant.current.with('dummy_2') do
231
+ Tenant.create! key: '1', value: 'dummy_2'
232
+ Global.create! key: '2', value: 'global'
233
+
234
+ Tenant.count.should eq 1
235
+ Global.count.should eq 1
236
+ end
237
+
238
+ Global.count.should eq 1
239
+
240
+ ActiveTenant.current.with('dummy_1') do
241
+ Tenant.count.should eq 0
242
+ Global.count.should eq 1
243
+ end
244
+ end
245
+
246
+ end
247
+
248
+ end
249
+
218
250
  end
@@ -1,10 +1,10 @@
1
- class CreateGlobals < ActiveRecord::Migration
2
- def change
3
- create_table :globals do |t|
4
- t.string :key, null: false
5
- t.string :value, null: false
6
-
7
- t.timestamps
8
- end
9
- end
10
- end
1
+ class CreateGlobals < ActiveRecord::Migration
2
+ def change
3
+ create_table :globals do |t|
4
+ t.string :key, null: false
5
+ t.string :value, null: false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -1,12 +1,12 @@
1
- class CreateTenants < ActiveRecord::Migration
2
- tenant :all
3
-
4
- def change
5
- create_table :tenants do |t|
6
- t.string :key, null: false
7
- t.string :value, null: false
8
-
9
- t.timestamps
10
- end
11
- end
12
- end
1
+ class CreateTenants < ActiveRecord::Migration
2
+ tenant :all
3
+
4
+ def change
5
+ create_table :tenants do |t|
6
+ t.string :key, null: false
7
+ t.string :value, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,12 @@
1
- class CreateOtherTenants < ActiveRecord::Migration
2
- tenant :all
3
-
4
- def change
5
- create_table :other_tenants do |t|
6
- t.string :key, null: false
7
- t.string :value, null: false
8
-
9
- t.timestamps
10
- end
11
- end
12
- end
1
+ class CreateOtherTenants < ActiveRecord::Migration
2
+ tenant :all
3
+
4
+ def change
5
+ create_table :other_tenants do |t|
6
+ t.string :key, null: false
7
+ t.string :value, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,12 @@
1
- class CreateCustoms < ActiveRecord::Migration
2
- tenant :custom
3
-
4
- def change
5
- create_table :customs do |t|
6
- t.string :key, null: false
7
- t.string :value, null: false
8
-
9
- t.timestamps
10
- end
11
- end
12
- end
1
+ class CreateCustoms < ActiveRecord::Migration
2
+ tenant :custom
3
+
4
+ def change
5
+ create_table :customs do |t|
6
+ t.string :key, null: false
7
+ t.string :value, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,12 @@
1
+ class Global < ActiveRecord::Base
2
+ belongs_to_tenant_global
3
+ end
4
+
5
+ class Tenant < ActiveRecord::Base
6
+ end
7
+
8
+ class OtherTenant < ActiveRecord::Base
9
+ end
10
+
11
+ class Custom < ActiveRecord::Base
12
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,9 @@
1
- require 'active_tenant'
2
-
3
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
-
5
- TEMP_PATH = ENV['TMP'].gsub("\\", '/')
6
-
7
- ActiveRecord::Base.logger = Logger.new($stdout)
8
- ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
9
-
10
- RSpec.configure do |config|
1
+ require 'active_tenant'
2
+
3
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
+
5
+ ActiveRecord::Base.logger = Logger.new($stdout)
6
+ ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
7
+
8
+ RSpec.configure do |config|
11
9
  end