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 3 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,20 @@
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

"""
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
68 changes: 68 additions & 0 deletions tests/unit/testmonitor/test_testmonitor_dataframe_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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(status_type=StatusType.PASSED),
id=uuid.uuid1().hex,
part_number=uuid.uuid1().hex,
keywords=["keyword1", "keyword2"],
properties={"property1": "value1", "property2": "value2"},
),
Result(
status=Status(status_type=StatusType.PASSED),
id=uuid.uuid1().hex,
part_number=uuid.uuid1().hex,
keywords=[],
),
Result(
status=Status(status_type=StatusType.PASSED),

Choose a reason for hiding this comment

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

there are some static methods available inside Status like Status.Passed(). It can be used

id=uuid.uuid1().hex,
part_number=uuid.uuid1().hex,
properties={
"property1": "value1",
"property2": "value2",
"property3": "value3",
},
),
]

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) == 3
assert len(results_dataframe.columns.tolist()) == 7
assert results_dataframe.equals(expected_results_dataframe)

Choose a reason for hiding this comment

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

a assert function part of pandas library was used in products utility. we can make use of that


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