Skip to content

Commit fe75156

Browse files
committed
Refactors test environment detection logic
1 parent 4350c65 commit fe75156

File tree

6 files changed

+66
-40
lines changed

6 files changed

+66
-40
lines changed

tests/unit/easydiffraction/analysis/fit_helpers/test_tracking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_tracker_terminal_flow_prints_and_updates_best(monkeypatch, capsys):
1616
import easydiffraction.analysis.fit_helpers.tracking as tracking_mod
1717
from easydiffraction.analysis.fit_helpers.tracking import FitProgressTracker
1818

19-
# Force terminal branch (not notebook)
20-
monkeypatch.setattr(tracking_mod, 'is_notebook', lambda: False)
19+
# Force terminal branch (not notebook): tracking imports in_jupyter directly
20+
monkeypatch.setattr(tracking_mod, 'in_jupyter', lambda: False)
2121

2222
tracker = FitProgressTracker()
2323
tracker.start_tracking('dummy')

tests/unit/easydiffraction/analysis/test_analysis_access_params.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# SPDX-FileCopyrightText: 2021-2025 EasyDiffraction contributors <https://github.com/easyscience/diffraction>
22
# SPDX-License-Identifier: BSD-3-Clause
33

4-
def test_how_to_access_parameters_prints_paths_and_uids(capsys):
4+
def test_how_to_access_parameters_prints_paths_and_uids(capsys, monkeypatch):
55
from easydiffraction.analysis.analysis import Analysis
66
from easydiffraction.core.parameters import Parameter
77
from easydiffraction.core.validation import AttributeSpec
88
from easydiffraction.core.validation import DataTypes
99
from easydiffraction.io.cif.handler import CifHandler
10+
import easydiffraction.analysis.analysis as analysis_mod
1011

1112
# Build two parameters with identity metadata set directly
1213
def make_param(db, cat, entry, name, val):
@@ -38,13 +39,45 @@ def __init__(self):
3839
self.sample_models = Coll([p1])
3940
self.experiments = Coll([p2])
4041

42+
# Capture the table payload by monkeypatching render_table to avoid
43+
# terminal wrapping/ellipsis affecting string matching.
44+
captured = {}
45+
46+
def fake_render_table(**kwargs):
47+
captured.update(kwargs)
48+
49+
monkeypatch.setattr(analysis_mod, 'render_table', fake_render_table)
4150
a = Analysis(Project())
4251
a.how_to_access_parameters()
52+
4353
out = capsys.readouterr().out
4454
assert 'How to access parameters' in out
45-
# Expect code path strings
46-
assert "proj.sample_models['db1'].catA.alpha" in out
47-
assert "proj.experiments['db2'].catB['row1'].beta" in out
48-
# Expect CIF uid (owner.unique_name) present for both
49-
assert 'db1.catA.alpha' in out
50-
assert 'db2.catB.row1.beta' in out
55+
56+
# Validate headers and row contents independent of terminal renderer
57+
headers = captured.get('columns_headers') or []
58+
data = captured.get('columns_data') or []
59+
60+
assert 'How to Access in Python Code' in headers
61+
62+
# Flatten rows to strings for simple membership checks
63+
flat_rows = [' '.join(map(str, row)) for row in data]
64+
65+
# Python access paths
66+
assert any("proj.sample_models['db1'].catA.alpha" in r for r in flat_rows)
67+
assert any("proj.experiments['db2'].catB['row1'].beta" in r for r in flat_rows)
68+
69+
# Now check CIF unique identifiers via the new API
70+
captured2 = {}
71+
72+
def fake_render_table2(**kwargs):
73+
captured2.update(kwargs)
74+
75+
monkeypatch.setattr(analysis_mod, 'render_table', fake_render_table2)
76+
a.show_parameter_cif_uids()
77+
headers2 = captured2.get('columns_headers') or []
78+
data2 = captured2.get('columns_data') or []
79+
assert 'Unique Identifier for CIF Constraints' in headers2
80+
flat_rows2 = [' '.join(map(str, row)) for row in data2]
81+
# Unique names are datablock.category[.entry].parameter
82+
assert any('db1 catA alpha' in r.replace('.', ' ') for r in flat_rows2)
83+
assert any('db2 catB row1 beta' in r.replace('.', ' ') for r in flat_rows2)

tests/unit/easydiffraction/display/plotters/test_plotly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_get_trace_and_plot(monkeypatch):
1313
import easydiffraction.display.plotters.plotly as pp
1414

1515
# Arrange: force non-PyCharm branch and stub fig.show/HTML/display so nothing opens
16-
monkeypatch.setattr(pp, 'is_pycharm', lambda: False)
16+
monkeypatch.setattr(pp, 'in_pycharm', lambda: False)
1717

1818
shown = {'count': 0}
1919

tests/unit/easydiffraction/display/test_plotting.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,32 @@ def __init__(self):
7171

7272
p = Plotter()
7373

74-
# Error paths
74+
# Error paths (now log errors via console; messages are printed)
7575
p.plot_meas(Ptn(x=None, meas=None), 'E', ExptType())
7676
out = capsys.readouterr().out
77-
# plot_meas uses formatting.error(...) without printing -> no stdout
78-
assert out == ''
77+
assert 'No data available for experiment E' in out
7978

8079
p.plot_meas(Ptn(x=[1], meas=None), 'E', ExptType())
8180
out = capsys.readouterr().out
82-
# Same here: no print, so no stdout
83-
assert out == ''
81+
assert 'No measured data available for experiment E' in out
8482

8583
p.plot_calc(Ptn(x=None, calc=None), 'E', ExptType())
8684
out = capsys.readouterr().out
87-
# error path should not print to stdout
88-
assert out == ''
85+
assert 'No data available for experiment E' in out
8986

9087
p.plot_calc(Ptn(x=[1], calc=None), 'E', ExptType())
9188
out = capsys.readouterr().out
92-
# assert 'No calculated data available' in out or 'No calculated data' in out
93-
# error path should not print to stdout in new API
94-
assert out == ''
89+
assert 'No calculated data available for experiment E' in out
9590

9691
p.plot_meas_vs_calc(Ptn(x=None), 'E', ExptType())
9792
out = capsys.readouterr().out
98-
# assert 'No data available' in out
99-
assert out == ''
93+
assert 'No data available for experiment E' in out
10094
p.plot_meas_vs_calc(Ptn(x=[1], meas=None, calc=[1]), 'E', ExptType())
10195
out = capsys.readouterr().out
102-
# assert 'No measured data available' in out
103-
assert out == ''
96+
assert 'No measured data available for experiment E' in out
10497
p.plot_meas_vs_calc(Ptn(x=[1], meas=[1], calc=None), 'E', ExptType())
10598
out = capsys.readouterr().out
106-
# assert 'No calculated data available' in out
107-
assert out == ''
99+
assert 'No calculated data available for experiment E' in out
108100
# TODO: Update assertions with new logging-based error handling
109101
# in the above line and elsewhere as needed.
110102

tests/unit/easydiffraction/project/test_project_d_spacing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def _load_ascii_data_to_experiment(self, data_path: str) -> None:
9797
# Act
9898
p.update_pattern_d_spacing('e1')
9999

100-
# Assert error was logged via logger (no stdout expected in new API)
100+
# Assert error is reported via console/logging in the new API
101101
out = capsys.readouterr().out
102-
# assert 'Unsupported beam mode' in out
103-
assert out == ''
102+
assert 'Unsupported beam mode' in out

tests/unit/easydiffraction/utils/test_utils.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def test_validate_url_rejects_non_http_https():
9393

9494

9595
def test_is_github_ci_env_true(monkeypatch):
96-
import easydiffraction.utils.env as env
96+
import easydiffraction.utils.environment as env
9797

9898
monkeypatch.setenv('GITHUB_ACTIONS', 'true')
9999
expected = True
100-
actual = env.is_github_ci()
100+
actual = env.in_github_ci()
101101
assert expected == actual
102102

103103

@@ -110,29 +110,31 @@ def test_package_version_missing_package_returns_none():
110110

111111

112112
def test_is_notebook_false_in_plain_env(monkeypatch):
113-
import easydiffraction.utils.env as env
113+
import easydiffraction.utils.environment as env
114114

115115
# Ensure no IPython and not PyCharm
116116
monkeypatch.setenv('PYCHARM_HOSTED', '', prepend=False)
117-
assert env.is_notebook() is False
117+
assert env.in_jupyter() is False
118118

119119

120120
def test_is_pycharm_and_is_colab(monkeypatch):
121-
import easydiffraction.utils.env as env
121+
import easydiffraction.utils.environment as env
122122

123123
# PyCharm
124124
monkeypatch.setenv('PYCHARM_HOSTED', '1')
125-
assert env.is_pycharm() is True
125+
assert env.in_pycharm() is True
126126
# Colab detection when module is absent -> False
127-
assert env.is_colab() is False
127+
assert env.in_colab() is False
128128

129129

130130
def test_render_table_terminal_branch(capsys, monkeypatch):
131131
import easydiffraction.utils.utils as MUT
132-
133-
import easydiffraction.utils.env as env
134-
monkeypatch.setattr(env, 'is_notebook', lambda: False)
135-
MUT.render_table(columns_data=[[1, 2], [3, 4]], columns_alignment=['left', 'left'])
132+
# Ensure non-notebook rendering; on CI/default env it's terminal anyway.
133+
MUT.render_table(
134+
columns_data=[[1, 2], [3, 4]],
135+
columns_alignment=['left', 'left'],
136+
columns_headers=['A', 'B'],
137+
)
136138
out = capsys.readouterr().out
137139
# fancy_outline uses box-drawing characters; accept a couple of expected ones
138140
assert ('╒' in out and '╕' in out) or ('┌' in out and '┐' in out)

0 commit comments

Comments
 (0)