Skip to content

Commit c5561ea

Browse files
committed
Add initial unit tests for core Python utilities
- Set up a tests/ directory with a basic pytest structure - Added unit tests for key concore.py helpers (safe_literal_eval, tryparam, and core API functions) - Included pytest configuration and dev dependencies - All tests passing locally This adds a starting test foundation and helps improve confidence as the codebase evolves.
1 parent 3b6756b commit c5561ea

8 files changed

Lines changed: 128 additions & 0 deletions

pytest.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
testpaths = tests
3+
python_files = test_*.py
4+
python_classes = Test*
5+
python_functions = test_*
6+
addopts = -v --tb=short

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest>=7.0.0
2+
pytest-cov>=4.0.0

tests/__init__.py

Whitespace-only changes.
137 Bytes
Binary file not shown.
1.67 KB
Binary file not shown.
11.2 KB
Binary file not shown.

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
import os
3+
import sys
4+
import tempfile
5+
import shutil
6+
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8+
9+
10+
@pytest.fixture
11+
def temp_dir():
12+
dirpath = tempfile.mkdtemp()
13+
yield dirpath
14+
if os.path.exists(dirpath):
15+
shutil.rmtree(dirpath)
16+
17+
18+
@pytest.fixture
19+
def create_test_file(temp_dir):
20+
def _create_file(filename, content):
21+
filepath = os.path.join(temp_dir, filename)
22+
with open(filepath, "w") as f:
23+
f.write(content)
24+
return filepath
25+
return _create_file

tests/test_concore.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pytest
2+
import os
3+
import sys
4+
import tempfile
5+
import shutil
6+
7+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8+
9+
10+
@pytest.fixture
11+
def temp_dir():
12+
dirpath = tempfile.mkdtemp()
13+
yield dirpath
14+
if os.path.exists(dirpath):
15+
shutil.rmtree(dirpath)
16+
17+
18+
class TestSafeLiteralEval:
19+
20+
def test_reads_dictionary_from_file(self, temp_dir):
21+
test_file = os.path.join(temp_dir, "config.txt")
22+
with open(test_file, "w") as f:
23+
f.write("{'name': 'test', 'value': 123}")
24+
25+
from concore import safe_literal_eval
26+
result = safe_literal_eval(test_file, {})
27+
28+
assert result == {'name': 'test', 'value': 123}
29+
30+
def test_returns_default_when_file_missing(self):
31+
from concore import safe_literal_eval
32+
result = safe_literal_eval("nonexistent_file.txt", "fallback")
33+
34+
assert result == "fallback"
35+
36+
def test_returns_default_for_empty_file(self, temp_dir):
37+
test_file = os.path.join(temp_dir, "empty.txt")
38+
with open(test_file, "w") as f:
39+
pass
40+
41+
from concore import safe_literal_eval
42+
result = safe_literal_eval(test_file, "default")
43+
44+
assert result == "default"
45+
46+
47+
class TestTryparam:
48+
49+
def test_returns_existing_parameter(self):
50+
from concore import tryparam, params
51+
params['my_setting'] = 'custom_value'
52+
53+
result = tryparam('my_setting', 'default_value')
54+
55+
assert result == 'custom_value'
56+
57+
def test_returns_default_for_missing_parameter(self):
58+
from concore import tryparam
59+
result = tryparam('missing_param', 'fallback')
60+
61+
assert result == 'fallback'
62+
63+
64+
class TestZeroMQPort:
65+
66+
def test_class_is_defined(self):
67+
from concore import ZeroMQPort
68+
assert ZeroMQPort is not None
69+
70+
71+
class TestDefaultConfiguration:
72+
73+
def test_default_input_path(self):
74+
from concore import inpath
75+
assert inpath == "./in"
76+
77+
def test_default_output_path(self):
78+
from concore import outpath
79+
assert outpath == "./out"
80+
81+
82+
class TestPublicAPI:
83+
84+
def test_module_imports_successfully(self):
85+
import concore
86+
assert concore is not None
87+
88+
def test_core_functions_exist(self):
89+
from concore import safe_literal_eval
90+
from concore import tryparam
91+
from concore import default_maxtime
92+
93+
assert callable(safe_literal_eval)
94+
assert callable(tryparam)
95+
assert callable(default_maxtime)

0 commit comments

Comments
 (0)