Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Export Results as Dataframe #100

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nisystemlink/clients/testmonitor/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._dataframe_utilities import convert_results_to_dataframe

# flake8: noqa
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List

import pandas as pd
from nisystemlink.clients.testmonitor.models import Result


def convert_results_to_dataframe(results: List[Result]) -> pd.DataFrame:
"""Normalizes the results into a Pandas DataFrame.

Args:
results: The list of results to normalize.

Returns:
A Pandas DataFrame with the normalized queried results.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add documentation for the code structure

status_type_summary will be normalized into the respective status types.
For example, status_type_summary.LOOPING, status_type_summary.PASSED.
Status is normalized into status.status_type and status.status_name.
Properties are normalized into respective properties. For example,
properties.property1, properties.property2 and etc.
"""
results_dict = [result.dict(exclude_unset=True) for result in results]
normalized_dataframe = pd.json_normalize(results_dict, sep=".")
normalized_dataframe.dropna(axis="columns", how="all", inplace=True)

return normalized_dataframe
328 changes: 313 additions & 15 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ requests = "^2.28.1"
uplink = "^0.9.7"
pydantic = "^1.10.2"
pyyaml = "^6.0.1"
pandas = "^2.1.0"

[tool.poetry.group.dev.dependencies]
black = ">=22.10,<25.0"
Expand Down
1 change: 1 addition & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# flake8: noqa
1 change: 1 addition & 0 deletions tests/unit/testmonitor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# flake8: noqa
65 changes: 65 additions & 0 deletions tests/unit/testmonitor/test_testmonitor_dataframe_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import uuid
from typing import List

import pandas as pd
import pytest
from nisystemlink.clients.testmonitor.models._result import Result
from nisystemlink.clients.testmonitor.models._status import Status, StatusType
from nisystemlink.clients.testmonitor.utilities._dataframe_utilities import (
convert_results_to_dataframe,
)


@pytest.fixture(scope="class")
def results() -> List[Result]:
"""Sample results for testing purposes."""
results = [
Result(
status = Status.PASSED(),
started_at = "2018-05-07T18:58:05.219692Z",
updated_at = "2018-05-07T18:58:05.219692Z",
program_name = "My Program Name",
id = uuid.uuid1().hex,
system_id = uuid.uuid1().hex,
host_name = "host name",
part_number = uuid.uuid1().hex,
serial_number = uuid.uuid1().hex,
total_time_in_seconds = 16.76845106446358,
keywords = ["keyword1", "keyword2"],
properties = {"property1": "value1", "property2": "value2"},
operator = "sample operator",
file_ids = [uuid.uuid1().hex, uuid.uuid1().hex],
data_table_ids = [uuid.uuid1().hex, uuid.uuid1().hex],
status_type_summary = {StatusType.PASSED: 1, StatusType.FAILED: 0},
workspace = uuid.uuid1().hex,
),
]

return results


@pytest.mark.enterprise
class TestTestmonitorDataframeUtilities:
def test__convert_results_to_dataframe__returns_results_dataframe(self, results):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add one test where the result contains all the fields value set

expected_results_dict = []
for result in results:
expected_results_dict.append(result.dict(exclude_unset=True))
expected_results_dataframe = pd.json_normalize(expected_results_dict, sep=".")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets manually create a df for expected data, it helps see the actual df structure

expected_results_dataframe.dropna(axis="columns", how="all", inplace=True)

results_dataframe = convert_results_to_dataframe(results=results)

assert not results_dataframe.empty
assert isinstance(results_dataframe, pd.DataFrame)
assert len(results_dataframe) == 1
assert len(results_dataframe.columns.tolist()) == 19
assert results_dataframe.equals(expected_results_dataframe), expected_results_dataframe
pd.testing.assert_frame_equal(results_dataframe, expected_results_dataframe, check_dtype=True), expected_results_dataframe

def test__convert_results_to_dataframe_with_no_results__returns_empty_dataframe(
self,
):
results_dataframe = convert_results_to_dataframe(results=[])

assert isinstance(results_dataframe, pd.DataFrame)
assert results_dataframe.empty
Loading