ENV['RACK_ENV'] = 'test'
require 'rspec'
require 'rack/test'

require 'mock_redis'

$LOAD_PATH.unshift(File.expand_path(File.join(__FILE__, "..", "..", "lib")))
require 'duckweed'
Dir['spec/shared/*.rb'].each {|shared| require shared}

MINUTE = 60
HOUR   = 60 * MINUTE
DAY    = 24 * HOUR

module SpecHelpers
  def event_happened(params={})
    event_name  = params[:event] || event
    event_time  = params[:at]
    event_count = params[:times] || 1

    event_count.times do
      params = default_params.dup
      if event_time
        params.merge!(:timestamp => event_time.to_i)
      end
      post "/track/#{event_name}", params
    end
  end

  def freeze_time
    @now = Time.now
    Time.stub!(:now).and_return(@now)
  end

  def open_page_in_browser
    path = "/tmp/duckweed_sample_#{$$}.html"
    File.open(path, 'w') do |f|
      f.write(last_response.body)
    end
    `which open && open #{path}`
  end
end

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
  conf.include SpecHelpers

  conf.before(:all) do
    Duckweed.redis = MockRedis.new
  end

  conf.before(:each) do
    Duckweed.redis.reset!
  end
end

ENV['RACK_ENV'] = 'test'
require 'rspec'
require 'rack/test'

require 'mock_redis'

$LOAD_PATH.unshift(File.expand_path(File.join(__FILE__, "..", "..", "lib")))
require 'duckweed'
Dir['spec/shared/*.rb'].each {|shared| require shared}

MINUTE = 60
HOUR   = 60 * MINUTE
DAY    = 24 * HOUR

module SpecHelpers
  def event_happened(params={})
    event_name  = params[:event] || event
    event_time  = params[:at]
    event_count = params[:times]

    params = default_params.dup
    if event_time
      params.merge!(:timestamp => event_time.to_i)
    end
    if event_count
      params.merge!(:quantity => event_count)
    end

    post "/track/#{event_name}", params

  end

  def freeze_time
    @now = Time.now
    Time.stub!(:now).and_return(@now)
  end

  def open_page_in_browser
    path = "/tmp/duckweed_sample_#{$$}.html"
    File.open(path, 'w') do |f|
      f.write(last_response.body)
    end
    `which open && open #{path}`
  end
end

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
  conf.include SpecHelpers

  conf.before(:all) do
    Duckweed.redis = MockRedis.new
  end

  conf.before(:each) do
    Duckweed.redis.reset!
  end
end

Speed up tests by ~90%.
Speed up tests by ~90%.

Make the helper method #event_happened much more efficient when the
event count is > 1. Prior to this change, event_happened(:times => N)
would make N POST requests to the app; now, it makes just one and sets
the :quantity parameter to N.

This slowness occurred because #event_happened predates the :quantity
parameter, so repeated requests was the only way to go.
