def test_agg(self): # This is copied over in the module self.assertEqual(AwcLocation.objects.count(), 92) self.maxDiff = None with maybe_atomic(AwcLocation): # if we ran the aggregation now, we'd have 55 fewer locations with self.assertRaisesRegex(LocationRemovedException, '55'): with get_cursor(AwcLocation) as cursor: self.helper.aggregate(cursor) try: with maybe_atomic(AwcLocation): # run agg again without any locations in awc_location with get_cursor(AwcLocation) as cursor: cursor.execute("DELETE FROM awc_location") self.helper.aggregate(cursor) self.assertEqual(AwcLocation.objects.count(), 8) self.assertEqual( list( AwcLocation.objects.values( 'aggregation_level', 'awc_is_test', 'awc_name', 'awc_site_code', 'supervisor_is_test', 'supervisor_name', 'supervisor_site_code', 'block_is_test', 'block_name', 'block_site_code', 'block_map_location_name', 'district_is_test', 'district_name', 'district_site_code', 'district_map_location_name', 'state_is_test', 'state_name', 'state_site_code', 'state_map_location_name', ).order_by('-aggregation_level', 'awc_name', 'supervisor_name', 'block_name', 'district_name', 'state_name').all()), self._expected_end_state) raise Exception("Don't allow this to be commited") except Exception as e: if 'allow this' not in str(e): raise
def test_missing_locations_query(self): missing_location_query = self.helper.missing_location_query() with get_cursor(AggregateInactiveAWW) as cursor: cursor.execute(missing_location_query) records = AggregateInactiveAWW.objects.filter( first_submission__isnull=False) self.assertEquals(records.count(), 0)
def test_aggregate_query(self): missing_location_query = self.helper.missing_location_query() aggregation_query, agg_params = self.helper.aggregate_query() with get_cursor(AggregateInactiveAWW) as cursor: cursor.execute(missing_location_query) cursor.execute(aggregation_query, agg_params) records = AggregateInactiveAWW.objects.filter(first_submission__isnull=False) self.assertEquals(records.count(), 46)
def test_submission_dates(self): missing_location_query = self.helper.missing_location_query() aggregation_query, agg_params = self.helper.aggregate_query() with get_cursor(AggregateInactiveAWW) as cursor: cursor.execute(missing_location_query) cursor.execute(aggregation_query, agg_params) record = AggregateInactiveAWW.objects.filter(awc_id='a10').first() self.assertEquals(date(2017, 4, 5), record.first_submission) self.assertEquals(date(2017, 5, 5), record.last_submission)
def test_aggregate_query(self): with freeze_time(self.agg_time): missing_location_query = self.helper.missing_location_query() aggregation_query, agg_params = self.helper.aggregate_query() with get_cursor(AggregateInactiveAWW) as cursor: cursor.execute(missing_location_query) cursor.execute(aggregation_query, agg_params) records = AggregateInactiveAWW.objects.filter(first_submission__isnull=False) self.assertEqual(records.count(), 46)
def _rename_location(location_name_column, old_name, new_name): try: location = SQLLocation.objects.get(name=old_name) location.name = new_name location.save() except SQLLocation.DoesNotExist: print(f'location {old_name} not found!') AwcLocation.objects.filter(**{location_name_column: old_name}).update(**{location_name_column: new_name}) with get_cursor(AwcLocationLocal) as cursor: cursor.execute( f"UPDATE awc_location_local SET {location_name_column}='{new_name}' where {location_name_column} = '{old_name}'" )