-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
260 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
SOURCES = [ | ||
{ | ||
"lines": [ | ||
b"Lat=0 Lon=0 Hub-Height=160 Timezone=00.0 Terrain-Height=0.0", | ||
b"Computed at 100 m resolution", | ||
b" ", | ||
b"YYYYMMDD HHMM M(m/s) D(deg) SD(m/s) DSD(deg) Gust3s(m/s) T(C) PRE(hPa) RiNumber VertM(m/s)", | ||
b"20151222 0000 20.54 211.0 1.22 0.3 21.00 11.9 992.8 0.15 0.18", | ||
b"20151222 0010 21.02 212.2 2.55 0.6 21.35 11.8 992.7 0.29 -0.09", | ||
], | ||
"encoding": "utf-8", | ||
"delimiter": "\s+", | ||
"datetime_fieldnames": ["YYYYMMDD", "HHMM"], | ||
"datetime_formats": [r"%Y%m%d %H%M"], | ||
"na_values": ["NAN"], | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# serializer version: 1 | ||
# name: test_import_to_db[lines0-utf-8-\\s+-datetime_fieldnames0-datetime_formats0-na_values0] | ||
<QuerySet [<Reading: M(m/s) @ 2015-12-22 00:00:00 = 20.54>, <Reading: D(deg) @ 2015-12-22 00:00:00 = 211.0>, <Reading: SD(m/s) @ 2015-12-22 00:00:00 = 1.22>, <Reading: DSD(deg) @ 2015-12-22 00:00:00 = 0.3>, <Reading: Gust3s(m/s) @ 2015-12-22 00:00:00 = 21.00>, <Reading: T(C) @ 2015-12-22 00:00:00 = 11.9>, <Reading: PRE(hPa) @ 2015-12-22 00:00:00 = 992.8>, <Reading: RiNumber @ 2015-12-22 00:00:00 = 0.15>, <Reading: VertM(m/s) @ 2015-12-22 00:00:00 = 0.18>, <Reading: @ 2015-12-22 00:00:00 = >, <Reading: M(m/s) @ 2015-12-22 00:10:00 = 21.02>, <Reading: D(deg) @ 2015-12-22 00:10:00 = 212.2>, <Reading: SD(m/s) @ 2015-12-22 00:10:00 = 2.55>, <Reading: DSD(deg) @ 2015-12-22 00:10:00 = 0.6>, <Reading: Gust3s(m/s) @ 2015-12-22 00:10:00 = 21.35>, <Reading: T(C) @ 2015-12-22 00:10:00 = 11.8>, <Reading: PRE(hPa) @ 2015-12-22 00:10:00 = 992.7>, <Reading: RiNumber @ 2015-12-22 00:10:00 = 0.29>, <Reading: VertM(m/s) @ 2015-12-22 00:10:00 = -0.09>]> | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# serializer version: 1 | ||
# name: TestUploadFile.test_cannot_upload_an_invalid_file[lines0-utf-8-\\s+-datetime_fieldnames0-datetime_formats0-na_values0] | ||
b'File type creation failed: <ul class="errorlist"><li>__all__<ul class="errorlist nonfield"><li>No `datetime_fieldnames` ['YYYYMMDD', 'HHMM'] found!</li></ul></li></ul>' | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# serializer version: 1 | ||
# name: TestUploadFile.test_cannot_upload_a_missing_file_type[lines0-utf-8-\\s+-datetime_fieldnames0-datetime_formats0-na_values0] | ||
b'{"type":["This field may not be null."]}' | ||
# --- | ||
# name: TestUploadFile.test_cannot_upload_an_invalid_file[lines0-utf-8-\\s+-datetime_fieldnames0-datetime_formats0-na_values0] | ||
b'{"non_field_errors":["No `datetime_fieldnames` [\'YYYYMMDD\', \'HHMM\'] found!"]}' | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from http import HTTPStatus | ||
|
||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
import pytest | ||
from rest_framework.reverse import reverse | ||
|
||
from sensor.models import FileType | ||
from tests.globals import SOURCES | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize( | ||
"lines,encoding,delimiter,datetime_fieldnames,datetime_formats,na_values", | ||
[ | ||
( | ||
source["lines"], | ||
source["encoding"], | ||
source["delimiter"], | ||
source["datetime_fieldnames"], | ||
source["datetime_formats"], | ||
source["na_values"], | ||
) | ||
for source in SOURCES | ||
] | ||
) | ||
class TestUploadFile(): | ||
|
||
def test_can_upload_a_valid_payload( | ||
self, | ||
client, | ||
lines, | ||
encoding, | ||
delimiter, | ||
datetime_fieldnames, | ||
datetime_formats, | ||
na_values, | ||
): | ||
file_type = FileType.objects.create( | ||
name="type", | ||
encoding=encoding, | ||
delimiter=delimiter, | ||
datetime_fieldnames=datetime_fieldnames, | ||
datetime_formats=datetime_formats, | ||
na_values=na_values, | ||
) | ||
file = SimpleUploadedFile( | ||
name="sensor-readings.txt", content=b"\n".join(l for l in lines), | ||
) | ||
url = reverse("api:sensor:file-list") | ||
|
||
response = client.post( | ||
url, | ||
{"file": file, "type": file_type.name} | ||
) | ||
|
||
assert response.status_code == HTTPStatus.CREATED | ||
|
||
def test_cannot_upload_an_invalid_file( | ||
self, | ||
client, | ||
lines, | ||
encoding, | ||
delimiter, | ||
datetime_fieldnames, | ||
datetime_formats, | ||
na_values, | ||
snapshot, | ||
): | ||
file_type = FileType.objects.create( | ||
name="type", | ||
encoding=encoding, | ||
delimiter=delimiter, | ||
datetime_fieldnames=datetime_fieldnames, | ||
datetime_formats=datetime_formats, | ||
na_values=na_values, | ||
) | ||
invalid_file = SimpleUploadedFile( | ||
name="sensor-readings.txt", content=b"I am invalid!",) | ||
url = reverse("api:sensor:file-list") | ||
|
||
response = client.post( | ||
url, | ||
{"file": invalid_file, "type": file_type.name} | ||
) | ||
|
||
assert response.status_code == HTTPStatus.BAD_REQUEST | ||
assert response.content == snapshot | ||
|
||
|
||
def test_cannot_upload_a_missing_file_type( | ||
self, | ||
client, | ||
lines, | ||
encoding, | ||
delimiter, | ||
datetime_fieldnames, | ||
datetime_formats, | ||
na_values, | ||
snapshot, | ||
): | ||
file = SimpleUploadedFile( | ||
name="sensor-readings.txt", content=b"\n".join(l for l in lines), | ||
) | ||
url = reverse("api:sensor:file-list") | ||
|
||
response = client.post( | ||
url, | ||
{"file": file, "type": ""} | ||
) | ||
|
||
assert response.status_code == HTTPStatus.BAD_REQUEST | ||
assert response.content == snapshot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.