connection_manager 0.0.1 → 0.1.0

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.
Files changed (67) hide show
  1. data/.gitignore +1 -0
  2. data/.rspec +1 -0
  3. data/Gemfile.lock +8 -3
  4. data/README.md +26 -26
  5. data/connection_manager.gemspec +2 -0
  6. data/lib/connection_manager.rb +1 -0
  7. data/lib/connection_manager/connections.rb +34 -25
  8. data/lib/connection_manager/replication_builder.rb +11 -12
  9. data/lib/connection_manager/version.rb +1 -1
  10. data/spec/database.yml +20 -0
  11. data/spec/factories.rb +31 -0
  12. data/spec/helpers/database_spec_helper.rb +68 -0
  13. data/spec/helpers/models_spec_helper.rb +26 -0
  14. data/spec/integration/replication_builder_spec.rb +97 -0
  15. data/{test_app/spec/connection_manager → spec/lib}/associations_spec.rb +0 -0
  16. data/spec/lib/connections_spec.rb +93 -0
  17. data/{test_app/spec/connection_manager → spec/lib}/replication_builder_spec.rb +17 -1
  18. data/spec/spec_helper.rb +17 -5
  19. metadata +49 -60
  20. data/test_app/.gitignore +0 -15
  21. data/test_app/.rspec +0 -1
  22. data/test_app/Gemfile +0 -14
  23. data/test_app/Gemfile.lock +0 -140
  24. data/test_app/README +0 -261
  25. data/test_app/Rakefile +0 -7
  26. data/test_app/app/models/.gitkeep +0 -0
  27. data/test_app/app/models/basket.rb +0 -4
  28. data/test_app/app/models/fruit.rb +0 -6
  29. data/test_app/app/models/fruit_basket.rb +0 -4
  30. data/test_app/app/models/region.rb +0 -3
  31. data/test_app/app/models/type.rb +0 -2
  32. data/test_app/config.ru +0 -4
  33. data/test_app/config/application.rb +0 -52
  34. data/test_app/config/boot.rb +0 -6
  35. data/test_app/config/database.yml +0 -42
  36. data/test_app/config/environment.rb +0 -5
  37. data/test_app/config/environments/development.rb +0 -30
  38. data/test_app/config/environments/production.rb +0 -60
  39. data/test_app/config/environments/test.rb +0 -39
  40. data/test_app/config/initializers/backtrace_silencers.rb +0 -7
  41. data/test_app/config/initializers/inflections.rb +0 -10
  42. data/test_app/config/initializers/load_connection_manager.rb +0 -6
  43. data/test_app/config/initializers/mime_types.rb +0 -5
  44. data/test_app/config/initializers/secret_token.rb +0 -7
  45. data/test_app/config/initializers/session_store.rb +0 -8
  46. data/test_app/config/initializers/wrap_parameters.rb +0 -14
  47. data/test_app/config/locales/en.yml +0 -5
  48. data/test_app/config/routes.rb +0 -58
  49. data/test_app/db/migrate/20111127040654_create_fruits.rb +0 -9
  50. data/test_app/db/migrate/20111127040720_create_baskets.rb +0 -9
  51. data/test_app/db/migrate/20111127040846_create_fruit_baskets.rb +0 -9
  52. data/test_app/db/migrate/20111127040915_create_regions.rb +0 -9
  53. data/test_app/db/migrate/20111127060322_create_types.rb +0 -9
  54. data/test_app/db/schema.rb +0 -16
  55. data/test_app/db/seeds.rb +0 -7
  56. data/test_app/log/.gitkeep +0 -0
  57. data/test_app/script/rails +0 -6
  58. data/test_app/spec/connection_manager/connections_spec.rb +0 -51
  59. data/test_app/spec/factories/baskets.rb +0 -7
  60. data/test_app/spec/factories/fruit_baskets.rb +0 -8
  61. data/test_app/spec/factories/fruits.rb +0 -8
  62. data/test_app/spec/factories/regions.rb +0 -7
  63. data/test_app/spec/factories/types.rb +0 -7
  64. data/test_app/spec/models/type_spec.rb +0 -5
  65. data/test_app/spec/spec.opts +0 -4
  66. data/test_app/spec/spec_helper.rb +0 -42
  67. data/test_app/test_app +0 -0
@@ -0,0 +1,26 @@
1
+ class Basket < ActiveRecord::Base
2
+ has_many :fruit_baskets
3
+ has_many :fruit, :through => :fruit_baskets
4
+ #replicated
5
+ end
6
+
7
+
8
+ class Fruit < ActiveRecord::Base
9
+ belongs_to :region
10
+ has_many :fruit_baskets
11
+ has_many :baskets, :through => :fruit_baskets
12
+ #replicated
13
+ end
14
+
15
+ #Join table
16
+ class FruitBasket < ActiveRecord::Base
17
+ belongs_to :fruit
18
+ belongs_to :basket
19
+ #replicated
20
+ end
21
+
22
+ class Region < ActiveRecord::Base
23
+ has_one :fruit
24
+ #replicated
25
+ end
26
+
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ # Tests for associations build form replication.
4
+
5
+ describe ConnectionManager::ReplicationBuilder do
6
+ before(:all) do
7
+ # Make sure connections recreated in other tests do not presist to current
8
+ ConnectionManager::Connections.all.clear
9
+ ConnectionManager::Connections.replication_connections.clear
10
+
11
+ #Initialize
12
+ ConnectionManager::Connections.initialize(:env => 'test')
13
+
14
+ # Add Replication to models
15
+ Fruit.replicated
16
+ Basket.replicated
17
+ FruitBasket.replicated
18
+ Region.replicated :readonly => false
19
+ end
20
+
21
+ context "models that have been replicated" do
22
+ it "should respond to slave_1" do
23
+ Fruit.respond_to?(:slave_1).should be_true
24
+ end
25
+
26
+ it "should respond to slave" do
27
+ Fruit.respond_to?(:slave).should be_true
28
+ end
29
+
30
+ it "should have a subclass of Slave1" do
31
+ defined?(Fruit::Slave1).should be_true
32
+ end
33
+
34
+ context "subclasses" do
35
+ it "should have a superclass of the parent class" do
36
+ Fruit::Slave1.superclass.should eql(Fruit)
37
+ end
38
+ end
39
+ end
40
+
41
+ context "readonly" do
42
+ end
43
+
44
+ context "slave" do
45
+ context('belongs_to') do
46
+ it "should return the same belongs to object as master" do
47
+ fruit = Factory.create(:fruit)
48
+
49
+ slave_fruit = Fruit.slave.where(:id => fruit.id).first
50
+ slave_fruit.region.id.should eql(fruit.region.id)
51
+ end
52
+ end
53
+
54
+ context('has_one') do
55
+ it "should return the same has_on object as master" do
56
+ region = Factory.create(:fruit).region
57
+ slave_region = Region.slave.where(:id => region.id).first
58
+ slave_region.fruit.id.should eql(region.fruit.id)
59
+ end
60
+ end
61
+
62
+ context('has_many') do
63
+ it "should return the same has_many objects as master" do
64
+ fruit = Factory.create(:fruit)
65
+ 3.times do
66
+ Factory.create(:fruit_basket, :fruit_id => fruit.id)
67
+ end
68
+ fruit.fruit_baskets.length.should eql(3)
69
+ slave_fruit = Fruit.slave.where(:id => fruit.id).first
70
+ slave_fruit.fruit_baskets.length.should eql(fruit.fruit_baskets.length)
71
+ slave_fruit.fruit_baskets.collect(&:id).should eql(fruit.fruit_baskets.collect(&:id))
72
+ end
73
+ end
74
+
75
+ context('has_many :through') do
76
+ it "should return the same fruit as master" do
77
+ basket = Factory.create(:fruit_basket)
78
+ slave_fruit = Fruit.slave.where(:id => basket.fruit_id).first
79
+ master_fruit = Fruit.where(:id => basket.fruit_id).first
80
+ slave_fruit.basket_ids.should eql(master_fruit.basket_ids)
81
+ end
82
+ end
83
+ context "readonly" do
84
+ it "should return a readonly object by default" do
85
+ Factory.create(:fruit)
86
+ readonly_fruit = Fruit.slave.first
87
+ readonly_fruit.readonly?.should be_true
88
+ lambda { readonly_fruit.save }.should raise_error
89
+ end
90
+ it "should not return readonly if replicated readonly is to false" do
91
+ Factory.create(:region)
92
+ Region.slave.first.readonly?.should be_false
93
+ end
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+ describe ConnectionManager::Connections do
3
+ # For all tests set the env to "test"
4
+ before(:all) do
5
+ ConnectionManager::Connections.env = "test"
6
+ end
7
+ context '#clean_sqlite_db_name' do
8
+ it "should remove the directory .sqlite3, Rails.env from the string" do
9
+ ConnectionManager::Connections.clean_sqlite_db_name("db/my_database_test.sqlite3").should eql("my_database")
10
+ end
11
+ end
12
+
13
+ context '#clean_bd_name' do
14
+ context "when name is not just the Rails.env" do
15
+ it "should remove the Rails.env from the string" do
16
+ ConnectionManager::Connections.clean_bd_name("my_database_test").should eql("my_database")
17
+ end
18
+ end
19
+ context "when the name is only the Rails.env" do
20
+ it "should use the name of the database and remove the Rails.env" do
21
+ ConnectionManager::Connections.stubs(:database_name_from_yml).returns("my_database_test")
22
+ ConnectionManager::Connections.clean_bd_name("test").should eql("my_database")
23
+ end
24
+ it "should account for sqlite3 database name" do
25
+ ConnectionManager::Connections.stubs(:database_name_from_yml).returns("db/my_database_test.sqlite3")
26
+ ConnectionManager::Connections.clean_bd_name("test").should eql("my_database")
27
+ end
28
+ end
29
+ end
30
+
31
+ context '#connection_class_name' do
32
+
33
+ it "should return a string for a class name appended with 'Connection' " do
34
+ ConnectionManager::Connections.connection_class_name("my_database").should eql("MyDatabaseConnection")
35
+ end
36
+ it "should return remove the appended rails env" do
37
+ ConnectionManager::Connections.connection_class_name("my_database_test").should eql("MyDatabaseConnection")
38
+ end
39
+ it "should handle sqlite database names correctly " do
40
+ ConnectionManager::Connections.connection_class_name("db/my_database_test.sqlite3").should eql("MyDatabaseConnection")
41
+ end
42
+ it "should use the database name from the database.yml if supplied string is only is only the Rails.env" do
43
+ ConnectionManager::Connections.stubs(:database_name_from_yml).returns("my_test_test")
44
+ ConnectionManager::Connections.connection_class_name("test").should eql("MyTestConnection")
45
+ end
46
+ end
47
+
48
+
49
+ context 'after #initialize' do
50
+ before(:all) do
51
+ # Make sure connections recreated in other tests do not presist to tests tests
52
+ ConnectionManager::Connections.all.clear
53
+ ConnectionManager::Connections.replication_connections.clear
54
+
55
+ #Initialize
56
+ ConnectionManager::Connections.initialize(:env => 'test')
57
+ end
58
+
59
+ context '#all' do
60
+ it "should return the database.yml entries for the current rails environment" do
61
+ ConnectionManager::Connections.all.should eql(["CmConnection",
62
+ "Slave1CmConnection", "Slave2CmConnection"])
63
+ end
64
+ end
65
+
66
+ context '#replication_connections' do
67
+ it "should return a hash where the keys are the generic undescored names for all connections" do
68
+ ConnectionManager::Connections.replication_connections.keys.
69
+ should eql([:cm, :slave_cm])
70
+ end
71
+ it "should return a hash where the values are an array of connection class names as strings" do
72
+ first_value = ConnectionManager::Connections.replication_connections.values.first
73
+ first_value.class.should eql(Array)
74
+ defined?((ConnectionManager::Connections.class_eval(first_value[0]))).should be_true
75
+ end
76
+ end
77
+
78
+ context '#build_connection_class' do
79
+ before(:all) do
80
+ ConnectionManager::Connections.build_connection_class("MyConnectionClass", 'test')
81
+ end
82
+ it "should add a class with supplied class name to ConnectionManager::Connections" do
83
+ defined?(ConnectionManager::Connections::MyConnectionClass).should be_true
84
+ ConnectionManager::Connections::MyConnectionClass.is_a?(Class).should be_true
85
+ end
86
+ it "should have a super class of ActiveRecord::Base" do
87
+ ConnectionManager::Connections::MyConnectionClass.superclass.should eql(ActiveRecord::Base)
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+
@@ -4,7 +4,7 @@ describe ConnectionManager::ReplicationBuilder do
4
4
 
5
5
  context '#database_name' do
6
6
  it "should return the name of the database the model is using" do
7
- Fruit.database_name.should eql('test_app')
7
+ Fruit.database_name.should eql('spec/cm_test.sqlite3')
8
8
  end
9
9
  end
10
10
 
@@ -29,5 +29,21 @@ describe ConnectionManager::ReplicationBuilder do
29
29
  end
30
30
  end
31
31
  end
32
+
33
+ context '#replication_connection_classes' do
34
+ it "should return the :using array with the array elements classified and append with Connection" do
35
+ Fruit.replication_connection_classes({:using => ['slave_1_test_db','slave_2_test_db']}).
36
+ should eql(["Slave1TestDbConnection", "Slave2TestDbConnection"])
37
+ end
38
+
39
+ end
40
+
41
+ context '#replicated' do
42
+ it "should raise an exception if no replication_connection_classes are found" do
43
+ Fruit.stubs(:replication_connection_classes).returns([])
44
+ lambda { Fruit.replicated }.should raise_error
45
+ end
46
+
47
+ end
32
48
  end
33
49
 
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,25 @@
1
- #$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- #$LOAD_PATH.unshift(File.dirname(__FILE__))
3
1
  require 'connection_manager'
4
2
  require 'rspec'
5
3
  require 'bundler/setup'
4
+ require 'active_record'
5
+ require 'factory_girl'
6
+ require 'helpers/database_spec_helper'
6
7
 
7
- RSpec.configure do |config|
8
-
8
+ TestDB.connect
9
+ TestMigrations.up
10
+ FactoryGirl.find_definitions
9
11
 
12
+ RSpec.configure do |config|
13
+ config.mock_with :mocha
14
+ # Loads database.yml and establishes primary connection
15
+ # Create tables when tests are completed
16
+ config.before(:all) {
17
+ require 'helpers/models_spec_helper'
18
+ }
19
+ # Drops tables when tests are completed
20
+ config.after(:each){
21
+ TestDB.clean
22
+ }
10
23
  end
11
24
 
12
25
 
13
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-29 00:00:00.000000000Z
12
+ date: 2011-12-01 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70269785054740 !ruby/object:Gem::Requirement
16
+ requirement: &70351911140200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70269785054740
24
+ version_requirements: *70351911140200
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70351911139780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70351911139780
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rspec
27
- requirement: &70269785054320 !ruby/object:Gem::Requirement
38
+ requirement: &70351911139320 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70269785054320
46
+ version_requirements: *70351911139320
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: autotest
38
- requirement: &70269785053860 !ruby/object:Gem::Requirement
49
+ requirement: &70351911138900 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,21 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70269785053860
57
+ version_requirements: *70351911138900
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: mocha
49
- requirement: &70269785082360 !ruby/object:Gem::Requirement
60
+ requirement: &70351911138480 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70351911138480
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: &70351911138060 !ruby/object:Gem::Requirement
50
72
  none: false
51
73
  requirements:
52
74
  - - ! '>='
@@ -54,7 +76,7 @@ dependencies:
54
76
  version: '0'
55
77
  type: :development
56
78
  prerelease: false
57
- version_requirements: *70269785082360
79
+ version_requirements: *70351911138060
58
80
  description: Simplifies connecting to Muliple and Replication databases with rails
59
81
  and active_record
60
82
  email:
@@ -64,6 +86,7 @@ extensions: []
64
86
  extra_rdoc_files: []
65
87
  files:
66
88
  - .gitignore
89
+ - .rspec
67
90
  - Gemfile
68
91
  - Gemfile.lock
69
92
  - LICENSE.txt
@@ -76,58 +99,16 @@ files:
76
99
  - lib/connection_manager/connections.rb
77
100
  - lib/connection_manager/replication_builder.rb
78
101
  - lib/connection_manager/version.rb
102
+ - spec/database.yml
103
+ - spec/factories.rb
104
+ - spec/helpers/database_spec_helper.rb
105
+ - spec/helpers/models_spec_helper.rb
106
+ - spec/integration/replication_builder_spec.rb
107
+ - spec/lib/associations_spec.rb
108
+ - spec/lib/connections_spec.rb
109
+ - spec/lib/replication_builder_spec.rb
79
110
  - spec/spec.opts
80
111
  - spec/spec_helper.rb
81
- - test_app/.gitignore
82
- - test_app/.rspec
83
- - test_app/Gemfile
84
- - test_app/Gemfile.lock
85
- - test_app/README
86
- - test_app/Rakefile
87
- - test_app/app/models/.gitkeep
88
- - test_app/app/models/basket.rb
89
- - test_app/app/models/fruit.rb
90
- - test_app/app/models/fruit_basket.rb
91
- - test_app/app/models/region.rb
92
- - test_app/app/models/type.rb
93
- - test_app/config.ru
94
- - test_app/config/application.rb
95
- - test_app/config/boot.rb
96
- - test_app/config/database.yml
97
- - test_app/config/environment.rb
98
- - test_app/config/environments/development.rb
99
- - test_app/config/environments/production.rb
100
- - test_app/config/environments/test.rb
101
- - test_app/config/initializers/backtrace_silencers.rb
102
- - test_app/config/initializers/inflections.rb
103
- - test_app/config/initializers/load_connection_manager.rb
104
- - test_app/config/initializers/mime_types.rb
105
- - test_app/config/initializers/secret_token.rb
106
- - test_app/config/initializers/session_store.rb
107
- - test_app/config/initializers/wrap_parameters.rb
108
- - test_app/config/locales/en.yml
109
- - test_app/config/routes.rb
110
- - test_app/db/migrate/20111127040654_create_fruits.rb
111
- - test_app/db/migrate/20111127040720_create_baskets.rb
112
- - test_app/db/migrate/20111127040846_create_fruit_baskets.rb
113
- - test_app/db/migrate/20111127040915_create_regions.rb
114
- - test_app/db/migrate/20111127060322_create_types.rb
115
- - test_app/db/schema.rb
116
- - test_app/db/seeds.rb
117
- - test_app/log/.gitkeep
118
- - test_app/script/rails
119
- - test_app/spec/connection_manager/associations_spec.rb
120
- - test_app/spec/connection_manager/connections_spec.rb
121
- - test_app/spec/connection_manager/replication_builder_spec.rb
122
- - test_app/spec/factories/baskets.rb
123
- - test_app/spec/factories/fruit_baskets.rb
124
- - test_app/spec/factories/fruits.rb
125
- - test_app/spec/factories/regions.rb
126
- - test_app/spec/factories/types.rb
127
- - test_app/spec/models/type_spec.rb
128
- - test_app/spec/spec.opts
129
- - test_app/spec/spec_helper.rb
130
- - test_app/test_app
131
112
  homepage: ''
132
113
  licenses: []
133
114
  post_install_message:
@@ -154,5 +135,13 @@ specification_version: 3
154
135
  summary: Simplifies connecting to Muliple and Replication databases with rails and
155
136
  active_record
156
137
  test_files:
138
+ - spec/database.yml
139
+ - spec/factories.rb
140
+ - spec/helpers/database_spec_helper.rb
141
+ - spec/helpers/models_spec_helper.rb
142
+ - spec/integration/replication_builder_spec.rb
143
+ - spec/lib/associations_spec.rb
144
+ - spec/lib/connections_spec.rb
145
+ - spec/lib/replication_builder_spec.rb
157
146
  - spec/spec.opts
158
147
  - spec/spec_helper.rb