Skip to content

Commit 47447c6

Browse files
test: improvise fixtures
1 parent 2886cb2 commit 47447c6

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

tests/unit/product/test_product_dataframe_utilities.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,40 @@
99

1010

1111
@pytest.fixture
12-
def mock_product_data() -> List[Product]:
12+
def mock_products_data() -> List[Product]:
1313
"""Fixture to return a mock product data."""
14-
product = Product(
15-
id="product_id",
16-
part_number="product_part_number",
17-
name="product_name",
14+
product1 = Product(
15+
id="5ffb2bf6771fa11e877838dd1",
16+
part_number="p1",
17+
name="product_1",
1818
family="product_family",
1919
updated_at=datetime(2024, 2, 2, 14, 22, 4, 625155),
20-
file_ids=["file1", "file2"],
21-
keywords=["keyword1", "keyword2"],
22-
properties={"property1": "property1_value", "property2": "property2_value"},
23-
workspace="product_workspace",
20+
file_ids=["file11", "file12"],
21+
keywords=["keyword11", "keyword12"],
22+
properties={"property11": "property11_value", "property12": "property12_value"},
23+
workspace="5ffb2bf6771fa11e877838dd0",
24+
)
25+
product2 = Product(
26+
id="5ffb2bf6771fa11e877838dd2",
27+
part_number="p2",
28+
name="product_2",
29+
family="product_family",
30+
updated_at=datetime(2024, 2, 2, 14, 22, 4, 625455),
31+
file_ids=["file21", "file22"],
32+
keywords=["keyword21", "keyword22"],
33+
properties={"property21": "property21_value"},
34+
workspace="5ffb2bf6771fa11e877838dd0",
2435
)
2536

26-
return [product]
37+
return [product1, product2]
2738

2839

2940
@pytest.fixture
30-
def expected_products_dataframe(mock_product_data) -> DataFrame:
41+
def expected_products_dataframe(mock_products_data: List[Product]) -> DataFrame:
3142
"""Fixture to return the expected DataFrame based on the mock product data."""
32-
product = mock_product_data[0]
33-
expected_dataframe_structure = {
34-
"id": product.id,
35-
"part_number": product.part_number,
36-
"name": product.name,
37-
"family": product.family,
38-
"updated_at": product.updated_at,
39-
"file_ids": product.file_ids,
40-
"keywords": product.keywords,
41-
"workspace": product.workspace,
42-
"properties.property1": "property1_value",
43-
"properties.property2": "property2_value",
44-
}
45-
46-
return pd.json_normalize(expected_dataframe_structure)
43+
return pd.json_normalize(
44+
[mock_product.dict() for mock_product in mock_products_data]
45+
)
4746

4847

4948
@pytest.fixture
@@ -56,10 +55,10 @@ def empty_products_data() -> List:
5655
@pytest.mark.unit
5756
class TestProductDataframeUtilities:
5857
def test__convert_products_to_dataframe__with_complete_data(
59-
self, mock_product_data, expected_products_dataframe
58+
self, mock_products_data: List[Product], expected_products_dataframe: DataFrame
6059
):
6160
"""Test normal case with valid product data."""
62-
products_dataframe = convert_products_to_dataframe(mock_product_data)
61+
products_dataframe = convert_products_to_dataframe(mock_products_data)
6362

6463
assert not products_dataframe.empty
6564
assert (
@@ -70,23 +69,29 @@ def test__convert_products_to_dataframe__with_complete_data(
7069
products_dataframe, expected_products_dataframe, check_dtype=True
7170
)
7271

73-
def test__convert_products_to_dataframe__with_empty_data(self, empty_products_data):
72+
def test__convert_products_to_dataframe__with_empty_data(
73+
self, empty_products_data: List
74+
):
7475
"""Test case when the input products data is empty."""
7576
products_dataframe = convert_products_to_dataframe(empty_products_data)
7677

7778
assert products_dataframe.empty
7879

7980
def test__convert_products_to_dataframe__with_missing_fields(
80-
self, mock_product_data, expected_products_dataframe
81+
self, mock_products_data: List[Product], expected_products_dataframe: DataFrame
8182
):
8283
"""Test case when some fields in product data are missing."""
83-
products = mock_product_data
84-
products[0].keywords = None
85-
products[0].properties = None
84+
products = mock_products_data
85+
for product in products:
86+
product.keywords = None
87+
product.properties = None
8688

8789
products_dataframe = convert_products_to_dataframe(products)
8890
expected_products_dataframe = expected_products_dataframe.drop(
89-
columns=["keywords", "properties.property1", "properties.property2"]
91+
columns=expected_products_dataframe.filter(
92+
like="properties"
93+
).columns.to_list()
94+
+ ["keywords"]
9095
)
9196

9297
assert not products_dataframe.empty

0 commit comments

Comments
 (0)