-
Notifications
You must be signed in to change notification settings - Fork 148
/
test_column_info.py
308 lines (231 loc) · 9.93 KB
/
test_column_info.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# SPDX-FileCopyrightText: Copyright (c) 2022-2023 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 os
from datetime import datetime
from functools import partial
import pandas as pd
import pytest
from morpheus.utils.column_info import ColumnInfo
from morpheus.utils.column_info import CustomColumn
from morpheus.utils.column_info import DataFrameInputSchema
from morpheus.utils.column_info import DateTimeColumn
from morpheus.utils.column_info import RenameColumn
from morpheus.utils.column_info import StringCatColumn
from morpheus.utils.column_info import StringJoinColumn
from morpheus.utils.column_info import process_dataframe
from utils import TEST_DIRS
@pytest.mark.use_python
def test_dataframe_input_schema_with_json_cols():
src_file = os.path.join(TEST_DIRS.tests_data_dir, "azure_ad_logs.json")
input_df = pd.read_json(src_file)
raw_data_columns = [
'time',
'resourceId',
'operationName',
'operationVersion',
'category',
'tenantId',
'resultType',
'resultSignature',
'resultDescription',
'durationMs',
'callerIpAddress',
'correlationId',
'identity',
'Level',
'location',
'properties'
]
assert len(input_df.columns) == 16
assert list(input_df.columns) == raw_data_columns
column_info = [
DateTimeColumn(name="timestamp", dtype=datetime, input_name="time"),
RenameColumn(name="userId", dtype=str, input_name="properties.userPrincipalName"),
RenameColumn(name="appDisplayName", dtype=str, input_name="properties.appDisplayName"),
ColumnInfo(name="category", dtype=str),
RenameColumn(name="clientAppUsed", dtype=str, input_name="properties.clientAppUsed"),
RenameColumn(name="deviceDetailbrowser", dtype=str, input_name="properties.deviceDetail.browser"),
RenameColumn(name="deviceDetaildisplayName", dtype=str, input_name="properties.deviceDetail.displayName"),
RenameColumn(name="deviceDetailoperatingSystem",
dtype=str,
input_name="properties.deviceDetail.operatingSystem"),
StringCatColumn(name="location",
dtype=str,
input_columns=[
"properties.location.city",
"properties.location.countryOrRegion",
],
sep=", "),
RenameColumn(name="statusfailureReason", dtype=str, input_name="properties.status.failureReason"),
]
schema = DataFrameInputSchema(json_columns=["properties"], column_info=column_info)
df_processed = process_dataframe(input_df, schema)
processed_df_cols = df_processed.columns
assert len(input_df) == len(df_processed)
assert len(processed_df_cols) == len(column_info)
assert "timestamp" in processed_df_cols
assert "userId" in processed_df_cols
assert "time" not in processed_df_cols
assert "properties.userPrincipalName" not in processed_df_cols
@pytest.mark.use_python
def test_dataframe_input_schema_without_json_cols():
src_file = os.path.join(TEST_DIRS.tests_data_dir, "azure_ad_logs.json")
input_df = pd.read_json(src_file)
assert len(input_df.columns) == 16
column_info = [
DateTimeColumn(name="timestamp", dtype=datetime, input_name="time"),
RenameColumn(name="userId", dtype=str, input_name="properties.userPrincipalName"),
RenameColumn(name="appDisplayName", dtype=str, input_name="properties.appDisplayName"),
ColumnInfo(name="category", dtype=str),
RenameColumn(name="clientAppUsed", dtype=str, input_name="properties.clientAppUsed"),
RenameColumn(name="deviceDetailbrowser", dtype=str, input_name="properties.deviceDetail.browser"),
RenameColumn(name="deviceDetaildisplayName", dtype=str, input_name="properties.deviceDetail.displayName"),
RenameColumn(name="deviceDetailoperatingSystem",
dtype=str,
input_name="properties.deviceDetail.operatingSystem"),
RenameColumn(name="statusfailureReason", dtype=str, input_name="properties.status.failureReason"),
]
schema = DataFrameInputSchema(column_info=column_info)
df_processed = process_dataframe(input_df, schema)
processed_df_cols = df_processed.columns
assert len(input_df) == len(df_processed)
assert len(processed_df_cols) == len(column_info)
assert "timestamp" in processed_df_cols
assert "time" not in processed_df_cols
assert "userId" in processed_df_cols
assert len(df_processed[~df_processed.userId.isna()]) == 0
column_info2 = [
DateTimeColumn(name="timestamp", dtype=datetime, input_name="time"),
RenameColumn(name="userId", dtype=str, input_name="properties.userPrincipalName"),
RenameColumn(name="appDisplayName", dtype=str, input_name="properties.appDisplayName"),
ColumnInfo(name="category", dtype=str),
RenameColumn(name="clientAppUsed", dtype=str, input_name="properties.clientAppUsed"),
RenameColumn(name="deviceDetailbrowser", dtype=str, input_name="properties.deviceDetail.browser"),
RenameColumn(name="deviceDetaildisplayName", dtype=str, input_name="properties.deviceDetail.displayName"),
RenameColumn(name="deviceDetailoperatingSystem",
dtype=str,
input_name="properties.deviceDetail.operatingSystem"),
StringCatColumn(name="location",
dtype=str,
input_columns=[
"properties.location.city",
"properties.location.countryOrRegion",
],
sep=", "),
RenameColumn(name="statusfailureReason", dtype=str, input_name="properties.status.failureReason"),
]
schema2 = DataFrameInputSchema(column_info=column_info2)
# When trying to concat columns that don't exist in the dataframe, an exception is raised.
with pytest.raises(Exception):
process_dataframe(input_df, schema2)
@pytest.mark.use_python
def test_string_cat_column():
cities = pd.Series([
"New York",
"Dallas",
"Austin",
])
countries = pd.Series([
"USA",
"USA",
"USA",
])
states = pd.Series([
"New York",
"Texas",
"Texas",
])
zipcodes = pd.Series([10001, 75001, 73301])
df = pd.DataFrame({"city": cities, "country": countries, "state": states, "zipcode": zipcodes})
string_cat_col = StringCatColumn(name="location", dtype=str, input_columns=[
"city",
"country",
], sep=", ")
actual = string_cat_col._process_column(df)
expected = pd.Series(["New York, USA", "Dallas, USA", "Austin, USA"])
assert actual.equals(expected)
string_cat_col_with_int = StringCatColumn(name="location", dtype=str, input_columns=[
"city",
"zipcode",
], sep=", ")
with pytest.raises(Exception):
string_cat_col_with_int._process_column(df)
@pytest.mark.use_python
def test_string_join_column():
cities = pd.Series([
"Boston",
"Dallas",
"Austin",
])
df = pd.DataFrame({"city": cities})
string_join_col = StringJoinColumn(name="city_new", dtype=str, input_name="city", sep="-")
actual = string_join_col._process_column(df)
expected = pd.Series(["B-o-s-t-o-n", "D-a-l-l-a-s", "A-u-s-t-i-n"])
assert actual.equals(expected)
@pytest.mark.use_python
def test_column_info():
cities = pd.Series([
"Boston",
"Dallas",
"Austin",
])
df = pd.DataFrame({"city": cities})
string_join_col = ColumnInfo(name="city", dtype=str)
actual = string_join_col._process_column(df)
assert actual.equals(cities)
assert string_join_col.name == "city"
@pytest.mark.use_python
def test_date_column():
time_series = pd.Series([
"2022-08-29T21:21:41.645157Z",
"2022-08-29T21:23:19.500982Z",
"2022-08-29T21:40:16.765798Z",
"2022-08-29T22:23:15.895201Z",
"2022-08-29T22:05:45.076460Z"
])
df = pd.DataFrame({"time": time_series})
datetime_col = DateTimeColumn(name="timestamp", dtype=datetime, input_name="time")
datetime_series = datetime_col._process_column(df)
assert datetime_series.dtype == 'datetime64[ns, UTC]'
@pytest.mark.use_python
def test_rename_column():
time_series = pd.Series([
"2022-08-29T21:21:41.645157Z",
"2022-08-29T21:23:19.500982Z",
"2022-08-29T21:40:16.765798Z",
"2022-08-29T22:23:15.895201Z",
"2022-08-29T22:05:45.076460Z"
])
df = pd.DataFrame({"time": time_series})
rename_col = RenameColumn(name="timestamp", dtype=str, input_name="time")
actutal = rename_col._process_column(df)
assert actutal.equals(time_series)
def convert_to_upper(df, column_name: str):
return df[column_name].str.upper()
@pytest.mark.use_python
def test_custom_column():
cities = pd.Series([
"New York",
"Dallas",
"Austin",
])
df = pd.DataFrame({"city": cities})
custom_col = CustomColumn(name="city_upper",
dtype=str,
process_column_fn=partial(convert_to_upper, column_name="city"))
actutal = custom_col._process_column(df)
expected = pd.Series(["NEW YORK", "DALLAS", "AUSTIN"])
assert actutal.equals(expected)