lookout-vanity 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/.autotest +22 -0
  2. data/.gitignore +10 -0
  3. data/.rvmrc +3 -0
  4. data/.travis.yml +15 -0
  5. data/Appraisals +15 -0
  6. data/CHANGELOG +381 -0
  7. data/Gemfile +25 -0
  8. data/Gemfile.lock +110 -0
  9. data/MIT-LICENSE +21 -0
  10. data/README.rdoc +109 -0
  11. data/Rakefile +169 -0
  12. data/bin/vanity +16 -0
  13. data/doc/_config.yml +2 -0
  14. data/doc/_layouts/_header.html +34 -0
  15. data/doc/_layouts/page.html +47 -0
  16. data/doc/_metrics.textile +12 -0
  17. data/doc/ab_testing.textile +210 -0
  18. data/doc/configuring.textile +45 -0
  19. data/doc/contributing.textile +93 -0
  20. data/doc/credits.textile +23 -0
  21. data/doc/css/page.css +83 -0
  22. data/doc/css/print.css +43 -0
  23. data/doc/css/syntax.css +7 -0
  24. data/doc/email.textile +129 -0
  25. data/doc/experimental.textile +31 -0
  26. data/doc/faq.textile +8 -0
  27. data/doc/identity.textile +43 -0
  28. data/doc/images/ab_in_dashboard.png +0 -0
  29. data/doc/images/clear_winner.png +0 -0
  30. data/doc/images/price_options.png +0 -0
  31. data/doc/images/sidebar_test.png +0 -0
  32. data/doc/images/signup_metric.png +0 -0
  33. data/doc/images/vanity.png +0 -0
  34. data/doc/index.textile +91 -0
  35. data/doc/metrics.textile +231 -0
  36. data/doc/rails.textile +89 -0
  37. data/doc/site.js +27 -0
  38. data/gemfiles/rails3.gemfile +20 -0
  39. data/gemfiles/rails3.gemfile.lock +135 -0
  40. data/gemfiles/rails31.gemfile +20 -0
  41. data/gemfiles/rails31.gemfile.lock +146 -0
  42. data/gemfiles/rails32.gemfile +20 -0
  43. data/gemfiles/rails32.gemfile.lock +144 -0
  44. data/generators/templates/vanity_migration.rb +53 -0
  45. data/generators/vanity_generator.rb +8 -0
  46. data/lib/generators/templates/vanity_migration.rb +53 -0
  47. data/lib/generators/vanity_generator.rb +15 -0
  48. data/lib/vanity.rb +36 -0
  49. data/lib/vanity/adapters/abstract_adapter.rb +145 -0
  50. data/lib/vanity/adapters/active_record_adapter.rb +263 -0
  51. data/lib/vanity/adapters/mock_adapter.rb +157 -0
  52. data/lib/vanity/adapters/mongodb_adapter.rb +178 -0
  53. data/lib/vanity/adapters/redis_adapter.rb +160 -0
  54. data/lib/vanity/backport.rb +26 -0
  55. data/lib/vanity/commands/list.rb +21 -0
  56. data/lib/vanity/commands/report.rb +64 -0
  57. data/lib/vanity/commands/upgrade.rb +34 -0
  58. data/lib/vanity/experiment/ab_test.rb +582 -0
  59. data/lib/vanity/experiment/base.rb +218 -0
  60. data/lib/vanity/frameworks.rb +16 -0
  61. data/lib/vanity/frameworks/rails.rb +325 -0
  62. data/lib/vanity/helpers.rb +71 -0
  63. data/lib/vanity/images/x.gif +0 -0
  64. data/lib/vanity/metric/active_record.rb +93 -0
  65. data/lib/vanity/metric/base.rb +244 -0
  66. data/lib/vanity/metric/google_analytics.rb +83 -0
  67. data/lib/vanity/metric/remote.rb +53 -0
  68. data/lib/vanity/playground.rb +408 -0
  69. data/lib/vanity/templates/_ab_test.erb +28 -0
  70. data/lib/vanity/templates/_experiment.erb +5 -0
  71. data/lib/vanity/templates/_experiments.erb +7 -0
  72. data/lib/vanity/templates/_metric.erb +14 -0
  73. data/lib/vanity/templates/_metrics.erb +13 -0
  74. data/lib/vanity/templates/_report.erb +27 -0
  75. data/lib/vanity/templates/_vanity.js.erb +20 -0
  76. data/lib/vanity/templates/flot.min.js +1 -0
  77. data/lib/vanity/templates/jquery.min.js +19 -0
  78. data/lib/vanity/templates/vanity.css +26 -0
  79. data/lib/vanity/templates/vanity.js +82 -0
  80. data/lib/vanity/version.rb +11 -0
  81. data/lookout-vanity.gemspec +26 -0
  82. data/test/adapters/redis_adapter_test.rb +17 -0
  83. data/test/dummy/Rakefile +7 -0
  84. data/test/dummy/app/controllers/application_controller.rb +3 -0
  85. data/test/dummy/app/helpers/application_helper.rb +2 -0
  86. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  87. data/test/dummy/config.ru +4 -0
  88. data/test/dummy/config/application.rb +44 -0
  89. data/test/dummy/config/boot.rb +10 -0
  90. data/test/dummy/config/database.yml +5 -0
  91. data/test/dummy/config/environment.rb +5 -0
  92. data/test/dummy/config/environments/development.rb +26 -0
  93. data/test/dummy/config/environments/production.rb +49 -0
  94. data/test/dummy/config/environments/test.rb +35 -0
  95. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  96. data/test/dummy/config/initializers/inflections.rb +10 -0
  97. data/test/dummy/config/initializers/mime_types.rb +5 -0
  98. data/test/dummy/config/initializers/secret_token.rb +7 -0
  99. data/test/dummy/config/initializers/session_store.rb +8 -0
  100. data/test/dummy/config/locales/en.yml +5 -0
  101. data/test/dummy/config/routes.rb +58 -0
  102. data/test/dummy/public/favicon.ico +0 -0
  103. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  104. data/test/dummy/script/rails +6 -0
  105. data/test/experiment/ab_test.rb +859 -0
  106. data/test/experiment/base_test.rb +150 -0
  107. data/test/experiments/age_and_zipcode.rb +19 -0
  108. data/test/experiments/metrics/cheers.rb +3 -0
  109. data/test/experiments/metrics/signups.rb +2 -0
  110. data/test/experiments/metrics/yawns.rb +3 -0
  111. data/test/experiments/null_abc.rb +5 -0
  112. data/test/metric/active_record_test.rb +307 -0
  113. data/test/metric/base_test.rb +293 -0
  114. data/test/metric/google_analytics_test.rb +104 -0
  115. data/test/metric/remote_test.rb +109 -0
  116. data/test/myapp/app/controllers/application_controller.rb +2 -0
  117. data/test/myapp/app/controllers/main_controller.rb +7 -0
  118. data/test/myapp/config/boot.rb +110 -0
  119. data/test/myapp/config/environment.rb +10 -0
  120. data/test/myapp/config/environments/production.rb +0 -0
  121. data/test/myapp/config/routes.rb +3 -0
  122. data/test/passenger_test.rb +45 -0
  123. data/test/playground_test.rb +26 -0
  124. data/test/rails_dashboard_test.rb +37 -0
  125. data/test/rails_helper_test.rb +38 -0
  126. data/test/rails_test.rb +412 -0
  127. data/test/test_helper.rb +168 -0
  128. metadata +268 -0
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -0,0 +1,7 @@
1
+ class MainController < ApplicationController
2
+ def index
3
+ render :text=>"#{Vanity.playground.connection}\n#{Vanity.playground.connection.object_id}"
4
+ rescue Error=>ex
5
+ puts $!
6
+ end
7
+ end
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.1'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
@@ -0,0 +1,10 @@
1
+ require File.join(File.dirname(__FILE__), 'boot')
2
+
3
+ Rails::Initializer.run do |config|
4
+ config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
5
+ config.action_controller.session = { :key=>"_myapp_session", :secret=>"Stay hungry. Stay foolish. -- Steve Jobs" }
6
+ config.after_initialize do
7
+ $:.unshift File.dirname(__FILE__) + "/../../../lib/"
8
+ require "vanity"
9
+ end
10
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect "/", :controller=>:main, :action=>:index
3
+ end
@@ -0,0 +1,45 @@
1
+ require "test/test_helper"
2
+
3
+ #not supported for rails3
4
+ unless defined?(Rails::Railtie)
5
+ require "phusion_passenger/spawn_manager"
6
+ class PassengerTest < Test::Unit::TestCase
7
+ def setup
8
+ super
9
+ ActiveRecord::Base.connection.disconnect! # Otherwise AR metric tests fail
10
+ @original = Vanity.playground.connection
11
+ File.unlink "test/myapp/config/vanity.yml" rescue nil
12
+ File.open("test/myapp/config/vanity.yml", "w") do |io|
13
+ io.write YAML.dump({ "production"=>DATABASE })
14
+ end
15
+ @server = PhusionPassenger::SpawnManager.new
16
+ @server.start
17
+ Thread.pass until @server.started?
18
+ app_root = File.expand_path("myapp", File.dirname(__FILE__))
19
+ @app = @server.spawn_application "app_root"=>app_root, "spawn_method"=>"smart-lv2"
20
+ end
21
+
22
+ def test_reconnect
23
+ sleep 0.1
24
+ case @app.listen_socket_type
25
+ when "tcp" ; socket = TCPSocket.new(*@app.listen_socket_name.split(":"))
26
+ when "unix"; socket = UNIXSocket.new(@app.listen_socket_name)
27
+ else fail
28
+ end
29
+ channel = PhusionPassenger::MessageChannel.new(socket)
30
+ request = {"REQUEST_PATH"=>"/", "REQUEST_METHOD"=>"GET", "QUERY_STRING"=>" "}
31
+ channel.write_scalar request.to_a.join("\0")
32
+ response = socket.read.split("\r\n\r\n").last
33
+ socket.close
34
+ conn, obj_id = response.split("\n")
35
+ assert_equal @original.to_s, conn
36
+ assert_not_equal @original.object_id.to_s, obj_id
37
+ end
38
+
39
+ def teardown
40
+ super
41
+ @server.stop
42
+ File.unlink "test/myapp/config/vanity.yml"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ require "test/test_helper"
2
+
3
+ class PlaygroundTest < Test::Unit::TestCase
4
+
5
+ def test_has_one_global_instance
6
+ assert instance = Vanity.playground
7
+ assert_equal instance, Vanity.playground
8
+ end
9
+
10
+ def test_be_use_js
11
+ assert !Vanity.playground.using_js?
12
+ Vanity.playground.use_js!
13
+ assert Vanity.playground.using_js?
14
+ end
15
+
16
+ def test_chooses_path_sets_default
17
+ assert_equal Vanity.playground.add_participant_path, Vanity::Playground::DEFAULT_ADD_PARTICIPANT_PATH
18
+ end
19
+
20
+ def test_reconnects_with_existing_connection
21
+ Vanity.playground.establish_connection "mock:/"
22
+ Vanity.playground.reconnect!
23
+ assert_equal Vanity.playground.connection.to_s, "mock:/"
24
+ end
25
+
26
+ end
@@ -0,0 +1,37 @@
1
+ require "test/test_helper"
2
+
3
+ class VanityController < ActionController::Base
4
+ include Vanity::Rails::Dashboard
5
+ end
6
+
7
+ # Pages accessible to everyone, e.g. sign in, community search.
8
+ class RailsDashboardTest < ActionController::TestCase
9
+ tests VanityController
10
+
11
+ def setup
12
+ Vanity.playground.collecting = true
13
+ metric :sugar_high
14
+ new_ab_test :food do
15
+ alternatives :apple, :orange
16
+ metrics :sugar_high
17
+ identify { '1' }
18
+ end
19
+ end
20
+
21
+ def test_add_participant
22
+ xhr :post, :add_participant, :e => "food", :a => 0
23
+ assert_response :success
24
+ assert @response.body.blank?
25
+ end
26
+
27
+ def test_add_participant_no_params
28
+ xhr :post, :add_participant
29
+ assert_response :not_found
30
+ assert @response.body.blank?
31
+ end
32
+
33
+ def test_chooses
34
+ xhr :post, :chooses, :e => "food", :a => 0
35
+ assert_response :success
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require "test/test_helper"
2
+
3
+ class RailsHelperTest < ActionView::TestCase
4
+ include Vanity::Rails::Helpers
5
+
6
+ def setup
7
+ super
8
+ metric :sugar_high
9
+ new_ab_test :pie_or_cake do
10
+ metrics :sugar_high
11
+ identify { '1' }
12
+ alternatives :pie, :cake
13
+ end
14
+ end
15
+
16
+ def test_ab_test_returns_one_of_the_alternatives
17
+ assert [:pie, :cake].include?(ab_test(:pie_or_cake))
18
+ end
19
+
20
+ def test_ab_test_using_js_returns_the_same_alternative
21
+ Vanity.playground.use_js!
22
+ result = ab_test(:pie_or_cake)
23
+ assert [:pie, :cake].include?(result)
24
+ 10.times do
25
+ assert result == ab_test(:pie_or_cake)
26
+ end
27
+ end
28
+
29
+ def test_vanity_track_url_for_returns_url_with_identity_and_metrics
30
+ self.expects(:url_for).with(:controller => "controller", :action => "action", :_identity => '123', :_track => :sugar_high)
31
+ vanity_track_url_for("123", :sugar_high, :controller => "controller", :action => "action")
32
+ end
33
+
34
+ def test_vanity_tracking_image
35
+ self.expects(:url_for).with(:controller => :vanity, :action => :image, :_identity => '123', :_track => :sugar_high).returns("/url")
36
+ assert_equal image_tag("/url", :width => "1px", :height => "1px", :alt => ""), vanity_tracking_image("123", :sugar_high, options = {})
37
+ end
38
+ end
@@ -0,0 +1,412 @@
1
+ require "test/test_helper"
2
+
3
+ class UseVanityController < ActionController::Base
4
+ attr_accessor :current_user
5
+
6
+ def index
7
+ render :text=>ab_test(:pie_or_cake)
8
+ end
9
+
10
+ def js
11
+ ab_test(:pie_or_cake)
12
+ render :inline => "<%= vanity_js -%>"
13
+ end
14
+ end
15
+
16
+ # Pages accessible to everyone, e.g. sign in, community search.
17
+ class UseVanityControllerTest < ActionController::TestCase
18
+ tests UseVanityController
19
+
20
+ def setup
21
+ super
22
+ metric :sugar_high
23
+ new_ab_test :pie_or_cake do
24
+ metrics :sugar_high
25
+ end
26
+ UseVanityController.class_eval do
27
+ use_vanity :current_user
28
+ end
29
+ if ::Rails.respond_to?(:application) # Rails 3 configuration
30
+ ::Rails.application.config.session_options[:domain] = '.foo.bar'
31
+ end
32
+ end
33
+
34
+ def test_render_js_for_tests
35
+ Vanity.playground.use_js!
36
+ get :js
37
+ assert_match /script.*e=pie_or_cake.*script/m, @response.body
38
+ end
39
+
40
+ def test_chooses_sets_alternatives_for_rails_tests
41
+ experiment(:pie_or_cake).chooses(true)
42
+ get :index
43
+ assert_equal 'true', @response.body
44
+
45
+ experiment(:pie_or_cake).chooses(false)
46
+ get :index
47
+ assert_equal 'false', @response.body
48
+ end
49
+
50
+
51
+ def test_vanity_cookie_is_persistent
52
+ get :index
53
+ cookie = @response["Set-Cookie"].to_s
54
+ assert_match /vanity_id=[a-f0-9]{32};/, cookie
55
+ expires = cookie[/expires=(.*)(;|$)/, 1]
56
+ assert expires
57
+ assert_in_delta Time.parse(expires), Time.now + 1.month, 1.day
58
+ end
59
+
60
+ def test_vanity_cookie_default_id
61
+ get :index
62
+ assert cookies['vanity_id'] =~ /^[a-f0-9]{32}$/
63
+ end
64
+
65
+ def test_vanity_cookie_retains_id
66
+ @request.cookies['vanity_id'] = "from_last_time"
67
+ get :index
68
+ assert_equal "from_last_time", cookies['vanity_id']
69
+ end
70
+
71
+ def test_vanity_identity_set_from_cookie
72
+ @request.cookies['vanity_id'] = "from_last_time"
73
+ get :index
74
+ assert_equal "from_last_time", @controller.send(:vanity_identity)
75
+ end
76
+
77
+ def test_vanity_identity_set_from_user
78
+ @controller.current_user = mock("user", :id=>"user_id")
79
+ get :index
80
+ assert_equal "user_id", @controller.send(:vanity_identity)
81
+ end
82
+
83
+ def test_vanity_identity_with_no_user_model
84
+ UseVanityController.class_eval do
85
+ use_vanity nil
86
+ end
87
+ @controller.current_user = Object.new
88
+ get :index
89
+ assert cookies['vanity_id'] =~ /^[a-f0-9]{32}$/
90
+ end
91
+
92
+ def test_vanity_identity_set_with_block
93
+ UseVanityController.class_eval do
94
+ attr_accessor :project_id
95
+ use_vanity { |controller| controller.project_id }
96
+ end
97
+ @controller.project_id = "576"
98
+ get :index
99
+ assert_equal "576", @controller.send(:vanity_identity)
100
+ end
101
+
102
+ def test_vanity_identity_set_with_indentity_paramater
103
+ get :index, :_identity => "id_from_params"
104
+ assert_equal "id_from_params", @controller.send(:vanity_identity)
105
+
106
+ @request.cookies['vanity_id'] = "old_id"
107
+ get :index, :_identity => "id_from_params"
108
+ assert_equal "id_from_params", @controller.send(:vanity_identity)
109
+ assert cookies['vanity_id'], "id_from_params"
110
+ end
111
+
112
+ # query parameter filter
113
+
114
+ def test_redirects_and_loses_vanity_query_parameter
115
+ get :index, :foo=>"bar", :_vanity=>"567"
116
+ assert_redirected_to "/use_vanity?foo=bar"
117
+ end
118
+
119
+ def test_sets_choices_from_vanity_query_parameter
120
+ first = experiment(:pie_or_cake).alternatives.first
121
+ fingerprint = experiment(:pie_or_cake).fingerprint(first)
122
+ 10.times do
123
+ @controller = nil ; setup_controller_request_and_response
124
+ get :index, :_vanity => fingerprint
125
+ assert_equal experiment(:pie_or_cake).choose, experiment(:pie_or_cake).alternatives.first
126
+ assert experiment(:pie_or_cake).showing?(first)
127
+ end
128
+ end
129
+
130
+ def test_does_nothing_with_vanity_query_parameter_for_posts
131
+ experiment(:pie_or_cake).chooses(experiment(:pie_or_cake).alternatives.last.value)
132
+ first = experiment(:pie_or_cake).alternatives.first
133
+ fingerprint = experiment(:pie_or_cake).fingerprint(first)
134
+ post :index, :foo => "bar", :_vanity => fingerprint
135
+ assert_response :success
136
+ assert !experiment(:pie_or_cake).showing?(first)
137
+ end
138
+
139
+ def test_track_param_tracks_a_metric
140
+ get :index, :_identity => "123", :_track => "sugar_high"
141
+ assert_equal experiment(:pie_or_cake).alternatives[0].converted, 1
142
+ end
143
+
144
+ def test_cookie_domain_from_rails_configuration
145
+ get :index
146
+ assert_match /domain=.foo.bar/, @response["Set-Cookie"] if ::Rails.respond_to?(:application)
147
+ end
148
+
149
+ # -- Load path --
150
+
151
+ def test_load_path
152
+ assert_equal File.expand_path("tmp/experiments"), load_rails(<<-RB)
153
+ initializer.after_initialize
154
+ $stdout << Vanity.playground.load_path
155
+ RB
156
+ end
157
+
158
+ def test_settable_load_path
159
+ assert_equal File.expand_path("tmp/predictions"), load_rails(<<-RB)
160
+ Vanity.playground.load_path = "predictions"
161
+ initializer.after_initialize
162
+ $stdout << Vanity.playground.load_path
163
+ RB
164
+ end
165
+
166
+ def test_absolute_load_path
167
+ assert_equal File.expand_path("/tmp/var"), load_rails(<<-RB)
168
+ Vanity.playground.load_path = "/tmp/var"
169
+ initializer.after_initialize
170
+ $stdout << Vanity.playground.load_path
171
+ RB
172
+ end
173
+
174
+
175
+ # -- Connection configuration --
176
+
177
+ if ENV['DB'] == 'redis'
178
+ def test_default_connection
179
+ assert_equal "redis://127.0.0.1:6379/0", load_rails(<<-RB)
180
+ initializer.after_initialize
181
+ $stdout << Vanity.playground.connection
182
+ RB
183
+ end
184
+
185
+ def test_connection_from_string
186
+ assert_equal "redis://192.168.1.1:6379/5", load_rails(<<-RB)
187
+ Vanity.playground.establish_connection "redis://192.168.1.1:6379/5"
188
+ initializer.after_initialize
189
+ $stdout << Vanity.playground.connection
190
+ RB
191
+ end
192
+
193
+ def test_connection_from_yaml
194
+ FileUtils.mkpath "tmp/config"
195
+ ENV["RAILS_ENV"] = "production"
196
+ File.open("tmp/config/vanity.yml", "w") do |io|
197
+ io.write <<-YML
198
+ production:
199
+ adapter: redis
200
+ host: somehost
201
+ database: 15
202
+ YML
203
+ end
204
+ assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
205
+ initializer.after_initialize
206
+ $stdout << Vanity.playground.connection
207
+ RB
208
+ ensure
209
+ File.unlink "tmp/config/vanity.yml"
210
+ end
211
+
212
+ def test_connection_from_yaml_url
213
+ FileUtils.mkpath "tmp/config"
214
+ ENV["RAILS_ENV"] = "production"
215
+ File.open("tmp/config/vanity.yml", "w") do |io|
216
+ io.write <<-YML
217
+ production: redis://somehost/15
218
+ YML
219
+ end
220
+ assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
221
+ initializer.after_initialize
222
+ $stdout << Vanity.playground.connection
223
+ RB
224
+ ensure
225
+ File.unlink "tmp/config/vanity.yml"
226
+ end
227
+
228
+ def test_connection_from_yaml_with_erb
229
+ FileUtils.mkpath "tmp/config"
230
+ ENV["RAILS_ENV"] = "production"
231
+ # Pass storage URL through environment like heroku does
232
+ ENV["REDIS_URL"] = "redis://somehost:6379/15"
233
+ File.open("tmp/config/vanity.yml", "w") do |io|
234
+ io.write <<-YML
235
+ production: <%= ENV['REDIS_URL'] %>
236
+ YML
237
+ end
238
+ assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
239
+ initializer.after_initialize
240
+ $stdout << Vanity.playground.connection
241
+ RB
242
+ ensure
243
+ File.unlink "tmp/config/vanity.yml"
244
+ end
245
+
246
+ def test_connection_from_redis_yml
247
+ FileUtils.mkpath "tmp/config"
248
+ yml = File.open("tmp/config/redis.yml", "w")
249
+ yml << "production: internal.local:6379\n"
250
+ yml.flush
251
+ assert_equal "redis://internal.local:6379/0", load_rails(<<-RB)
252
+ initializer.after_initialize
253
+ $stdout << Vanity.playground.connection
254
+ RB
255
+ ensure
256
+ File.unlink yml.path
257
+ end
258
+
259
+
260
+ end
261
+
262
+ if ENV['DB'] == 'mongo'
263
+ def test_mongo_connection_from_yaml
264
+ FileUtils.mkpath "tmp/config"
265
+ File.open("tmp/config/vanity.yml", "w") do |io|
266
+ io.write <<-YML
267
+ mongodb:
268
+ adapter: mongodb
269
+ host: localhost
270
+ port: 27017
271
+ database: vanity_test
272
+ YML
273
+ end
274
+
275
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails(<<-RB, "mongodb")
276
+ initializer.after_initialize
277
+ $stdout << Vanity.playground.connection
278
+ RB
279
+ ensure
280
+ File.unlink "tmp/config/vanity.yml"
281
+ end
282
+
283
+ unless ENV['CI'] == 'true'
284
+ def test_mongodb_replica_set_connection
285
+ FileUtils.mkpath "tmp/config"
286
+ File.open("tmp/config/vanity.yml", "w") do |io|
287
+ io.write <<-YML
288
+ mongodb:
289
+ adapter: mongodb
290
+ hosts:
291
+ - localhost
292
+ port: 27017
293
+ database: vanity_test
294
+ YML
295
+ end
296
+
297
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails(<<-RB, "mongodb")
298
+ initializer.after_initialize
299
+ $stdout << Vanity.playground.connection
300
+ RB
301
+
302
+ assert_equal "Mongo::ReplSetConnection", load_rails(<<-RB, "mongodb")
303
+ initializer.after_initialize
304
+ $stdout << Vanity.playground.connection.mongo.class
305
+ RB
306
+ ensure
307
+ File.unlink "tmp/config/vanity.yml"
308
+ end
309
+ end
310
+ end
311
+
312
+ def test_connection_from_yaml_missing
313
+ FileUtils.mkpath "tmp/config"
314
+ File.open("tmp/config/vanity.yml", "w") do |io|
315
+ io.write <<-YML
316
+ production:
317
+ adapter: redis
318
+ YML
319
+ end
320
+ assert_equal "No configuration for development", load_rails(<<-RB, "development")
321
+ initializer.after_initialize
322
+ $stdout << (Vanity.playground.connection rescue $!.message)
323
+ RB
324
+ ensure
325
+ File.unlink "tmp/config/vanity.yml"
326
+ end
327
+
328
+ def test_collection_from_vanity_yaml
329
+ FileUtils.mkpath "tmp/config"
330
+ File.open("tmp/config/vanity.yml", "w") do |io|
331
+ io.write <<-YML
332
+ production:
333
+ collecting: false
334
+ YML
335
+ end
336
+ assert_equal "false", load_rails(<<-RB)
337
+ initializer.after_initialize
338
+ $stdout << Vanity.playground.collecting?
339
+ RB
340
+ ensure
341
+ File.unlink "tmp/config/vanity.yml"
342
+ end
343
+
344
+ def test_collection_true_in_production_by_default
345
+ assert_equal "true", load_rails(<<-RB, "production")
346
+ initializer.after_initialize
347
+ $stdout << Vanity.playground.collecting?
348
+ RB
349
+ end
350
+
351
+ def test_collection_false_in_production_when_configured
352
+ assert_equal "false", load_rails(<<-RB, "production")
353
+ Vanity.playground.collecting = false
354
+ initializer.after_initialize
355
+ $stdout << Vanity.playground.collecting?
356
+ RB
357
+ end
358
+
359
+ def test_collection_false_in_development_by_default
360
+ assert_equal "false", load_rails(<<-RB, "development")
361
+ initializer.after_initialize
362
+ $stdout << Vanity.playground.collecting?
363
+ RB
364
+ end
365
+
366
+ def test_collection_true_in_development_when_configured
367
+ assert_equal "true", load_rails(<<-RB, "development")
368
+ Vanity.playground.collecting = true
369
+ initializer.after_initialize
370
+ $stdout << Vanity.playground.collecting?
371
+ RB
372
+ end
373
+
374
+ def test_collection_false_after_test!
375
+ assert_equal "false", load_rails(<<-RB, "production")
376
+ initializer.after_initialize
377
+ Vanity.playground.test!
378
+ $stdout << Vanity.playground.collecting?
379
+ RB
380
+ end
381
+
382
+ def load_rails(code, env = "production")
383
+ tmp = Tempfile.open("test.rb")
384
+ tmp.write <<-RB
385
+ $:.delete_if { |path| path[/gems\\/vanity-\\d/] }
386
+ $:.unshift File.expand_path("../lib")
387
+ RAILS_ROOT = File.expand_path(".")
388
+ RAILS_ENV = ENV['RACK_ENV'] = "#{env}"
389
+ require "initializer"
390
+ require "active_support"
391
+ Rails.configuration = Rails::Configuration.new
392
+ initializer = Rails::Initializer.new(Rails.configuration)
393
+ initializer.check_gem_dependencies
394
+ require "vanity"
395
+ RB
396
+ tmp.write code
397
+ tmp.flush
398
+ Dir.chdir "tmp" do
399
+ open("|ruby #{tmp.path}").read
400
+ end
401
+ rescue
402
+ tmp.close!
403
+ end
404
+
405
+
406
+ def teardown
407
+ super
408
+ if !rails3?
409
+ UseVanityController.send(:filter_chain).clear
410
+ end
411
+ end
412
+ end