|
| 1 | +#!/usr/bin/env python |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import pandas as pd |
| 20 | +import pytest |
| 21 | + |
| 22 | +import cudf |
| 23 | + |
| 24 | +from _utils import TEST_DIRS |
| 25 | +from _utils import assert_results |
| 26 | +from morpheus.config import Config |
| 27 | +from morpheus.io.deserializers import read_file_to_df |
| 28 | +from morpheus.messages import MessageMeta |
| 29 | +from morpheus.pipeline import LinearPipeline |
| 30 | +from morpheus.stages.input.in_memory_source_stage import InMemorySourceStage |
| 31 | +from morpheus.stages.output.in_memory_sink_stage import InMemorySinkStage |
| 32 | +from morpheus.stages.preprocess.group_by_column_stage import GroupByColumnStage |
| 33 | +from morpheus.utils.compare_df import compare_df |
| 34 | +from morpheus.utils.type_aliases import DataFrameType |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(name="_test_df", scope="module") |
| 38 | +def _test_df_fixture(): |
| 39 | + """ |
| 40 | + Read the source data only once |
| 41 | + """ |
| 42 | + # Manually reading this in since we need lines=False |
| 43 | + yield read_file_to_df(os.path.join(TEST_DIRS.tests_data_dir, 'azure_ad_logs.json'), |
| 44 | + parser_kwargs={'lines': False}, |
| 45 | + df_type='pandas') |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture(name="test_df") |
| 49 | +def test_df_fixture(_test_df: DataFrameType): |
| 50 | + """ |
| 51 | + Ensure each test gets a unique copy |
| 52 | + """ |
| 53 | + yield _test_df.copy(deep=True) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("group_by_column", ["identity", "location"]) |
| 57 | +def test_group_by_column_stage_pipe(config: Config, group_by_column: str, test_df: DataFrameType): |
| 58 | + input_df = cudf.from_pandas(test_df) |
| 59 | + input_df.drop(columns=["properties"], inplace=True) # Remove once #1527 is resolved |
| 60 | + |
| 61 | + # Intentionally constructing the expected data in a manual way not involving pandas or cudf to avoid using the same |
| 62 | + # technology as the GroupByColumnStage |
| 63 | + rows = test_df.to_dict(orient="records") |
| 64 | + expected_data: dict[str, list[dict]] = {} |
| 65 | + for row in rows: |
| 66 | + key = row[group_by_column] |
| 67 | + if key not in expected_data: |
| 68 | + expected_data[key] = [] |
| 69 | + |
| 70 | + row.pop('properties') # Remove once #1527 is resolved |
| 71 | + expected_data[key].append(row) |
| 72 | + |
| 73 | + expected_dataframes: list[DataFrameType] = [] |
| 74 | + for key in sorted(expected_data.keys()): |
| 75 | + df = pd.DataFrame(expected_data[key]) |
| 76 | + expected_dataframes.append(df) |
| 77 | + |
| 78 | + pipe = LinearPipeline(config) |
| 79 | + pipe.set_source(InMemorySourceStage(config, dataframes=[input_df])) |
| 80 | + pipe.add_stage(GroupByColumnStage(config, column_name=group_by_column)) |
| 81 | + sink = pipe.add_stage(InMemorySinkStage(config)) |
| 82 | + |
| 83 | + pipe.run() |
| 84 | + |
| 85 | + messages: MessageMeta = sink.get_messages() |
| 86 | + assert len(messages) == len(expected_dataframes) |
| 87 | + for (i, message) in enumerate(messages): |
| 88 | + output_df = message.copy_dataframe().to_pandas() |
| 89 | + output_df.reset_index(drop=True, inplace=True) |
| 90 | + |
| 91 | + expected_df = expected_dataframes[i] |
| 92 | + |
| 93 | + assert_results(compare_df(expected_df, output_df)) |
0 commit comments