Skip to content

Commit 3ba3038

Browse files
committed
Use deepdiff to compare do.__dict__
1 parent 9772e1c commit 3ba3038

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

news/scattering_obt.rst

-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@
1919
* <news item>
2020

2121
**Security:**
22-
23-
* <news item>

requirements/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pytest-env
66
pytest-mock
77
pytest-cov
88
freezegun
9+
DeepDiff

tests/test_diffraction_objects.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
from deepdiff import DeepDiff
56
from freezegun import freeze_time
67

78
from diffpy.utils.diffraction_objects import DiffractionObject
@@ -248,7 +249,7 @@ def test_dump(tmp_path, mocker):
248249
(
249250
{},
250251
{
251-
"all_arrays": np.empty(shape=(0, 4)), # instantiate empty
252+
"_all_arrays": np.empty(shape=(0, 4)), # instantiate empty
252253
"metadata": {},
253254
"input_xtype": "",
254255
"name": "",
@@ -265,7 +266,7 @@ def test_dump(tmp_path, mocker):
265266
( # instantiate just non-array attributes
266267
{"name": "test", "scat_quantity": "x-ray", "metadata": {"thing": "1", "another": "2"}},
267268
{
268-
"all_arrays": np.empty(shape=(0, 4)),
269+
"_all_arrays": np.empty(shape=(0, 4)),
269270
"metadata": {"thing": "1", "another": "2"},
270271
"input_xtype": "",
271272
"name": "test",
@@ -287,7 +288,7 @@ def test_dump(tmp_path, mocker):
287288
"wavelength": 4.0 * np.pi,
288289
},
289290
{
290-
"all_arrays": np.array(
291+
"_all_arrays": np.array(
291292
[
292293
[1.0, 0.0, 0.0, np.float64(np.inf)],
293294
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
@@ -316,7 +317,7 @@ def test_dump(tmp_path, mocker):
316317
"scat_quantity": "x-ray",
317318
},
318319
{
319-
"all_arrays": np.array(
320+
"_all_arrays": np.array(
320321
[
321322
[1.0, 0.0, 0.0, np.float64(np.inf)],
322323
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
@@ -342,21 +343,26 @@ def test_dump(tmp_path, mocker):
342343
@pytest.mark.parametrize("inputs, expected", tc_params)
343344
def test_constructor(inputs, expected):
344345
actual_do = DiffractionObject(**inputs)
345-
actual_dict = {
346-
"all_arrays": actual_do.all_arrays,
347-
"metadata": actual_do.metadata,
348-
"input_xtype": actual_do.input_xtype,
349-
"name": actual_do.name,
350-
"scat_quantity": actual_do.scat_quantity,
351-
"qmin": actual_do.qmin,
352-
"qmax": actual_do.qmax,
353-
"tthmin": actual_do.tthmin,
354-
"tthmax": actual_do.tthmax,
355-
"dmin": actual_do.dmin,
356-
"dmax": actual_do.dmax,
357-
"wavelength": actual_do.wavelength,
358-
}
359-
compare_dicts(actual_dict, expected)
346+
diff = DeepDiff(actual_do.__dict__, expected, ignore_order=True, significant_digits=4)
347+
# Ensure there is no difference
348+
assert diff == {}
349+
350+
351+
def test_all_array_getter():
352+
actual_do = DiffractionObject(
353+
xarray=np.array([0.0, 90.0, 180.0]),
354+
yarray=np.array([1.0, 2.0, 3.0]),
355+
xtype="tth",
356+
wavelength=4.0 * np.pi,
357+
)
358+
expected_all_arrays = np.array(
359+
[
360+
[1.0, 0.0, 0.0, np.float64(np.inf)],
361+
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
362+
[3.0, 1.0, 180.0, 1.0 * 2 * np.pi],
363+
]
364+
)
365+
assert np.array_equal(actual_do.all_arrays, expected_all_arrays)
360366

361367

362368
def test_all_array_setter():

0 commit comments

Comments
 (0)