|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | import tempfile |
3 | 4 | from pathlib import Path |
4 | 5 | from shutil import rmtree |
5 | 6 |
|
| 7 | +import numpy as np |
6 | 8 | import pytest |
7 | 9 |
|
8 | 10 | from diffpy.nmf_mapping.main import main |
@@ -64,8 +66,31 @@ def test_nmf_mapping_code(tm, temp_dir, capsys): |
64 | 66 | if file in os.listdir(test_specific_dir): |
65 | 67 | fn1 = os.path.join(results_dir, file) |
66 | 68 | with open(fn1, "r") as f: |
67 | | - actual = f.read() |
| 69 | + actual_json_data = json.load(f) |
68 | 70 | fn2 = os.path.join(test_specific_dir, file) |
69 | 71 | 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