apartment 0.14.4 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,10 +4,10 @@ connections:
4
4
  database: apartment_postgresql_test
5
5
  min_messages: WARNING
6
6
  username: root
7
- password:
7
+ password:
8
8
 
9
9
  mysql:
10
10
  adapter: mysql2
11
11
  database: apartment_mysql_test
12
12
  username: root
13
- password:
13
+ password:
@@ -0,0 +1,110 @@
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){ Apartment::Test.next_db }
11
+ let(:database2){ Apartment::Test.next_db }
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
+ subject.stub(:config).and_return config # Use postgresql database config for this test
18
+ end
19
+
20
+ describe "#adapter" do
21
+ before do
22
+ subject.reload!
23
+ end
24
+
25
+ it "should load postgresql adapter" do
26
+ subject.adapter
27
+ Apartment::Adapters::PostgresqlAdapter.should be_a(Class)
28
+ end
29
+
30
+ it "should raise exception with invalid adapter specified" do
31
+ subject.stub(:config).and_return config.merge(:adapter => 'unkown')
32
+
33
+ expect {
34
+ Apartment::Database.adapter
35
+ }.to raise_error
36
+ end
37
+
38
+ end
39
+
40
+ context "with schemas" do
41
+
42
+ before do
43
+ Apartment.configure do |config|
44
+ config.excluded_models = []
45
+ config.use_postgres_schemas = true
46
+ config.seed_after_create = true
47
+ end
48
+ subject.create database
49
+ end
50
+
51
+ after{ subject.drop database }
52
+
53
+ describe "#create" do
54
+ it "should seed data" do
55
+ subject.switch database
56
+ User.count.should be > 0
57
+ end
58
+ end
59
+
60
+ describe "#switch" do
61
+
62
+ let(:x){ rand(3) }
63
+
64
+ context "creating models" do
65
+
66
+ before{ subject.create database2 }
67
+ after{ subject.drop database2 }
68
+
69
+ it "should create a model instance in the current schema" do
70
+ subject.switch database2
71
+ db2_count = User.count + x.times{ User.create }
72
+
73
+ subject.switch database
74
+ db_count = User.count + x.times{ User.create }
75
+
76
+ subject.switch database2
77
+ User.count.should == db2_count
78
+
79
+ subject.switch database
80
+ User.count.should == db_count
81
+ end
82
+ end
83
+
84
+ context "with excluded models" do
85
+
86
+ before do
87
+ Apartment.configure do |config|
88
+ config.excluded_models = ["Company"]
89
+ end
90
+ subject.init
91
+ end
92
+
93
+ it "should create excluded models in public schema" do
94
+ subject.reset # ensure we're on public schema
95
+ count = Company.count + x.times{ Company.create }
96
+
97
+ subject.switch database
98
+ x.times{ Company.create }
99
+ Company.count.should == count + x
100
+ subject.reset
101
+ Company.count.should == count + x
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+ end
@@ -11,7 +11,13 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20110613152810) do
14
+ ActiveRecord::Schema.define(:version => 20111202022214) do
15
+
16
+ create_table "books", :force => true do |t|
17
+ t.string "name"
18
+ t.integer "pages"
19
+ t.datetime "published"
20
+ end
15
21
 
16
22
  create_table "companies", :force => true do |t|
17
23
  t.boolean "dummy"
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "a db based apartment adapter" do
4
+ include Apartment::Spec::AdapterRequirements
5
+
6
+ let(:default_database){ subject.process{ ActiveRecord::Base.connection.current_database } }
7
+
8
+ describe "#init" do
9
+
10
+ it "should process model exclusions" do
11
+ Apartment.configure do |config|
12
+ config.excluded_models = ["Company"]
13
+ end
14
+
15
+ Apartment::Database.init
16
+
17
+ Company.connection.object_id.should_not == ActiveRecord::Base.connection.object_id
18
+ end
19
+ end
20
+
21
+ describe "#drop" do
22
+ it "should raise an error for unknown database" do
23
+ expect {
24
+ subject.drop 'unknown_database'
25
+ }.to raise_error(Apartment::DatabaseNotFound)
26
+ end
27
+ end
28
+
29
+ describe "#switch" do
30
+ it "should raise an error if database is invalid" do
31
+ expect {
32
+ subject.switch 'unknown_database'
33
+ }.to raise_error(Apartment::DatabaseNotFound)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "a generic apartment adapter" do
4
+ include Apartment::Spec::AdapterRequirements
5
+
6
+ before{ Apartment.prepend_environment = false }
7
+
8
+ #
9
+ # Creates happen already in our before_filter
10
+ #
11
+ describe "#create" do
12
+
13
+ it "should create the new databases" do
14
+ database_names.should include(db1)
15
+ database_names.should include(db2)
16
+ end
17
+
18
+ it "should load schema.rb to new schema" do
19
+ subject.process(db1) do
20
+ connection.tables.should include('companies')
21
+ end
22
+ end
23
+
24
+ it "should yield to block if passed and reset" do
25
+ subject.drop(db2) # so we don't get errors on creation
26
+
27
+ @count = 0 # set our variable so its visible in and outside of blocks
28
+
29
+ subject.create(db2) do
30
+ @count = User.count
31
+ subject.current_database.should == db2
32
+ User.create
33
+ end
34
+
35
+ subject.current_database.should_not == db2
36
+
37
+ subject.process(db2){ User.count.should == @count + 1 }
38
+ end
39
+ end
40
+
41
+ describe "#drop" do
42
+ it "should remove the db" do
43
+ subject.drop db1
44
+ database_names.should_not include(db1)
45
+ end
46
+ end
47
+
48
+ describe "#process" do
49
+ it "should connect" do
50
+ subject.process(db1) do
51
+ subject.current_database.should == db1
52
+ end
53
+ end
54
+
55
+ it "should reset" do
56
+ subject.process(db1)
57
+ subject.current_database.should == default_database
58
+ end
59
+
60
+ # We're often finding when using Apartment in tests, the `current_database` (ie the previously connect to db)
61
+ # gets dropped, but process will try to return to that db in a test. We should just reset if it doesn't exist
62
+ it "should not throw exception if current_database is no longer accessible" do
63
+ subject.switch(db2)
64
+
65
+ expect {
66
+ subject.process(db1){ subject.drop(db2) }
67
+ }.to_not raise_error
68
+ end
69
+ end
70
+
71
+ describe "#reset" do
72
+ it "should reset connection" do
73
+ subject.switch(db1)
74
+ subject.reset
75
+ subject.current_database.should == default_database
76
+ end
77
+ end
78
+
79
+ describe "#switch" do
80
+ it "should connect to new db" do
81
+ subject.switch(db1)
82
+ subject.current_database.should == db1
83
+ end
84
+
85
+ it "should reset connection if database is nil" do
86
+ subject.switch
87
+ subject.current_database.should == default_database
88
+ end
89
+ end
90
+
91
+ describe "#current_database" do
92
+ it "should return the current db name" do
93
+ subject.switch(db1)
94
+ subject.current_database.should == db1
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "a schema based apartment adapter" do
4
+ include Apartment::Spec::AdapterRequirements
5
+
6
+ let(:schema1){ db1 }
7
+ let(:schema2){ db2 }
8
+ let(:public_schema){ default_database }
9
+
10
+ describe "#init" do
11
+
12
+ it "should process model exclusions" do
13
+ Apartment.configure do |config|
14
+ config.excluded_models = ["Company"]
15
+ end
16
+
17
+ Apartment::Database.init
18
+
19
+ Company.table_name.should == "public.companies"
20
+ end
21
+ end
22
+
23
+ #
24
+ # Creates happen already in our before_filter
25
+ #
26
+ describe "#create" do
27
+
28
+ it "should load schema.rb to new schema" do
29
+ connection.schema_search_path = schema1
30
+ connection.tables.should include('companies')
31
+ end
32
+
33
+ it "should yield to block if passed and reset" do
34
+ subject.drop(schema2) # so we don't get errors on creation
35
+
36
+ @count = 0 # set our variable so its visible in and outside of blocks
37
+
38
+ subject.create(schema2) do
39
+ @count = User.count
40
+ connection.schema_search_path.should == schema2
41
+ User.create
42
+ end
43
+
44
+ connection.schema_search_path.should_not == schema2
45
+
46
+ subject.process(schema2){ User.count.should == @count + 1 }
47
+ end
48
+
49
+ it "should allow numeric database names" do
50
+ expect {
51
+ subject.create(1234)
52
+ }.to_not raise_error
53
+ database_names.should include("1234")
54
+ # cleanup
55
+ subject.drop(1234)
56
+ end
57
+
58
+ end
59
+
60
+ describe "#drop" do
61
+ it "should raise an error for unknown database" do
62
+ expect {
63
+ subject.drop "unknown_database"
64
+ }.to raise_error(Apartment::SchemaNotFound)
65
+ end
66
+
67
+ it "should be able to drop numeric dbs" do
68
+ subject.create(1234)
69
+ expect {
70
+ subject.drop(1234)
71
+ }.to_not raise_error
72
+ database_names.should_not include("1234")
73
+ end
74
+ end
75
+
76
+ describe "#process" do
77
+ it "should connect" do
78
+ subject.process(schema1) do
79
+ connection.schema_search_path.should == schema1
80
+ end
81
+ end
82
+
83
+ it "should reset" do
84
+ subject.process(schema1)
85
+ connection.schema_search_path.should == public_schema
86
+ end
87
+ end
88
+
89
+ describe "#reset" do
90
+ it "should reset connection" do
91
+ subject.switch(schema1)
92
+ subject.reset
93
+ connection.schema_search_path.should == public_schema
94
+ end
95
+ end
96
+
97
+ describe "#switch" do
98
+ it "should connect to new schema" do
99
+ subject.switch(schema1)
100
+ connection.schema_search_path.should == schema1
101
+ end
102
+
103
+ it "should reset connection if database is nil" do
104
+ subject.switch
105
+ connection.schema_search_path.should == public_schema
106
+ end
107
+
108
+ it "should raise an error if schema is invalid" do
109
+ expect {
110
+ subject.switch 'unknown_schema'
111
+ }.to raise_error(Apartment::SchemaNotFound)
112
+ end
113
+
114
+ it "should connect to numeric dbs" do
115
+ subject.create(1234)
116
+ expect {
117
+ subject.switch(1234)
118
+ }.to_not raise_error
119
+ subject.drop(1234)
120
+ end
121
+ end
122
+
123
+ describe "#current_database" do
124
+ it "should return the current schema name" do
125
+ subject.switch(schema1)
126
+ subject.current_database.should == schema1
127
+ end
128
+ end
129
+
130
+ end
@@ -29,7 +29,8 @@ describe "apartment rake tasks" do
29
29
  context "with x number of databases" do
30
30
 
31
31
  let(:x){ 1 + rand(5) } # random number of dbs to create
32
- let(:db_names){ x.times.map{|y| "database_#{y}" } }
32
+ let(:db_names){ x.times.map{ Apartment::Test.next_db } }
33
+ let!(:company_count){ Company.count + db_names.length }
33
34
 
34
35
  before do
35
36
  db_names.collect do |db_name|
@@ -45,7 +46,7 @@ describe "apartment rake tasks" do
45
46
 
46
47
  describe "#migrate" do
47
48
  it "should migrate all databases" do
48
- Apartment::Migrator.should_receive(:migrate).exactly(db_names.length).times
49
+ Apartment::Migrator.should_receive(:migrate).exactly(company_count).times
49
50
 
50
51
  @rake['apartment:migrate'].invoke
51
52
  end
@@ -64,7 +65,7 @@ describe "apartment rake tasks" do
64
65
 
65
66
  describe "apartment:seed" do
66
67
  it "should seed all databases" do
67
- Apartment::Database.should_receive(:seed).exactly(db_names.length).times
68
+ Apartment::Database.should_receive(:seed).exactly(company_count).times
68
69
 
69
70
  @rake['apartment:seed'].invoke
70
71
  end
@@ -7,14 +7,13 @@ describe Apartment::Delayed do
7
7
  # See apartment.yml file in dummy app config
8
8
 
9
9
  let(:config){ Apartment::Test.config['connections']['postgresql'].symbolize_keys }
10
- let(:database){ "some_new_database" }
11
- let(:database2){ "another_db" }
10
+ let(:database){ Apartment::Test.next_db }
11
+ let(:database2){ Apartment::Test.next_db }
12
12
 
13
13
  before do
14
14
  ActiveRecord::Base.establish_connection config
15
15
  Apartment::Test.load_schema # load the Rails schema in the public db schema
16
16
  Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
17
- @schema_search_path = ActiveRecord::Base.connection.schema_search_path
18
17
 
19
18
  Apartment.configure do |config|
20
19
  config.use_postgres_schemas = true