Skip to content

Commit

Permalink
Fixup 'S casing and species addition bug
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Oct 31, 2024
1 parent a5826b4 commit ef25e5b
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM python:3.10-slim-bullseye AS runtime

COPY naturerecorderpy-1.9.0.0 /opt/naturerecorderpy
COPY naturerecorderpy-1.10.0.0 /opt/naturerecorderpy

WORKDIR /opt/naturerecorderpy

RUN apt-get update -y
RUN pip install -r requirements.txt
RUN pip install nature_recorder-1.9.0-py3-none-any.whl
RUN pip install nature_recorder-1.10.0-py3-none-any.whl

ENV NATURE_RECORDER_DATA_FOLDER=/var/opt/naturerecorderpy
ENV NATURE_RECORDER_DB=/var/opt/naturerecorderpy/naturerecorder.db
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def find_package_files(directory, remove_root):

setuptools.setup(
name="nature_recorder",
version="1.9.0",
version="1.10.0",
description="Wildlife sightings database",
packages=setuptools.find_packages("src"),
include_package_data=True,
Expand Down
6 changes: 3 additions & 3 deletions src/naturerec_model/logic/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime as dt
from sqlalchemy.exc import IntegrityError, NoResultFound
from ..model import Session, Category, Species

from .naming import tidy_string, Casing

def _check_for_existing_records(session, name):
"""
Expand Down Expand Up @@ -36,7 +36,7 @@ def create_category(name, user):
with Session.begin() as session:
# There is a check constraint to prevent duplicates in the Python model but the pre-existing database
# does not have that constraint so explicitly check for duplicates before adding a new record
tidied = " ".join(name.split()).title() if name else None
tidied = tidy_string(name, Casing.TITLE_CASE)
if len(_check_for_existing_records(session, tidied)):
raise ValueError("Duplicate category found")

Expand Down Expand Up @@ -68,7 +68,7 @@ def update_category(category_id, name, user):
with Session.begin() as session:
# There is a check constraint to prevent duplicates in the Python model but the pre-existing database
# does not have that constraint so explicitly check for duplicates before adding a new record
tidied = " ".join(name.split()).title() if name else None
tidied = tidy_string(name, Casing.TITLE_CASE)
category_ids = _check_for_existing_records(session, tidied)

# Remove the current category from the list, if it's there
Expand Down
30 changes: 30 additions & 0 deletions src/naturerec_model/logic/naming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Species and category common and scientific naming logic
"""

class Casing:
TITLE_CASE = "title_case"
CAPITALISED = "capitalised"


def tidy_string(text, required_case):
"""
Tidy the case of the specified string and remove duplicate spaces
:param name: Text string to tidy
:param required_case: Required casing
:returns: Tidied text string
"""

tidied_text = None

if text:
match required_case:
case Casing.TITLE_CASE:
tidied_text = " ".join(text.split()).title().replace("'S", "'s")
case Casing.CAPITALISED:
tidied_text = " ".join(text.split()).capitalize()
case _:
tidied_text = text

return tidied_text
9 changes: 5 additions & 4 deletions src/naturerec_model/logic/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime as dt
from sqlalchemy.exc import IntegrityError, NoResultFound
from ..model import Session, Species, Sighting, SpeciesStatusRating
from .naming import tidy_string, Casing


def _check_for_existing_records(session, category_id, name):
Expand Down Expand Up @@ -41,8 +42,8 @@ def create_species(category_id, name, scientific_name, user):
with Session.begin() as session:
# There is a check constraint to prevent duplicates in the Python model but the pre-existing database
# does not have that constraint so explicitly check for duplicates before adding a new record
tidied_name = " ".join(name.split()).title() if name else None
tidied_scientific_name = " ".join(scientific_name.split()).title() if scientific_name else None
tidied_name = tidy_string(name, Casing.TITLE_CASE)
tidied_scientific_name = tidy_string(scientific_name, Casing.CAPITALISED)
if len(_check_for_existing_records(session, category_id, tidied_name)):
raise ValueError("Duplicate category found")

Expand Down Expand Up @@ -77,8 +78,8 @@ def update_species(species_id, category_id, name, scientific_name, user):
with Session.begin() as session:
# There is a check constraint to prevent duplicates in the Python model but the pre-existing database
# does not have that constraint so explicitly check for duplicates before adding a new record
tidied_name = " ".join(name.split()).title() if name else None
tidied_scientific_name = " ".join(scientific_name.split()).title() if scientific_name else None
tidied_name = tidy_string(name, Casing.TITLE_CASE)
tidied_scientific_name = tidy_string(scientific_name, Casing.CAPITALISED)
species_ids = _check_for_existing_records(session, category_id, tidied_name)

# Remove the current category from the list, if it's there
Expand Down
2 changes: 1 addition & 1 deletion src/naturerec_web/species/species_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def edit(species_id):
if species_id:
_ = update_species(species_id, get_posted_int("category"), request.form["name"], request.form["scientific_name"], current_user)
else:
_ = create_species(get_posted_int("category"), request.form["name"], current_user)
_ = create_species(get_posted_int("category"), request.form["name"], request.form["scientific_name"], current_user)
return redirect("/species/list")
except ValueError as e:
return _render_species_editing_page(species_id, e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_can_export_sightings(self):
self.assertEqual(SightingsExportHelper.COLUMN_NAMES, rows[0])
self.assertEqual(16, len(rows[1]))
self.assertEqual("Black-Headed Gull", rows[1][0])
self.assertEqual("Chroicocephalus Ridibundus", rows[1][1])
self.assertEqual("Chroicocephalus ridibundus", rows[1][1])
self.assertEqual("Birds", rows[1][2])
self.assertEqual("", rows[1][3])
self.assertEqual("Unknown", rows[1][4])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _perform_valid_import(self):
category = get_category("Birds")
self.assertEqual(1, len(category.species))
self.assertEqual("Robin", category.species[0].name)
self.assertEqual("Erithacus Rubecula", category.species[0].scientific_name)
self.assertEqual("Erithacus rubecula", category.species[0].scientific_name)

# Check the location was imported correctly
location = get_location("Abingdon")
Expand Down
10 changes: 5 additions & 5 deletions tests/naturerec_model/logic/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def test_can_create_species(self):
species = session.query(Species).one()
self.assertTrue(category.id, species.categoryId)
self.assertEqual("Red Kite", species.name)
self.assertEqual("Milvus Milvus", species.scientific_name)
self.assertEqual("Milvus milvus", species.scientific_name)

def test_cannot_create_duplicate_species(self):
with self.assertRaises(ValueError), Session.begin() as session:
category_id = session.query(Species).one().categoryId
_ = create_species(category_id, "Red Kite", "Milvus Milvus", self._user)
_ = create_species(category_id, "Red Kite", "Milvus milvus", self._user)

def test_can_update_species(self):
category_id = create_category("Insects", self._user).id
Expand All @@ -35,13 +35,13 @@ def test_can_update_species(self):
species = get_species(species_id)
self.assertEqual("Insects", species.category.name)
self.assertEqual("Azure Damselfly", species.name)
self.assertEqual("Coenagrion Puella", species.scientific_name)
self.assertEqual("Coenagrion puella", species.scientific_name)

def test_cannot_update_species_to_create_duplicate(self):
category = get_category("Birds")
species = create_species(category.id, "Robin", "Erithacus rubecula", self._user)
with self.assertRaises(ValueError):
_ = update_species(species.id, category.id, "Red Kite", "Milvus Milvus", self._user)
_ = update_species(species.id, category.id, "Red Kite", "Milvus milvus", self._user)

def test_cannot_update_missing_species(self):
category = get_category("Birds")
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_can_list_species(self):
species = list_species(category_id)
self.assertEqual(1, len(species))
self.assertEqual("Red Kite", species[0].name)
self.assertEqual("Milvus Milvus", species[0].scientific_name)
self.assertEqual("Milvus milvus", species[0].scientific_name)

def test_can_delete_species(self):
category_id = get_category("Birds").id
Expand Down
2 changes: 1 addition & 1 deletion tests/naturerec_model/logic/test_species_status_ratings.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_can_list_filter_ratings_by_species(self):
ratings = list_species_status_ratings(species_id=species.id)
self.assertEqual(1, len(ratings))
# Python title casing's a bit interesting!
self.assertEqual("Bewick'S Swan", ratings[0].species.name)
self.assertEqual("Bewick's Swan", ratings[0].species.name)
self.assertEqual("BOCC4", ratings[0].rating.scheme.name)
self.assertEqual("Amber", ratings[0].rating.name)
self.assertEqual("United Kingdom", ratings[0].region)
Expand Down
4 changes: 2 additions & 2 deletions tests/naturerec_model/model/test_sighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUp(self) -> None:
create_database()
self._user = User(id=1)
self._category = create_category("Birds", self._user)
self._gull = create_species(self._category.id, "Black-Headed Gull", "Chroicocephalus Ridibundus", self._user)
self._gull = create_species(self._category.id, "Black-Headed Gull", "Chroicocephalus ridibundus", self._user)
self._cormorant = create_species(self._category.id, "Cormorant", None, self._user)
self._location = create_location(name="Radley Lakes", county="Oxfordshire", country="United Kingdom", user=self._user)
_ = create_sighting(self._location.id, self._gull.id, datetime.date(2021, 12, 14), None, Gender.UNKNOWN, False,
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_can_get_csv_columns(self):
columns = session.query(Sighting).one().csv_columns
self.assertEqual(16, len(columns))
self.assertEqual("Black-Headed Gull", columns[0])
self.assertEqual("Chroicocephalus Ridibundus", columns[1])
self.assertEqual("Chroicocephalus ridibundus", columns[1])
self.assertEqual("Birds", columns[2])
self.assertIsNone(columns[3])
self.assertEqual("Unknown", columns[4])
Expand Down
4 changes: 2 additions & 2 deletions tests/naturerec_model/model/test_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ def setUp(self) -> None:
create_database()
self._user = User(id=1)
category = create_category("Birds", self._user)
_ = create_species(category.id, "Red Kite", "Milvus Milvus", self._user)
_ = create_species(category.id, "Red Kite", "Milvus milvus", self._user)

def test_can_create_species(self):
with Session.begin() as session:
species = session.query(Species).one()
self.assertEqual("Red Kite", species.name)
self.assertEqual("Milvus Milvus", species.scientific_name)
self.assertEqual("Milvus milvus", species.scientific_name)
self.assertEqual("Birds", species.category.name)

def test_cannot_create_species_against_missing_category(self):
Expand Down

0 comments on commit ef25e5b

Please sign in to comment.