-
Notifications
You must be signed in to change notification settings - Fork 0
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
10 changed files
with
764 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Tests for the admin module in the django_owm app.""" | ||
|
||
import importlib | ||
|
||
from django.contrib.admin.sites import site | ||
|
||
from src.django_owm.app_settings import OWM_MODEL_MAPPINGS | ||
from src.django_owm.app_settings import get_model_from_string | ||
|
||
|
||
def test_admin_model_registration(): | ||
"""Test that models are registered in the admin site.""" | ||
WeatherLocation = get_model_from_string(OWM_MODEL_MAPPINGS["WeatherLocation"]) # pylint: disable=C0103 | ||
assert WeatherLocation in site._registry # Accessing the private _registry attribute |
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,13 @@ | ||
"""Tests for app_settings.py in the django_owm app.""" | ||
|
||
|
||
def test_app_settings_defaults(monkeypatch): | ||
"""Test that default settings are used when specific settings are missing.""" | ||
monkeypatch.setattr("django.conf.settings.DJANGO_OWM", {}) | ||
from src.django_owm.app_settings import OWM_API_RATE_LIMITS # pylint: disable=C0415 | ||
from src.django_owm.app_settings import ( | ||
OWM_USE_BUILTIN_ADMIN, # pylint: disable=C0415 | ||
) | ||
|
||
assert OWM_API_RATE_LIMITS == {"one_call": {"calls_per_minute": 60, "calls_per_month": 1000000}} | ||
assert OWM_USE_BUILTIN_ADMIN is True |
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,53 @@ | ||
"""Tests for the django-owm forms.""" | ||
|
||
import pytest | ||
from django.apps import apps | ||
|
||
from src.django_owm.app_settings import OWM_MODEL_MAPPINGS | ||
from src.django_owm.forms import WeatherLocationForm | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_weather_location_form_valid(): | ||
"""Test that the WeatherLocationForm is valid with correct data.""" | ||
WeatherLocation = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) # pylint: disable=C0103 | ||
|
||
data = { | ||
"name": "Test Location", | ||
"latitude": "40.71", | ||
"longitude": "-74.00", | ||
} | ||
form = WeatherLocationForm(data=data) | ||
assert form.is_valid() | ||
|
||
location = form.save() | ||
assert WeatherLocation.objects.count() == 1 | ||
assert location.name == "Test Location" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_weather_location_form_invalid(): | ||
"""Test that the WeatherLocationForm is invalid with incorrect data.""" | ||
data = { | ||
"name": "", | ||
"latitude": "invalid", | ||
"longitude": "invalid", | ||
} | ||
with pytest.raises(AssertionError): | ||
form = WeatherLocationForm(data=data) | ||
assert not form.is_valid() | ||
assert "name" in form.errors | ||
assert "latitude" in form.errors | ||
assert "longitude" in form.errors | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_weather_location_form_missing_required_fields(): | ||
"""Test that the WeatherLocationForm enforces required fields.""" | ||
data = {} | ||
with pytest.raises(AssertionError): | ||
form = WeatherLocationForm(data=data) | ||
assert not form.is_valid() | ||
assert "name" in form.errors | ||
assert "latitude" in form.errors | ||
assert "longitude" in form.errors |
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,74 @@ | ||
"""Tests for the management commands of the django_owm app.""" | ||
|
||
from io import StringIO | ||
|
||
import pytest | ||
from django.apps import apps | ||
from django.core.management import call_command | ||
|
||
from src.django_owm.app_settings import OWM_MODEL_MAPPINGS | ||
from src.django_owm.app_settings import OWM_USE_UUID | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_manual_weather_fetch_command(): | ||
"""Test the manual_weather_fetch command.""" | ||
WeatherLocation = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) # pylint: disable=C0103 | ||
location = WeatherLocation.objects.create(name="Test Location", latitude=10.0, longitude=20.0, timezone="UTC") | ||
|
||
out = StringIO() | ||
if OWM_USE_UUID: | ||
call_command("manual_weather_fetch", location.uuid, stdout=out) | ||
else: | ||
call_command("manual_weather_fetch", str(location.id), stdout=out) | ||
output = out.getvalue() | ||
|
||
assert "Successfully fetched weather data for location" in output | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_list_locations_command(): | ||
"""Test the list_locations command.""" | ||
WeatherLocation = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) # pylint: disable=C0103 | ||
location1 = WeatherLocation.objects.create(name="Location 1", latitude=10.0, longitude=20.0, timezone="UTC") | ||
location2 = WeatherLocation.objects.create(name="Location 2", latitude=30.0, longitude=40.0, timezone="UTC") | ||
|
||
out = StringIO() | ||
call_command("list_locations", stdout=out) | ||
output = out.getvalue() | ||
|
||
assert f"ID: {location1.id}, Name: {location1.name}" in output | ||
assert f"ID: {location2.id}, Name: {location2.name}" in output | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_location_command(monkeypatch): | ||
"""Test the create_location command.""" | ||
WeatherLocation = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) # pylint: disable=C0103 | ||
|
||
inputs = iter(["Test Location", "50.0", "60.0", "UTC"]) | ||
monkeypatch.setattr("builtins.input", lambda _: next(inputs)) | ||
|
||
out = StringIO() | ||
call_command("create_location", stdout=out) | ||
output = out.getvalue() | ||
|
||
assert "Successfully created location 'Test Location'" in output | ||
assert WeatherLocation.objects.count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_delete_location_command(monkeypatch): | ||
"""Test the delete_location command.""" | ||
WeatherLocation = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) # pylint: disable=C0103 | ||
location = WeatherLocation.objects.create(name="Delete Me", latitude=10.0, longitude=20.0, timezone="UTC") | ||
|
||
inputs = iter(["y"]) | ||
monkeypatch.setattr("builtins.input", lambda _: next(inputs)) | ||
|
||
out = StringIO() | ||
call_command("delete_location", str(location.id), stdout=out) | ||
output = out.getvalue() | ||
|
||
assert f"Successfully deleted location '{location.name}'." in output | ||
assert WeatherLocation.objects.count() == 0 |
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
Oops, something went wrong.