vanity 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -108,7 +108,9 @@ class ExperimentTest < Test::Unit::TestCase
108
108
 
109
109
  def test_experiment_keeps_created_timestamp_across_definitions
110
110
  past = Date.today - 1
111
- Timecop.freeze past do
111
+ Timecop.freeze past.to_time do
112
+ puts "timecop: #{Time.now.to_i}"
113
+ assert_equal past.to_time.to_i, Time.now.to_i
112
114
  new_ab_test(:ice_cream_flavor) { metrics :happiness }
113
115
  assert_equal past.to_time.to_i, experiment(:ice_cream_flavor).created_at.to_i
114
116
  end
@@ -86,8 +86,8 @@ context "Metric tracking" do
86
86
 
87
87
  test "can tell the time" do
88
88
  metric "Yawns/sec"
89
- Timecop.freeze(today - 4) { 4.times { Vanity.playground.track! :yawns_sec } }
90
- Timecop.freeze(today - 2) { 2.times { Vanity.playground.track! :yawns_sec } }
89
+ Timecop.freeze((today - 4).to_time) { 4.times { Vanity.playground.track! :yawns_sec } }
90
+ Timecop.freeze((today - 2).to_time) { 2.times { Vanity.playground.track! :yawns_sec } }
91
91
  1.times { Vanity.playground.track! :yawns_sec }
92
92
  boredom = Vanity.playground.metric(:yawns_sec).values(today - 5, today)
93
93
  assert_equal [0,4,0,2,0,1], boredom
@@ -103,8 +103,8 @@ context "Metric tracking" do
103
103
 
104
104
  test "with count" do
105
105
  metric "Yawns/sec"
106
- Timecop.freeze(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
107
- Timecop.freeze(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
106
+ Timecop.freeze((today - 4).to_time) { Vanity.playground.track! :yawns_sec, 4 }
107
+ Timecop.freeze((today - 2).to_time) { Vanity.playground.track! :yawns_sec, 2 }
108
108
  Vanity.playground.track! :yawns_sec
109
109
  boredom = Vanity.playground.metric(:yawns_sec).values(today - 5, today)
110
110
  assert_equal [0,4,0,2,0,1], boredom
@@ -243,8 +243,8 @@ end
243
243
  context "Metric data" do
244
244
  test "explicit dates" do
245
245
  metric "Yawns/sec"
246
- Timecop.freeze(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
247
- Timecop.freeze(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
246
+ Timecop.freeze((today - 4).to_time) { Vanity.playground.track! :yawns_sec, 4 }
247
+ Timecop.freeze((today - 2).to_time) { Vanity.playground.track! :yawns_sec, 2 }
248
248
  Vanity.playground.track! :yawns_sec
249
249
  boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), Date.today - 5, Date.today)
250
250
  assert_equal [[today - 5, 0], [today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
@@ -252,8 +252,8 @@ context "Metric data" do
252
252
 
253
253
  test "start date only" do
254
254
  metric "Yawns/sec"
255
- Timecop.freeze(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
256
- Timecop.freeze(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
255
+ Timecop.freeze((today - 4).to_time) { Vanity.playground.track! :yawns_sec, 4 }
256
+ Timecop.freeze((today - 2).to_time) { Vanity.playground.track! :yawns_sec, 2 }
257
257
  Vanity.playground.track! :yawns_sec
258
258
  boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), Date.today - 4)
259
259
  assert_equal [[today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
@@ -261,8 +261,8 @@ context "Metric data" do
261
261
 
262
262
  test "start date and duration" do
263
263
  metric "Yawns/sec"
264
- Timecop.freeze(today - 4) { Vanity.playground.track! :yawns_sec, 4 }
265
- Timecop.freeze(today - 2) { Vanity.playground.track! :yawns_sec, 2 }
264
+ Timecop.freeze((today - 4).to_time) { Vanity.playground.track! :yawns_sec, 4 }
265
+ Timecop.freeze((today - 2).to_time) { Vanity.playground.track! :yawns_sec, 2 }
266
266
  Vanity.playground.track! :yawns_sec
267
267
  boredom = Vanity::Metric.data(Vanity.playground.metric(:yawns_sec), 5)
268
268
  assert_equal [[today - 4, 4], [today - 3, 0], [today - 2, 2], [today - 1, 0], [today, 1]], boredom
@@ -16,10 +16,14 @@ class PassengerTest < Test::Unit::TestCase
16
16
  @server.start
17
17
  Thread.pass until @server.started?
18
18
  app_root = File.expand_path("myapp", File.dirname(__FILE__))
19
- @app = @server.spawn_application "app_root"=>app_root, "spawn_method"=>"smart-lv2"
19
+ @app = @server.spawn_application "app_root"=>app_root, "spawn_method"=>"smart"
20
20
  end
21
21
 
22
22
  def test_reconnect
23
+ # When using AR adapter, we're not responsible to reconnect, and we're going
24
+ # to get the same "connect" (AR connection handler) either way.
25
+ # return if defined?(Vanity::Adapters::ActiveRecordAdapter) && Vanity::Adapters::ActiveRecordAdapter === Vanity.playground.connection
26
+
23
27
  sleep 0.1
24
28
  case @app.listen_socket_type
25
29
  when "tcp" ; socket = TCPSocket.new(*@app.listen_socket_name.split(":"))
@@ -38,7 +42,9 @@ class PassengerTest < Test::Unit::TestCase
38
42
 
39
43
  def teardown
40
44
  super
45
+ @server.cleanup
41
46
  @server.stop
47
+ Process.kill('SIGKILL', @app.pid.to_i) # Just in case...KIDS, GET OUT OF THE POOL!
42
48
  File.unlink "test/myapp/config/vanity.yml"
43
49
  end
44
50
  end
@@ -23,4 +23,15 @@ class PlaygroundTest < Test::Unit::TestCase
23
23
  assert_equal Vanity.playground.connection.to_s, "mock:/"
24
24
  end
25
25
 
26
+ def test_autoconnect_establishes_connection_by_default
27
+ instance = Vanity::Playground.new(:connection=>"mock:/")
28
+ assert instance.connected?
29
+ end
30
+
31
+ def test_autoconnect_can_skip_connection
32
+ Vanity::Autoconnect.stubs(:playground_should_autoconnect?).returns(false)
33
+ instance = Vanity::Playground.new(:connection=>"mock:/")
34
+ assert !instance.connected?
35
+ end
36
+
26
37
  end
@@ -41,7 +41,7 @@ class UseVanityControllerTest < ActionController::TestCase
41
41
  experiment(:pie_or_cake).chooses(true)
42
42
  get :index
43
43
  assert_equal 'true', @response.body
44
-
44
+
45
45
  experiment(:pie_or_cake).chooses(false)
46
46
  get :index
47
47
  assert_equal 'false', @response.body
@@ -98,17 +98,17 @@ class UseVanityControllerTest < ActionController::TestCase
98
98
  get :index
99
99
  assert_equal "576", @controller.send(:vanity_identity)
100
100
  end
101
-
101
+
102
102
  def test_vanity_identity_set_with_indentity_paramater
103
103
  get :index, :_identity => "id_from_params"
104
104
  assert_equal "id_from_params", @controller.send(:vanity_identity)
105
-
105
+
106
106
  @request.cookies['vanity_id'] = "old_id"
107
107
  get :index, :_identity => "id_from_params"
108
108
  assert_equal "id_from_params", @controller.send(:vanity_identity)
109
109
  assert cookies['vanity_id'], "id_from_params"
110
110
  end
111
-
111
+
112
112
  # query parameter filter
113
113
 
114
114
  def test_redirects_and_loses_vanity_query_parameter
@@ -140,52 +140,114 @@ class UseVanityControllerTest < ActionController::TestCase
140
140
  get :index, :_identity => "123", :_track => "sugar_high"
141
141
  assert_equal experiment(:pie_or_cake).alternatives[0].converted, 1
142
142
  end
143
-
143
+
144
144
  def test_cookie_domain_from_rails_configuration
145
145
  get :index
146
146
  assert_match /domain=.foo.bar/, @response["Set-Cookie"] if ::Rails.respond_to?(:application)
147
147
  end
148
148
 
149
- # -- Load path --
149
+ def teardown
150
+ super
151
+ if !rails3?
152
+ UseVanityController.send(:filter_chain).clear
153
+ end
154
+ end
155
+
156
+ end
157
+
158
+ class VanityMailer < ActionMailer::Base
159
+ include Vanity::Rails::Helpers
160
+ include ActionView::Helpers::AssetTagHelper
161
+ include ActionView::Helpers::TagHelper
162
+
163
+ def ab_test_subject(user, forced_outcome=true)
164
+ use_vanity_mailer user
165
+ experiment(:pie_or_cake).chooses(forced_outcome)
166
+
167
+ if defined?(Rails::Railtie)
168
+ mail :subject =>ab_test(:pie_or_cake).to_s
169
+ else
170
+ subject ab_test(:pie_or_cake).to_s
171
+ body ""
172
+ end
173
+ end
174
+
175
+ def ab_test_content(user)
176
+ use_vanity_mailer user
177
+
178
+ if defined?(Rails::Railtie)
179
+ mail do |format|
180
+ format.html { render :text=>view_context.vanity_tracking_image(Vanity.context.vanity_identity, :open, :host => "127.0.0.1:3000") }
181
+ end
182
+ else
183
+ body vanity_tracking_image(Vanity.context.vanity_identity, :open, :host => "127.0.0.1:3000")
184
+ end
185
+ end
186
+ end
187
+
188
+ class UseVanityMailerTest < ActionMailer::TestCase
189
+ tests VanityMailer
190
+
191
+ def setup
192
+ super
193
+ metric :sugar_high
194
+ new_ab_test :pie_or_cake do
195
+ metrics :sugar_high
196
+ end
197
+ end
198
+
199
+ def test_js_enabled_still_adds_participant
200
+ Vanity.playground.use_js!
201
+ rails3? ? VanityMailer.ab_test_subject(nil, true) : VanityMailer.deliver_ab_test_subject(nil, true)
202
+
203
+ alts = experiment(:pie_or_cake).alternatives
204
+ assert_equal 1, alts.map(&:participants).sum
205
+ end
206
+
207
+ def test_returns_different_alternatives
208
+ email = rails3? ? VanityMailer.ab_test_subject(nil, true) : VanityMailer.deliver_ab_test_subject(nil, true)
209
+ assert_equal 'true', email.subject
210
+
211
+ email = rails3? ? VanityMailer.ab_test_subject(nil, false) : VanityMailer.deliver_ab_test_subject(nil, false)
212
+ assert_equal 'false', email.subject
213
+ end
214
+
215
+ def test_tracking_image_is_rendered
216
+ email = rails3? ? VanityMailer.ab_test_content(nil) : VanityMailer.deliver_ab_test_content(nil)
217
+ assert email.body =~ /<img/
218
+ assert email.body =~ /_identity=/
219
+ end
220
+ end
221
+
222
+ class LoadPathAndConnectionConfigurationTest < Test::Unit::TestCase
150
223
 
151
224
  def test_load_path
152
- assert_equal File.expand_path("tmp/experiments"), load_rails(<<-RB)
153
- initializer.after_initialize
225
+ assert_equal File.expand_path("tmp/experiments"), load_rails("", <<-RB)
154
226
  $stdout << Vanity.playground.load_path
155
227
  RB
156
228
  end
157
229
 
158
230
  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
231
+ assert_equal File.expand_path("tmp/predictions"), load_rails(%Q{\nVanity.playground.load_path = "predictions"\n}, <<-RB)
162
232
  $stdout << Vanity.playground.load_path
163
233
  RB
164
234
  end
165
235
 
166
236
  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
237
+ assert_equal File.expand_path("/tmp/var"), load_rails(%Q{\nVanity.playground.load_path = "/tmp/var"\n}, <<-RB)
170
238
  $stdout << Vanity.playground.load_path
171
239
  RB
172
240
  end
173
241
 
174
-
175
- # -- Connection configuration --
176
-
177
242
  if ENV['DB'] == 'redis'
178
243
  def test_default_connection
179
- assert_equal "redis://127.0.0.1:6379/0", load_rails(<<-RB)
180
- initializer.after_initialize
244
+ assert_equal "redis://127.0.0.1:6379/0", load_rails("", <<-RB)
181
245
  $stdout << Vanity.playground.connection
182
246
  RB
183
247
  end
184
248
 
185
249
  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
250
+ assert_equal "redis://192.168.1.1:6379/5", load_rails(%Q{\nVanity.playground.establish_connection "redis://192.168.1.1:6379/5"\n}, <<-RB)
189
251
  $stdout << Vanity.playground.connection
190
252
  RB
191
253
  end
@@ -201,8 +263,7 @@ production:
201
263
  database: 15
202
264
  YML
203
265
  end
204
- assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
205
- initializer.after_initialize
266
+ assert_equal "redis://somehost:6379/15", load_rails("", <<-RB)
206
267
  $stdout << Vanity.playground.connection
207
268
  RB
208
269
  ensure
@@ -217,8 +278,7 @@ $stdout << Vanity.playground.connection
217
278
  production: redis://somehost/15
218
279
  YML
219
280
  end
220
- assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
221
- initializer.after_initialize
281
+ assert_equal "redis://somehost:6379/15", load_rails("", <<-RB)
222
282
  $stdout << Vanity.playground.connection
223
283
  RB
224
284
  ensure
@@ -235,8 +295,7 @@ $stdout << Vanity.playground.connection
235
295
  production: <%= ENV['REDIS_URL'] %>
236
296
  YML
237
297
  end
238
- assert_equal "redis://somehost:6379/15", load_rails(<<-RB)
239
- initializer.after_initialize
298
+ assert_equal "redis://somehost:6379/15", load_rails("", <<-RB)
240
299
  $stdout << Vanity.playground.connection
241
300
  RB
242
301
  ensure
@@ -248,14 +307,13 @@ $stdout << Vanity.playground.connection
248
307
  yml = File.open("tmp/config/redis.yml", "w")
249
308
  yml << "production: internal.local:6379\n"
250
309
  yml.flush
251
- assert_equal "redis://internal.local:6379/0", load_rails(<<-RB)
252
- initializer.after_initialize
310
+ assert_equal "redis://internal.local:6379/0", load_rails("", <<-RB)
253
311
  $stdout << Vanity.playground.connection
254
312
  RB
255
313
  ensure
256
314
  File.unlink yml.path
257
315
  end
258
-
316
+
259
317
 
260
318
  end
261
319
 
@@ -272,8 +330,7 @@ mongodb:
272
330
  YML
273
331
  end
274
332
 
275
- assert_equal "mongodb://localhost:27017/vanity_test", load_rails(<<-RB, "mongodb")
276
- initializer.after_initialize
333
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<-RB, "mongodb")
277
334
  $stdout << Vanity.playground.connection
278
335
  RB
279
336
  ensure
@@ -294,13 +351,11 @@ mongodb:
294
351
  YML
295
352
  end
296
353
 
297
- assert_equal "mongodb://localhost:27017/vanity_test", load_rails(<<-RB, "mongodb")
298
- initializer.after_initialize
354
+ assert_equal "mongodb://localhost:27017/vanity_test", load_rails("", <<-RB, "mongodb")
299
355
  $stdout << Vanity.playground.connection
300
356
  RB
301
357
 
302
- assert_equal "Mongo::ReplSetConnection", load_rails(<<-RB, "mongodb")
303
- initializer.after_initialize
358
+ assert_equal "Mongo::ReplSetConnection", load_rails("", <<-RB, "mongodb")
304
359
  $stdout << Vanity.playground.connection.mongo.class
305
360
  RB
306
361
  ensure
@@ -317,8 +372,7 @@ production:
317
372
  adapter: redis
318
373
  YML
319
374
  end
320
- assert_equal "No configuration for development", load_rails(<<-RB, "development")
321
- initializer.after_initialize
375
+ assert_equal "No configuration for development", load_rails("", <<-RB, "development")
322
376
  $stdout << (Vanity.playground.connection rescue $!.message)
323
377
  RB
324
378
  ensure
@@ -333,8 +387,7 @@ production:
333
387
  collecting: false
334
388
  YML
335
389
  end
336
- assert_equal "false", load_rails(<<-RB)
337
- initializer.after_initialize
390
+ assert_equal "false", load_rails("", <<-RB)
338
391
  $stdout << Vanity.playground.collecting?
339
392
  RB
340
393
  ensure
@@ -342,71 +395,99 @@ $stdout << Vanity.playground.collecting?
342
395
  end
343
396
 
344
397
  def test_collection_true_in_production_by_default
345
- assert_equal "true", load_rails(<<-RB, "production")
346
- initializer.after_initialize
398
+ assert_equal "true", load_rails("", <<-RB)
347
399
  $stdout << Vanity.playground.collecting?
348
400
  RB
349
401
  end
350
402
 
351
403
  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
404
+ assert_equal "false", load_rails("\nVanity.playground.collecting = false\n", <<-RB)
355
405
  $stdout << Vanity.playground.collecting?
356
406
  RB
357
407
  end
358
408
 
359
409
  def test_collection_false_in_development_by_default
360
- assert_equal "false", load_rails(<<-RB, "development")
361
- initializer.after_initialize
410
+ assert_equal "false", load_rails("", <<-RB, "development")
362
411
  $stdout << Vanity.playground.collecting?
363
412
  RB
364
413
  end
365
414
 
366
415
  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
416
+ assert_equal "true", load_rails("\nVanity.playground.collecting = true\n", <<-RB, "development")
370
417
  $stdout << Vanity.playground.collecting?
371
418
  RB
372
419
  end
373
420
 
374
421
  def test_collection_false_after_test!
375
- assert_equal "false", load_rails(<<-RB, "production")
376
- initializer.after_initialize
422
+ assert_equal "false", load_rails("", <<-RB)
377
423
  Vanity.playground.test!
378
424
  $stdout << Vanity.playground.collecting?
379
425
  RB
380
426
  end
381
427
 
382
- def load_rails(code, env = "production")
428
+ def load_rails(before_initialize, after_initialize, env="production")
383
429
  tmp = Tempfile.open("test.rb")
384
- tmp.write <<-RB
430
+ begin
431
+ code_setup = <<-RB
385
432
  $:.delete_if { |path| path[/gems\\/vanity-\\d/] }
386
433
  $:.unshift File.expand_path("../lib")
387
434
  RAILS_ROOT = File.expand_path(".")
435
+ RB
436
+ code = code_setup
437
+ code += defined?(Rails::Railtie) ? load_rails_3(env) : load_rails_2(env)
438
+ code += %Q{\nrequire "vanity"\n}
439
+ code += before_initialize
440
+ code += defined?(Rails::Railtie) ? initialize_rails_3 : initialize_rails_2
441
+ code += after_initialize
442
+ tmp.write code
443
+ tmp.flush
444
+ Dir.chdir "tmp" do
445
+ open("| ruby #{tmp.path}").read
446
+ end
447
+ ensure
448
+ tmp.close!
449
+ end
450
+ end
451
+
452
+ def load_rails_2(env)
453
+ <<-RB
388
454
  RAILS_ENV = ENV['RACK_ENV'] = "#{env}"
389
455
  require "initializer"
390
456
  require "active_support"
391
457
  Rails.configuration = Rails::Configuration.new
392
458
  initializer = Rails::Initializer.new(Rails.configuration)
393
459
  initializer.check_gem_dependencies
394
- require "vanity"
395
460
  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
461
  end
404
462
 
463
+ def load_rails_3(env)
464
+ <<-RB
465
+ ENV['BUNDLE_GEMFILE'] ||= "#{ENV['BUNDLE_GEMFILE']}"
466
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
467
+ ENV['RAILS_ENV'] = ENV['RACK_ENV'] = "#{env}"
468
+ require "active_model/railtie"
469
+ require "action_controller/railtie"
405
470
 
406
- def teardown
407
- super
408
- if !rails3?
409
- UseVanityController.send(:filter_chain).clear
410
- end
471
+ Bundler.require(:default)
472
+
473
+ module Foo
474
+ class Application < Rails::Application
475
+ config.active_support.deprecation = :notify
411
476
  end
477
+ end
478
+ RB
479
+ end
480
+
481
+ def initialize_rails_2
482
+ <<-RB
483
+ initializer.after_initialize
484
+ RB
485
+ end
486
+
487
+ def initialize_rails_3
488
+ <<-RB
489
+ Foo::Application.initialize!
490
+ RB
491
+ end
492
+
412
493
  end