def setUp(self): # Avoid connecting to real API during tests. self.build_service = self.create_patch( 'affiliates.links.google_analytics.build_service') with self._mock_open(): self.service = AnalyticsService('keyfile.p12', '*****@*****.**', 125)
def test_init_oauth2error(self): """ If there is an error authenticating via oauth, raise AnalyticsError. """ with self._mock_open(read_data='asdf'): self.build_service.side_effect = OAuth2Error with self.assertRaises(AnalyticsError): AnalyticsService('keyfile.p12', '*****@*****.**', 125)
def test_init_defaults(self): with self._mock_open(read_data='asdf') as _open: SignedJwtAssertionCredentials = self.create_patch( 'affiliates.links.google_analytics.SignedJwtAssertionCredentials' ) with self.settings(GA_API_PROFILE_ID=125, GA_API_KEYFILE='keyfile.p12', GA_API_ACCOUNT_EMAIL='*****@*****.**'): service = AnalyticsService() _open.assert_called_with('keyfile.p12', ANY) SignedJwtAssertionCredentials.assert_called_with( '*****@*****.**', ANY, scope=ANY) eq_(service.profile_id, 125)
def test_init(self): with self._mock_open(read_data='asdf') as _open: SignedJwtAssertionCredentials = self.create_patch( 'affiliates.links.google_analytics.SignedJwtAssertionCredentials' ) credentials = SignedJwtAssertionCredentials.return_value http = credentials.authorize.return_value service = AnalyticsService('keyfile.p12', '*****@*****.**', 125) _open.assert_called_with('keyfile.p12', 'rb') SignedJwtAssertionCredentials.assert_called_with( '*****@*****.**', 'asdf', scope='https://www.googleapis.com/auth/analytics.readonly') self.build_service.assert_called_with('analytics', 'v3', http=http) eq_(service._service, self.build_service.return_value) eq_(service.profile_id, 125)
def handle_quiet(self, query_date=None, *args, **kwargs): try: service = AnalyticsService() except AnalyticsError as e: raise CommandError( 'Could not connect to analytics service: {0}'.format(e), e) if query_date == 'today': query_date = timezone.now().date() elif query_date: try: unaware_query_datetime = datetime.strptime( query_date, '%d-%m-%Y') except ValueError: raise CommandError( 'Date argument invalid. It must be in DD-MM-YYYY format') query_date = timezone.make_aware(unaware_query_datetime, timezone.utc).date() else: query_date = timezone.now() - timedelta(days=2) self.output('Downloading click counts from GA...') try: clicks = service.get_clicks_for_date(query_date) except AnalyticsError as e: raise CommandError( 'Could not retrieve click data from analytics service: {0}'. format(e)) self.output('Adding datapoints to database...') datapoints = [] for pk in Link.objects.values_list('id', flat=True): datapoint = DataPoint(link_id=pk, date=query_date, link_clicks=clicks.get(unicode(pk), 0)) datapoints.append(datapoint) # Remove existing data, because supposedly our data is more # up-to-date. DataPoint.objects.filter(date=query_date).delete() DataPoint.objects.bulk_create(datapoints) self.output('Done!')
def test_init_keyfile_missing(self): """If the keyfile can't be found, raise AnalyticsError.""" with self._mock_open() as _open: _open.side_effect = IOError with self.assertRaises(AnalyticsError): AnalyticsService('keyfile.p12', '*****@*****.**', 125)