diff --git a/examples/spec/get_and_query_specs.py b/examples/spec/get_and_query_specs.py index 144a2880..c112f832 100644 --- a/examples/spec/get_and_query_specs.py +++ b/examples/spec/get_and_query_specs.py @@ -6,6 +6,7 @@ ConditionType, CreateSpecificationsRequest, CreateSpecificationsRequestObject, + MeasurementMappingItem, NumericConditionValue, QuerySpecificationsRequest, SpecificationLimit, @@ -44,6 +45,12 @@ name="input voltage", limit=SpecificationLimit(min=0.02, max=0.15), unit="mV", + measurement_mapping=[ + MeasurementMappingItem( + filter='data.parameters.Any(it["name"] == "Input Voltage")', + result_filter='partNumber == "VR1234"', + ) + ], conditions=[ Condition( name="Temperature", diff --git a/examples/spec/update_and_delete_specs.py b/examples/spec/update_and_delete_specs.py index 17ca80b0..4e4202cb 100644 --- a/examples/spec/update_and_delete_specs.py +++ b/examples/spec/update_and_delete_specs.py @@ -3,6 +3,7 @@ from nisystemlink.clients.core import HttpConfiguration from nisystemlink.clients.spec import SpecClient from nisystemlink.clients.spec.models import ( + MeasurementMappingItem, QuerySpecificationsRequest, SpecificationType, UpdateSpecificationsRequest, @@ -24,18 +25,22 @@ # The query and delete examples assume you have created the specs from the query_specs example product = "Amplifier" -# update spec1 to change the block to "modifiedBlock" -# query the original spec +# update spec1 to change the block to "modifiedBlock" and spec2 to set a measurement mapping, +# both in a single request +# query the original specs +spec_filter = 'specId == "spec1" || specId == "spec2"' response = client.query_specs( - QuerySpecificationsRequest(product_ids=[product], filter='specId == "spec1"') + QuerySpecificationsRequest(product_ids=[product], filter=spec_filter) ) -if response.specs: - original_spec1 = response.specs[0] +original_specs = {spec.spec_id: spec for spec in response.specs or []} +if "spec1" in original_specs and "spec2" in original_specs: + original_spec1 = original_specs["spec1"] + original_spec2 = original_specs["spec2"] print(f"Original spec1 block: {original_spec1.block}") print(f"Original spec1 version: {original_spec1.version}") # make the modifications - modified_spec = UpdateSpecificationsRequestObject( + modified_spec1 = UpdateSpecificationsRequestObject( id=cast(str, original_spec1.id), product_id=cast(str, original_spec1.product_id), spec_id=cast(str, original_spec1.spec_id), @@ -45,19 +50,38 @@ version=cast(int, original_spec1.version), workspace=cast(str, original_spec1.workspace), ) + modified_spec2 = UpdateSpecificationsRequestObject( + id=cast(str, original_spec2.id), + product_id=cast(str, original_spec2.product_id), + spec_id=cast(str, original_spec2.spec_id), + type=SpecificationType.PARAMETRIC, + measurement_mapping=[ + MeasurementMappingItem( + filter='data.parameters.Any(it["name"] == "Input Voltage")', + result_filter='partNumber == "VR5678"', + ) + ], + version=cast(int, original_spec2.version), + workspace=cast(str, original_spec2.workspace), + ) update_response = client.update_specs( - specs=UpdateSpecificationsRequest(specs=[modified_spec]) + specs=UpdateSpecificationsRequest(specs=[modified_spec1, modified_spec2]) ) if update_response and update_response.updated_specs: - print(f"New spec1 version: {update_response.updated_specs[0].version}") + for updated_spec in update_response.updated_specs: + print(f"New {updated_spec.spec_id} version: {updated_spec.version}") -# query again to see new version +# query again to see the new versions response = client.query_specs( - QuerySpecificationsRequest(product_ids=[product], filter='specId == "spec1"') + QuerySpecificationsRequest(product_ids=[product], filter=spec_filter) ) -if response.specs: - original_spec1 = response.specs[0] - print(f"Modified spec1 block: {original_spec1.block}") +modified_specs = {spec.spec_id: spec for spec in response.specs or []} +if "spec1" in modified_specs and "spec2" in modified_specs: + print(f"Modified spec1 block: {modified_specs['spec1'].block}") + print( + "Modified spec2 measurement mapping: " + f"{modified_specs['spec2'].measurement_mapping}" + ) # delete all the specs for the product # query all specs diff --git a/nisystemlink/clients/spec/models/__init__.py b/nisystemlink/clients/spec/models/__init__.py index 1a88b150..7f66f1b3 100644 --- a/nisystemlink/clients/spec/models/__init__.py +++ b/nisystemlink/clients/spec/models/__init__.py @@ -20,6 +20,7 @@ SpecificationProjection, ) from ._specification import ( + MeasurementMappingItem, Specification, SpecificationDefinition, SpecificationLimit, @@ -49,6 +50,7 @@ "PagedSpecifications", "SpecificationOrderBy", "SpecificationProjection", + "MeasurementMappingItem", "Specification", "SpecificationDefinition", "SpecificationLimit", diff --git a/nisystemlink/clients/spec/models/_query_specs.py b/nisystemlink/clients/spec/models/_query_specs.py index 024739fa..9ea8c860 100644 --- a/nisystemlink/clients/spec/models/_query_specs.py +++ b/nisystemlink/clients/spec/models/_query_specs.py @@ -33,6 +33,7 @@ class SpecificationProjection(str, Enum): CREATED_AT = "CREATED_AT" CREATED_BY = "CREATED_BY" UPDATED_AT = "UPDATED_AT" + MEASUREMENT_MAPPING = "MEASUREMENT_MAPPING" class SpecificationOrderBy(Enum): @@ -82,6 +83,8 @@ class QuerySpecificationsRequest(JsonModel): - `createdAt`: ISO-8601 formatted UTC timestamp indicating when the specification was created. - `updatedBy`: String representing the ID of the user who updated the specification. - `updatedAt`: ISO-8601 formatted UTC timestamp indicating when the specification was updated. + - `measurementMapping`: Query filters used to associate the specification with its measurement + data. See [Dynamic Linq](https://github.com/ni/systemlink-OpenAPI-documents/wiki/Dynamic-Linq-Query-Language) documentation for more details. diff --git a/nisystemlink/clients/spec/models/_specification.py b/nisystemlink/clients/spec/models/_specification.py index d1d13f75..7729ea4f 100644 --- a/nisystemlink/clients/spec/models/_specification.py +++ b/nisystemlink/clients/spec/models/_specification.py @@ -40,6 +40,24 @@ class SpecificationType(Enum): """Functional specs only have pass/fail status.""" +class MeasurementMappingItem(JsonModel): + """Filters that map a specification to its measurement data.""" + + filter: str | None = None + """Query string specifying the filter criteria for selecting test steps mapped to the + specification. + + Uses the Dynamic LINQ format of the Test Monitor query-steps API filter. + """ + + result_filter: str | None = None + """Query string specifying the filter criteria for selecting test results containing steps + mapped to the specification. + + Uses the Dynamic LINQ format of the Test Monitor query-steps API resultFilter. + """ + + class SpecificationDefinition(JsonModel): product_id: str | None = None @@ -84,6 +102,9 @@ class SpecificationDefinition(JsonModel): properties: Dict[str, str] | None = None """Additional properties associated with the specification.""" + measurement_mapping: List[MeasurementMappingItem] | None = None + """Query filters used to associate the specification with its measurement data.""" + workspace: str | None = None """Id of the workspace to which the specification will be associated. diff --git a/nisystemlink/clients/spec/utilities/__init__.py b/nisystemlink/clients/spec/utilities/__init__.py index 1148e217..c5764fdb 100644 --- a/nisystemlink/clients/spec/utilities/__init__.py +++ b/nisystemlink/clients/spec/utilities/__init__.py @@ -3,6 +3,9 @@ summarize_conditions_as_a_string, normalize_conditions_per_column, normalize_conditions_per_row, + summarize_measurement_mappings_as_a_string, + normalize_measurement_mappings_per_column, + normalize_measurement_mappings_per_row, ) # flake8: noqa diff --git a/nisystemlink/clients/spec/utilities/_dataframe_utilities.py b/nisystemlink/clients/spec/utilities/_dataframe_utilities.py index c2be3e5d..9cdab8c6 100644 --- a/nisystemlink/clients/spec/utilities/_dataframe_utilities.py +++ b/nisystemlink/clients/spec/utilities/_dataframe_utilities.py @@ -7,6 +7,7 @@ StringConditionValue, ) from nisystemlink.clients.spec.models._specification import ( + MeasurementMappingItem, Specification, SpecificationLimit, SpecificationType, @@ -92,11 +93,89 @@ def normalize_conditions_per_row( ] +def summarize_measurement_mappings_as_a_string( + measurement_mappings: List[MeasurementMappingItem], +) -> List[Dict[str, str]]: + """Converts the measurement mappings to an easily readable string format. + + Args: + measurement_mappings: List of all measurement mappings in a spec. + + Returns: + A list with a single dictionary. The key will be "measurement_mapping" and the value + will be "[filter: ; result_filter: ], [filter: ]" where each + bracketed group is a mapping item. Fields that are None are left out and items + without any field are skipped. The list is empty if there is nothing to summarize. + """ + summaries = [] + for item in measurement_mappings: + fields = __serialize_measurement_mapping_fields(item) + if fields: + summaries.append( + "[" + + "; ".join(f"{key}: {value}" for key, value in fields.items()) + + "]" + ) + + return [{"measurement_mapping": ", ".join(summaries)}] if summaries else [] + + +def normalize_measurement_mappings_per_column( + measurement_mappings: List[MeasurementMappingItem], +) -> List[Dict[str, Any]]: + """Convert measurement mappings into a single dictionary where each mapping item is + split into indexed keys. + + Args: + measurement_mappings: List of all measurement mappings in a spec. + + Returns: + A list with a single dictionary. The keys will be + "measurement_mapping[].filter" and "measurement_mapping[].result_filter" + and the values will be the corresponding mapping item field. Fields that are None + are left out. The list is empty if no mapping item has a value. + """ + columns = { + f"measurement_mapping[{index}].{key}": value + for index, item in enumerate(measurement_mappings) + for key, value in __serialize_measurement_mapping_fields(item).items() + } + + return [columns] if columns else [] + + +def normalize_measurement_mappings_per_row( + measurement_mappings: List[MeasurementMappingItem], +) -> List[Dict[str, Any]]: + """Convert measurement mappings into list of dictionaries where dictionary keys will be + 'measurement_mapping.filter' and 'measurement_mapping.result_filter'. + + Args: + measurement_mappings: List of all measurement mappings in a spec. + + Returns: + A list of dictionaries, one for each mapping item that has a value. Fields that + are None are left out. Each mapping item will be logged as separate dictionary + entry in the list which translates to separate row in the dataframe. + """ + return [ + {f"measurement_mapping.{key}": value for key, value in fields.items()} + for fields in ( + __serialize_measurement_mapping_fields(item) + for item in measurement_mappings + ) + if fields + ] + + def convert_specs_to_dataframe( specs: List[Specification], condition_format: ( Callable[[List[Condition]], List[Dict[str, Any]]] | None ) = normalize_conditions_per_column, + measurement_mapping_format: ( + Callable[[List[MeasurementMappingItem]], List[Dict[str, Any]]] | None + ) = normalize_measurement_mappings_per_column, ) -> pd.DataFrame: """Creates a Pandas DataFrame for the specs. @@ -120,23 +199,52 @@ def convert_specs_to_dataframe( If condition value is needed as a string summary of condition data, the public method `summarize_conditions_as_a_string` can be provided as this callback function. If None is passed, conditions will not be included in the dataframe. + measurement_mapping_format: A callback function which takes in a list of measurement mapping + items of a spec and returns a list of dictionary of measurement mapping + data. Dataframe rows will be constructed based on these list of + dictionaries. Each dictionary in the list indicates a row. If there is + more than one dictionary in the list, it will be considered as a new + row and other spec column data will be duplicated. Keys will be used as + the dataframe column header and values will be used as the row cells + for the respective column header. + If not passed, each mapping item will be split into the indexed columns + `measurement_mapping[].filter` and + `measurement_mapping[].result_filter`. + If measurement mapping is needed as one row per mapping item, the public + method `normalize_measurement_mappings_per_row` can be provided as the + callback function. + If measurement mapping is needed as a string summary, the public method + `summarize_measurement_mappings_as_a_string` can be provided as this + callback function. + If None is passed, measurement mappings will not be included in the dataframe. Returns: A Pandas DataFrame with the each spec fields having a separate column. Following fields are split into sub-columns. - conditions: format of the condition columns are decided by the `condition_format` argument of this function. + - measurement_mapping: format of the measurement mapping columns are decided by the + `measurement_mapping_format` argument of this function. - Properties: All the unique properties across all specs will be split into separate columns. For example, properties.property1, properties.property2, etc. """ specs_dict = [ - __convert_spec_to_dict(spec=spec, condition=condition) + __convert_spec_to_dict( + spec=spec, + condition=condition, + measurement_mapping=measurement_mapping, + ) for spec in specs for condition in ( condition_format(spec.conditions) if (spec.conditions and condition_format) else [{}] ) + for measurement_mapping in ( + (measurement_mapping_format(spec.measurement_mapping) or [{}]) + if (spec.measurement_mapping and measurement_mapping_format) + else [{}] + ) ] specs_dataframe = pd.json_normalize(specs_dict) @@ -147,29 +255,54 @@ def convert_specs_to_dataframe( def __convert_spec_to_dict( - spec: Specification, condition: Dict[str, Any] + spec: Specification, + condition: Dict[str, Any], + measurement_mapping: Dict[str, Any], ) -> Dict[str, Any]: """Converts a spec into dictionary. Args: spec: Spec object. condition: Condition as a dictionary which is added to the output spec dictionary. + measurement_mapping: Measurement mapping as a dictionary which is added to the output spec dictionary. Returns: - Spec as a dictionary with the provided condition dictionary included. + Spec as a dictionary with the provided condition and measurement mapping dictionaries included. """ return { **{ key: value for key, value in vars(spec).items() - if key not in ["type", "limit", "conditions"] + if key not in ["type", "limit", "conditions", "measurement_mapping"] }, **(__serialize_type(spec.type) if spec.type else {}), **(__serialize_limits(spec.limit) if spec.limit else {}), + **{key: value for key, value in measurement_mapping.items()}, **{key: value for key, value in condition.items()}, } +def __serialize_measurement_mapping_fields( + measurement_mapping: MeasurementMappingItem, +) -> Dict[str, str]: + """Get the fields of a measurement mapping item that have a value. + + Args: + measurement_mapping: Measurement mapping item of a spec. + + Returns: + Dictionary with the keys "filter" and "result_filter" when their values are not None. + """ + return { + key: value + for key, value in ( + ("filter", measurement_mapping.filter), + ("result_filter", measurement_mapping.result_filter), + ) + if value is not None + } + + def __serialize_limits(limit: SpecificationLimit) -> Dict[str, str]: """Serialize limit into limit.min, limit.typical and limit.max. diff --git a/tests/integration/spec/test_spec.py b/tests/integration/spec/test_spec.py index d9cff32d..cad97a8c 100644 --- a/tests/integration/spec/test_spec.py +++ b/tests/integration/spec/test_spec.py @@ -13,6 +13,7 @@ CreateSpecificationsPartialSuccess, CreateSpecificationsRequest, CreateSpecificationsRequestObject, + MeasurementMappingItem, NumericConditionValue, QuerySpecificationsRequest, SpecificationLimit, @@ -71,6 +72,12 @@ def create_specs_for_query(create_specs, product): name="output voltage", limit=SpecificationLimit(min=1.2, max=1.5), unit="mV", + measurement_mapping=[ + MeasurementMappingItem( + filter='data.parameters.Any(it["name"] == "Output Voltage")', + result_filter='partNumber == "VR1234"', + ) + ], ), CreateSpecificationsRequestObject( product_id=product, @@ -208,7 +215,7 @@ def test__update_single_same_version__version_updates( spec = CreateSpecificationsRequestObject( product_id=product, spec_id="spec1", - type=SpecificationType.FUNCTIONAL, + type=SpecificationType.PARAMETRIC, keywords=["work", "reviewed"], category="Parametric Specs", block="newBlock", @@ -223,9 +230,12 @@ def test__update_single_same_version__version_updates( id=created_spec.id, product_id=created_spec.product_id, spec_id=created_spec.spec_id, - type=SpecificationType.FUNCTIONAL, + type=SpecificationType.PARAMETRIC, keywords=["work", "reveiwed"], block="modifiedBlock", + measurement_mapping=[ + MeasurementMappingItem(filter="mapped == true", result_filter=None) + ], version=created_spec.version, workspace=created_spec.workspace, ) @@ -239,6 +249,59 @@ def test__update_single_same_version__version_updates( updated_spec = update_response.updated_specs[0] assert updated_spec.version == 1 + stored_spec = client.get_spec(created_spec.id) + assert stored_spec.measurement_mapping == [ + MeasurementMappingItem(filter="mapped == true") + ] + + def test__update_spec_measurement_mapping__omitted_is_kept_and_none_clears( + self, client: SpecClient, create_specs, product + ): + mapping = [MeasurementMappingItem(filter="mapped == true")] + response = create_specs( + CreateSpecificationsRequest( + specs=[ + CreateSpecificationsRequestObject( + product_id=product, + spec_id=uuid.uuid1().hex, + type=SpecificationType.PARAMETRIC, + measurement_mapping=mapping, + ) + ] + ) + ) + assert response.created_specs + created_spec = response.created_specs[0] + + def update(**fields) -> int: + update_response = client.update_specs( + UpdateSpecificationsRequest( + specs=[ + UpdateSpecificationsRequestObject( + id=created_spec.id, + product_id=created_spec.product_id, + spec_id=created_spec.spec_id, + type=SpecificationType.PARAMETRIC, + version=version, + workspace=created_spec.workspace, + **fields, + ) + ] + ) + ) + assert update_response is not None + assert update_response.updated_specs + return update_response.updated_specs[0].version + + version = created_spec.version + version = update(block="otherBlock") + kept_spec = client.get_spec(created_spec.id) + assert kept_spec.measurement_mapping == mapping + + version = update(measurement_mapping=None) + cleared_spec = client.get_spec(created_spec.id) + assert cleared_spec.measurement_mapping is None + def test__get_spec_by_id__spec_matches_expected( self, client: SpecClient, create_specs, product ): @@ -246,10 +309,15 @@ def test__get_spec_by_id__spec_matches_expected( spec = CreateSpecificationsRequestObject( product_id=productId, spec_id="spec1", - type=SpecificationType.FUNCTIONAL, + type=SpecificationType.PARAMETRIC, keywords=["work", "reviewed"], category="Parametric Specs", block="newBlock", + measurement_mapping=[ + MeasurementMappingItem( + filter="mapped == true", result_filter='partNumber == "VR1234"' + ) + ], ) response = create_specs(CreateSpecificationsRequest(specs=[spec])) @@ -263,6 +331,7 @@ def test__get_spec_by_id__spec_matches_expected( assert get_spec_response is not None assert get_spec_response.id == created_spec.id assert get_spec_response.product_id == productId + assert get_spec_response.measurement_mapping == spec.measurement_mapping def test__get_non_existant_spec_by_id__get_spec_fails(self, client: SpecClient): non_existant_spec_id = "10" @@ -301,6 +370,25 @@ def test__query_spec_category_one_returned( assert response.specs assert len(response.specs) == 1 + def test__query_spec_measurement_mapping_filter__only_mapped_spec_returned( + self, client: SpecClient, create_specs, create_specs_for_query, product + ): + request = QuerySpecificationsRequest( + product_ids=[product], + filter='measurementMapping.Any(m => m.resultFilter.Contains("VR1234"))', + ) + response = client.query_specs(request) + assert response.specs + assert len(response.specs) == 1 + assert response.specs[0].name == "output voltage" + + request = QuerySpecificationsRequest( + product_ids=[product], filter="!measurementMapping.Any()" + ) + response = client.query_specs(request) + assert response.specs + assert len(response.specs) == 2 + def test__query_input_voltage__conditions_match( self, client: SpecClient, create_specs, create_specs_for_query, product ): @@ -323,6 +411,7 @@ def test__query_spec_projection_columns__columns_returned( SpecificationProjection.SPEC_ID, SpecificationProjection.NAME, SpecificationProjection.UPDATED_AT, + SpecificationProjection.MEASUREMENT_MAPPING, ], ) @@ -334,10 +423,11 @@ def test__query_spec_projection_columns__columns_returned( assert response.specs assert len(response.specs) == 3 - assert len(spec_columns) == 3 + assert len(spec_columns) == 4 assert "spec_id" in spec_columns assert "name" in spec_columns assert "updated_at" in spec_columns + assert "measurement_mapping" in spec_columns def test__query_specs__returns_condition_value_type_correctly( self, client: SpecClient, create_specs, create_specs_for_query, product diff --git a/tests/spec/test_spec_dataframe_utilitites.py b/tests/spec/test_spec_dataframe_utilitites.py index 25eb7fc0..9955cd0e 100644 --- a/tests/spec/test_spec_dataframe_utilitites.py +++ b/tests/spec/test_spec_dataframe_utilitites.py @@ -12,6 +12,7 @@ StringConditionValue, ) from nisystemlink.clients.spec.models._specification import ( + MeasurementMappingItem, Specification, SpecificationLimit, SpecificationType, @@ -20,7 +21,10 @@ from nisystemlink.clients.spec.utilities._dataframe_utilities import ( normalize_conditions_per_column, normalize_conditions_per_row, + normalize_measurement_mappings_per_column, + normalize_measurement_mappings_per_row, summarize_conditions_as_a_string, + summarize_measurement_mappings_as_a_string, ) # Python datetime fixtures infer nanoseconds in pandas 2 and microseconds in pandas 3. @@ -65,6 +69,16 @@ def specs() -> List[Specification]: ], keywords=["Test specification only", "First"], properties={"Comments": "comma separated with unicode"}, + measurement_mapping=[ + MeasurementMappingItem( + filter='data.parameters.Any(it["name"] == "Output Voltage")', + result_filter='partNumber == "VR1234"', + ), + MeasurementMappingItem( + filter='data.parameters.Any(it["name"] == "Input Voltage")', + result_filter='partNumber == "VR5678"', + ), + ], workspace=uuid.uuid1().hex, id=uuid.uuid1().hex, created_at=datetime(2024, 3, 28, 13, 59, 12, 744000, tzinfo=timezone.utc), @@ -104,6 +118,9 @@ def specs() -> List[Specification]: ), ), ], + measurement_mapping=[ + MeasurementMappingItem(result_filter='programName == "Production Test"') + ], ), Specification( product_id=uuid.uuid1().hex, @@ -122,6 +139,28 @@ def specs() -> List[Specification]: return specs +@pytest.fixture(scope="class") +def measurement_mapping_specs() -> List[Specification]: + """Two specs covering the measurement mapping cases. + + S1 has a full item, filter only, empty item, result filter only and empty strings. + S2 has only an empty item. + """ + return [ + Specification( + spec_id="S1", + measurement_mapping=[ + MeasurementMappingItem(filter="F1", result_filter="R1"), + MeasurementMappingItem(filter="F2"), + MeasurementMappingItem(), + MeasurementMappingItem(result_filter="R4"), + MeasurementMappingItem(filter="", result_filter=""), + ], + ), + Specification(spec_id="S2", measurement_mapping=[MeasurementMappingItem()]), + ] + + @pytest.mark.unit class TestSpecDataframeUtilities: def test__convert_specs_to_dataframe_with_summarize_conditions__returns_specs_dataframe_with_string_conditions( @@ -275,6 +314,77 @@ def test__convert_specs_to_dataframe_without_condition_values__returns_specs_dat assert not specs_df.empty pd.testing.assert_frame_equal(specs_df, expected_specs_df, check_dtype=True) + @pytest.mark.parametrize( + "measurement_mapping_format, expected_spec_ids, expected_columns", + [ + ( + normalize_measurement_mappings_per_column, + ["S1", "S2"], + { + "measurement_mapping[0].filter": ["F1", float("nan")], + "measurement_mapping[0].result_filter": ["R1", float("nan")], + "measurement_mapping[1].filter": ["F2", float("nan")], + "measurement_mapping[3].result_filter": ["R4", float("nan")], + "measurement_mapping[4].filter": ["", float("nan")], + "measurement_mapping[4].result_filter": ["", float("nan")], + }, + ), + ( + normalize_measurement_mappings_per_row, + ["S1", "S1", "S1", "S1", "S2"], + { + "measurement_mapping.filter": [ + "F1", + "F2", + float("nan"), + "", + float("nan"), + ], + "measurement_mapping.result_filter": [ + "R1", + float("nan"), + "R4", + "", + float("nan"), + ], + }, + ), + ( + summarize_measurement_mappings_as_a_string, + ["S1", "S2"], + { + "measurement_mapping": [ + "[filter: F1; result_filter: R1], [filter: F2], " + "[result_filter: R4], [filter: ; result_filter: ]", + float("nan"), + ], + }, + ), + (None, ["S1", "S2"], {}), + ( + lambda items: [{"mapped_filters": [item.filter for item in items]}], + ["S1", "S2"], + {"mapped_filters": [["F1", "F2", None, None, ""], [None]]}, + ), + ], + ids=["per_column", "per_row", "summary", "none", "custom"], + ) + def test__convert_specs_to_dataframe_with_measurement_mapping_format__returns_expected_dataframe( + self, + measurement_mapping_specs, + measurement_mapping_format, + expected_spec_ids, + expected_columns, + ): + expected_df = pd.DataFrame({"spec_id": expected_spec_ids, **expected_columns}) + + specs_df = convert_specs_to_dataframe( + specs=measurement_mapping_specs, + measurement_mapping_format=measurement_mapping_format, + ) + + pd.testing.assert_frame_equal(specs_df, expected_df, check_dtype=True) + def test__convert_specs_to_dataframe_with_condition_per_column__returns_dataframe_with_condition_per_column( self, specs ): @@ -623,6 +733,17 @@ def __expected_specs_dataframe( "limit.min": spec.limit.min if spec.limit else None, "limit.typical": spec.limit.typical if spec.limit else None, "limit.max": spec.limit.max if spec.limit else None, + **{ + f"measurement_mapping[{mapping_index}].{key}": value + for mapping_index, mapping in enumerate( + spec.measurement_mapping or [] + ) + for key, value in ( + ("filter", mapping.filter), + ("result_filter", mapping.result_filter), + ) + if value is not None + }, }, **{ key: float("nan") if value is None else value