Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 3 additions & 45 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,6 @@

from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject


def compare_dicts(dict1, dict2):
Copy link
Contributor Author

@bobleesj bobleesj Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are no longer used in our test code - We. may use the DeepDiff library instead to compare two dicts as implemented in our previous PR:

@pytest.mark.parametrize("inputs, expected", tc_params)
def test_constructor(inputs, expected):
    actual_do = DiffractionObject(**inputs)
    diff = DeepDiff(actual_do.__dict__, expected, ignore_order=True, significant_digits=13)
    assert diff == {}

assert dict1.keys() == dict2.keys(), "Keys mismatch"
for key in dict1:
val1, val2 = dict1[key], dict2[key]
if isinstance(val1, np.ndarray) and isinstance(val2, np.ndarray):
assert np.allclose(val1, val2), f"Arrays for key '{key}' differ"
elif isinstance(val1, np.float64) and isinstance(val2, np.float64):
assert np.isclose(val1, val2), f"Float64 values for key '{key}' differ"
else:
assert val1 == val2, f"Values for key '{key}' differ: {val1} != {val2}"


def dicts_equal(dict1, dict2):
equal = True
print("")
print(dict1)
print(dict2)
if not dict1.keys() == dict2.keys():
equal = False
for key in dict1:
val1, val2 = dict1[key], dict2[key]
if isinstance(val1, np.ndarray) and isinstance(val2, np.ndarray):
if not np.allclose(val1, val2):
equal = False
elif isinstance(val1, list) and isinstance(val2, list):
if not val1.all() == val2.all():
equal = False
elif isinstance(val1, np.float64) and isinstance(val2, np.float64):
if not np.isclose(val1, val2):
equal = False
else:
if not val1 == val2:
equal = False
return equal


params = [
( # Default
{},
Expand Down Expand Up @@ -193,14 +156,9 @@ def dicts_equal(dict1, dict2):

@pytest.mark.parametrize("inputs1, inputs2, expected", params)
def test_diffraction_objects_equality(inputs1, inputs2, expected):
diffraction_object1 = DiffractionObject(**inputs1)
diffraction_object2 = DiffractionObject(**inputs2)
# diffraction_object1_attributes = [key for key in diffraction_object1.__dict__ if not key.startswith("_")]
# for i, attribute in enumerate(diffraction_object1_attributes):
# setattr(diffraction_object1, attribute, inputs1[i])
# setattr(diffraction_object2, attribute, inputs2[i])
print(dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__), expected)
assert dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__) == expected
Copy link
Contributor Author

@bobleesj bobleesj Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we will be introducing an unique uuid via _id, this test will fail. But rather we use __eq__ method to compare two objects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobleesj yes, this is much cleaner. A sure sign that our code and API is improving, don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely

do_1 = DiffractionObject(**inputs1)
do_2 = DiffractionObject(**inputs2)
assert (do_1 == do_2) == expected


def test_on_xtype():
Expand Down
Loading