-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathtest_conftest.py
274 lines (185 loc) · 7.77 KB
/
test_conftest.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# SPDX-FileCopyrightText: Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing
import pandas as pd
import pytest
import cudf
from _utils.dataset_manager import DatasetManager
from morpheus.config import CppConfig
@pytest.fixture(name="cpp_from_marker", scope="function")
def cpp_from_marker_fixture(request: pytest.FixtureRequest) -> bool:
use_cpp = len([x for x in request.node.iter_markers("use_cpp") if "added_by" in x.kwargs]) > 0
use_python = len([x for x in request.node.iter_markers("use_python") if "added_by" in x.kwargs]) > 0
assert use_cpp != use_python
return use_cpp
@pytest.fixture(name="df_type_from_marker", scope="function")
def df_type_from_marker_fixture(request: pytest.FixtureRequest) -> bool:
use_cudf = len([x for x in request.node.iter_markers("use_cudf") if "added_by" in x.kwargs]) > 0
use_pandas = len([x for x in request.node.iter_markers("use_pandas") if "added_by" in x.kwargs]) > 0
assert use_cudf != use_pandas
return "cudf" if use_cudf else "pandas"
@pytest.mark.use_cudf
@pytest.mark.use_pandas
def test_dataset_works_with_marks(dataset: DatasetManager):
# Test is parameterized so df runs twice, once as pandas and another time as cudf
df = dataset["filter_probs.csv"]
assert isinstance(df, (pd.DataFrame, cudf.DataFrame))
def test_dataset_only_pandas(dataset_pandas: DatasetManager):
# Test only runs with pandas
df = dataset_pandas["filter_probs.csv"]
assert isinstance(df, pd.DataFrame)
def test_dataset_only_cudf(dataset_cudf: DatasetManager):
# Test only runs with cudf
df = dataset_cudf["filter_probs.csv"]
assert isinstance(df, cudf.DataFrame)
def test_dataset_both(dataset: DatasetManager):
# By default, requesting dataset will parameterize both
df = dataset["filter_probs.csv"]
assert isinstance(df, (pd.DataFrame, cudf.DataFrame))
def test_dataset_manager_singleton(df_type: typing.Literal["cudf", "pandas"]):
dataset = DatasetManager(df_type=df_type)
assert dataset.default_df_type == df_type
assert getattr(dataset, df_type) is dataset
assert DatasetManager(df_type=df_type) is dataset
alt_type = DatasetManager.get_alt_df_type(df_type=df_type)
assert df_type != alt_type
assert DatasetManager(alt_type) is not dataset
assert getattr(dataset, alt_type) is not dataset
def test_dataset_dftype(dataset: DatasetManager):
df = dataset["filter_probs.csv"] # type will match the df_type parameter
if dataset.default_df_type == 'pandas':
assert isinstance(df, pd.DataFrame)
else:
assert isinstance(df, cudf.DataFrame)
def test_dataset_properties(dataset: DatasetManager):
pdf = dataset.pandas["filter_probs.csv"]
assert isinstance(pdf, pd.DataFrame)
cdf = dataset.cudf["filter_probs.csv"]
assert isinstance(cdf, cudf.DataFrame)
def test_dataset_reader_args(dataset: DatasetManager):
# When `filter_nulls=False` this returns all 20 rows, or 3 when True
input_file = 'examples/abp_pcap_detection/abp_pcap.jsonlines'
assert len(dataset.get_df(input_file, filter_nulls=False)) == 20
assert len(dataset[input_file]) == 3
# input_file is now in the cache with the default args, double check to make sure we still ignore the cache
assert len(dataset.get_df(input_file, filter_nulls=False)) == 20
# === No Marks ===
def test_no_mark():
assert CppConfig.get_should_use_cpp()
# === No Marks ===
@pytest.mark.use_cpp
def test_mark_use_cpp():
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_python
def test_mark_use_python():
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
@pytest.mark.use_python
def test_mark_both(cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
# === Marks and Config ===
@pytest.mark.use_cpp
@pytest.mark.usefixtures("config")
def test_mark_and_config_use_cpp():
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_python
@pytest.mark.usefixtures("config")
def test_mark_and_config_use_python():
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
@pytest.mark.use_python
@pytest.mark.usefixtures("config")
def test_mark_and_config_both(cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
@pytest.mark.usefixtures("config")
def test_mark_and_config_neither(cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
# === Fixture ===
@pytest.mark.use_cpp
def test_fixture_use_cpp(use_cpp: bool):
assert use_cpp
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_python
def test_fixture_use_python(use_cpp: bool):
assert not use_cpp
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
@pytest.mark.use_python
def test_fixture_both(use_cpp: bool):
assert CppConfig.get_should_use_cpp() == use_cpp
def test_fixture_neither(use_cpp: bool):
assert CppConfig.get_should_use_cpp() == use_cpp
# === Config Fixture ===
@pytest.mark.usefixtures("config_no_cpp")
def test_config_fixture_no_cpp():
assert not CppConfig.get_should_use_cpp()
@pytest.mark.usefixtures("config_only_cpp")
def test_config_fixture_only_cpp():
assert CppConfig.get_should_use_cpp()
class TestNoMarkerClass:
def test_no_marker(self):
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_python
def test_python_marker(self):
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
def test_cpp_marker(self):
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
@pytest.mark.use_python
def test_marker_both(self, cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
@pytest.mark.slow
def test_other_marker(self, use_cpp: bool):
assert CppConfig.get_should_use_cpp() == use_cpp
@pytest.mark.use_python
class TestPythonMarkerClass:
def test_no_marker(self):
assert not CppConfig.get_should_use_cpp()
def test_with_fixture(self, use_cpp: bool):
assert not use_cpp
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_python
def test_extra_marker(self):
assert not CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
def test_add_marker(self, cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
@pytest.mark.use_cpp
class TestCppMarkerClass:
def test_no_marker(self):
assert CppConfig.get_should_use_cpp()
def test_with_fixture(self, use_cpp: bool):
assert use_cpp
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_cpp
def test_extra_marker(self):
assert CppConfig.get_should_use_cpp()
@pytest.mark.use_python
def test_add_marker(self, cpp_from_marker: bool):
assert CppConfig.get_should_use_cpp() == cpp_from_marker
# === DF Type ===
def test_df_type_no_marks(df_type, df_type_from_marker):
assert df_type == df_type_from_marker
@pytest.mark.use_pandas
def test_df_type_pandas_marker(df_type):
assert df_type == "pandas"
@pytest.mark.use_cudf
def test_df_type_cudf_marker(df_type):
assert df_type == "cudf"
@pytest.mark.use_cudf
@pytest.mark.use_pandas
def test_df_type_both_markers(df_type, df_type_from_marker):
assert df_type == df_type_from_marker