Skip to content

Commit dc7cd2d

Browse files
committed
add basic tests
1 parent 5121c4c commit dc7cd2d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
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)