Skip to content

Commit

Permalink
Fix Location detail view, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leethobbit committed Dec 4, 2024
1 parent 354742d commit 0ef5918
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 16 deletions.
5 changes: 5 additions & 0 deletions dragonroost/animals/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class Meta:


class MedicalRecordForm(forms.ModelForm):
current_weight = MeasurementField(
measurement=Weight,
unit_choices=(("lb", "lb"), ("kg", "kg"), ("g", "g")),
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
Expand Down
2 changes: 1 addition & 1 deletion dragonroost/animals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def get_animals_needing_medical_attention(self):
i.e. where the status is "MEDICAL_HOLD".
"""

return self.filter(status="MEDICAL_HOLD" / "QUARANTINE")
return self.filter(status="QUARANTINE")
6 changes: 3 additions & 3 deletions dragonroost/animals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("species_detail", args=[str(self.id)])
return reverse("animals:species-detail", args=[str(self.id)])


class Breed(models.Model):
Expand Down Expand Up @@ -75,7 +75,7 @@ def __str__(self):
return self.name

def get_absolute_url(self):
return reverse("breed_detail", args=[str(self.id)])
return reverse("animals:breed-detail", args=[str(self.id)])


class Animal(models.Model):
Expand Down Expand Up @@ -190,7 +190,7 @@ def animal_tag(self):

@property
def is_available(self):
return self.status != "ADOPTED"
return self.status == "AVAILABLE"

@property
def days_since_intake(self):
Expand Down
30 changes: 30 additions & 0 deletions dragonroost/animals/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import factory
from measurement.measures import Weight

from dragonroost.animals.models import Breed
from dragonroost.animals.models import Species
from dragonroost.business.tests.factories import LocationFactory
from dragonroost.people.tests.factories import PersonFactory


class SpeciesFactory(factory.django.DjangoModelFactory):
Expand All @@ -21,6 +24,19 @@ class Meta:
is_ohio_native = factory.Iterator([True, False])


class BreedFactory(factory.django.DjangoModelFactory):
class Meta:
model = "animals.Breed"
django_get_or_create = ("name", "origin", "temperament")

name = factory.Sequence(lambda n: f"Breed {n}")
origin = factory.Faker("country")
temperament = factory.Faker(
"random_element",
elements=[x[0] for x in Breed.TEMPERAMENT_CHOICES],
)


class AnimalFactory(factory.django.DjangoModelFactory):
class Meta:
model = "animals.Animal"
Expand All @@ -29,3 +45,17 @@ class Meta:
name = factory.Faker("first_name")
species = factory.SubFactory(SpeciesFactory)
location = factory.SubFactory(LocationFactory)
breed = None
starting_weight = Weight(lb=10)
status = factory.Iterator(["AVAILABLE", "QUARANTINE"])


class MedicalRecordFactory(factory.django.DjangoModelFactory):
class Meta:
model = "animals.MedicalRecord"
django_get_or_create = ("animal", "notes")

notes = factory.Faker("text")
current_weight = Weight(lb=15)
animal = factory.SubFactory(AnimalFactory)
q_volunteer = factory.SubFactory(PersonFactory)
72 changes: 72 additions & 0 deletions dragonroost/animals/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from dragonroost.animals.models import Animal
from dragonroost.animals.models import MedicalRecord


# Animal model tests
@pytest.mark.django_db
def test_animal_queryset_get_available_animals(animals):
qs = Animal.objects.get_available_animals()
assert qs.count() > 0
assert all(animal.status == "AVAILABLE" for animal in qs)


@pytest.mark.django_db
def test_animal_queryset_get_animals_needing_medical_attention(animals):
qs = Animal.objects.get_animals_needing_medical_attention()
assert qs.count() > 0
assert all(animal.status == "QUARANTINE" for animal in qs)


@pytest.mark.django_db
def test_animal_str_method(animal):
assert str(animal) == animal.name


@pytest.mark.django_db
def test_animal_get_absolute_url(animal):
assert animal.get_absolute_url() == f"/animals/{animal.pk}/"


@pytest.mark.django_db
def test_animal_get_animal_tag(animal):
assert animal.animal_tag == f"A-{animal.pk:05d}"


@pytest.mark.django_db
def test_animal_is_available(animal_is_available):
assert animal_is_available.is_available is True


@pytest.mark.django_db
def test_animal_number_of_medical_records(medical_record):
animal = medical_record.animal
number_of_records = 1
assert animal.number_of_medical_records == number_of_records


@pytest.mark.django_db
def test_animal_latest_medical_record(medical_record):
animal = medical_record.animal
latest_medical_record = MedicalRecord.objects.filter(animal=animal).latest(
"created",
)
assert animal.latest_medical_record == latest_medical_record


# Species model tests
@pytest.mark.django_db
def test_species_str_method(species):
assert str(species) == species.name


@pytest.mark.django_db
def test_species_get_absolute_url(species):
assert species.get_absolute_url() == f"/animals/species/{species.pk}/detail/"


# MedicalRecord model tests
@pytest.mark.django_db
def test_medical_record_str_method(medical_record):
assert str(medical_record) == medical_record.notes
19 changes: 15 additions & 4 deletions dragonroost/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dragonroost.animals.models import MedicalRecord
from dragonroost.animals.models import Species
from dragonroost.animals.tests.factories import AnimalFactory
from dragonroost.animals.tests.factories import MedicalRecordFactory
from dragonroost.animals.tests.factories import SpeciesFactory
from dragonroost.users.models import User
from dragonroost.users.tests.factories import UserFactory
Expand All @@ -21,14 +22,24 @@ def user(db) -> User:

@pytest.fixture
def species(db) -> Species:
return SpeciesFactory.create_batch(5)
return SpeciesFactory.create()


@pytest.fixture
def animals(db, species) -> Animal:
def animal(db) -> Animal:
return AnimalFactory()


@pytest.fixture
def animals(db) -> Animal:
return AnimalFactory.create_batch(5)


@pytest.fixture
def medical_records(db, animals) -> MedicalRecord:
return MedicalRecord.objects.filter(animal__in=animals)
def animal_is_available(db) -> Animal:
return AnimalFactory.create(status="AVAILABLE")


@pytest.fixture
def medical_record(db) -> MedicalRecord:
return MedicalRecordFactory()
13 changes: 13 additions & 0 deletions dragonroost/people/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory

from dragonroost.people.models import Person


class PersonFactory(factory.django.DjangoModelFactory):
class Meta:
model = Person

first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
email = factory.Faker("email")
phone_number = "555-5555"
28 changes: 20 additions & 8 deletions dragonroost/templates/business/location_detail.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
{% extends "base.html" %}

{% block content %}
<h1>{{ location.name }} - Detail</h1>
<p>{{ location.description }}</p>
<a href="{% url 'business:location-update' location.id %}">Edit</a>
<a href="{% url 'business:location-delete' location.id %}">Delete</a>
{% endblock content %}
<div class="modal modal-content">
<div class="modal-header">
<h5>Location - {{ location.name }}</h5>
</div>
<div class="modal-body">
<p class="card-text">Description: {{ location.description }}</p>
<a href="{% url 'business:location-update' location.id %}"
hx-boost="true"
hx-push-url="false"
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-primary">Edit</a>
<a href="{% url 'business:location-delete' location.id %}"
hx-boost="true"
hx-push-url="false"
hx-target="#dialog"
hx-swap="innerHTML"
class="btn btn-outline-danger">Delete</a>
</div>
</div>

0 comments on commit 0ef5918

Please sign in to comment.