def test_good_report_with_two_known_date_ranges(self): expected = { 'dateRanges': [ { 'startDate': '2021-03-23', 'endDate': '2021-03-29' }, { 'startDate': '2020-03-23', 'endDate': '2020-03-29' }, ] } expected.update(self.BASE_EXPECTED) definition = \ ga360.GA360ReportDefinition(view_id='', sampling_level=ga360.GA360SamplingLevel.LARGE, metrics=self.METRICS, date_ranges=[ ga360.GA360DateRange(start_date='2021-03-23', end_date='2021-03-29'), ga360.GA360DateRange(start_date='2020-03-23', end_date='2020-03-29') ], dimensions=self.DIMENSIONS) self.assertEqual(expected, definition.report_request)
def test_bad_report_with_more_than_two_date_ranges(self): definition = \ ga360.GA360ReportDefinition(view_id='', sampling_level=ga360.GA360SamplingLevel.LARGE, metrics=self.METRICS, date_ranges=[ ga360.GA360DateRange(), ga360.GA360DateRange(), ga360.GA360DateRange() ], dimensions=self.DIMENSIONS) with self.assertRaises(ValueError): definition.report_request
def test_no_dates_given(self): date_range = \ ga360.GA360DateRange() self.assertEqual({ 'startDate': LAST_WEEK, 'endDate': YESTERDAY }, date_range.date_range)
def test_end_date_only(self): date_range = \ ga360.GA360DateRange(end_date='2021-03-29') self.assertEqual({ 'startDate': LAST_WEEK, 'endDate': '2021-03-29' }, date_range.date_range)
def test_start_date_only(self): date_range = \ ga360.GA360DateRange(start_date='2021-03-23') self.assertEqual({ 'startDate': '2021-03-23', 'endDate': YESTERDAY }, date_range.date_range)
def test_known_date_range(self): date_range = \ ga360.GA360DateRange(start_date='2021-03-23', end_date='2021-03-29') self.assertEqual({ 'startDate': '2021-03-23', 'endDate': '2021-03-29' }, date_range.date_range)
def test_load_from_json(self): expected = { 'dateRanges': [ ga360.GA360DateRange().date_range, ] } expected.update(self.BASE_EXPECTED) definition = \ ga360.GA360ReportDefinition.from_json(json.dumps(self.BASE_EXPECTED)) self.assertEqual(expected, definition.report_request)
def test_load_from_json_and_edit(self): view_id = '00000001' date_range = ga360.GA360DateRange(start_date='30daysAgo', end_date='yesterday') updates = {'viewId': view_id, 'dateRanges': [date_range.date_range]} expected = {} expected.update(self.BASE_EXPECTED, **updates) definition = \ ga360.GA360ReportDefinition.from_json(json.dumps(self.BASE_EXPECTED)) definition.view_id = view_id definition.date_ranges = [date_range] self.assertEqual(expected, definition.report_request)
def test_date_math(self): def mock_ga360_date_range(d): d.__setattr__('_lazy__now', date(2021, 4, 1)) return d date_ranges = [ mock_ga360_date_range(ga360.GA360DateRange(start_date='7daysAgo')), mock_ga360_date_range( ga360.GA360DateRange(start_date='14daysAgo')), mock_ga360_date_range( ga360.GA360DateRange(start_date='1weeksAgo')), mock_ga360_date_range( ga360.GA360DateRange(start_date='1monthsAgo')), mock_ga360_date_range( ga360.GA360DateRange(start_date='12MonthsAgo')), mock_ga360_date_range( ga360.GA360DateRange(start_date='1yearsago')), ] self.assertEqual([ { 'startDate': '2021-03-25', 'endDate': '2021-03-31' }, { 'startDate': '2021-03-18', 'endDate': '2021-03-31' }, { 'startDate': '2021-03-25', 'endDate': '2021-03-31' }, { 'startDate': '2021-03-01', 'endDate': '2021-03-31' }, { 'startDate': '2020-04-01', 'endDate': '2021-03-31' }, { 'startDate': '2020-04-01', 'endDate': '2021-03-31' }, ], [d.date_range for d in date_ranges])
def test_invalid_date_math(self): with self.assertRaises(NotImplementedError): ga360.GA360DateRange(start_date='7hoursAgo').date_range
self.firestore.get_document(type=Type.GA360_RPT, id=self._report_id) if report_config := \ self.firestore.get_document(type=Type.GA360_RPT, id='_reports', key=runner.get('report')): definition = \ ga360_report.GA360ReportDefinition.from_json( json.dumps(report_config)) else: raise NotImplementedError(f'No such runner: {self._report_id}') definition.view_id = runner.get('view_id') ranges = [] for date_range in runner.get('date_ranges'): range = \ ga360_report.GA360DateRange(start_date=date_range.get('start_date'), end_date=date_range.get('end_date')) ranges.append(range) definition.date_ranges = ranges request_body = {'reportRequests': [definition.report_request]} ga360_service = discovery.get_service(Service.GA360, self.credentials) request = ga360_service.reports().batchGet(body=request_body) response = request.execute() # TODO: Right now, process just the first result, we're not allowing for # multiple reports in the same request. # Also, we are assuming that this whole report can be processed in # a single chunk in memory. If this is not the case, we have a much, # much larger issue to handle a json file in pieces. This is not # supported currently.