Skip to content

Commit 1f08691

Browse files
simplify tests
1 parent da98d88 commit 1f08691

File tree

2 files changed

+10
-106
lines changed

2 files changed

+10
-106
lines changed

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ def user_filesystem(tmp_path):
1111
input_dir.mkdir(parents=True, exist_ok=True)
1212
home_dir = base_dir / "home_dir"
1313
home_dir.mkdir(parents=True, exist_ok=True)
14-
cwd_dir = base_dir / "cwd_dir"
15-
cwd_dir.mkdir(parents=True, exist_ok=True)
1614

1715
chi_data = "dataformat = twotheta\n mode = xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
1816
xy_data = "1 2\n 3 4\n 5 6\n 7 8"

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 10 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os
32
import re
43
from pathlib import Path
@@ -245,117 +244,24 @@ def test_load_user_metadata_bad(inputs, msg):
245244
actual_args = load_user_metadata(actual_args)
246245

247246

248-
def _setup_dirs(monkeypatch, user_filesystem):
247+
params_user_info = [
248+
([None, None], ["home_username", "[email protected]"]),
249+
(["cli_username", None], ["cli_username", "[email protected]"]),
250+
([None, "[email protected]"], ["home_username", "[email protected]"]),
251+
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
252+
]
253+
254+
255+
@pytest.mark.parametrize("inputs, expected", params_user_info)
256+
def test_load_user_info(monkeypatch, inputs, expected, user_filesystem):
249257
cwd = Path(user_filesystem)
250258
home_dir = cwd / "home_dir"
251259
monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
252260
os.chdir(cwd)
253-
return home_dir
254261

255-
256-
def _run_tests(inputs, expected):
257262
expected_username, expected_email = expected
258263
cli_inputs = ["2.5", "data.xy", "--username", inputs[0], "--email", inputs[1]]
259264
actual_args = get_args(cli_inputs)
260265
actual_args = load_user_info(actual_args)
261266
assert actual_args.username == expected_username
262267
assert actual_args.email == expected_email
263-
264-
265-
params_user_info_with_home_conf_file = [
266-
(["", ""], ["home_username", "[email protected]"]),
267-
(["cli_username", ""], ["cli_username", "[email protected]"]),
268-
(["", "[email protected]"], ["home_username", "[email protected]"]),
269-
([None, None], ["home_username", "[email protected]"]),
270-
(["cli_username", None], ["cli_username", "[email protected]"]),
271-
([None, "[email protected]"], ["home_username", "[email protected]"]),
272-
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
273-
]
274-
params_user_info_with_local_conf_file = [
275-
(["", ""], ["cwd_username", "[email protected]"]),
276-
(["cli_username", ""], ["cli_username", "[email protected]"]),
277-
(["", "[email protected]"], ["cwd_username", "[email protected]"]),
278-
([None, None], ["cwd_username", "[email protected]"]),
279-
(["cli_username", None], ["cli_username", "[email protected]"]),
280-
([None, "[email protected]"], ["cwd_username", "[email protected]"]),
281-
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
282-
]
283-
params_user_info_with_no_home_conf_file = [
284-
(
285-
[None, None],
286-
["input_username", "[email protected]"],
287-
["input_username", "[email protected]"],
288-
),
289-
(
290-
["cli_username", None],
291-
292-
["cli_username", "[email protected]"],
293-
),
294-
(
295-
[None, "[email protected]"],
296-
["input_username", ""],
297-
["input_username", "[email protected]"],
298-
),
299-
(
300-
["", ""],
301-
["input_username", "[email protected]"],
302-
["input_username", "[email protected]"],
303-
),
304-
(
305-
["cli_username", ""],
306-
307-
["cli_username", "[email protected]"],
308-
),
309-
(
310-
311-
["input_username", ""],
312-
["input_username", "[email protected]"],
313-
),
314-
(
315-
["cli_username", "[email protected]"],
316-
["input_username", "[email protected]"],
317-
["cli_username", "[email protected]"],
318-
),
319-
]
320-
params_user_info_no_conf_file_no_inputs = [
321-
([None, None], ["", ""], ["", ""]),
322-
]
323-
324-
325-
@pytest.mark.parametrize("inputs, expected", params_user_info_with_home_conf_file)
326-
def test_load_user_info_with_home_conf_file(monkeypatch, inputs, expected, user_filesystem):
327-
_setup_dirs(monkeypatch, user_filesystem)
328-
_run_tests(inputs, expected)
329-
330-
331-
@pytest.mark.parametrize("inputs, expected", params_user_info_with_local_conf_file)
332-
def test_load_user_info_with_local_conf_file(monkeypatch, inputs, expected, user_filesystem):
333-
_setup_dirs(monkeypatch, user_filesystem)
334-
local_config_data = {"username": "cwd_username", "email": "[email protected]"}
335-
with open(Path(user_filesystem) / "diffpyconfig.json", "w") as f:
336-
json.dump(local_config_data, f)
337-
_run_tests(inputs, expected)
338-
os.remove(Path().home() / "diffpyconfig.json")
339-
_run_tests(inputs, expected)
340-
341-
342-
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
343-
def test_load_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
344-
_setup_dirs(monkeypatch, user_filesystem)
345-
os.remove(Path().home() / "diffpyconfig.json")
346-
inp_iter = iter(inputsb)
347-
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
348-
_run_tests(inputsa, expected)
349-
confile = Path().home() / "diffpyconfig.json"
350-
assert confile.is_file()
351-
352-
353-
@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
354-
def test_load_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
355-
_setup_dirs(monkeypatch, user_filesystem)
356-
os.remove(Path().home() / "diffpyconfig.json")
357-
inp_iter = iter(inputsb)
358-
monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
359-
_run_tests(inputsa, expected)
360-
confile = Path().home() / "diffpyconfig.json"
361-
assert confile.exists() is False

0 commit comments

Comments
 (0)