From ccca81bbab284ec413ea273df01e7f0efb01ab52 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 8 Feb 2024 23:05:20 -0500 Subject: [PATCH] Add tests for Location models --- vrobbler/apps/locations/tests/test_models.py | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 vrobbler/apps/locations/tests/test_models.py diff --git a/vrobbler/apps/locations/tests/test_models.py b/vrobbler/apps/locations/tests/test_models.py new file mode 100644 index 0000000..cc65816 --- /dev/null +++ b/vrobbler/apps/locations/tests/test_models.py @@ -0,0 +1,41 @@ +import pytest +import logging + +from locations.models import GeoLocation + +logger = logging.getLogger(__name__) + + +def test_find_or_create(caplog): + assert not GeoLocation.find_or_create({}) + assert "No lat or lon keys in data dict" in caplog.text + + +@pytest.mark.django_db +def test_find_or_create_truncation(): + loc = GeoLocation.find_or_create( + {"lat": 44.2345, "lon": -68.2345, "alt": 60.356} + ) + assert loc.lat == 44.234 + assert loc.lon == -68.234 + assert loc.altitude == 60 + + +@pytest.mark.django_db +def test_find_or_create_finds_existing(): + extant = GeoLocation.objects.create(lat=44.234, lon=-68.234, altitude=50) + + loc = GeoLocation.find_or_create( + {"lat": 44.2345, "lon": -68.2345, "alt": 60.356} + ) + assert loc.id == extant.id + + +@pytest.mark.django_db +def test_find_or_create_creates_new(): + extant = GeoLocation.objects.create(lat=44.234, lon=-69.234, altitude=60) + + loc = GeoLocation.find_or_create( + {"lat": 44.2345, "lon": -68.2345, "alt": 60.356} + ) + assert not loc.id == extant.id