Skip to content

Commit b4fecff

Browse files
authored
Merge pull request #207 from GREENRAT-K405/fix/simulation-numpy-tests
Enhance simulation data integrity and add NumPy type safety tests
2 parents 80a9987 + ed330fb commit b4fecff

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

tests/test_concore.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import os
3+
import numpy as np
34

45
class TestSafeLiteralEval:
56

@@ -87,6 +88,95 @@ def test_core_functions_exist(self):
8788
assert callable(default_maxtime)
8889

8990

91+
class TestNumpyConversion:
92+
def test_convert_scalar(self):
93+
from concore import convert_numpy_to_python
94+
val = np.float64(3.14)
95+
res = convert_numpy_to_python(val)
96+
assert type(res) == float
97+
assert res == 3.14
98+
99+
def test_convert_list_and_dict(self):
100+
from concore import convert_numpy_to_python
101+
data = {
102+
'a': np.int32(10),
103+
'b': [np.float64(1.1), np.float64(2.2)]
104+
}
105+
res = convert_numpy_to_python(data)
106+
assert type(res['a']) == int
107+
assert type(res['b'][0]) == float
108+
assert res['b'][1] == 2.2
109+
110+
class TestInitVal:
111+
@pytest.fixture(autouse=True)
112+
def reset_simtime(self):
113+
import concore
114+
old_simtime = concore.simtime
115+
yield
116+
concore.simtime = old_simtime
117+
118+
def test_initval_updates_simtime(self):
119+
import concore
120+
concore.simtime = 0
121+
# initval takes string repr of a list [time, val1, val2...]
122+
result = concore.initval("[100, 'data']")
123+
124+
assert concore.simtime == 100
125+
assert result == ['data']
126+
127+
def test_initval_handles_bad_input(self):
128+
import concore
129+
concore.simtime = 0
130+
# Input that isn't a list
131+
result = concore.initval("not_a_list")
132+
assert concore.simtime == 0
133+
assert result == []
134+
135+
class TestDefaultMaxTime:
136+
def test_uses_file_value(self, temp_dir, monkeypatch):
137+
import concore
138+
# Mock the path to maxtime file
139+
maxtime_file = os.path.join(temp_dir, "concore.maxtime")
140+
with open(maxtime_file, "w") as f:
141+
f.write("500")
142+
143+
monkeypatch.setattr(concore, 'concore_maxtime_file', maxtime_file)
144+
concore.default_maxtime(100)
145+
146+
assert concore.maxtime == 500
147+
148+
def test_uses_default_when_missing(self, monkeypatch):
149+
import concore
150+
monkeypatch.setattr(concore, 'concore_maxtime_file', "missing_file")
151+
concore.default_maxtime(999)
152+
assert concore.maxtime == 999
153+
154+
class TestUnchanged:
155+
@pytest.fixture(autouse=True)
156+
def reset_globals(self):
157+
import concore
158+
old_s = concore.s
159+
old_olds = concore.olds
160+
yield
161+
concore.s = old_s
162+
concore.olds = old_olds
163+
164+
def test_unchanged_returns_true_if_same(self):
165+
import concore
166+
concore.s = "same"
167+
concore.olds = "same"
168+
169+
# Should return True and reset s to empty
170+
assert concore.unchanged() is True
171+
assert concore.s == ''
172+
173+
def test_unchanged_returns_false_if_diff(self):
174+
import concore
175+
concore.s = "new"
176+
concore.olds = "old"
177+
178+
assert concore.unchanged() is False
179+
assert concore.olds == "new"
90180
class TestParseParams:
91181

92182
def test_simple_key_value_pairs(self):
@@ -121,4 +211,4 @@ def test_windows_quoted_input(self):
121211
s = "\"a=1;b=2\""
122212
s = s[1:-1] # simulate quote stripping before parse_params
123213
params = parse_params(s)
124-
assert params == {"a": 1, "b": 2}
214+
assert params == {"a": 1, "b": 2}

0 commit comments

Comments
 (0)