diff --git a/nisystemlink/clients/spec/models/__init__.py b/nisystemlink/clients/spec/models/__init__.py index b30499ec..7656ced6 100644 --- a/nisystemlink/clients/spec/models/__init__.py +++ b/nisystemlink/clients/spec/models/__init__.py @@ -16,6 +16,7 @@ from ._query_specs import ( QuerySpecificationsRequest, PagedSpecifications, + SpecificationOrderBy, SpecificationProjection, ) from ._specification import ( diff --git a/nisystemlink/clients/spec/models/_query_specs.py b/nisystemlink/clients/spec/models/_query_specs.py index 1d827b99..024739fa 100644 --- a/nisystemlink/clients/spec/models/_query_specs.py +++ b/nisystemlink/clients/spec/models/_query_specs.py @@ -32,6 +32,7 @@ class SpecificationProjection(str, Enum): WORKSPACE = "WORKSPACE" CREATED_AT = "CREATED_AT" CREATED_BY = "CREATED_BY" + UPDATED_AT = "UPDATED_AT" class SpecificationOrderBy(Enum): @@ -39,6 +40,7 @@ class SpecificationOrderBy(Enum): ID = "ID" SPEC_ID = "SPEC_ID" + UPDATED_AT = "UPDATED_AT" class QuerySpecificationsRequest(JsonModel): diff --git a/tests/integration/spec/test_spec.py b/tests/integration/spec/test_spec.py index a1c0d470..d9cff32d 100644 --- a/tests/integration/spec/test_spec.py +++ b/tests/integration/spec/test_spec.py @@ -16,6 +16,7 @@ NumericConditionValue, QuerySpecificationsRequest, SpecificationLimit, + SpecificationOrderBy, SpecificationProjection, SpecificationType, StringConditionValue, @@ -318,7 +319,11 @@ def test__query_spec_projection_columns__columns_returned( ): request = QuerySpecificationsRequest( product_ids=[product], - projection=[SpecificationProjection.SPEC_ID, SpecificationProjection.NAME], + projection=[ + SpecificationProjection.SPEC_ID, + SpecificationProjection.NAME, + SpecificationProjection.UPDATED_AT, + ], ) response = client.query_specs(request) @@ -329,9 +334,10 @@ def test__query_spec_projection_columns__columns_returned( assert response.specs assert len(response.specs) == 3 - assert len(spec_columns) == 2 + assert len(spec_columns) == 3 assert "spec_id" in spec_columns assert "name" in spec_columns + assert "updated_at" in spec_columns def test__query_specs__returns_condition_value_type_correctly( self, client: SpecClient, create_specs, create_specs_for_query, product @@ -405,3 +411,71 @@ def test__without_condition_type_projection__query_specs__condition_type_field_i assert "condition_name" in spec_columns assert "condition_unit" in spec_columns assert "condition_type" not in spec_columns + + @pytest.mark.parametrize( + "order_by_descending, expected_first, expected_second", + [ + (False, "spec_2_id", "spec_1_id"), + (True, "spec_1_id", "spec_2_id"), + ], + ) + def test__query_specs_order_by_updated_at__returns_in_expected_order( + self, + client: SpecClient, + create_specs, + product, + order_by_descending: bool, + expected_first: str, + expected_second: str, + ): + # Create two specs, then update the first so it has a later updated_at than the second. + spec_1_id = uuid.uuid1().hex + spec_2_id = uuid.uuid1().hex + response = create_specs( + CreateSpecificationsRequest( + specs=[ + CreateSpecificationsRequestObject( + product_id=product, + spec_id=spec_1_id, + type=SpecificationType.FUNCTIONAL, + ), + CreateSpecificationsRequestObject( + product_id=product, + spec_id=spec_2_id, + type=SpecificationType.FUNCTIONAL, + ), + ] + ) + ) + spec_1 = next( + spec for spec in response.created_specs if spec.spec_id == spec_1_id + ) + update_response = client.update_specs( + UpdateSpecificationsRequest( + specs=[ + UpdateSpecificationsRequestObject( + id=spec_1.id, + product_id=spec_1.product_id, + spec_id=spec_1.spec_id, + type=SpecificationType.PARAMETRIC, + version=spec_1.version, + workspace=spec_1.workspace, + ) + ] + ) + ) + assert update_response + assert update_response.updated_specs + assert len(update_response.updated_specs) == 1 + + request = QuerySpecificationsRequest( + product_ids=[product], + order_by=SpecificationOrderBy.UPDATED_AT, + order_by_descending=order_by_descending, + ) + response = client.query_specs(request) + + ids = {"spec_1_id": spec_1_id, "spec_2_id": spec_2_id} + assert response.specs + assert response.specs[0].spec_id == ids[expected_first] + assert response.specs[1].spec_id == ids[expected_second]