Skip to content

Commit f27ab7a

Browse files
committed
tests: fitrecipe
1 parent be7eeb6 commit f27ab7a

File tree

2 files changed

+63
-31
lines changed

2 files changed

+63
-31
lines changed

tests/conftest.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import importlib.resources
22
import json
33
import logging
4+
import sys
45
from functools import lru_cache
56
from pathlib import Path
67

78
import pytest
9+
import six
810

911
import diffpy.srfit.equation.literals as literals
1012
from diffpy.srfit.sas.sasimport import sasimport
@@ -15,8 +17,8 @@
1517
@lru_cache()
1618
def has_sas():
1719
try:
18-
__import__("sas.pr.invertor")
19-
__import__("sas.models")
20+
sasimport("sas.pr.invertor")
21+
sasimport("sas.models")
2022
return True
2123
except ImportError:
2224
return False
@@ -25,7 +27,6 @@ def has_sas():
2527
# diffpy.structure
2628
@lru_cache()
2729
def has_diffpy_structure():
28-
_msg_nostructure = "No module named 'diffpy.structure'"
2930
try:
3031
import diffpy.structure as m
3132

@@ -40,7 +41,6 @@ def has_diffpy_structure():
4041

4142
@lru_cache()
4243
def has_pyobjcryst():
43-
_msg_nopyobjcryst = "No module named 'pyobjcryst'"
4444
try:
4545
import pyobjcryst as m
4646

@@ -56,7 +56,6 @@ def has_pyobjcryst():
5656

5757
@lru_cache()
5858
def has_diffpy_srreal():
59-
_msg_nosrreal = "No module named 'diffpy.srreal'"
6059
try:
6160
import diffpy.srreal.pdfcalculator as m
6261

@@ -87,7 +86,7 @@ def pyobjcryst_available():
8786
return has_pyobjcryst()
8887

8988

90-
@pytest.fixture
89+
@pytest.fixture(scope="session")
9190
def user_filesystem(tmp_path):
9291
base_dir = Path(tmp_path)
9392
home_dir = base_dir / "home_dir"
@@ -102,7 +101,7 @@ def user_filesystem(tmp_path):
102101
yield tmp_path
103102

104103

105-
@pytest.fixture
104+
@pytest.fixture(scope="session")
106105
def datafile():
107106
"""Fixture to load a test data file from the testdata package directory."""
108107

@@ -144,3 +143,19 @@ def _noObserversInGlobalBuilders():
144143
return rv
145144

146145
return _noObserversInGlobalBuilders()
146+
147+
148+
@pytest.fixture(scope="session")
149+
def capturestdout():
150+
def _capturestdout(f, *args, **kwargs):
151+
"""Capture the standard output from a call of function f."""
152+
savestdout = sys.stdout
153+
fp = six.StringIO()
154+
try:
155+
sys.stdout = fp
156+
f(*args, **kwargs)
157+
finally:
158+
sys.stdout = savestdout
159+
return fp.getvalue()
160+
161+
return _capturestdout

tests/test_fitrecipe.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
from diffpy.srfit.fitbase.parameter import Parameter
2424
from diffpy.srfit.fitbase.profile import Profile
2525

26-
from .utils import capturestdout
27-
2826

2927
class TestFitRecipe(unittest.TestCase):
3028

@@ -239,32 +237,51 @@ def testResidual(self):
239237

240238
return
241239

242-
def testPrintFitHook(self):
243-
"check output from default PrintFitHook."
244-
self.recipe.addVar(self.fitcontribution.c)
245-
self.recipe.restrain("c", lb=5)
246-
(pfh,) = self.recipe.getFitHooks()
247-
out = capturestdout(self.recipe.scalarResidual)
248-
self.assertEqual("", out)
249-
pfh.verbose = 1
250-
out = capturestdout(self.recipe.scalarResidual)
251-
self.assertTrue(out.strip().isdigit())
252-
self.assertFalse("\nRestraints:" in out)
253-
pfh.verbose = 2
254-
out = capturestdout(self.recipe.scalarResidual)
255-
self.assertTrue("\nResidual:" in out)
256-
self.assertTrue("\nRestraints:" in out)
257-
self.assertFalse("\nVariables" in out)
258-
pfh.verbose = 3
259-
out = capturestdout(self.recipe.scalarResidual)
260-
self.assertTrue("\nVariables" in out)
261-
self.assertTrue("c = " in out)
262-
return
263-
264240

265241
# End of class TestFitRecipe
266242

243+
267244
# ----------------------------------------------------------------------------
245+
def testPrintFitHook(capturestdout):
246+
"check output from default PrintFitHook."
247+
recipe = FitRecipe("recipe")
248+
recipe.fithooks[0].verbose = 0
249+
250+
# Set up the Profile
251+
profile = Profile()
252+
x = linspace(0, pi, 10)
253+
y = sin(x)
254+
profile.setObservedProfile(x, y)
255+
256+
# Set up the FitContribution
257+
fitcontribution = FitContribution("cont")
258+
fitcontribution.setProfile(profile)
259+
fitcontribution.setEquation("A*sin(k*x + c)")
260+
fitcontribution.A.setValue(1)
261+
fitcontribution.k.setValue(1)
262+
fitcontribution.c.setValue(0)
263+
264+
recipe.addContribution(fitcontribution)
265+
266+
recipe.addVar(fitcontribution.c)
267+
recipe.restrain("c", lb=5)
268+
(pfh,) = recipe.getFitHooks()
269+
out = capturestdout(recipe.scalarResidual)
270+
assert "" == out
271+
pfh.verbose = 1
272+
out = capturestdout(recipe.scalarResidual)
273+
assert out.strip().isdigit()
274+
assert "\nRestraints:" not in out
275+
pfh.verbose = 2
276+
out = capturestdout(recipe.scalarResidual)
277+
assert "\nResidual:" in out
278+
assert "\nRestraints:" in out
279+
assert "\nVariables" not in out
280+
pfh.verbose = 3
281+
out = capturestdout(recipe.scalarResidual)
282+
assert "\nVariables" in out
283+
assert "c = " in out
284+
return
268285

269286

270287
if __name__ == "__main__":

0 commit comments

Comments
 (0)