-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
c0dc98d
46398ca
6800b0f
42460dc
46eaa97
ffbcceb
8772a2f
9200e70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
""" | ||
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 |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# flake8: noqa |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# flake8: noqa |
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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are some static methods available inside Status like |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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=".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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