Skip to content

Commit ba4ca7e

Browse files
authored
add basic tests with pytest
Add some basic tests
2 parents 5121c4c + 4576c5c commit ba4ca7e

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,19 @@ jobs:
4040
python-version: ${{ matrix.python-version }}
4141
- run: pip install build setuptools wheel
4242
- run: python -m build
43-
- run: python -m pip install .
43+
- run: python -m pip install .
44+
45+
test:
46+
runs-on: ubuntu-latest
47+
strategy:
48+
fail-fast: false
49+
matrix:
50+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
51+
steps:
52+
- uses: actions/checkout@v4
53+
- uses: actions/setup-python@v5
54+
with:
55+
python-version: ${{ matrix.python-version }}
56+
- run: pip install pytest pytest-cov
57+
- run: pip install .
58+
- run: pytest --cov=src --cov-report=xml --cov-report=term

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"tests"
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.pytestEnabled": true
7+
}

csv2cmi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def create_date(date_string: Optional[str]) -> Optional[Element]:
417417
raise ValueError(f'unable to parse "{date_string}" as TEI date')
418418

419419
@staticmethod
420-
def create_place_name(place_name_text: str, geonames_uri: str | None = None) -> Element:
420+
def create_place_name(place_name_text: str, geonames_uri: Optional[str] = None) -> Element:
421421
"""Create a placeName element."""
422422
place_name = Element("placeName")
423423
place_name_text = place_name_text.strip()
File renamed without changes.

tests/test_cmi_methods.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
3+
import csv2cmi
4+
5+
6+
@pytest.fixture
7+
def table():
8+
table
9+
10+
11+
def test_create_date_valid():
12+
assert csv2cmi.CMI.create_date("2016-04-01").attrib["when"] == "2016-04-01"
13+
assert csv2cmi.CMI.create_date("1673-05").attrib["when"] == "1673-05"
14+
assert csv2cmi.CMI.create_date("[..1760-12-03]").attrib["notAfter"] == "1760-12-03"
15+
assert csv2cmi.CMI.create_date("[1760-12..]").attrib["notBefore"] == "1760-12"
16+
assert csv2cmi.CMI.create_date("1979-10-12/").attrib["from"] == "1979-10-12"
17+
assert csv2cmi.CMI.create_date("/1985-04-12").attrib["to"] == "1985-04-12"
18+
assert csv2cmi.CMI.create_date("{-0400,-0390,-0370}") is not None
19+
20+
21+
def test_create_date_invalid():
22+
with pytest.raises(ValueError):
23+
csv2cmi.CMI.create_date("not-a-date")
24+
25+
26+
def test_create_place_name():
27+
tei_placename = csv2cmi.CMI.create_place_name("Berlin")
28+
assert tei_placename.tag == "placeName"
29+
assert tei_placename.text == "Berlin"
30+
31+
32+
def test_create_place_name_with_uri():
33+
tei_placename = csv2cmi.CMI.create_place_name("Mokhdān", "https://www.geonames.org/123456")
34+
assert tei_placename.tag == "placeName"
35+
assert tei_placename.attrib["ref"] == "https://www.geonames.org/123456"
36+
assert tei_placename.text == "Mokhdān"
37+
38+
39+
def test_generate_id_and_uuid():
40+
id1 = csv2cmi.CMI.generate_id("test")
41+
id2 = csv2cmi.CMI.generate_id("test")
42+
assert id1 != id2
43+
cmi = csv2cmi.CMI()
44+
uuid1 = cmi.generate_uuid()
45+
uuid2 = cmi.generate_uuid()
46+
assert uuid1 != uuid2

tests/test_csv2cmi.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import runpy
2+
import sys
3+
from pathlib import Path
4+
from xml.etree.ElementTree import parse
5+
6+
import pytest
7+
8+
9+
@pytest.fixture
10+
def csv_file() -> Path:
11+
csv_path = Path(__file__).parent / "data" / "test.csv"
12+
return csv_path
13+
14+
15+
@pytest.fixture
16+
def temp_output_file(tmp_path: Path) -> Path:
17+
return tmp_path / "output.xml"
18+
19+
20+
def test_cli_conversion(csv_file: Path, temp_output_file: Path, monkeypatch: pytest.MonkeyPatch):
21+
# Simulate CLI call
22+
sys_argv = ["csv2cmi.py", str(csv_file), "-o", str(temp_output_file)]
23+
monkeypatch.setattr(sys, "argv", sys_argv)
24+
runpy.run_module("csv2cmi", run_name="__main__")
25+
assert temp_output_file.exists()
26+
# Check XML root
27+
tree = parse(temp_output_file)
28+
root = tree.getroot()
29+
assert root.tag == "{http://www.tei-c.org/ns/1.0}TEI"
30+
31+
32+
def test_missing_file(monkeypatch: pytest.MonkeyPatch):
33+
sys_argv = ["csv2cmi.py", "nonexistent.csv"]
34+
monkeypatch.setattr(sys, "argv", sys_argv)
35+
with pytest.raises(SystemExit):
36+
import runpy
37+
38+
runpy.run_module("csv2cmi", run_name="__main__")

0 commit comments

Comments
 (0)