Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 130 lines (98 sloc) 4.215 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
require 'spec_helper'

describe GarbReporter do
  
  before(:all) do

   # get VCR to serve us up a previously-canned http response for successful login
   VCR.use_cassette('garb authentication') do
Garb::Session.login('stuart@example.com', 'password')
end

# get VCR to serve us up a previously-canned http response for fetching profiles
VCR.use_cassette('get profile') do
   @profile = Garb::Management::Profile.all.detect { |p| p.web_property_id == 'UA-12345678-1' }
   end

@report = GarbReporter::Report.new(@profile)
  end

  describe 'build_class_name' do

   it 'should capitalise the string' do
   @report.send(:build_class_name, 'visits').should == 'Visits'
   end

it 'should strip out underscores' do
   @report.send(:build_class_name, 'visits_by_office').should_not match(/_/)
   end

   it 'should handle really long strings' do
   @report.send(:build_class_name, 'visits_and_pageviews_and_clicks_by_date_and_office').should == 'VisitsAndPageviewsAndClicksByDateAndOffice'
   end
  end

  describe 'valid_method_name' do

   it 'should not allow a blank string' do
   @report.send(:valid_method_name?, '').should be_false
   end

   it 'should not allow more than one instance of "_by_"' do
   @report.send(:valid_method_name?, 'visits_by_office_by_date').should be_false
   end

   it 'should not start or end with "_by_" or "_and_' do
   @report.send(:valid_method_name?, '_by_date').should be_false
   @report.send(:valid_method_name?, 'visits_by_').should be_false
   @report.send(:valid_method_name?, '_and_date').should be_false
   @report.send(:valid_method_name?, 'visits_and_').should be_false
   end
  end

  describe 'extract_metrics' do

   it 'should extract a single metric' do
   @report.send(:extract_metrics, 'visits').should == [:visits]
   end

it 'should extract multiple metrics' do
@report.send(:extract_metrics, 'visits_and_clicks_and_foo_and_bar').should == [:visits, :clicks, :foo, :bar]
   end

   it 'should ignore a dimension' do
   @report.send(:extract_metrics, 'visits_and_clicks_by_date').should == [:visits, :clicks]
   end

it 'should ignore multiple dimensions' do
@report.send(:extract_metrics, 'visits_and_clicks_by_date_and_office').should == [:visits, :clicks]
   end
  end

  describe 'dimensions' do

   it 'should know when there are no dimensions' do
   @report.send(:extract_dimensions, 'visits').should == []
   end

   it 'should extract a single dimension' do
   @report.send(:extract_dimensions, 'visits_by_date').should == [:date]
   end

   it 'should extract multiple dimensions' do
   @report.send(:extract_dimensions, 'visits_by_date_and_office').should == [:date, :office]
   end
  end

  describe 'build_new_report_class' do

    before(:all) do
      @klass = @report.send(:build_new_report_class, 'visits_by_date', 'VisitsByDate')
    end

    it 'adds the class so the GarbReporter module' do
      @klass.to_s.should == 'GarbReporter::VisitsByDate'
    end

    it 'should give the new class the public "results" method from Garb::Model module' do
      @klass.respond_to?(:results, false).should be_true
    end

    it 'should apply the correct metrics' do
      report_params = @klass.instance_variable_get(:@metrics)
      report_params.instance_variable_get(:@elements).should == [ :visits ]
    end

    it 'should apply the correct dimensions' do
      report_params = @klass.instance_variable_get(:@dimensions)
      report_params.instance_variable_get(:@elements).should == [ :date ]
    end
  end

  describe 'passing additional parameters to Garb' do

    before(:all) do
      @klass = GarbReporter.const_set('Exits', Class.new())
      @stubbed_report = GarbReporter::Report.new(@profile)
      @stubbed_report.stub!(:existing_report_class).and_return(@klass)
    end

    it 'should pass no args when none are provided' do
      @klass.should_receive(:results).once.with(@profile)
      @stubbed_report.exits()
    end

    it 'should pass start and end dates when provided' do
      @klass.should_receive(:results).with(@profile, :start_date => '2011-01-01', :end_date => '2011-12-31')
      @stubbed_report.exits(:start_date => '2011-01-01', :end_date => '2011-12-31')
    end
  end
end
Something went wrong with that request. Please try again.