Skip to content

Commit 3280325

Browse files
committed
feat: added unit tests for ReflectionProfile methods
1 parent f9b114a commit 3280325

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

tests/test_reflectionprofile.py

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pytest
1414

1515
from pyobjcryst.powderpattern import PowderPattern
16+
from pyobjcryst.refinableobj import RefinableObj
1617

1718

1819
class TestReflectionProfile(unittest.TestCase):
@@ -24,7 +25,7 @@ def prepare_fixture(self, loadcifdata):
2425

2526
def setUp(self):
2627
"""Set up a ReflectionProfile instance for testing."""
27-
x = np.linspace(0, 40, 8001)
28+
x = np.linspace(0, 40, 1000)
2829
c = self.loadcifdata("paracetamol.cif")
2930

3031
self.pp = PowderPattern()
@@ -37,19 +38,76 @@ def setUp(self):
3738
self.profile = self.ppd.GetProfile()
3839

3940
def test_get_computed_profile(self):
40-
assert True
41+
"""Sample a profile slice and verify broadening lowers the peak height."""
42+
x = self.pp.GetPowderPatternX()
43+
hkl = (1, 0, 0)
44+
window = x[100:200]
45+
xcenter = float(window[len(window) // 2])
46+
47+
prof_default = self.profile.GetProfile(window, xcenter, *hkl)
48+
self.assertEqual(len(prof_default), len(window))
49+
self.assertGreater(prof_default.max(), 0)
50+
51+
# broaden and ensure the peak height drops while shape changes
52+
self.profile.GetPar("W").SetValue(0.05)
53+
prof_broader = self.profile.GetProfile(window, xcenter, *hkl)
54+
55+
self.assertFalse(np.allclose(prof_default, prof_broader))
56+
self.assertLess(prof_broader.max(), prof_default.max())
57+
self.assertEqual(len(prof_default), len(prof_broader))
4158

4259
def test_get_profile_width(self):
43-
assert True
60+
"""Ensure full-width increases when W increases."""
61+
xcenter = float(self.pp.GetPowderPatternX()[len(self.pp.GetPowderPatternX()) // 4])
62+
width_default = self.profile.GetFullProfileWidth(0.5, xcenter, 1, 0, 0)
63+
self.assertGreater(width_default, 0)
64+
65+
self.profile.GetPar("W").SetValue(0.05)
66+
width_broader = self.profile.GetFullProfileWidth(0.5, xcenter, 1, 0, 0)
67+
self.assertGreater(width_broader, width_default)
4468

4569
def test_create_copy(self):
46-
assert True
70+
"""Ensure copy returns an independent profile with identical initial params."""
71+
copy = self.profile.CreateCopy()
72+
73+
self.assertIsNot(copy, self.profile)
74+
self.assertEqual(copy.GetClassName(), self.profile.GetClassName())
75+
76+
eta0_original = self.profile.GetPar("Eta0").GetValue()
77+
eta0_copy = copy.GetPar("Eta0").GetValue()
78+
self.assertAlmostEqual(eta0_copy, eta0_original)
79+
80+
self.profile.GetPar("Eta0").SetValue(eta0_original + 0.1)
81+
copy.GetPar("Eta0").SetValue(eta0_copy + 0.2)
82+
83+
self.assertAlmostEqual(copy.GetPar("Eta0").GetValue(), eta0_original + 0.2)
84+
self.assertAlmostEqual(self.profile.GetPar("Eta0").GetValue(), eta0_original + 0.1)
4785

4886
def test_xml_input(self):
49-
assert True
87+
"""Ensure XMLInput restores parameters previously serialized with xml()."""
88+
xml_state = self.profile.xml()
89+
eta0_original = self.profile.GetPar("Eta0").GetValue()
90+
91+
self.profile.GetPar("Eta0").SetValue(eta0_original + 0.3)
92+
self.assertNotAlmostEqual(self.profile.GetPar("Eta0").GetValue(), eta0_original)
93+
94+
RefinableObj.XMLInput(self.profile, xml_state)
95+
self.assertAlmostEqual(self.profile.GetPar("Eta0").GetValue(), eta0_original)
5096

5197
def test_xml_output(self):
52-
assert True
98+
"""Ensure XMLOutput emits parameter tags and the expected root element."""
99+
xml_state = self.profile.xml()
100+
101+
self.assertIn("<ReflectionProfile", xml_state)
102+
for par_name in ("U", "V", "W", "Eta0"):
103+
self.assertIn(f'Name="{par_name}"', xml_state)
104+
105+
import io
106+
107+
buf = io.StringIO()
108+
RefinableObj.XMLOutput(self.profile, buf, 0)
109+
xml_from_stream = buf.getvalue()
110+
self.assertTrue(xml_from_stream.startswith("<ReflectionProfile"))
53111

54112

55113
if __name__ == "__main__":

0 commit comments

Comments
 (0)