-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_loaders.py
161 lines (139 loc) · 5.78 KB
/
test_loaders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Test cases for loaders used to load dataframes"""
import os
from importlib.util import find_spec
from typing import List
import dask.dataframe as ddf
import pytest
from _pytest.mark.structures import ParameterSet
from sed.core.config import parse_config
from sed.loader.base.loader import BaseLoader
from sed.loader.loader_interface import get_loader
from sed.loader.loader_interface import get_names_of_all_loaders
from sed.loader.utils import gather_files
package_dir = os.path.dirname(find_spec("sed").origin)
test_data_dir = os.path.join(package_dir, "..", "tests", "data")
read_types = ["one_file", "files", "one_folder", "folders", "one_run", "runs"]
runs = {"generic": None, "mpes": None, "flash": ["43878", "43878"]}
def get_loader_name_from_loader_object(loader: BaseLoader) -> str:
"""Helper function to find the name of a loader given the loader object.
Args:
loader (BaseLoader): Loader object from which to get the name.
Returns:
str: extracted name.
"""
for loader_name in get_names_of_all_loaders():
gotten_loader = get_loader(
loader_name,
config=parse_config(
os.path.join(
test_data_dir,
"loader",
loader_name,
"config.yaml",
),
),
)
if loader.__name__ is gotten_loader.__name__:
return loader_name
return ""
def get_all_loaders() -> List[ParameterSet]:
"""Scans through the loader list and returns them for pytest parametrization"""
loaders = []
for loader in [
get_loader(
loader_name,
config=parse_config(
os.path.join(
test_data_dir,
"loader",
loader_name,
"config.yaml",
),
),
)
for loader_name in get_names_of_all_loaders()
]:
loaders.append(pytest.param(loader))
return loaders
@pytest.mark.parametrize("loader", get_all_loaders())
def test_if_loaders_are_children_of_base_loader(loader):
"""Test to verify that all loaders are children of BaseLoader"""
if loader.__name__ != "BaseLoader":
assert isinstance(loader, BaseLoader)
@pytest.mark.parametrize("loader", get_all_loaders())
@pytest.mark.parametrize("read_type", read_types)
def test_has_correct_read_dataframe_func(loader, read_type):
"""Test if all loaders have a valid read function implemented"""
assert callable(loader.read_dataframe)
if loader.__name__ != "BaseLoader":
assert hasattr(loader, "files")
assert hasattr(loader, "supported_file_types")
loader_name = get_loader_name_from_loader_object(loader)
input_folder = os.path.join(test_data_dir, "loader", loader_name)
for supported_file_type in loader.supported_file_types:
input_files = gather_files(
folder=input_folder,
extension=supported_file_type,
)
if read_type == "one_file":
loaded_dataframe, loaded_metadata = loader.read_dataframe(
files=input_files[0],
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = 1
elif read_type == "files":
loaded_dataframe, loaded_metadata = loader.read_dataframe(
files=list(input_files),
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = len(input_files)
elif read_type == "one_folder":
loaded_dataframe, loaded_metadata = loader.read_dataframe(
folders=input_folder,
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = len(input_files)
elif read_type == "folders":
loaded_dataframe, loaded_metadata = loader.read_dataframe(
folders=[input_folder],
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = len(input_files)
elif read_type == "one_run":
if runs[get_loader_name_from_loader_object(loader)] is None:
pytest.skip("Not implemented")
loaded_dataframe, loaded_metadata = loader.read_dataframe(
runs=runs[get_loader_name_from_loader_object(loader)][0],
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = 1
elif read_type == "runs":
if runs[get_loader_name_from_loader_object(loader)] is None:
pytest.skip("Not implemented")
loaded_dataframe, loaded_metadata = loader.read_dataframe(
runs=runs[get_loader_name_from_loader_object(loader)],
ftype=supported_file_type,
collect_metadata=False,
)
expected_size = len(
runs[get_loader_name_from_loader_object(loader)],
)
assert isinstance(loaded_dataframe, ddf.DataFrame)
assert loaded_dataframe.npartitions == expected_size
assert isinstance(loaded_metadata, dict)
def test_mpes_timestamps():
"""Function to test if the timestamps are loaded correctly"""
loader_name = "mpes"
loader = get_loader(loader_name)
input_folder = os.path.join(test_data_dir, "loader", loader_name)
df, _ = loader.read_dataframe(
folders=input_folder,
collect_metadata=False,
time_stamps=True,
)
assert "timeStamps" in df.columns