Skip to content

Commit

Permalink
Merge pull request #63 from ansys/feat/10-document-legislation-id-change
Browse files Browse the repository at this point in the history
Rename with_legislations to with_legislation_ids
  • Loading branch information
Andy-Grigg authored Oct 10, 2023
2 parents 797c906 + 6d0214b commit 2cffdc7
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ doc/build

# Pypi things
deps

venv/
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Here's a brief example of how to use PyGranta BoM Analytics:
>>> query = (
... queries.MaterialImpactedSubstancesQuery()
... .with_material_ids(['plastic-abs-pvc-flame'])
... .with_legislations(['Candidate_AnnexXV'])
... .with_legislation_ids(['Candidate_AnnexXV'])
... )
# Print out the result from the query.
Expand Down
2 changes: 1 addition & 1 deletion doc/source/api/impacted_substances/bom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Query definition
.. autoclass:: ansys.grantami.bomanalytics.queries.BomImpactedSubstancesQuery
:members:

.. automethod:: with_legislations
.. automethod:: with_legislation_ids
.. automethod:: with_bom

.. _ref_grantami_bomanalytics_api_impactedsubstances_bom_queryresult:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/api/impacted_substances/materials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Query definition
.. automethod:: with_record_history_ids
.. automethod:: with_material_ids
.. automethod:: with_batch_size
.. automethod:: with_legislations
.. automethod:: with_legislation_ids

Query result
~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion doc/source/api/impacted_substances/parts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Query definition
.. autoclass:: ansys.grantami.bomanalytics.queries.PartImpactedSubstancesQuery
:members:

.. automethod:: with_legislations
.. automethod:: with_legislation_ids
.. automethod:: with_part_numbers
.. automethod:: with_record_guids
.. automethod:: with_record_history_guids
Expand Down
2 changes: 1 addition & 1 deletion doc/source/api/impacted_substances/specifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Query definition
.. autoclass:: ansys.grantami.bomanalytics.queries.SpecificationImpactedSubstancesQuery
:members:

.. automethod:: with_legislations
.. automethod:: with_legislation_ids
.. automethod:: with_specification_ids
.. automethod:: with_record_guids
.. automethod:: with_record_history_guids
Expand Down
2 changes: 1 addition & 1 deletion doc/source/index/quick_code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ high concern) in an ABS/PVC blend:
>>> query = (
... queries.MaterialImpactedSubstancesQuery()
... .with_material_ids(['plastic-abs-pvc-flame'])
... .with_legislations(['EU REACH - The Candidate List'])
... .with_legislation_ids(['Candidate_AnnexXV'])
... )
>>> result = cxn.run(query)
>>> pprint(result.impacted_substances)
Expand Down
6 changes: 3 additions & 3 deletions examples/0_Getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@
# ``Candidate_AnnexXV`` is the ID of the ``EU REACH - The Candidate List`` legislation.

# + tags=[]
query = query.with_legislations(["Candidate_AnnexXV"])
query = query.with_legislation_ids(["Candidate_AnnexXV"])
query
# -

# Fluent interfaces are designed to allow a complex object to be constructed in a single line of code. As such, you can
# consolidate the cells above into a single step:

# + tags=[]
query = queries.MaterialImpactedSubstancesQuery().with_material_ids(["plastic-abs-high-impact"]).with_legislations(["Candidate_AnnexXV"]) # noqa: E501
query = queries.MaterialImpactedSubstancesQuery().with_material_ids(["plastic-abs-high-impact"]).with_legislation_ids(["Candidate_AnnexXV"]) # noqa: E501
query
# -

Expand All @@ -106,7 +106,7 @@
query = (
queries.MaterialImpactedSubstancesQuery()
.with_material_ids(["plastic-abs-high-impact"])
.with_legislations(["Candidate_AnnexXV"])
.with_legislation_ids(["Candidate_AnnexXV"])
)
query
# -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
mat_query = (
queries.MaterialImpactedSubstancesQuery()
.with_material_ids([PPS_ID, PC_ID])
.with_legislations([REACH, SIN_LIST])
.with_legislation_ids([REACH, SIN_LIST])
)
# -

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
part_query = (
queries.PartImpactedSubstancesQuery()
.with_part_numbers([DRILL, WING])
.with_legislations([SIN_LIST, REACH])
.with_legislation_ids([SIN_LIST, REACH])
)
# -

Expand Down
4 changes: 2 additions & 2 deletions examples/3_Advanced_Topics/3-1_Working_with_XML_BoMs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def xml_validator(xml: str, schema_file: str) -> bool:
impacted_substances_query = (
queries.BomImpactedSubstancesQuery()
.with_bom(valid_xml)
.with_legislations([SIN_LIST])
.with_legislation_ids([SIN_LIST])
)
# -

Expand Down Expand Up @@ -162,7 +162,7 @@ def xml_validator(xml: str, schema_file: str) -> bool:
broken_query = (
queries.BomImpactedSubstancesQuery()
.with_bom(invalid_xml)
.with_legislations([SIN_LIST])
.with_legislation_ids([SIN_LIST])
)

RUN_QUERY = False
Expand Down
2 changes: 2 additions & 0 deletions src/ansys/grantami/bomanalytics/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class RoHSIndicator(_Indicator):

def __init__(
self,
*,
name: str,
legislation_ids: List[str],
default_threshold_percentage: Optional[float] = None,
Expand Down Expand Up @@ -526,6 +527,7 @@ class WatchListIndicator(_Indicator):

def __init__(
self,
*,
name: str,
legislation_ids: List[str],
default_threshold_percentage: Optional[float] = None,
Expand Down
14 changes: 7 additions & 7 deletions src/ansys/grantami/bomanalytics/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,14 @@ def __repr__(self) -> str:
return result

@validate_argument_type([str], {str})
def with_legislations(self: _ImpactedSubstanceQuery, legislation_names: List[str]) -> _ImpactedSubstanceQuery:
def with_legislation_ids(self: _ImpactedSubstanceQuery, legislation_ids: List[str]) -> _ImpactedSubstanceQuery:
"""Add a list or set of legislations to retrieve the impacted substances for.
Legislations are identified based on their ``Legislation ID`` attribute value.
Parameters
----------
legislation_names : list[str] | set[str]
legislation_ids : list[str] | set[str]
List or set of legislation ids.
Returns
Expand All @@ -765,7 +765,7 @@ def with_legislations(self: _ImpactedSubstanceQuery, legislation_names: List[str
Error to raise if the method is called with values that do not match the types described earlier.
"""

self._legislations.extend(legislation_names)
self._legislations.extend(legislation_ids)
return self

def _run_query(self, api_instance: api.ImpactedSubstancesApi, static_arguments: Dict) -> ResultBaseClass:
Expand Down Expand Up @@ -912,7 +912,7 @@ class MaterialImpactedSubstancesQuery(_ImpactedSubstanceMixin, _MaterialQueryBui
>>> query = (
... MaterialImpactedSubstancesQuery()
... .with_material_ids(['elastomer-butadienerubber', 'NBR-100'])
... .with_legislations(["Candidate_AnnexXV"])
... .with_legislation_ids(["Candidate_AnnexXV"])
... )
>>> cxn.run(query)
<MaterialImpactedSubstancesQueryResult: 2 MaterialWithImpactedSubstances results>
Expand Down Expand Up @@ -1018,7 +1018,7 @@ class PartImpactedSubstancesQuery(_ImpactedSubstanceMixin, _PartQueryBuilder):
>>> query = (
... PartImpactedSubstancesQuery()
... .with_part_numbers(['DRILL', 'FLRY34'])
... .with_legislations(["Candidate_AnnexXV"])
... .with_legislation_ids(["Candidate_AnnexXV"])
... )
>>> cxn.run(query)
<PartImpactedSubstancesQueryResult: 2 PartWithImpactedSubstances results>
Expand Down Expand Up @@ -1127,7 +1127,7 @@ class SpecificationImpactedSubstancesQuery(_ImpactedSubstanceMixin, _Specificati
>>> query = (
... SpecificationImpactedSubstancesQuery()
... .with_specification_ids(['MIL-A-8625', 'PSP101'])
... .with_legislations(["Candidate_AnnexXV"])
... .with_legislation_ids(["Candidate_AnnexXV"])
... )
>>> cxn.run(query)
<SpecificationImpactedSubstancesQueryResult:
Expand Down Expand Up @@ -1742,7 +1742,7 @@ class BomImpactedSubstancesQuery(_ImpactedSubstanceMixin, _Bom1711QueryBuilder):
>>> query = (
... BomImpactedSubstancesQuery()
... .with_bom("<PartsEco xmlns...")
... .with_legislations(["Candidate_AnnexXV"])
... .with_legislation_ids(["Candidate_AnnexXV"])
... )
>>> cxn.run(query)
<BomImpactedSubstancesQueryResult: 1 Bom1711WithImpactedSubstances results>
Expand Down
20 changes: 11 additions & 9 deletions tests/test_integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestMaterialQueries:
ids = ["plastic-abs-pvc-flame", "plastic-pmma-pc"]

def test_impacted_substances(self, connection_with_db_variants):
query = queries.MaterialImpactedSubstancesQuery().with_material_ids(self.ids).with_legislations(LEGISLATIONS)
query = queries.MaterialImpactedSubstancesQuery().with_material_ids(self.ids).with_legislation_ids(LEGISLATIONS)
response = connection_with_db_variants.run(query)
assert response.impacted_substances
assert response.impacted_substances_by_legislation
Expand All @@ -32,7 +32,7 @@ class TestPartQueries:
ids = ["DRILL", "asm_flap_mating"]

def test_impacted_substances(self, connection_with_db_variants):
query = queries.PartImpactedSubstancesQuery().with_part_numbers(self.ids).with_legislations(LEGISLATIONS)
query = queries.PartImpactedSubstancesQuery().with_part_numbers(self.ids).with_legislation_ids(LEGISLATIONS)
response = connection_with_db_variants.run(query)

assert response.impacted_substances
Expand All @@ -55,7 +55,7 @@ def test_impacted_substances(self, connection_with_db_variants):
query = (
queries.SpecificationImpactedSubstancesQuery()
.with_specification_ids(self.ids)
.with_legislations(LEGISLATIONS)
.with_legislation_ids(LEGISLATIONS)
)
response = connection_with_db_variants.run(query)

Expand Down Expand Up @@ -95,7 +95,7 @@ def bom(self, connection_with_db_variants):
return sample_bom_complex

def test_impacted_substances(self, bom, connection_with_db_variants):
query = queries.BomImpactedSubstancesQuery().with_bom(bom).with_legislations(LEGISLATIONS)
query = queries.BomImpactedSubstancesQuery().with_bom(bom).with_legislation_ids(LEGISLATIONS)
response = connection_with_db_variants.run(query)

assert response.impacted_substances
Expand All @@ -116,14 +116,16 @@ def connection_missing_db(self, connection):
return connection

def test_missing_database_raises_grantami_exception(self, connection_missing_db):
query = queries.MaterialImpactedSubstancesQuery().with_material_ids(["mat_id"]).with_legislations(LEGISLATIONS)
query = (
queries.MaterialImpactedSubstancesQuery().with_material_ids(["mat_id"]).with_legislation_ids(LEGISLATIONS)
)
with pytest.raises(GrantaMIException) as e:
connection_missing_db.run(query)
assert str(e.value) == "None of the record references resolved to material records in 'MI_Missing_Database'."


def test_missing_table_raises_grantami_exception(connection):
query = queries.BomImpactedSubstancesQuery().with_bom(sample_bom_custom_db).with_legislations(LEGISLATIONS)
query = queries.BomImpactedSubstancesQuery().with_bom(sample_bom_custom_db).with_legislation_ids(LEGISLATIONS)
with pytest.raises(GrantaMIException) as e:
connection.run(query)
assert "Table name" in str(e.value) and "not found in database" in str(e.value)
Expand All @@ -147,7 +149,7 @@ def _run_query(self, connection):
mat_query = (
queries.MaterialImpactedSubstancesQuery()
.with_material_ids([MATERIAL_ID])
.with_legislations([LEGISLATION_ID])
.with_legislation_ids([LEGISLATION_ID])
)
results = connection.run(mat_query)
return results
Expand Down Expand Up @@ -176,7 +178,7 @@ def test_legislation_is_affected_with_link_depth_one(self, connection_custom_db)
query = (
queries.SpecificationImpactedSubstancesQuery()
.with_specification_ids(self.spec_ids)
.with_legislations(self.legislation_ids)
.with_legislation_ids(self.legislation_ids)
)
response = connection_custom_db.run(query)
assert len(response.impacted_substances) == 1
Expand All @@ -194,7 +196,7 @@ def test_legislation_is_not_affected_with_no_links(self, connection_custom_db):
query = (
queries.SpecificationImpactedSubstancesQuery()
.with_specification_ids(self.spec_ids)
.with_legislations(self.legislation_ids)
.with_legislation_ids(self.legislation_ids)
)
response = connection_custom_db.run(query)
assert len(response.impacted_substances) == 0
Expand Down
4 changes: 3 additions & 1 deletion tests/test_mocked_responses/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

class TestImpactedSubstances(BaseMockTester):
query = (
queries.MaterialImpactedSubstancesQuery().with_legislations(["Fake legislation"]).with_material_ids(["Fake ID"])
queries.MaterialImpactedSubstancesQuery()
.with_legislation_ids(["Fake legislation"])
.with_material_ids(["Fake ID"])
)
mock_key = GetImpactedSubstancesForMaterialsResponse.__name__

Expand Down
4 changes: 2 additions & 2 deletions tests/test_mocked_responses/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_critical_error_raises_exception(self, mock_connection, caplog):
def test_non_critical_error_printed_to_stdout(self, mock_connection, severity, caplog):
self.query = (
queries.MaterialImpactedSubstancesQuery()
.with_legislations(["Fake legislation"])
.with_legislation_ids(["Fake legislation"])
.with_material_ids(["Fake ID"])
)
mock_key = GetImpactedSubstancesForMaterialsResponse.__name__
Expand All @@ -45,7 +45,7 @@ def test_non_critical_error_printed_to_stdout(self, mock_connection, severity, c
def test_info(self, mock_connection, caplog):
self.query = (
queries.MaterialImpactedSubstancesQuery()
.with_legislations(["Fake legislation"])
.with_legislation_ids(["Fake legislation"])
.with_material_ids(["Fake ID"])
)
mock_key = GetImpactedSubstancesForMaterialsResponse.__name__
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mocked_responses/test_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TestImpactedSubstances(BaseMockTester):
query = (
queries.PartImpactedSubstancesQuery()
.with_legislations(["Fake legislation"])
.with_legislation_ids(["Fake legislation"])
.with_part_numbers(["Fake part number"])
)
mock_key = GetImpactedSubstancesForPartsResponse.__name__
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mocked_responses/test_specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TestImpactedSubstances(BaseMockTester):
query = (
queries.SpecificationImpactedSubstancesQuery()
.with_legislations(["Fake legislation"])
.with_legislation_ids(["Fake legislation"])
.with_specification_ids(["Fake ID"])
)
mock_key = GetImpactedSubstancesForSpecificationsResponse.__name__
Expand Down
8 changes: 4 additions & 4 deletions tests/test_query_builders/test_common_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_add_indicators_wrong_types_type_error(self, query_type):

def test_add_legislations_attribute_error(self, query_type):
with pytest.raises(AttributeError):
query_type().with_legislations(LEGISLATIONS)
query_type().with_legislation_ids(LEGISLATIONS)


@pytest.mark.parametrize(
Expand All @@ -180,15 +180,15 @@ def test_add_legislations_attribute_error(self, query_type):
)
class TestSubstanceQueries:
def test_add_legislations_correct_types(self, query_type):
query = query_type().with_legislations(LEGISLATIONS)
query = query_type().with_legislation_ids(LEGISLATIONS)
assert len(query._legislations) == len(LEGISLATIONS)
for idx, legislation in enumerate(LEGISLATIONS):
assert query._legislations[idx] == legislation

def test_add_legislations_wrong_types_type_error(self, query_type):
with pytest.raises(TypeError):
query_type().with_legislations(INDICATORS)
query_type().with_legislations(legislations=INDICATORS)
query_type().with_legislation_ids(INDICATORS)
query_type().with_legislation_ids(legislations=INDICATORS)

def test_add_indicators_attribute_error(self, query_type):
with pytest.raises(AttributeError):
Expand Down

0 comments on commit 2cffdc7

Please sign in to comment.