This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Add integration tests for hourly visitors collector.

  • Loading branch information...
robyoung committed Jan 2, 2013
1 parent f5e0943 commit cbc60b08d057310ab140767bb52da6aa574ffb1e
@@ -0,0 +1 @@
+{"kind":"analytics#gaData","id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-27&end-date=2012-12-27","query":{"start-date":"2012-12-27","end-date":"2012-12-27","ids":"ga:53872948","dimensions":"ga:hour","metrics":["ga:visitors"],"start-index":1,"max-results":1000},"itemsPerPage":1000,"totalResults":24,"selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-27&end-date=2012-12-27","profileInfo":{"profileId":"53872948","accountId":"26179049","webPropertyId":"UA-26179049-1","internalWebPropertyId":"50705554","profileName":"1. GOV.UK (Entire site - Filtered)","tableId":"ga:53872948"},"containsSampledData":true,"columnHeaders":[{"name":"ga:hour","columnType":"DIMENSION","dataType":"STRING"},{"name":"ga:visitors","columnType":"METRIC","dataType":"INTEGER"}],"totalsForAllResults":{"ga:visitors":"686492"},"rows":[["00","11419"],["01","7004"],["02","4727"],["03","3524"],["04","3142"],["05","3280"],["06","4576"],["07","8116"],["08","17915"],["09","34129"],["10","47722"],["11","53461"],["12","52705"],["13","50566"],["14","50707"],["15","48845"],["16","44976"],["17","39353"],["18","39820"],["19","38519"],["20","35801"],["21","35358"],["22","29374"],["23","21453"]]}
@@ -0,0 +1 @@
+{"kind":"analytics#gaData","id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-28&end-date=2012-12-28","query":{"start-date":"2012-12-28","end-date":"2012-12-28","ids":"ga:53872948","dimensions":"ga:hour","metrics":["ga:visitors"],"start-index":1,"max-results":1000},"itemsPerPage":1000,"totalResults":24,"selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-28&end-date=2012-12-28","profileInfo":{"profileId":"53872948","accountId":"26179049","webPropertyId":"UA-26179049-1","internalWebPropertyId":"50705554","profileName":"1. GOV.UK (Entire site - Filtered)","tableId":"ga:53872948"},"containsSampledData":true,"columnHeaders":[{"name":"ga:hour","columnType":"DIMENSION","dataType":"STRING"},{"name":"ga:visitors","columnType":"METRIC","dataType":"INTEGER"}],"totalsForAllResults":{"ga:visitors":"751865"},"rows":[["00","14577"],["01","8473"],["02","5603"],["03","3838"],["04","3210"],["05","3426"],["06","5260"],["07","11022"],["08","22283"],["09","41684"],["10","56275"],["11","63435"],["12","61235"],["13","58163"],["14","56551"],["15","54371"],["16","49226"],["17","42602"],["18","38173"],["19","36575"],["20","35190"],["21","32413"],["22","27573"],["23","20707"]]}
@@ -0,0 +1 @@
+{"kind":"analytics#gaData","id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-29&end-date=2012-12-29","query":{"start-date":"2012-12-29","end-date":"2012-12-29","ids":"ga:53872948","dimensions":"ga:hour","metrics":["ga:visitors"],"start-index":1,"max-results":1000},"itemsPerPage":1000,"totalResults":24,"selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-12-29&end-date=2012-12-29","profileInfo":{"profileId":"53872948","accountId":"26179049","webPropertyId":"UA-26179049-1","internalWebPropertyId":"50705554","profileName":"1. GOV.UK (Entire site - Filtered)","tableId":"ga:53872948"},"containsSampledData":true,"columnHeaders":[{"name":"ga:hour","columnType":"DIMENSION","dataType":"STRING"},{"name":"ga:visitors","columnType":"METRIC","dataType":"INTEGER"}],"totalsForAllResults":{"ga:visitors":"558100"},"rows":[["00","14874"],["01","8488"],["02","5387"],["03","3714"],["04","2976"],["05","2930"],["06","4016"],["07","7266"],["08","14744"],["09","26539"],["10","36549"],["11","42402"],["12","42286"],["13","39619"],["14","38545"],["15","37019"],["16","34643"],["17","33681"],["18","31303"],["19","29293"],["20","30041"],["21","28355"],["22","24158"],["23","19272"]]}
@@ -0,0 +1,76 @@
+require_relative "spec_helper"
+
+describe "Hourly visitors spec" do
+ before(:each) do
+ stub_credentials
+ register_oauth_refresh
+ register_api_discovery
+
+ @ga_request = setup_ga_request(
+ :ids => "ga:53872948",
+ :metrics => "ga:visitors",
+ :dimensions => "ga:hour"
+ )
+ end
+
+ it "should query google analytics for a specific date" do
+ @ga_request.register(
+ "2012-12-29", "2012-12-29",
+ "hourly-visitors-for-2012-12-29.json"
+ )
+ configs = [GoogleAnalytics::Config::HourlyVisitors.last_before(Date.new(2012, 12, 29))]
+ collector = GoogleAnalytics::Collector.new(nil, configs)
+
+ response = collector.collect_as_json
+ response.should have(24).item
+
+ response[0].should be_for_collector("Google Analytics")
+ response[0].should be_for_time_period(
+ DateTime.new(2012, 12, 29), DateTime.new(2012, 12, 29, 1))
+ response[0].should have_payload_value(
+ "visitors" => 14874, "site" => "govuk")
+
+ response[23].should be_for_time_period(
+ DateTime.new(2012, 12, 29, 23), DateTime.new(2012, 12, 30))
+ response[23].should have_payload_value(
+ "visitors" => 19272, "site" => "govuk")
+ end
+
+ it "should query google analytics for the previous three days" do
+ @ga_request.register(
+ "2012-12-27", "2012-12-27",
+ "hourly-visitors-for-2012-12-27.json"
+ )
+ @ga_request.register(
+ "2012-12-28", "2012-12-28",
+ "hourly-visitors-for-2012-12-28.json"
+ )
+ @ga_request.register(
+ "2012-12-29", "2012-12-29",
+ "hourly-visitors-for-2012-12-29.json"
+ )
+
+ Timecop.travel(DateTime.parse("2012-12-29"))
+ configs = GoogleAnalytics::Config::HourlyVisitors.all_within(
+ Date.today - 2, Date.today)
+ collector = GoogleAnalytics::Collector.new(nil, configs)
+
+ response = collector.collect_as_json
+ response.should have(72).items
+
+ response[0].should be_for_time_period(
+ DateTime.new(2012, 12, 27), DateTime.new(2012, 12, 27, 1))
+ response[0].should have_payload_value(
+ "visitors" => 11419, "site" => "govuk")
+
+ response[24].should be_for_time_period(
+ DateTime.new(2012, 12, 28), DateTime.new(2012, 12, 28, 1))
+ response[24].should have_payload_value(
+ "visitors" => 14577, "site" => "govuk")
+
+ response[48].should be_for_time_period(
+ DateTime.new(2012, 12, 29), DateTime.new(2012, 12, 29, 1))
+ response[48].should have_payload_value(
+ "visitors" => 14874, "site" => "govuk")
+ end
+end

0 comments on commit cbc60b0

Please sign in to comment.