Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7ac26bab4d
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 173 lines (147 sloc) 6.309 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
import datetime
import unittest
import googleanalytics
from googleanalytics.exception import GoogleAnalyticsClientError
from googleanalytics import config

class GoogleAnalyticsTest(unittest.TestCase):
  def test_goodconnection(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    assert connection.auth_token is not None
  
  def test_badconnection(self):
    Connection = googleanalytics.Connection
    try:
      connection = Connection('clintecker@gmail.com', 'fakefake')
    except GoogleAnalyticsClientError, e:
      assert str(e.reason) == "HTTP Error 403: Forbidden"
        
  def test_accountlist(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()
    for c in range(1, len(valid_profile_ids)):
      accounts = connection.get_accounts(max_results=c)
      assert len(accounts) == c
  
  def test_account_retrieval_unvalidated(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()
    for c in range(len(valid_profile_ids)):
      account = connection.get_account(valid_profile_ids[c])
  
  def test_bad_date_order(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()
    
    start_date = datetime.date(2009, 02, 21)
    end_date = datetime.date(2009, 02, 20)
    account = connection.get_account(valid_profile_ids[0])
    try:
      data = account.get_data(start_date=start_date, end_date=end_date)
    except GoogleAnalyticsClientError, e:
      assert str(e.reason) == "Date orders are reversed"
      
  def test_basic_get_data_no_params(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()
    
    start_date = datetime.date(2009, 04, 10)
    end_date = datetime.date(2009, 04, 10)
    
    for c in range(len(valid_profile_ids)):
      account = connection.get_account(valid_profile_ids[c])
      data = account.get_data(start_date=start_date, end_date=end_date)
      assert len(data) == 0
      
  def test_dimensions_basic_get_data(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()
  
    start_date = datetime.date(2009, 04, 10)
    end_date = datetime.date(2009, 04, 10)
  
    for c in range(len(valid_profile_ids)):
      account = connection.get_account(valid_profile_ids[c])
      data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['browser',], metrics=['pageviews',])
      assert len(data) > 0
      data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['pagePath',], metrics=['pageviews',])
      assert len(data) > 0

  def test_dimensions_basic_get_data_output(self):
    Connection = googleanalytics.Connection
    connection = Connection()
    valid_profile_ids = config.get_valid_profiles()

    start_date = datetime.date(2009, 04, 10)
    end_date = datetime.date(2009, 04, 10)

    for c in range(len(valid_profile_ids)):
      account = connection.get_account(valid_profile_ids[c])
      data = account.get_data(start_date=start_date, end_date=end_date, dimensions=['browser',], metrics=['pageviews',], sort=['-pageviews',])
      assert len(data) > 0
      assert isinstance(data.list, list)
      assert isinstance(data.list[0], list)
      assert isinstance(data.tuple, tuple)
      assert isinstance(data.tuple[0], tuple)
      assert isinstance(data.dict, dict)
      
  def test_basic_filter(self):
    filters = [
      ['country', '==', 'United States'],
    ]
    account = googleanalytics.account.Account()
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==United States'
  
  def test_filter_escaping(self):
    filters = [
      ['country', '==', 'United,States'],
    ]
    account = googleanalytics.account.Account()
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==United\,States'
    filters = [
      ['country', '==', 'United\States'],
    ]
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==United\\\\States'
    
    filters = [
      ['country', '==', 'Uni,tedSt,ates'],
    ]
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==Uni\,tedSt\,ates'
    
    filters = [
      ['country', '==', 'Uni,tedSt;at,es'],
    ]
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==Uni\,tedSt\;at\,es'
  
  def test_bad_operator_rejection(self):
    filters = [
      ['country', '@@', 'United,States'],
    ]
    account = googleanalytics.account.Account()
    filter_string = account.process_filters(filters)
    assert filter_string == ''
    
  def test_multiple_filters(self):
    filters = [
      ['country', '==', 'United States', 'AND'],
      ['country', '==', 'Canada']
    ]
    account = googleanalytics.account.Account()
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:country==United States;ga:country==Canada'
    
    filters = [
      ['city', '=~', '^L', 'AND'],
      ['browser', '=~', '^Fire']
    ]
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:city=~^L;ga:browser=~^Fire'
    
    filters = [
      ['browser', '=~', '^Fire', 'OR'],
      ['browser', '=~', '^Internet', 'OR'],
      ['browser', '=~', '^Saf'],
    ]
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:browser=~^Fire,ga:browser=~^Internet,ga:browser=~^Saf'
    
  def test_multiple_filters_mix_ops(self):
    filters = [
      ['browser', '=~', 'Firefox', 'AND'],
      ['browser', '=~', 'Internet (Explorer|Exploder)', 'OR'],
      ['city', '=@', 'York', 'OR'],
      ['state', '!=', 'California', 'AND'],
      ['timeOnPage', '<', '10'],
    ]
    account = googleanalytics.account.Account()
    filter_string = account.process_filters(filters)
    assert filter_string == 'ga:browser=~Firefox;ga:browser=~Internet (Explorer|Exploder),ga:city=@York,ga:state!=California;ga:timeOnPage<10'

def test_suite():
  return unittest.makeSuite(GoogleAnalyticsTest)
Something went wrong with that request. Please try again.