From 64f42deb2917a5bff8e38065641d8e4d3c582988 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Sun, 2 Nov 2025 14:35:23 +0100 Subject: [PATCH] Improves death year extraction and pytest configuration Fixes death year extraction using regex to correctly parse years Removes manual sys.path modifications in test files Adds pythonpath configuration to pytest.ini for simpler imports Enhances test coverage for life span formatting --- pytest.ini | 1 + src/index_generator.py | 7 +++++- tests/test_gedcom_parser.py | 4 ---- tests/test_index_generator.py | 39 ++++++++++++++++++++++++++------ tests/test_individual.py | 4 ---- tests/test_main.py | 4 ---- tests/test_markdown_generator.py | 4 ---- 7 files changed, 39 insertions(+), 24 deletions(-) diff --git a/pytest.ini b/pytest.ini index 909ef9b..296c840 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,5 +1,6 @@ [pytest] testpaths = tests +pythonpath = src python_files = test_*.py python_classes = Test* python_functions = test_* diff --git a/src/index_generator.py b/src/index_generator.py index 7482da6..f9d9895 100644 --- a/src/index_generator.py +++ b/src/index_generator.py @@ -8,6 +8,7 @@ family tree. from pathlib import Path from typing import List import logging +import re from individual import Individual @@ -85,7 +86,11 @@ class IndexGenerator: # Format life span if birth_info["year"] or death_info["date"]: - death_year = death_info["date"][:4] if death_info["date"] else "" + death_year = "" + if death_info["date"]: + match = re.search(r"(\d{4})\b", death_info["date"]) + if match: + death_year = match.group(1) life_span = f" ({birth_info['year']}-{death_year})" else: life_span = "" diff --git a/tests/test_gedcom_parser.py b/tests/test_gedcom_parser.py index 1a8f702..105b782 100644 --- a/tests/test_gedcom_parser.py +++ b/tests/test_gedcom_parser.py @@ -9,12 +9,8 @@ This module tests GEDCOM file parsing functionality including: """ import pytest -import sys from pathlib import Path -# Add src to path for imports -sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) - from gedcom_parser import GedcomParser diff --git a/tests/test_index_generator.py b/tests/test_index_generator.py index 2f4efc6..a3921a6 100644 --- a/tests/test_index_generator.py +++ b/tests/test_index_generator.py @@ -10,12 +10,8 @@ This module tests index file generation including: """ import pytest -import sys from pathlib import Path -# Add src to path for imports -sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) - from gedcom_parser import GedcomParser from individual import Individual from index_generator import IndexGenerator @@ -269,9 +265,8 @@ class TestLifeSpanFormatting: index_path = generator.generate_index(individuals_with_dates) content = index_path.read_text() - # Death date extraction gets first 4 chars: "15 J" from "15 JUN 2020" - # This is actually a bug in the code but we test actual behavior - assert '(1950-' in content # Just verify birth year is present + # Death date "15 JUN 2020" should extract year "2020" + assert '(1950-2020)' in content def test_life_span_with_only_birth(self, temp_dir, output_dir): """Test life span with only birth date.""" @@ -328,6 +323,36 @@ class TestLifeSpanFormatting: # Line should end with WikiLink, not life span assert not lines[0].strip().endswith(')') + def test_death_year_extraction_from_day_first_format(self, temp_dir, output_dir): + """Test that death year is correctly extracted when date starts with day.""" + gedcom_content = """0 HEAD +1 SOUR TestApp +1 GEDC +2 VERS 5.5.1 +1 CHAR UTF-8 +0 @I1@ INDI +1 NAME Test /Person/ +1 BIRT +2 DATE 1950 +1 DEAT +2 DATE 15 JUN 2020 +0 TRLR +""" + temp_file = temp_dir / "day_first.ged" + temp_file.write_text(gedcom_content, encoding='utf-8') + + parser = GedcomParser(temp_file) + elements = parser.get_individuals() + individuals = [Individual(elem, parser) for elem in elements] + + generator = IndexGenerator(output_dir) + index_path = generator.generate_index(individuals) + content = index_path.read_text() + + # Should show (1950-2020), not (1950-15 J) + assert '(1950-2020)' in content + assert '15 J' not in content + class TestIndexStatistics: """Tests for index statistics and metadata.""" diff --git a/tests/test_individual.py b/tests/test_individual.py index 34396a4..b339b74 100644 --- a/tests/test_individual.py +++ b/tests/test_individual.py @@ -11,12 +11,8 @@ This module tests individual person data extraction including: """ import pytest -import sys from pathlib import Path -# Add src to path for imports -sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) - from gedcom_parser import GedcomParser from individual import Individual diff --git a/tests/test_main.py b/tests/test_main.py index 10ad668..4fc3044 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,15 +10,11 @@ This module tests the command-line interface and main conversion workflow includ """ import pytest -import sys import zipfile from pathlib import Path from unittest.mock import patch, MagicMock from io import StringIO -# Add src to path for imports -sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) - import main from main import ( setup_logging, diff --git a/tests/test_markdown_generator.py b/tests/test_markdown_generator.py index c930073..9d4571f 100644 --- a/tests/test_markdown_generator.py +++ b/tests/test_markdown_generator.py @@ -10,12 +10,8 @@ This module tests Obsidian markdown note generation including: """ import pytest -import sys from pathlib import Path -# Add src to path for imports -sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) - from gedcom_parser import GedcomParser from individual import Individual from markdown_generator import MarkdownGenerator