apartment 0.14.4 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,200 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Apartment::Database do
4
-
5
- context "using postgresql" do
6
-
7
- # See apartment.yml file in dummy app config
8
-
9
- let(:config){ Apartment::Test.config['connections']['postgresql'].symbolize_keys }
10
- let(:database){ "some_new_database" }
11
- let(:database2){ "yet_another_database" }
12
-
13
- before do
14
- Apartment.use_postgres_schemas = true
15
- ActiveRecord::Base.establish_connection config
16
- Apartment::Test.load_schema # load the Rails schema in the public db schema
17
- Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
18
- @schema_search_path = ActiveRecord::Base.connection.schema_search_path
19
- end
20
-
21
- after do
22
- Apartment::Test.reset
23
- end
24
-
25
- describe "#init" do
26
-
27
- it "should process model exclusions" do
28
- Apartment.configure do |config|
29
- config.excluded_models = ["Company"]
30
- end
31
-
32
- Apartment::Database.init
33
-
34
- Company.table_name.should == "public.companies"
35
- end
36
-
37
- end
38
-
39
- describe "#adapter" do
40
- before do
41
- Apartment::Database.reload!
42
- end
43
-
44
- it "should load postgresql adapter" do
45
- Apartment::Database.adapter
46
- Apartment::Adapters::PostgresqlAdapter.should be_a(Class)
47
- end
48
-
49
- it "should raise exception with invalid adapter specified" do
50
- Apartment::Database.stub(:config).and_return config.merge(:adapter => 'unkown')
51
-
52
- expect {
53
- Apartment::Database.adapter
54
- }.to raise_error
55
- end
56
-
57
- end
58
-
59
- context "with schemas" do
60
-
61
- before do
62
- Apartment.configure do |config|
63
- config.excluded_models = []
64
- config.use_postgres_schemas = true
65
- config.seed_after_create = true
66
- end
67
- Apartment::Database.create database
68
- end
69
-
70
- after do
71
- Apartment::Test.drop_schema database
72
- end
73
-
74
- describe "#process" do
75
-
76
- before do
77
- Apartment::Database.create database2
78
- end
79
-
80
- after do
81
- Apartment::Test.drop_schema database2
82
- end
83
-
84
- it "should connect to new schema" do
85
- Apartment::Database.process(database) do
86
- Apartment::Database.current_database.should == database
87
- end
88
- end
89
-
90
- it "should reset connection to the previous db" do
91
- Apartment::Database.switch(database2)
92
- Apartment::Database.process(database)
93
- Apartment::Database.current_database.should == database2
94
- end
95
-
96
- it "should reset to previous schema if database is nil" do
97
- Apartment::Database.switch(database)
98
- Apartment::Database.process
99
- Apartment::Database.current_database.should == database
100
- end
101
-
102
- it "should set to public schema if database is nil" do
103
- Apartment::Database.process do
104
- Apartment::Database.current_database.should == @schema_search_path
105
- end
106
- end
107
-
108
- end
109
-
110
- describe "#create" do
111
- it "should create new postgres schema" do
112
- ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect{|row| row['nspname']}.should include(database)
113
- end
114
-
115
- it "should seed data" do
116
- Apartment::Database.switch database
117
- User.count.should be > 0
118
- end
119
- end
120
-
121
- describe "#switch" do
122
-
123
- let(:x){ rand(3) }
124
-
125
- it "should connect to new schema" do
126
- Apartment::Database.switch database
127
- ActiveRecord::Base.connection.schema_search_path.should == database
128
- end
129
-
130
- it "should fail with invalid schema" do
131
- expect {
132
- Apartment::Database.switch('some_nonexistent_schema')
133
- }.to raise_error Apartment::SchemaNotFound
134
- end
135
-
136
- context "creating models" do
137
-
138
- before do
139
- Apartment::Database.create database2
140
- end
141
-
142
- after do
143
- Apartment::Test.drop_schema database2
144
- end
145
-
146
- it "should create a model instance in the current schema" do
147
- Apartment::Database.switch database2
148
- db2_count = User.count + x.times{ User.create }
149
-
150
- Apartment::Database.switch database
151
- db_count = User.count + x.times{ User.create }
152
-
153
- Apartment::Database.switch database2
154
- User.count.should == db2_count
155
-
156
- Apartment::Database.switch database
157
- User.count.should == db_count
158
- end
159
- end
160
-
161
- context "with excluded models" do
162
-
163
- before do
164
- Apartment.configure do |config|
165
- config.excluded_models = ["Company"]
166
- end
167
- Apartment::Database.init
168
- end
169
-
170
- it "should ignore excluded models" do
171
- Apartment::Database.switch database
172
- Company.table_name.should include('public')
173
- end
174
-
175
- it "should create excluded models in public schema" do
176
- Apartment::Database.reset # ensure we're on public schema
177
- count = Company.count + x.times{ Company.create }
178
-
179
- Apartment::Database.switch database
180
- x.times{ Company.create }
181
- Company.count.should == count + x
182
- Apartment::Database.reset
183
- Company.count.should == count + x
184
- end
185
- end
186
-
187
- end
188
-
189
- describe "#current_database" do
190
-
191
- it "should return the current schema search path" do
192
- Apartment::Database.switch database
193
- Apartment::Database.current_database.should == database
194
- end
195
- end
196
-
197
- end
198
-
199
- end
200
- end