Skip to content

Commit

Permalink
Add depends_on and in_use_by to product block graphql type (#336)
Browse files Browse the repository at this point in the history
* Add depends_on and in_use_by to product block graphql type

* Bump version to 1.2.3rc5

* Fix ruff issues

* add helper function for tests to assert assert no difference

- uses deepdiff with its exclude_paths.
- change product_blocks graphql schema to use list instead of List

---------

Co-authored-by: Tjeerd.Verschragen <tjeerd.verschragen@surf.nl>
  • Loading branch information
tjeerddie and tjeerddie authored Aug 21, 2023
1 parent 6225588 commit e137bd9
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.2.3rc4
current_version = 1.2.3rc5
commit = False
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(rc(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

"""This is the orchestrator workflow engine."""

__version__ = "1.2.3rc4"
__version__ = "1.2.3rc5"

from orchestrator.app import OrchestratorCore
from orchestrator.settings import app_settings, oauth2_settings
Expand Down
12 changes: 10 additions & 2 deletions orchestrator/graphql/schemas/product_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Annotated

import strawberry

Expand All @@ -15,4 +15,12 @@ class ProductBlock:
status: strawberry.auto
created_at: strawberry.auto
end_date: strawberry.auto
resource_types: Optional[list[ResourceType]]
resource_types: list[ResourceType]

@strawberry.field(description="Return all product blocks that this product block depends on") # type: ignore
async def depends_on(self) -> list[Annotated["ProductBlock", strawberry.lazy(".product_block")]]:
return [ProductBlock.from_pydantic(product_block) for product_block in self._original_model.depends_on] # type: ignore

@strawberry.field(description="Return all product blocks that uses this product block") # type: ignore
async def in_use_by(self) -> list[Annotated["ProductBlock", strawberry.lazy(".product_block")]]:
return [ProductBlock.from_pydantic(product_block) for product_block in self._original_model.in_use_by] # type: ignore
2 changes: 1 addition & 1 deletion orchestrator/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def map_value(mapping: dict[str, Callable], k: str, v: Any) -> tuple[Any, ...]:
if f := mapping.get(k):
if v is None:
return k, None
if type(v) == dict:
if isinstance(v, dict):
return result if type(result := f(**v)) is tuple else (k, result)
return result if type(result := f(v)) is tuple else (k, result)
return k, v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def sender(self, websocket: WebSocket, channel: str) -> None:
await websocket.send_text(event.message)

json = json_loads(event.message)
if type(json) is dict and "close" in json and json["close"] and channel != "processes":
if isinstance(json, dict) and "close" in json and json["close"] and channel != "processes":
await self.disconnect(websocket)
break

Expand Down
3 changes: 3 additions & 0 deletions test/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def generic_product_block_1(generic_resource_type_1):
tag="PB1",
status="active",
resource_types=[generic_resource_type_1],
created_at=datetime.datetime.fromisoformat("2023-05-24T00:00:00+00:00"),
)
db.session.add(pb)
db.session.commit()
Expand All @@ -395,6 +396,7 @@ def generic_product_block_2(generic_resource_type_2, generic_resource_type_3):
tag="PB2",
status="active",
resource_types=[generic_resource_type_2, generic_resource_type_3],
created_at=datetime.datetime.fromisoformat("2023-05-24T00:00:00+00:00"),
)
db.session.add(pb)
db.session.commit()
Expand All @@ -409,6 +411,7 @@ def generic_product_block_3(generic_resource_type_2):
tag="PB3",
status="active",
resource_types=[generic_resource_type_2],
created_at=datetime.datetime.fromisoformat("2023-05-24T00:00:00+00:00"),
)
db.session.add(pb)
db.session.commit()
Expand Down
108 changes: 106 additions & 2 deletions test/unit_tests/graphql/test_product_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from fastapi import Response

from test.unit_tests.helpers import assert_no_diff


def get_product_blocks_query(
first: int = 10,
Expand All @@ -16,17 +18,23 @@ def get_product_blocks_query(
productBlocks(first: $first, after: $after, filterBy: $filterBy, sortBy: $sortBy) {
page {
name
productBlockId
endDate
description
createdAt
status
tag
resourceTypes {
resourceTypeId
resourceType
description
}
dependsOn {
name
description
}
inUseBy {
name
description
}
}
pageInfo {
endCursor
Expand Down Expand Up @@ -72,6 +80,102 @@ def test_product_blocks_query(test_client):
"totalItems": 6,
}

expected = [
{
"name": "PB_1",
"endDate": None,
"description": "Generic Product Block 1",
"createdAt": "2023-05-24T00:00:00+00:00",
"status": "ACTIVE",
"tag": "PB1",
"resourceTypes": [{"resourceType": "rt_1", "description": "Resource Type one"}],
"dependsOn": [],
"inUseBy": [],
},
{
"name": "PB_2",
"endDate": None,
"description": "Generic Product Block 2",
"createdAt": "2023-05-24T00:00:00+00:00",
"status": "ACTIVE",
"tag": "PB2",
"resourceTypes": [
{"resourceType": "rt_2", "description": "Resource Type two"},
{"resourceType": "rt_3", "description": "Resource Type three"},
],
"dependsOn": [],
"inUseBy": [],
},
]
assert product_blocks == expected


def test_product_block_query_with_relations(test_client):
data = get_product_blocks_query(filter_by={"field": "name", "value": "ForTest"})
response: Response = test_client.post("/api/graphql", content=data, headers={"Content-Type": "application/json"})

assert HTTPStatus.OK == response.status_code
result = response.json()
product_blocks_data = result["data"]["productBlocks"]
product_blocks = product_blocks_data["page"]
pageinfo = product_blocks_data["pageInfo"]

assert len(product_blocks) == 3

assert pageinfo == {
"hasPreviousPage": False,
"hasNextPage": False,
"startCursor": 0,
"endCursor": 2,
"totalItems": 3,
}

expected = [
{
"name": "SubBlockOneForTest",
"endDate": None,
"description": "Test Sub Block One",
"status": "ACTIVE",
"tag": "TEST",
"resourceTypes": [
{"resourceType": "int_field", "description": ""},
{"resourceType": "str_field", "description": ""},
],
"dependsOn": [],
"inUseBy": [{"name": "ProductBlockWithListUnionForTest", "description": "Test Union Sub Block"}],
},
{
"name": "SubBlockTwoForTest",
"endDate": None,
"description": "Test Sub Block Two",
"status": "ACTIVE",
"tag": "TEST",
"resourceTypes": [{"resourceType": "int_field_2", "description": ""}],
"dependsOn": [],
"inUseBy": [{"name": "ProductBlockWithListUnionForTest", "description": "Test Union Sub Block"}],
},
{
"name": "ProductBlockWithListUnionForTest",
"endDate": None,
"description": "Test Union Sub Block",
"status": "ACTIVE",
"tag": "TEST",
"resourceTypes": [
{"resourceType": "int_field", "description": ""},
{"resourceType": "str_field", "description": ""},
{"resourceType": "list_field", "description": ""},
],
"dependsOn": [
{"name": "SubBlockOneForTest", "description": "Test Sub Block One"},
{"name": "SubBlockTwoForTest", "description": "Test Sub Block Two"},
],
"inUseBy": [],
},
]

exclude_paths = exclude_paths = [f"root[{i}]['createdAt']" for i in range(len(expected))]
assert_no_diff(expected, product_blocks, exclude_paths=exclude_paths)


def test_product_blocks_has_previous_page(test_client):
data = get_product_blocks_query(after=1)
Expand Down
10 changes: 10 additions & 0 deletions test/unit_tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json

from deepdiff import DeepDiff


def assert_no_diff(expected, actual, exclude_paths=None):
diff = DeepDiff(expected, actual, ignore_order=True, exclude_paths=exclude_paths)
prettydiff = f"Difference: {json.dumps(diff, indent=2, default=lambda x: str(x))}"

assert diff == {}, f"Difference between expected and actual output\n{prettydiff}"

0 comments on commit e137bd9

Please sign in to comment.