Skip to content

Commit ab8f719

Browse files
committed
move test for whether test files are readble or not to conftest
1 parent 3a71c8f commit ab8f719

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
import pytest
4+
5+
from diffpy.utils.parsers import loadData
6+
7+
8+
# Test that our readable and unreadable files are indeed readable and
9+
# unreadable by loadData (which is our definition of readable and unreadable)
10+
def test_loadData_with_input_files(user_filesystem):
11+
os.chdir(user_filesystem)
12+
xarray_chi, yarray_chi = loadData("good_data.chi", unpack=True)
13+
xarray_xy, yarray_xy = loadData("good_data.xy", unpack=True)
14+
xarray_txt, yarray_txt = loadData("good_data.txt", unpack=True)
15+
with pytest.raises(ValueError):
16+
xarray_txt, yarray_txt = loadData("unreadable_file.txt", unpack=True)
17+
with pytest.raises(ValueError):
18+
xarray_pkl, yarray_pkl = loadData("binary.pkl", unpack=True)

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,106 @@
55
import pytest
66

77
from diffpy.labpdfproc.labpdfprocapp import get_args
8-
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
8+
from diffpy.labpdfproc.tools import (
9+
known_sources,
10+
load_user_metadata,
11+
set_input_files,
12+
set_output_directory,
13+
set_wavelength,
14+
)
15+
16+
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
17+
18+
# This test covers existing single input file, directory, a file list, and multiple files
19+
# We store absolute path into input_directory and file names into input_file
20+
params_input = [
21+
(["good_data.chi"], ["good_data.chi"]), # single good file, same directory
22+
(["input_dir/good_data.chi"], ["input_dir/good_data.chi"]), # single good file, input directory
23+
( # glob current directory
24+
["."],
25+
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
26+
),
27+
( # glob input directory
28+
["./input_dir"],
29+
[
30+
"input_dir/good_data.chi",
31+
"input_dir/good_data.xy",
32+
"input_dir/good_data.txt",
33+
"input_dir/unreadable_file.txt",
34+
"input_dir/binary.pkl",
35+
],
36+
),
37+
( # list of files provided (we skip if encountering invalid files)
38+
["good_data.chi", "good_data.xy", "unreadable_file.txt", "missing_file.txt"],
39+
["good_data.chi", "good_data.xy", "unreadable_file.txt"],
40+
),
41+
( # list of files provided (with invalid files and files in different directories)
42+
["input_dir/good_data.chi", "good_data.chi", "missing_file.txt"],
43+
["input_dir/good_data.chi", "good_data.chi"],
44+
),
45+
( # file_list.txt list of files provided
46+
["file_list_dir/file_list.txt"],
47+
["good_data.chi", "good_data.xy", "good_data.txt"],
48+
),
49+
( # file_list_example2.txt list of files provided in different directories
50+
["file_list_dir/file_list_example2.txt"],
51+
["input_dir/good_data.chi", "good_data.xy", "input_dir/good_data.txt"],
52+
),
53+
]
54+
55+
56+
@pytest.mark.parametrize("inputs, expected", params_input)
57+
def test_set_input_files(inputs, expected, user_filesystem):
58+
expected_input_directory = []
59+
for expected_path in expected:
60+
expected_input_directory.append(Path(user_filesystem) / expected_path)
61+
62+
cli_inputs = ["2.5"] + inputs
63+
actual_args = get_args(cli_inputs)
64+
actual_args = set_input_files(actual_args)
65+
assert set(actual_args.input_directory) == set(expected_input_directory)
66+
67+
68+
# This test is for existing single input file or directory absolute path not in cwd
69+
# Here we are in user_filesystem/input_dir, testing for a file or directory in user_filesystem
70+
params_input_not_cwd = [
71+
(["good_data.chi"], ["good_data.chi"]),
72+
(["."], ["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"]),
73+
]
74+
75+
76+
@pytest.mark.parametrize("inputs, expected", params_input_not_cwd)
77+
def test_set_input_files_not_cwd(inputs, expected, user_filesystem):
78+
expected_input_directory = []
79+
for expected_path in expected:
80+
expected_input_directory.append(Path(user_filesystem) / expected_path)
81+
actual_input = [str(Path(user_filesystem) / inputs[0])]
82+
os.chdir("input_dir")
83+
84+
cli_inputs = ["2.5"] + actual_input
85+
actual_args = get_args(cli_inputs)
86+
actual_args = set_input_files(actual_args)
87+
assert set(actual_args.input_directory) == set(expected_input_directory)
88+
89+
90+
# This test covers non-existing single input file or directory, in this case we raise an error with message
91+
params_input_bad = [
92+
(["non_existing_file.xy"], "Please specify at least one valid input file or directory."),
93+
(["./input_dir/non_existing_file.xy"], "Please specify at least one valid input file or directory."),
94+
(["./non_existing_dir"], "Please specify at least one valid input file or directory."),
95+
]
96+
97+
98+
@pytest.mark.parametrize("inputs, msg", params_input_bad)
99+
def test_set_input_files_bad(inputs, msg, user_filesystem):
100+
cli_inputs = ["2.5"] + inputs
101+
actual_args = get_args(cli_inputs)
102+
with pytest.raises(ValueError, match=msg[0]):
103+
actual_args = set_input_files(actual_args)
104+
105+
106+
# Pass files to loadData and use it to check if file is valid or not
107+
9108

10109
params1 = [
11110
([], ["."]),

0 commit comments

Comments
 (0)