diff --git a/plants/templates/plants/partials/species_results.html b/plants/templates/plants/partials/species_results.html new file mode 100644 index 0000000..7ec09ab --- /dev/null +++ b/plants/templates/plants/partials/species_results.html @@ -0,0 +1,28 @@ +{% for result in results %} +
No results for "{{ q }}"
{% endif %} +{% endfor %} diff --git a/plants/templates/plants/species_search.html b/plants/templates/plants/species_search.html new file mode 100644 index 0000000..8ac15c4 --- /dev/null +++ b/plants/templates/plants/species_search.html @@ -0,0 +1,26 @@ +{% extends "plants/base.html" %} +{% block title %}Add Plant — PlantDB{% endblock %} +{% block content %} +Search for the species to auto-fill care info, or skip to enter details manually.
+ + + + + + +{% endblock %} diff --git a/plants/tests/test_views.py b/plants/tests/test_views.py index e7199da..0563bab 100644 --- a/plants/tests/test_views.py +++ b/plants/tests/test_views.py @@ -105,3 +105,47 @@ class TestLogPruning: {'pruned_on': '2026-05-01', 'notes': ''}, ) assert resp.templates[0].name == 'plants/partials/pruning_strip.html' + + +@pytest.mark.django_db +class TestSpeciesSearch: + def test_search_page_returns_200(self, client): + resp = client.get(reverse('plant_add')) + assert resp.status_code == 200 + + def test_htmx_search_returns_partial(self, client, settings): + settings.PERENUAL_API_KEY = '' + resp = client.get( + reverse('species_search') + '?q=rose', + HTTP_HX_REQUEST='true', + ) + assert resp.status_code == 200 + assert resp.templates[0].name == 'plants/partials/species_results.html' + + def test_species_select_creates_species_and_redirects(self, client): + resp = client.post(reverse('species_select'), { + 'perenual_id': '42', + 'common_name': 'Test Rose', + 'scientific_name': 'Rosa testii', + 'watering': 'Weekly', + 'sunlight': 'Full sun', + 'pruning_months': 'February, August', + 'api_image_url': '', + }) + assert resp.status_code == 302 + from plants.models import Species + assert Species.objects.filter(perenual_id=42).exists() + + def test_species_select_deduplicates(self, client): + from plants.models import Species + Species.objects.create(perenual_id=99, common_name='Existing') + client.post(reverse('species_select'), { + 'perenual_id': '99', + 'common_name': 'Existing', + 'scientific_name': '', + 'watering': '', + 'sunlight': '', + 'pruning_months': '', + 'api_image_url': '', + }) + assert Species.objects.filter(perenual_id=99).count() == 1 diff --git a/plants/urls.py b/plants/urls.py index 4a0611f..b710f20 100644 --- a/plants/urls.py +++ b/plants/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from plants.views import dashboard, plants, pruning +from plants.views import dashboard, plants, pruning, species urlpatterns = [ path('', dashboard.dashboard, name='dashboard'), @@ -10,4 +10,6 @@ urlpatterns = [ path('plants/