Skip to content

Commit 256f5cf

Browse files
authored
Merge pull request #178 from Sahil-u07/add-unit-tests
Add unit tests for core Python components
2 parents 1df90a7 + 738c5ca commit 256f5cf

6 files changed

Lines changed: 131 additions & 1 deletion

File tree

.gitignore

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
**/.DS_Store
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*.class
5+
*.so
6+
.Python
7+
venv/
8+
env/
9+
ENV/
10+
11+
# IDE
12+
.vscode/
13+
.idea/
14+
15+
# Testing
16+
.pytest_cache/
17+
htmlcov/
18+
.coverage
19+
20+
# Concore specific
21+
concorekill.bat

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.

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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)

tests/test_concore.py

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

0 commit comments

Comments
 (0)