Skip to content

Commit

Permalink
refactor(product_footprint.py): migrate company_ids attr to CompanyId…
Browse files Browse the repository at this point in the history
…List
  • Loading branch information
JohnVonNeumann committed Jan 28, 2025
1 parent 9d3d36c commit d55c920
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 79 deletions.
53 changes: 27 additions & 26 deletions pact_methodology/product_footprint/product_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pact_methodology.product_footprint.validity_period import ValidityPeriod
from pact_methodology.urn import CompanyId, ProductId
from pact_methodology.product_footprint.product_id_list import ProductIdList
from pact_methodology.product_footprint.company_id_list import CompanyIdList


class ProductFootprint:
Expand All @@ -27,7 +28,7 @@ class ProductFootprint:
status_comment (str | None): A comment describing the status of the ProductFootprint.
validity_period (ValidityPeriod | None): The validity period for the ProductFootprint.
company_name (str): The name of the company that owns the ProductFootprint.
company_ids (list[CompanyId]): A list of CompanyIds for the company that owns the ProductFootprint.
company_ids (CompanyIdList): A list of CompanyIds for the company that owns the ProductFootprint.
product_description (str): A description of the product.
product_ids (ProductIdList): A list of ProductIds for the product.
product_category_cpc (CPC): The category of the product according to the CPC (Central Product Classification) system.
Expand All @@ -44,9 +45,9 @@ class ProductFootprint:
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand All @@ -67,7 +68,7 @@ def __init__(
status_info: ProductFootprintStatus,
validity_period: ValidityPeriod | None = None,
company_name: str,
company_ids: list[CompanyId],
company_ids: CompanyIdList,
product_description: str,
product_ids: ProductIdList,
product_category_cpc: CPC,
Expand All @@ -90,9 +91,9 @@ def __init__(
status_comment (str | None): A comment describing the status of the ProductFootprint.
validity_period (ValidityPeriod | None): The validity period for the ProductFootprint.
company_name (str): The name of the company that owns the ProductFootprint.
company_ids (list[CompanyId]): A list of CompanyIds for the company that owns the ProductFootprint.
company_ids (CompanyIdList): A list of CompanyIds for the company that owns the ProductFootprint.
product_description (str): A description of the product.
product_ids (list[ProductId]): A list of ProductIds for the product.
product_ids (ProductIdList): A list of ProductIds for the product.
product_category_cpc (CPC): The category of the product according to the CPC (Central Product Classification) system.
product_name_company (str): The name of the product as used by the company.
comment (str): A comment about the ProductFootprint.
Expand All @@ -107,9 +108,9 @@ def __init__(
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand Down Expand Up @@ -324,21 +325,21 @@ def company_name(self, value):

@property
def company_ids(self):
"""Gets the list of CompanyIds for the company that owns the ProductFootprint."""
"""Gets the CompanyIdList for the company that owns the ProductFootprint."""
return self._company_ids

@company_ids.setter
def company_ids(self, value):
"""Sets the list of CompanyIds for the company that owns the ProductFootprint.
"""Sets the CompanyIdList for the company that owns the ProductFootprint.
Args:
value (list[CompanyId]): The list of company IDs to set.
value (CompanyIdList): The CompanyIdList to set.
Raises:
ValueError: If value is not a list of CompanyId instances.
ValueError: If value is not an instance of CompanyIdList.
"""
if not isinstance(value, list) or not all(isinstance(company_id, CompanyId) for company_id in value):
raise ValueError("company_ids must be a list of CompanyId")
if not isinstance(value, CompanyIdList):
raise ValueError("company_ids must be an instance of CompanyIdList")
self._company_ids = value

@property
Expand Down Expand Up @@ -507,9 +508,9 @@ def __str__(self):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand Down Expand Up @@ -538,9 +539,9 @@ def __repr__(self):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand Down Expand Up @@ -569,9 +570,9 @@ def __eq__(self, other):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand All @@ -582,9 +583,9 @@ def __eq__(self, other):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand All @@ -606,9 +607,9 @@ def __ne__(self, other):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Example Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand All @@ -619,9 +620,9 @@ def __ne__(self, other):
... created=DateTime(),
... status=ProductFootprintStatus(),
... company_name="Different Company",
... company_ids=[CompanyId()],
... company_ids=CompanyIdList([CompanyId()]),
... product_description="Example Product",
... product_ids=[ProductId()],
... product_ids=ProductIdList([ProductId()]),
... product_category_cpc=CPC(),
... product_name_company="Example Product Name",
... comment="Example comment",
Expand Down
69 changes: 16 additions & 53 deletions tests/product_footprint/test_product_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pact_methodology.product_footprint.status import ProductFootprintStatus, Status
from pact_methodology.urn import CompanyId, ProductId
from pact_methodology.product_footprint.product_id_list import ProductIdList
from pact_methodology.product_footprint.company_id_list import CompanyIdList
from pact_methodology.product_footprint.cpc import CPCCodeLookup, CPC
from pact_methodology.product_footprint.version import Version
from pact_methodology.datetime import DateTime
Expand Down Expand Up @@ -46,8 +47,8 @@


@pytest.fixture(scope="module")
def company_ids() -> list[CompanyId]:
return [CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")]
def company_ids() -> CompanyIdList:
return CompanyIdList([CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")])


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -144,9 +145,9 @@ def carbon_footprint(valid_carbon_footprint_data):

@pytest.fixture
def valid_product_footprint_data(valid_carbon_footprint_data, carbon_footprint):
company_ids = [
company_ids = CompanyIdList([
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")
]
])
product_ids = ProductIdList([
ProductId("urn:pathfinder:product:customcode:buyer-assigned:acme-product")
])
Expand Down Expand Up @@ -194,9 +195,10 @@ def test_product_footprint_initialization(valid_product_footprint_data):
assert product_footprint.status_comment == "This is a comment"
assert isinstance(product_footprint.validity_period, ValidityPeriod)
assert product_footprint.company_name == "Company Name"
assert product_footprint.company_ids == [
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")
]
assert isinstance(product_footprint.company_ids, CompanyIdList)
assert all(
isinstance(company_id, CompanyId) for company_id in product_footprint.company_ids
)
assert product_footprint.product_description == "Product Description"
assert isinstance(product_footprint.product_ids, ProductIdList)
assert all(
Expand Down Expand Up @@ -356,13 +358,13 @@ def test_product_footprint_company_name(valid_product_footprint_data):
@pytest.mark.parametrize(
"company_ids",
[
[
CompanyIdList([
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")
], # list with a single item
[
]), # list with a single item
CompanyIdList([
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp2"),
], # list with multiple items
]), # list with multiple items
],
)
def test_product_footprint_company_ids(valid_product_footprint_data, company_ids):
Expand All @@ -371,6 +373,7 @@ def test_product_footprint_company_ids(valid_product_footprint_data, company_ids
"company_ids": company_ids,
}
product_footprint = ProductFootprint(**product_footprint_data)
assert isinstance(product_footprint.company_ids, CompanyIdList)
assert len(product_footprint.company_ids) == len(company_ids)
for company_id in product_footprint.company_ids:
assert isinstance(company_id, CompanyId)
Expand All @@ -385,6 +388,7 @@ def test_product_footprint_company_ids(valid_product_footprint_data, company_ids
"string",
{},
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
[CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp")],
],
)
def test_product_footprint_invalid_company_ids(
Expand All @@ -394,7 +398,7 @@ def test_product_footprint_invalid_company_ids(
**valid_product_footprint_data,
"company_ids": company_ids,
}
with pytest.raises(ValueError, match="company_ids must be a list of CompanyId"):
with pytest.raises(ValueError, match="company_ids must be an instance of CompanyIdList"):
ProductFootprint(**invalid_product_footprint_data)


Expand Down Expand Up @@ -446,47 +450,6 @@ def test_product_footprint_product_ids_invalid_value_error(valid_product_footpri
product_footprint.product_ids = "string"


@pytest.mark.parametrize(
"company_ids",
[
["string"], # list with a single invalid item
[
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
"string",
], # list with a mix of valid and invalid items
[1, 2, 3], # list with invalid items of a different type
[
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
"string",
], # list with multiple valid items and an invalid item
],
)
def test_product_footprint_invalid_company_ids_list(
valid_product_footprint_data, company_ids
):
invalid_product_footprint_data = {
**valid_product_footprint_data,
"company_ids": company_ids,
}
with pytest.raises(ValueError, match="company_ids must be a list of CompanyId"):
ProductFootprint(**invalid_product_footprint_data)


@pytest.mark.xfail(reason="Functionality not implemented yet")
def test_product_footprint_duplicate_company_ids(valid_product_footprint_data):
company_ids = [
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
CompanyId("urn:pathfinder:company:customcode:buyer-assigned:acme-corp"),
]
product_footprint_data = {
**valid_product_footprint_data,
"company_ids": company_ids,
}
with pytest.raises(ValueError, match="Duplicate company_ids are not allowed"):
ProductFootprint(**product_footprint_data)


def test_product_footprint_product_category_cpc(valid_product_footprint_data):
product_footprint = ProductFootprint(**valid_product_footprint_data)
assert isinstance(product_footprint.product_category_cpc, CPC)
Expand Down

0 comments on commit d55c920

Please sign in to comment.