Files
gedcom-to-markdown/tests/test_markdown_generator.py
T
Jared Howland 2108325252 feat: extract place lat/long coordinates
* Show coordinates for any place that has them
* Add tests for coordinates
2026-04-16 22:16:34 -06:00

447 lines
15 KiB
Python

"""
Tests for the MarkdownGenerator module.
This module tests Obsidian markdown note generation including:
- Note file creation
- Metadata formatting
- WikiLinks generation
- Section formatting
- Story file generation
"""
import pytest
from pathlib import Path
from gedcom_parser import GedcomParser
from individual import Individual
from markdown_generator import MarkdownGenerator
class TestMarkdownGeneratorInitialization:
"""Tests for MarkdownGenerator initialization."""
def test_init_with_valid_output_dir(self, output_dir):
"""Test that generator initializes with valid directory."""
generator = MarkdownGenerator(output_dir)
assert generator.output_dir == output_dir
assert generator.media_subdir == ''
assert generator.stories_subdir == ''
def test_init_with_subdirs(self, output_dir):
"""Test initialization with subdirectory configuration."""
generator = MarkdownGenerator(
output_dir,
media_subdir='media',
stories_subdir='stories'
)
assert generator.media_subdir == 'media'
assert generator.stories_subdir == 'stories'
def test_init_with_nonexistent_dir(self, temp_dir):
"""Test that ValueError is raised for non-existent directory."""
nonexistent = temp_dir / "nonexistent"
with pytest.raises(ValueError) as exc_info:
MarkdownGenerator(nonexistent)
assert "doesn't exist" in str(exc_info.value)
def test_init_with_file_instead_of_dir(self, temp_dir):
"""Test that ValueError is raised when path is a file."""
file_path = temp_dir / "file.txt"
file_path.write_text("test")
with pytest.raises(ValueError) as exc_info:
MarkdownGenerator(file_path)
assert "not a directory" in str(exc_info.value)
class TestNoteGeneration:
"""Tests for individual note generation."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
@pytest.fixture
def john_doe(self, sample_gedcom_file):
"""Get John Doe individual for testing."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser.parser)
def test_generate_note(self, generator, john_doe):
"""Test basic note generation."""
file_path = generator.generate_note(john_doe)
assert file_path.exists()
assert file_path.suffix == '.md'
assert 'Doe John 1950' in file_path.name
content = file_path.read_text()
assert '# John Doe' in content
def test_generate_note_content_structure(self, generator, john_doe):
"""Test that generated note has correct structure."""
file_path = generator.generate_note(john_doe)
content = file_path.read_text()
# Check for YAML frontmatter
assert content.startswith('---\n')
assert 'ID: I1' in content
# Check for main sections
assert '# John Doe' in content
assert '## Life Events' in content or '## Families' in content
def test_generate_all(self, generator, sample_gedcom_file):
"""Test generating notes for all individuals."""
parser = GedcomParser(sample_gedcom_file)
individual_elements = parser.get_individuals()
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
paths = generator.generate_all(individuals)
assert len(paths) == len(individuals)
assert all(p.exists() for p in paths)
assert all(p.suffix == '.md' for p in paths)
class TestMetadataFormatting:
"""Tests for Obsidian metadata formatting."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
@pytest.fixture
def john_doe(self, sample_gedcom_file):
"""Get John Doe individual for testing."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser.parser)
def test_visible_metadata(self, generator, john_doe):
"""Test YAML frontmatter format."""
file_path = generator.generate_note(john_doe)
content = file_path.read_text()
# Check for YAML frontmatter metadata
assert 'ID: I1' in content
assert 'Name: John Doe' in content
assert 'Sex: M' in content
def test_hidden_metadata_for_families(self, generator, john_doe):
"""Test hidden metadata format (key:: value) in families."""
file_path = generator.generate_note(john_doe)
content = file_path.read_text()
# Hidden metadata for partner links
if '(Partner::' in content:
assert '(Partner:: [[Jane Smith 1952]])' in content or '(Partner::' in content
def test_birth_death_metadata(self, generator, john_doe):
"""Test birth and death metadata in YAML frontmatter."""
file_path = generator.generate_note(john_doe)
content = file_path.read_text()
# Check for YAML frontmatter birth/death data
assert 'Lived: 1950-2020' in content
assert 'Born: 1 JAN 1950' in content
assert 'Passed away: 15 JUN 2020' in content
class TestWikiLinks:
"""Tests for WikiLink generation."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
def test_wiki_links_in_families(self, generator, sample_gedcom_file):
"""Test WikiLinks to partners and children."""
parser = GedcomParser(sample_gedcom_file)
individual_elements = parser.get_individuals()
# Generate all notes first
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
generator.generate_all(individuals)
# Check John's note for WikiLinks to Jane and Alice
john = [ind for ind in individuals if 'John' in ind.get_full_name()]
john_file = generator.output_dir / f"{john[0].get_file_name()}.md"
content = john_file.read_text()
# Should have WikiLinks to Jane (partner) and Alice (child)
assert '[[Jane Smith 1952]]' in content or 'Jane' in content
assert '[[Alice Doe 1980]]' in content or 'Alice' in content
def test_wiki_links_in_parents(self, generator, sample_gedcom_file):
"""Test WikiLinks to parents."""
parser = GedcomParser(sample_gedcom_file)
individual_elements = parser.get_individuals()
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
generator.generate_all(individuals)
# Check Alice's note for WikiLinks to parents
alice = [ind for ind in individuals if 'Alice' in ind.get_full_name()]
alice_file = generator.output_dir / f"{alice[0].get_file_name()}.md"
content = alice_file.read_text()
# Should have WikiLinks to both parents
assert '## Parents' in content
assert 'John' in content
assert 'Jane' in content
class TestEventFormatting:
"""Tests for event formatting in notes."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
def test_occupation_event(self, generator, sample_gedcom_file):
"""Test occupation event formatting."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
# Check for occupation in life events
assert '## Life Events' in content
assert 'Occupation' in content or 'OCCU' in content
assert 'Engineer' in content
def test_event_filtering(self, generator, sample_gedcom_file):
"""Test that BIRT and DEAT are filtered from Life Events."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
# BIRT and DEAT should be in Attributes, not Life Events
life_events_section = content.split('## Life Events')[1] if '## Life Events' in content else ''
if life_events_section:
# Birth should not appear as a separate event
assert 'Birth' not in life_events_section or 'Marriage' in content
def test_coordinates_written(self, generator, temp_dir):
"""Ensure that coordinates extracted from PLAC/MAP are written into notes."""
gedcom_content = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @I1@ INDI
1 NAME Locator /Person/
1 RESI
2 PLAC Mapville
3 MAP
4 LATI 51.5074
4 LONG -0.1278
0 TRLR
"""
temp_file = temp_dir / "coords_note.ged"
temp_file.write_text(gedcom_content, encoding='utf-8')
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
file_path = generator.generate_note(person)
content = file_path.read_text()
assert '- **Coordinates**: 51.5074, -0.1278' in content
class TestImageHandling:
"""Tests for image embedding in notes."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
def test_images_without_subdirs(self, generator, sample_gedcom_file):
"""Test image paths without media subdirectory."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
# Image should use flat path
assert '## Images' in content
assert '![Photo of John](john_photo.jpg)' in content
def test_images_with_subdirs(self, output_dir, sample_gedcom_file):
"""Test image paths with media subdirectory."""
generator = MarkdownGenerator(output_dir, media_subdir='media')
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
# Image should use media/ prefix
assert '## Images' in content
assert '![Photo of John](media/john_photo.jpg)' in content
class TestStoryGeneration:
"""Tests for story file generation."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
stories_dir = output_dir / 'stories'
stories_dir.mkdir(exist_ok=True)
return MarkdownGenerator(
output_dir,
stories_subdir='stories',
stories_dir=stories_dir
)
def test_story_file_creation(self, generator, temp_dir, sample_gedcom_with_stories):
"""Test that separate story files are created."""
temp_file = temp_dir / "stories.ged"
temp_file.write_text(sample_gedcom_with_stories, encoding='utf-8')
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
# Generate note (should also generate story files if stories are found)
generator.generate_note(person)
# Story extraction depends on exact GEDCOM format
# Just verify the method runs without error
assert True
def test_story_link_in_main_note(self, generator, temp_dir, sample_gedcom_with_stories):
"""Test that main note links to story file."""
temp_file = temp_dir / "stories.ged"
temp_file.write_text(sample_gedcom_with_stories, encoding='utf-8')
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
file_path = generator.generate_note(person)
content = file_path.read_text()
# Should have link to story
assert '## Notes' in content
assert '### Stories' in content
assert '[[stories/Life Story|Life Story]]' in content
def test_story_not_duplicated(self, generator, temp_dir, sample_gedcom_with_stories):
"""Test that story files are not regenerated if already created."""
temp_file = temp_dir / "stories.ged"
temp_file.write_text(sample_gedcom_with_stories, encoding='utf-8')
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
# Generate twice
generator.generate_note(person)
story_file = generator.stories_dir / "Life Story.md"
first_mtime = story_file.stat().st_mtime
# Small delay to ensure different mtime if file is rewritten
import time
time.sleep(0.01)
# Generate again - should use cached story
generator.generate_note(person)
second_mtime = story_file.stat().st_mtime
# File should not have been rewritten
assert first_mtime == second_mtime
class TestFamilyFormatting:
"""Tests for family section formatting."""
@pytest.fixture
def generator(self, output_dir):
"""Create a markdown generator."""
return MarkdownGenerator(output_dir)
def test_single_marriage(self, generator, sample_gedcom_file):
"""Test formatting of single marriage."""
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
assert '## Families' in content
# With single marriage, number should be omitted (but may have trailing space)
assert '### Marriage' in content
def test_multiple_marriages(self, temp_dir):
"""Test formatting of multiple marriages."""
gedcom_content = """0 HEAD
1 SOUR TestApp
1 GEDC
2 VERS 5.5.1
1 CHAR UTF-8
0 @I1@ INDI
1 NAME Person /One/
1 SEX M
1 FAMS @F1@
1 FAMS @F2@
0 @I2@ INDI
1 NAME Spouse /One/
1 SEX F
1 FAMS @F1@
0 @I3@ INDI
1 NAME Spouse /Two/
1 SEX F
1 FAMS @F2@
0 @F1@ FAM
1 HUSB @I1@
1 WIFE @I2@
0 @F2@ FAM
1 HUSB @I1@
1 WIFE @I3@
0 TRLR
"""
temp_file = temp_dir / "multiple.ged"
temp_file.write_text(gedcom_content, encoding='utf-8')
output_dir = temp_dir / "output"
output_dir.mkdir()
generator = MarkdownGenerator(output_dir)
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser.parser)
file_path = generator.generate_note(person)
content = file_path.read_text()
# With multiple marriages, should be numbered
assert '### Marriage 1' in content
assert '### Marriage 2' in content