Skip to content

Commit 84891eb

Browse files
refactor: doc strings and test file
1 parent 1c5d889 commit 84891eb

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

nisystemlink/clients/product/utilities/_dataframe_utilities.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ def convert_products_to_dataframe(products: List[Product]) -> DataFrame:
99
"""Converts a list of products into a normalized dataframe.
1010
1111
Args:
12-
products (List[Product]): A list of product responses retrieved from the API.
12+
products (List[Product]): A list of products
1313
1414
Returns:
1515
DataFrame:
16-
- A Pandas DataFrame containing the normalized product data. The DataFrame would consist
17-
of all the fields received from the product response.
18-
19-
- Further, the properties field alone would get normalized. In the sense, there
20-
would be separate columns for each of the properties in the list of all products. The
21-
normalized column headers would be in the format `properties.property_name`.
16+
- A Pandas DataFrame containing the product data. The DataFrame would consist of all the
17+
fields in the input products.
18+
- A new column would be created for unique properties across all products. The property
19+
columns would be named in the format `properties.property_name`.
2220
"""
2321
products_dict_representation = [product.dict() for product in products]
2422
normalized_products_dataframe = pd.json_normalize(

tests/unit/product/test_product_dataframe_utilities.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,28 @@ def mock_products_data() -> List[Product]:
4040
@pytest.fixture
4141
def expected_products_dataframe(mock_products_data: List[Product]) -> DataFrame:
4242
"""Fixture to return the expected DataFrame based on the mock product data."""
43-
return pd.json_normalize(
44-
[mock_product.dict() for mock_product in mock_products_data]
45-
)
43+
restructured_mock_products = []
44+
45+
for product in mock_products_data:
46+
properties = (
47+
{f"properties.{key}": value for key, value in product.properties.items()}
48+
if product.properties
49+
else {}
50+
)
51+
restructured_product = {
52+
"id": product.id,
53+
"part_number": product.part_number,
54+
"name": product.name,
55+
"family": product.family,
56+
"updated_at": product.updated_at,
57+
"file_ids": product.file_ids,
58+
"keywords": product.keywords,
59+
"workspace": product.workspace,
60+
**properties,
61+
}
62+
restructured_mock_products.append(restructured_product)
63+
64+
return pd.json_normalize(restructured_mock_products)
4665

4766

4867
@pytest.fixture

0 commit comments

Comments
 (0)