Skip to content

Commit

Permalink
Add tests and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j committed Apr 19, 2024
1 parent 6e0a5d1 commit 3337254
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
25 changes: 23 additions & 2 deletions environment_data/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def stations(parameters):
@pytest.fixture
def measurements(parameters):
Measurement.objects.create(id=1, parameter=Parameter.objects.get(id=1), value=1.5)
Measurement.objects.create(id=2, parameter=Parameter.objects.get(id=2), value=2)

return Measurement.objects.all()


Expand All @@ -58,21 +60,22 @@ def parameters():
Parameter.objects.create(id=1, name="AQINDEX_PT1H_avg")
Parameter.objects.create(id=2, name="NO2_PT1H_avg")
Parameter.objects.create(id=3, name="WS_PT1H_avg")

return Parameter.objects.all()


@pytest.mark.django_db
@pytest.fixture
def years():
Year.objects.create(id=1, year_number=2023)
Year.objects.create(id=2, year_number=2022)
return Year.objects.all()


@pytest.mark.django_db
@pytest.fixture
def months(years):
Month.objects.create(month_number=1, year=years[0])
Month.objects.create(month_number=1, year=years[1])
return Month.objects.all()


Expand All @@ -81,6 +84,8 @@ def months(years):
def weeks(years):
week = Week.objects.create(week_number=1)
week.years.add(years[0])
week = Week.objects.create(week_number=1)
week.years.add(years[1])
return Week.objects.all()


Expand All @@ -93,13 +98,20 @@ def days(years, months, weeks):
month=months[0],
week=weeks[0],
)
Day.objects.create(
date=parser.parse("2022-01-01 00:00:00"),
year=years[1],
month=months[1],
week=weeks[1],
)
return Day.objects.all()


@pytest.mark.django_db
@pytest.fixture
def hours(days):
Hour.objects.create(day=days[0], hour_number=0)
Hour.objects.create(day=days[1], hour_number=0)
return Hour.objects.all()


Expand All @@ -108,6 +120,8 @@ def hours(days):
def year_datas(stations, years, measurements):
year_data = YearData.objects.create(station=stations[0], year=years[0])
year_data.measurements.add(measurements[0])
year_data = YearData.objects.create(station=stations[0], year=years[1])
year_data.measurements.add(measurements[1])
return YearData.objects.all()


Expand All @@ -116,6 +130,8 @@ def year_datas(stations, years, measurements):
def month_datas(stations, months, measurements):
month_data = MonthData.objects.create(station=stations[0], month=months[0])
month_data.measurements.add(measurements[0])
month_data = MonthData.objects.create(station=stations[0], month=months[1])
month_data.measurements.add(measurements[1])
return MonthData.objects.all()


Expand All @@ -124,14 +140,17 @@ def month_datas(stations, months, measurements):
def week_datas(stations, weeks, measurements):
week_data = WeekData.objects.create(station=stations[0], week=weeks[0])
week_data.measurements.add(measurements[0])
week_data = WeekData.objects.create(station=stations[0], week=weeks[1])
week_data.measurements.add(measurements[1])
return WeekData.objects.all()


@pytest.mark.django_db
@pytest.fixture
def day_datas(stations, days, measurements):
day_data = DayData.objects.create(station=stations[0], day=days[0])
day_data.measurements.add(measurements[0])
day_data = DayData.objects.create(station=stations[0], day=days[1])
day_data.measurements.add(measurements[1])
return DayData.objects.all()


Expand All @@ -140,4 +159,6 @@ def day_datas(stations, days, measurements):
def hour_datas(stations, hours, measurements):
hour_data = HourData.objects.create(station=stations[0], hour=hours[0])
hour_data.measurements.add(measurements[0])
hour_data = HourData.objects.create(station=stations[0], hour=hours[1])
hour_data.measurements.add(measurements[1])
return HourData.objects.all()
41 changes: 41 additions & 0 deletions environment_data/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,25 @@ def test_day_data(api_client, day_datas, parameters):
)
response = api_client.get(url)
assert response.status_code == 200
assert len(response.json()["results"]) == 1
json_data = response.json()["results"][0]
assert len(json_data["measurements"]) == 1
assert json_data["measurements"][0]["value"] == 1.5
assert json_data["measurements"][0]["parameter"] == parameters[0].name
assert json_data["date"] == "2023-01-01"


@pytest.mark.django_db
def test_day_data_non_existing_year(api_client, day_datas, parameters):
url = (
reverse("environment_data:data-list")
+ "?year=2020&start=01-01&end=02-01&station_id=1&type=day"
)
response = api_client.get(url)
assert response.status_code == 200
assert len(response.json()["results"]) == 0


@pytest.mark.django_db
def test_week_data(api_client, week_datas, parameters):
url = (
Expand All @@ -63,6 +75,7 @@ def test_week_data(api_client, week_datas, parameters):
)
response = api_client.get(url)
assert response.status_code == 200
assert len(response.json()["results"]) == 1
json_data = response.json()["results"][0]
assert len(json_data["measurements"]) == 1
assert json_data["measurements"][0]["value"] == 1.5
Expand All @@ -78,11 +91,38 @@ def test_month_data(api_client, month_datas, parameters):
)
response = api_client.get(url)
assert response.status_code == 200
assert len(response.json()["results"]) == 1
json_data = response.json()["results"][0]
assert len(json_data["measurements"]) == 1
assert json_data["measurements"][0]["value"] == 1.5
assert json_data["measurements"][0]["parameter"] == parameters[0].name
assert json_data["month_number"] == 1
url = (
reverse("environment_data:data-list")
+ "?year=2023&start=1&end=1&station_id=411&type=month"
)
response = api_client.get(url)
assert len(response.json()["results"]) == 0


@pytest.mark.django_db
def test_month_data_non_existing_year(api_client, month_datas, parameters):
url = (
reverse("environment_data:data-list")
+ "?year=2020&start=1&end=1&station_id=411&type=month"
)
response = api_client.get(url)
assert len(response.json()["results"]) == 0


@pytest.mark.django_db
def test_month_data_chars_in_arguments(api_client, month_datas, parameters):
url = (
reverse("environment_data:data-list")
+ "?year=foo&start=abc&end=dce&station_id=foobar&type=month"
)
response = api_client.get(url)
assert len(response.json()["results"]) == 0


@pytest.mark.django_db
Expand All @@ -94,6 +134,7 @@ def test_year_data(api_client, year_datas, parameters):
response = api_client.get(url)
assert response.status_code == 200
json_data = response.json()["results"][0]
assert len(response.json()["results"]) == 1
assert len(json_data["measurements"]) == 1
assert json_data["measurements"][0]["value"] == 1.5
assert json_data["measurements"][0]["parameter"] == parameters[0].name
Expand Down

0 comments on commit 3337254

Please sign in to comment.