Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/euring/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,18 @@
EuringField(name="More Other Marks", key="more_other_marks", euring_type=TYPE_ALPHABETIC, required=False),
]

# These are the field definitions per format as per the EURING Code Manual
# All keys
EURING_KEYS = [field.key for field in EURING_FIELDS]

# Map keys to index
EURING_KEY_INDEX = {key: index for index, key in enumerate(EURING_KEYS)}

# Fields per format (as per the EURING Code Manual)
EURING2020_FIELDS = EURING_FIELDS # 64 fields
EURING2000PLUS_FIELDS = EURING_FIELDS[:60]
EURING2000_FIELDS = EURING_FIELDS[:33]

# Keys per format
EURING2020_KEYS = [field.key for field in EURING2020_FIELDS]
EURING2000PLUS_KEYS = [field.key for field in EURING2000PLUS_FIELDS]
EURING2000_KEYS = [field.key for field in EURING2000_FIELDS]
15 changes: 7 additions & 8 deletions src/euring/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ def format_display_name(format: str) -> str:

def format_hint(format: str) -> str | None:
"""Suggest the closest machine-friendly format name."""
raw = format.strip()
lower = raw.lower()
if lower in FORMAT_VALUES:
return lower
if "2020" in lower:
value = format.strip().lower()
if value in FORMAT_VALUES:
return value
if "2020" in value:
return FORMAT_EURING2020
if "2000" in lower:
if "plus" in lower or "+" in lower:
if "2000" in value:
if "plus" in value or "+" in value:
return FORMAT_EURING2000PLUS
return FORMAT_EURING2000
return None


def unknown_format_error(format: str, name: str = "format") -> str:
def unknown_format_error_message(format: str, name: str = "format") -> str:
"""Return an error message for an unknown EURING format."""
hint = format_hint(format)
message = f'Unknown {name} "{format}". Use {FORMAT_EURING2000}, {FORMAT_EURING2000PLUS}, or {FORMAT_EURING2020}."'
Expand Down
4 changes: 2 additions & 2 deletions src/euring/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
FORMAT_EURING2000PLUS,
FORMAT_EURING2020,
normalize_format,
unknown_format_error,
unknown_format_error_message,
)
from .record import EuringRecord

Expand Down Expand Up @@ -531,7 +531,7 @@ def fields(
try:
normalized_format = normalize_format(format)
except ValueError:
typer.echo(unknown_format_error(format, name="format"), err=True)
typer.echo(unknown_format_error_message(format, name="format"), err=True)
raise typer.Exit(1)
if normalized_format == FORMAT_EURING2000:
allowed_keys = keys_2000
Expand Down
10 changes: 5 additions & 5 deletions src/euring/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
FORMAT_JSON,
format_display_name,
normalize_format,
unknown_format_error,
unknown_format_error_message,
)
from .rules import record_rule_errors, requires_euring2020
from .utils import is_all_hyphens, is_empty
Expand Down Expand Up @@ -410,7 +410,7 @@ def _apply_coordinate_downgrade(
raise ValueError(f'Unsupported alphabetic accuracy code "{accuracy}".')
values_by_key["accuracy_of_coordinates"] = mapped
coords = values_by_key.get("geographical_coordinates", "")
if coords.strip():
if coords.strip() and set(coords) != {"."}:
return
latitude = values_by_key.get("latitude", "")
longitude = values_by_key.get("longitude", "")
Expand Down Expand Up @@ -447,7 +447,7 @@ def _normalize_target_format(target_format: str) -> str:
try:
return normalize_format(target_format)
except ValueError:
raise ValueError(unknown_format_error(target_format, "target format"))
raise ValueError(unknown_format_error_message(target_format, "target format"))


def _normalize_source_format(source_format: str | None, value: str) -> str:
Expand All @@ -467,7 +467,7 @@ def _normalize_source_format(source_format: str | None, value: str) -> str:
try:
return normalize_format(source_format)
except ValueError:
raise ValueError(unknown_format_error(source_format, "source format"))
raise ValueError(unknown_format_error_message(source_format, "source format"))


def _field_index(key: str) -> int:
Expand All @@ -485,7 +485,7 @@ def _normalize_decode_format(format: str | None) -> str | None:
try:
return normalize_format(format)
except ValueError:
raise EuringConstraintException(unknown_format_error(format))
raise EuringConstraintException(unknown_format_error_message(format))


def _decode_raw_record(value: object, format: str | None) -> tuple[str, dict[str, str], list[dict[str, str]]]:
Expand Down
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# Tests for the EURING library
130 changes: 130 additions & 0 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""Shared test fixtures for EURING records."""

from euring.fields import (
EURING2000_KEYS,
EURING2000PLUS_KEYS,
EURING2020_KEYS,
)
from euring.formats import FORMAT_EURING2000, FORMAT_EURING2000PLUS, FORMAT_EURING2020

DEFAULT_TEST_VALUES = {
# Default test data for a EURING record in key-value format
"ringing_scheme": "GBB",
"primary_identification_method": "A0",
"identification_number": "1234567890",
"verification_of_the_metal_ring": "0",
"metal_ring_information": "1",
"other_marks_information": "ZZ",
"species_mentioned": "00010",
"species_concluded": "00010",
"manipulated": "N",
"moved_before_recovery": "0",
"catching_method": "M",
"catching_lures": "U",
"sex_mentioned": "U",
"sex_concluded": "U",
"age_mentioned": "2",
"age_concluded": "2",
"status": "U",
"brood_size": "99",
"pullus_age": "99",
"accuracy_of_pullus_age": "0",
"date": "01012024",
"accuracy_of_date": "0",
"time": "0000",
"place_code": "AB00",
"geographical_coordinates": "+0000000+0000000",
"accuracy_of_coordinates": "1",
"condition": "9",
"circumstances": "99",
"circumstances_presumed": "0",
"euring_code_identifier": "4",
"distance": "00000",
"direction": "000",
"elapsed_time": "00000",
# EURING2000 fields stop here.
"wing_length": "",
"third_primary": "",
"state_of_wing_point": "",
"mass": "",
"moult": "",
"plumage_code": "",
"hind_claw": "",
"bill_length": "",
"bill_method": "",
"total_head_length": "",
"tarsus": "",
"tarsus_method": "",
"tail_length": "",
"tail_difference": "",
"fat_score": "",
"fat_score_method": "",
"pectoral_muscle": "",
"brood_patch": "",
"primary_score": "",
"primary_moult": "",
"old_greater_coverts": "",
"alula": "",
"carpal_covert": "",
"sexing_method": "",
"place_name": "",
"remarks": "",
"reference": "",
# EURING2000+ fields stop here.
"latitude": "",
"longitude": "",
"current_place_code": "",
"more_other_marks": "",
}


def _make_euring_record(data: dict, format: str) -> str:
if format == FORMAT_EURING2000:
keys = EURING2000_KEYS
separator = ""
elif format == FORMAT_EURING2000PLUS:
keys = EURING2000PLUS_KEYS
separator = "|"
elif format == FORMAT_EURING2020:
keys = EURING2020_KEYS
separator = "|"
else:
raise ValueError(f"Unknown format: {format}")
record_dict = {key: value for key, value in DEFAULT_TEST_VALUES.items() if key in keys}
for key, value in data.items():
assert key in keys, f"Invalid key: {key}"
record_dict[key] = value
return separator.join(record_dict.values())


def _make_euring2000_record(**kwargs) -> str:
return _make_euring_record(kwargs, format=FORMAT_EURING2000)


def _make_euring2000plus_record(**kwargs) -> str:
return _make_euring_record(kwargs, format=FORMAT_EURING2000PLUS)


def _make_euring2020_record(**kwargs) -> str:
return _make_euring_record(kwargs, format=FORMAT_EURING2020)


def _make_euring2000plus_record_with_invalid_species(*, accuracy_of_coordinates: str = "1") -> str:
return _make_euring2000plus_record(
accuracy_of_coordinates=accuracy_of_coordinates,
species_mentioned="12ABC",
species_concluded="12ABC",
)


def _make_euring2020_record_for_coords(**kwargs) -> str:
return _make_euring2020_record(**kwargs)


def _make_euring2020_record_with_coords() -> str:
return _make_euring2020_record(
geographical_coordinates="." * 15,
accuracy_of_coordinates="A",
latitude="52.3760",
longitude="4.9000",
)
62 changes: 2 additions & 60 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,7 @@
from euring.coordinates import _lat_to_euring_coordinate, _lng_to_euring_coordinate
from euring.fields import EURING_FIELDS
from euring.main import app


def _make_euring2020_record_with_coords() -> str:
values = [""] * len(EURING_FIELDS)

def set_value(key: str, value: str) -> None:
for index, field in enumerate(EURING_FIELDS):
if field["key"] == key:
values[index] = value
return
raise ValueError(f"Unknown key: {key}")

set_value("ringing_scheme", "GBB")
set_value("primary_identification_method", "A0")
set_value("identification_number", "1234567890")
set_value("place_code", "AB00")
set_value("accuracy_of_coordinates", "A")
set_value("latitude", "52.3760")
set_value("longitude", "4.9000")
return "|".join(values)


def _make_euring2000_plus_record_with_invalid_species() -> str:
values = [
"GBB",
"A0",
"1234567890",
"0",
"1",
"ZZ",
"12ABC",
"12ABC",
"N",
"0",
"M",
"U",
"U",
"U",
"2",
"2",
"U",
"99",
"99",
"0",
"01012024",
"0",
"0000",
"AB00",
"+0000000+0000000",
"1",
"9",
"99",
"0",
"4",
"00000",
"000",
"00000",
]
return "|".join(values)
from tests.fixtures import _make_euring2000plus_record_with_invalid_species, _make_euring2020_record_with_coords


def test_lookup_place_verbose_includes_details():
Expand Down Expand Up @@ -306,7 +248,7 @@ def test_decode_cli_invalid_species_format_reports_errors():
import json

runner = CliRunner()
result = runner.invoke(app, ["decode", "--json", _make_euring2000_plus_record_with_invalid_species()])
result = runner.invoke(app, ["decode", "--json", _make_euring2000plus_record_with_invalid_species()])
assert result.exit_code == 1
payload = json.loads(result.output)
assert "errors" in payload
Expand Down
21 changes: 1 addition & 20 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from euring.converters import convert_euring_record
from euring.coordinates import _lat_to_euring_coordinate, _lng_to_euring_coordinate
from euring.fields import EURING_FIELDS
from tests.fixtures import _make_euring2020_record_with_coords


def _load_fixture(module_name: str, attr: str) -> str:
Expand All @@ -19,26 +20,6 @@ def _load_fixture(module_name: str, attr: str) -> str:
return getattr(module, attr)[0]


def _make_euring2020_record_with_coords() -> str:
values = [""] * len(EURING_FIELDS)
for index, field in enumerate(EURING_FIELDS):
if field["key"] == "ringing_scheme":
values[index] = "GBB"
if field["key"] == "primary_identification_method":
values[index] = "A0"
if field["key"] == "identification_number":
values[index] = "1234567890"
if field["key"] == "place_code":
values[index] = "AB00"
if field["key"] == "accuracy_of_coordinates":
values[index] = "A"
if field["key"] == "latitude":
values[index] = "52.3760"
if field["key"] == "longitude":
values[index] = "4.9000"
return "|".join(values)


def test_convert_unknown_target_format():
with pytest.raises(ValueError):
convert_euring_record("value", target_format="bad")
Expand Down
Loading