Skip to content

Commit 61bf143

Browse files
committed
fix: create comparision function for comparing derived and expected data using tolerance
Signed-off-by: Dasun Abeykoon <Dasun20202020@gmail.com>
1 parent 9b27a64 commit 61bf143

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

tests/test_NMF_analysis_code.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import json
12
import os
23
import tempfile
34
from pathlib import Path
45
from shutil import rmtree
56

7+
import numpy as np
68
import pytest
79

810
from diffpy.nmf_mapping.main import main
@@ -64,8 +66,31 @@ def test_nmf_mapping_code(tm, temp_dir, capsys):
6466
if file in os.listdir(test_specific_dir):
6567
fn1 = os.path.join(results_dir, file)
6668
with open(fn1, "r") as f:
67-
actual = f.read()
69+
actual_json_data = json.load(f)
6870
fn2 = os.path.join(test_specific_dir, file)
6971
with open(fn2, "r") as f:
70-
expected = f.read()
71-
assert expected == actual
72+
expected_json_data = json.load(f)
73+
74+
# Comparison function for json data
75+
def compare_json_data(expected, actual, path=""):
76+
if isinstance(expected, dict) and isinstance(actual, dict):
77+
# Check if both sets have same keys
78+
assert set(expected.keys()) == set(actual.keys())
79+
for key in expected:
80+
# For each key, compare every value
81+
compare_json_data(
82+
expected[key],
83+
actual[key],
84+
path=f"{path}.{key}",
85+
)
86+
elif isinstance(expected, (int, float)) and isinstance(
87+
actual, (int, float)
88+
):
89+
# For numerical values, directly compare
90+
np.testing.assert_allclose(
91+
expected, actual, rtol=1e-05, atol=1e-8
92+
)
93+
else:
94+
assert expected == actual
95+
96+
compare_json_data(expected_json_data, actual_json_data)

0 commit comments

Comments
 (0)