83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
import pytest
|
|
from plants.models import Species, Plant, PruningLog, Location
|
|
from datetime import date
|
|
|
|
|
|
def make_location(name):
|
|
loc, _ = Location.objects.get_or_create(name=name)
|
|
return loc
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_species_str():
|
|
s = Species.objects.create(common_name='Rose')
|
|
assert str(s) == 'Rose'
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_plant_str():
|
|
p = Plant.objects.create(name='Kitchen Fern', location=make_location('Kitchen'))
|
|
assert str(p) == 'Kitchen Fern'
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_plant_species_nullable():
|
|
p = Plant.objects.create(name='Unknown plant')
|
|
assert p.species is None
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_pruning_log_str():
|
|
p = Plant.objects.create(name='Rose', location=make_location('Garden'))
|
|
log = PruningLog.objects.create(plant=p, pruned_on=date(2026, 5, 1))
|
|
assert 'Rose' in str(log)
|
|
assert '2026-05-01' in str(log)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_plant_pruning_months_default():
|
|
p = Plant.objects.create(name='Fern', location=make_location('Office'))
|
|
assert p.pruning_months == []
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_species_pruning_months_default():
|
|
s = Species.objects.create(common_name='Oak')
|
|
assert s.pruning_months == []
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_delete_species_nulls_plant_species():
|
|
s = Species.objects.create(common_name='Rose')
|
|
p = Plant.objects.create(name='Garden rose', location=make_location('Garden'), species=s)
|
|
s.delete()
|
|
p.refresh_from_db()
|
|
assert p.species is None
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_card_photo_belongs_to_plant():
|
|
from plants.models import PlantCardPhoto
|
|
plant = Plant.objects.create(name='Test')
|
|
photo = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/test.jpg')
|
|
assert photo.plant == plant
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_card_photos_ordered_by_uploaded_at():
|
|
from plants.models import PlantCardPhoto
|
|
plant = Plant.objects.create(name='Test')
|
|
p1 = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/a.jpg')
|
|
p2 = PlantCardPhoto.objects.create(plant=plant, image='plants/cards/b.jpg')
|
|
qs = list(PlantCardPhoto.objects.filter(plant=plant))
|
|
assert qs[0] == p1
|
|
assert qs[1] == p2
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_card_photos_deleted_with_plant():
|
|
from plants.models import PlantCardPhoto
|
|
plant = Plant.objects.create(name='Test')
|
|
PlantCardPhoto.objects.create(plant=plant, image='plants/cards/test.jpg')
|
|
plant.delete()
|
|
assert PlantCardPhoto.objects.count() == 0
|