def setUp(self): super().setUp() self.collection_file = factories.CollectionFileFactory() self.pollutant = factories.PollutantFactory() self.n_values = 10 self.test_values = [] for _ in range(self.n_values): pollutant_value = factories.PollutantValueFactory( collection_file=self.collection_file, pollutant=self.pollutant) self.test_values.append(pollutant_value.value)
def test_create_collection(self): """Tests collection create method.""" starts_at = datetime.datetime(2019, 1, 1, 10, 15) ends_at = datetime.datetime(2019, 1, 1, 14, 15) with open(GPS_PATH) as gps_file, open(DUSTRAK_PATH) as dustrak_file: pollutant = factories.PollutantFactory() data = { "upload_files": [gps_file, dustrak_file], "starts_at": starts_at, "ends_at": ends_at, "pollutant": pollutant.pk, } response = self.client.post("/collection", data) assert response.status_code == 201
def test_create_collection_wrong_number(self): """Tests collection create method with wrong number of files.""" starts_at = datetime.datetime(2019, 1, 1, 10, 15) ends_at = datetime.datetime(2019, 1, 1, 14, 15) with open(GPS_PATH) as gps_file, open(DUSTRAK_PATH) as dustrak_file: pollutant = factories.PollutantFactory() data = { "upload_files": [gps_file, gps_file, dustrak_file], "starts_at": starts_at, "ends_at": ends_at, "pollutant": pollutant.pk, } response = self.client.post("/collection", data, format="json") assert response.status_code == 400 assert b"Please upload exactly 2 files." in response.content
def test_create_collection_missing_dustrak(self): """Tests collection create method when missing dustrak file.""" starts_at = datetime.datetime(2019, 1, 1, 10, 15) ends_at = datetime.datetime(2019, 1, 1, 14, 15) with open(GPS_PATH) as gps_file, open(RANDOM_PATH) as random_file: pollutant = factories.PollutantFactory() data = { "upload_files": [gps_file, random_file], "starts_at": starts_at, "ends_at": ends_at, "pollutant": pollutant.pk, } response = self.client.post("/collection", data) assert response.status_code == 400 assert response.content.startswith(b'["No Dustrak file found.')
def test_save(): """Test ability to save a session of joined GPS/air quality data to the database, based on measurement/value""" collection = factories.CollectionFactory() gps_collection_file = factories.CollectionFileFactory( collection=collection) pollutant_collection_file = factories.CollectionFileFactory( collection=collection) pollutant = factories.PollutantFactory() target_data = get_target_data(target_path=JOINED_PATH) dustrak.save( target_data, gps_collection_file=gps_collection_file, pollutant_collection_file=pollutant_collection_file, pollutant=pollutant, ) assert np.allclose( target_data["measurement"], models.PollutantValue.objects.values_list("value", flat=True), )
def handle(self, *args, **options): self.stdout.write( f"Starting to generate {options['observations']} mock observations " f"for {options['collections']} collections.") for i in range(options["collections"]): collection_file = factories.CollectionFileFactory() pollutant = factories.PollutantFactory() # Create series of TimeGeo incrementing by 1 second initial_time_geo = factories.TimeGeoFactory( collection_file=collection_file) time_geo_series = [initial_time_geo] + [ factories.TimeGeoFactory( collection_file=collection_file, time=initial_time_geo.time + datetime.timedelta(seconds=j + 1), ) for j in range(options["observations"] - 1) ] # Generate pollutant values for each TimeGeo [ factories.PollutantValueFactory( collection_file=collection_file, pollutant=pollutant, time_geo=time_geo, ) for time_geo in time_geo_series ] self.stdout.write( f"Generated {len(time_geo_series)} observations " f"for collection: {str(collection_file)}, pollutant: {str(pollutant)}." ) self.stdout.write( f"Done generating data for {options['collections']} collections.")