Flipper 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +3 -2
- data/Rakefile +4 -1
- data/lib/flipper.rb +12 -2
- data/test/flipper_test.rb +41 -28
- metadata +21 -4
data/README
CHANGED
@@ -22,7 +22,8 @@ database on the fly for each HTTP requests. This is what Flipper does.
|
|
22
22
|
|
23
23
|
== Status
|
24
24
|
|
25
|
-
The current status is quite good. Flipper is currently used on a daily basis on
|
25
|
+
The current status is quite good. Flipper is currently used on a daily basis on
|
26
|
+
commercial projects.
|
26
27
|
|
27
28
|
|
28
29
|
== Installation
|
@@ -48,7 +49,7 @@ To use flipper just include the <code>Flipper</code> module and add a before fil
|
|
48
49
|
The collection PER COLLECTION is licensed as follows:
|
49
50
|
|
50
51
|
Ruby Facets
|
51
|
-
Copyright (c)
|
52
|
+
Copyright (c) 2007 Philippe Hanrigou & Dan Manges
|
52
53
|
|
53
54
|
Distributed under the terms of the Ruby license.
|
54
55
|
|
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ Gem::manage_gems
|
|
29
29
|
specification = Gem::Specification.new do |s|
|
30
30
|
s.name = "Flipper"
|
31
31
|
s.summary = "Switch your Rails database on the fly for each HTTP request. Useful for integration and acceptance testing (test isolation & state management)."
|
32
|
-
s.version = "1.
|
32
|
+
s.version = "1.1"
|
33
33
|
s.author = 'Philippe Hanrigou & Dan Manges'
|
34
34
|
s.description = s.summary
|
35
35
|
s.email = 'flipper-developer@rubyforge.org'
|
@@ -43,6 +43,9 @@ specification = Gem::Specification.new do |s|
|
|
43
43
|
s.autorequire = 'flipper'
|
44
44
|
s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'Rakefile'].to_a
|
45
45
|
s.test_file = "test/all_tests.rb"
|
46
|
+
|
47
|
+
s.add_dependency('dust', '>= 1.0.5')
|
48
|
+
s.add_dependency('mocha', '>= 0.5.5')
|
46
49
|
end
|
47
50
|
|
48
51
|
Rake::GemPackageTask.new(specification) do |package|
|
data/lib/flipper.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
# class ApplicationController < ActionController::Base
|
5
5
|
# include Flipper
|
6
6
|
#
|
7
|
-
# before_filter :switch_database #
|
7
|
+
# before_filter :switch_database # unless RAILS_ENV == "production"
|
8
8
|
#
|
9
9
|
# ...
|
10
10
|
# end
|
@@ -20,7 +20,7 @@ module Flipper
|
|
20
20
|
# If anything goes wrong while switching database, attempts to rollback to the previous database configuration
|
21
21
|
# before raising an exception.
|
22
22
|
def switch_database
|
23
|
-
database_configuration =
|
23
|
+
database_configuration = flipper_database_configuration
|
24
24
|
return unless database_configuration
|
25
25
|
|
26
26
|
prior_configuration = ActiveRecord::Base.connection.instance_variable_get(:@config)
|
@@ -33,5 +33,15 @@ module Flipper
|
|
33
33
|
raise
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def flipper_database_configuration
|
40
|
+
if params[:flipper_db_config]
|
41
|
+
params[:flipper_db_config][:port] = params[:flipper_db_config][:port].to_i if params[:flipper_db_config][:port]
|
42
|
+
return params[:flipper_db_config]
|
43
|
+
end
|
44
|
+
session[:flipper_db_config]
|
45
|
+
end
|
36
46
|
|
37
47
|
end
|
data/test/flipper_test.rb
CHANGED
@@ -17,36 +17,17 @@ unit_tests do
|
|
17
17
|
@controller = Class.new { include Flipper }.new
|
18
18
|
end
|
19
19
|
|
20
|
-
test "controller establishes new db connection and clears active connection when
|
20
|
+
test "controller establishes new db connection and clears active connection when there is a flipper specific database_connection" do
|
21
21
|
@controller.stubs(:session).returns({})
|
22
|
-
@controller.stubs(:
|
23
|
-
ActiveRecord::Base.stubs(:connection).returns(stub_everything)
|
24
|
-
ActiveRecord::Base.expects(:clear_active_connections!)
|
25
|
-
ActiveRecord::Base.expects(:establish_connection).with(:connection_hash)
|
26
|
-
@controller.send(:switch_database)
|
27
|
-
end
|
28
|
-
|
29
|
-
test "controller establishes new db connection and clears active connection when receiving database_connection from session" do
|
30
|
-
@controller.stubs(:session).returns(:flipper_db_config => :connection_hash)
|
31
|
-
@controller.stubs(:params).returns(:flipper_db_config => nil)
|
22
|
+
@controller.stubs(:flipper_database_configuration).returns(:connection_hash)
|
32
23
|
ActiveRecord::Base.stubs(:connection).returns(stub_everything)
|
33
24
|
ActiveRecord::Base.expects(:clear_active_connections!)
|
34
25
|
ActiveRecord::Base.expects(:establish_connection).with(:connection_hash)
|
35
26
|
@controller.send(:switch_database)
|
36
27
|
end
|
37
28
|
|
38
|
-
test "controller
|
39
|
-
@controller.stubs(:
|
40
|
-
@controller.stubs(:params).returns(:flipper_db_config => :params_connection_hash)
|
41
|
-
ActiveRecord::Base.stubs(:connection).returns(stub_everything)
|
42
|
-
ActiveRecord::Base.expects(:clear_active_connections!)
|
43
|
-
ActiveRecord::Base.expects(:establish_connection).with(:params_connection_hash)
|
44
|
-
@controller.send(:switch_database)
|
45
|
-
end
|
46
|
-
|
47
|
-
test "controller does not establish a new db connection nor clears active connection when there is no database_connection neither in params nor in session" do
|
48
|
-
@controller.stubs(:session).returns({})
|
49
|
-
@controller.stubs(:params).returns(:flipper_db_config => nil)
|
29
|
+
test "controller does not establish a new db connection nor clears active connection when there is no flipper specific database_connection" do
|
30
|
+
@controller.stubs(:flipper_database_configuration).returns(nil)
|
50
31
|
ActiveRecord::Base.stubs(:connection).returns(stub_everything)
|
51
32
|
ActiveRecord::Base.expects(:clear_active_connections!).never
|
52
33
|
ActiveRecord::Base.expects(:establish_connection).never
|
@@ -54,18 +35,17 @@ unit_tests do
|
|
54
35
|
end
|
55
36
|
|
56
37
|
test "database configuration is stored in session after passed in through params" do
|
57
|
-
@controller.stubs(:session).returns(
|
58
|
-
@controller.stubs(:
|
38
|
+
@controller.stubs(:session).returns({})
|
39
|
+
@controller.stubs(:flipper_database_configuration).returns(:connection_hash)
|
59
40
|
ActiveRecord::Base.stubs(:connection).returns(stub_everything)
|
60
41
|
ActiveRecord::Base.stubs(:clear_active_connections!)
|
61
42
|
ActiveRecord::Base.stubs(:establish_connection)
|
62
43
|
@controller.send(:switch_database)
|
63
|
-
assert_equal :connection_hash, session[:flipper_db_config]
|
44
|
+
assert_equal :connection_hash, @controller.session[:flipper_db_config]
|
64
45
|
end
|
65
46
|
|
66
47
|
test "controller restores original db connection and raises exception when establishing a new database_connection fails" do
|
67
|
-
@controller.stubs(:
|
68
|
-
@controller.stubs(:params).returns(:flipper_db_config => :connection_hash)
|
48
|
+
@controller.stubs(:flipper_database_configuration).returns(:connection_hash)
|
69
49
|
ActiveRecord::Base.stubs(:connection).returns(connection = stub)
|
70
50
|
connection.instance_variable_set "@config", :old_configuration
|
71
51
|
ActiveRecord::Base.expects(:clear_active_connections!)
|
@@ -75,4 +55,37 @@ unit_tests do
|
|
75
55
|
assert_raises(RuntimeError) { @controller.send(:switch_database) }
|
76
56
|
end
|
77
57
|
|
58
|
+
test "flipper_database_configuration returns nil when no configuration is defined in params or session" do
|
59
|
+
@controller.stubs(:session).returns({})
|
60
|
+
@controller.stubs(:params).returns({})
|
61
|
+
assert_nil @controller.send :flipper_database_configuration
|
62
|
+
end
|
63
|
+
|
64
|
+
test "flipper_database_configuration returns flipper_db_config parameters when receiving database_connection from params" do
|
65
|
+
the_config = { :foo => :bar }
|
66
|
+
@controller.stubs(:session).returns({})
|
67
|
+
@controller.stubs(:params).returns(:flipper_db_config => the_config )
|
68
|
+
assert_equal the_config, @controller.send(:flipper_database_configuration)
|
69
|
+
end
|
70
|
+
|
71
|
+
test "flipper_database_configuration returns configuration defined in session when not specified in HTTP parameters" do
|
72
|
+
@controller.stubs(:session).returns({ :flipper_db_config => :connection_hash })
|
73
|
+
@controller.stubs(:params).returns({})
|
74
|
+
assert_equal :connection_hash, @controller.send(:flipper_database_configuration)
|
75
|
+
end
|
76
|
+
|
77
|
+
test "flipper_database_configuration convert port to integer when specified in HTTP parameters database configuration" do
|
78
|
+
@controller.stubs(:session).returns({})
|
79
|
+
@controller.stubs(:params).returns(:flipper_db_config => { :port => "1234" })
|
80
|
+
config = @controller.send(:flipper_database_configuration)
|
81
|
+
assert_equal 1234, config[:port]
|
82
|
+
end
|
83
|
+
|
84
|
+
test "database configuration defined in parameters overrides any configuration defined in session" do
|
85
|
+
the_config = { :foo => :bar }
|
86
|
+
@controller.stubs(:session).returns({ :flipper_db_config => :session_connection_hash })
|
87
|
+
@controller.stubs(:params).returns({ :flipper_db_config => the_config })
|
88
|
+
assert_equal the_config, @controller.send(:flipper_database_configuration)
|
89
|
+
end
|
90
|
+
|
78
91
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: Flipper
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "1.
|
7
|
-
date: 2007-
|
6
|
+
version: "1.1"
|
7
|
+
date: 2007-11-07 00:00:00 -05:00
|
8
8
|
summary: Switch your Rails database on the fly for each HTTP request. Useful for integration and acceptance testing (test isolation & state management).
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -50,5 +50,22 @@ extensions: []
|
|
50
50
|
|
51
51
|
requirements: []
|
52
52
|
|
53
|
-
dependencies:
|
54
|
-
|
53
|
+
dependencies:
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: dust
|
56
|
+
version_requirement:
|
57
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.0.5
|
62
|
+
version:
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: mocha
|
65
|
+
version_requirement:
|
66
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.5.5
|
71
|
+
version:
|