Skip to content

Commit b940116

Browse files
saket-geantkvklink
andauthored
Rename all_pb_names to all_product_block_names and refactor related logic (#968)
* Rename all_pb_names to all_product_block_names and refactor related logic for clarity * Update unit test for nested product blocks * Expand recursive product block test case --------- Co-authored-by: Karel van Klink <[email protected]>
1 parent 3150434 commit b940116

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

orchestrator/graphql/schemas/product.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,20 @@ async def subscriptions(
5252
return await resolve_subscriptions(info, filter_by_with_related_subscriptions, sort_by, first, after)
5353

5454
@strawberry.field(description="Returns list of all nested productblock names") # type: ignore
55-
async def all_pb_names(self) -> list[str]:
56-
55+
async def all_product_block_names(self) -> list[str]:
5756
model = get_original_model(self, ProductTable)
5857

59-
def get_all_pb_names(product_blocks: list[ProductBlockTable]) -> Iterable[str]:
58+
def get_names(product_blocks: list[ProductBlockTable], visited: set) -> Iterable[str]:
6059
for product_block in product_blocks:
60+
if product_block.product_block_id in visited:
61+
continue
62+
visited.add(product_block.product_block_id)
6163
yield product_block.name
62-
6364
if product_block.depends_on:
64-
yield from get_all_pb_names(product_block.depends_on)
65-
66-
names: list[str] = list(get_all_pb_names(model.product_blocks))
67-
names.sort()
65+
yield from get_names(product_block.depends_on, visited)
6866

69-
return names
67+
names = set(get_names(model.product_blocks, set()))
68+
return sorted(names)
7069

7170
@strawberry.field(description="Return product blocks") # type: ignore
7271
async def product_blocks(self) -> list[Annotated["ProductBlock", strawberry.lazy(".product_block")]]:

test/unit_tests/fixtures/products/product_blocks/product_block_one_nested.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_product_block_one_nested_db_in_use_by_block(resource_type_list, resourc
4848
name="ProductBlockOneNestedForTest", description="Test Block Parent", tag="TEST", status="active"
4949
)
5050
in_use_by_block.resource_types = [resource_type_int]
51+
in_use_by_block.in_use_by = [in_use_by_block]
5152

5253
db.session.add(in_use_by_block)
5354
db.session.commit()

test/unit_tests/fixtures/products/product_types/product_type_one_nested.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ class ProductTypeOneNestedForTest(
3636

3737

3838
@pytest.fixture
39-
def test_product_one_nested(test_product_block_one_nested_db_in_use_by_block):
39+
def test_product_one_nested(test_product_block_one_nested_db_in_use_by_block, generic_product_block_chain):
4040
product = ProductTable(
4141
name="TestProductOneNested", description="Test ProductTable", product_type="Test", tag="TEST", status="active"
4242
)
4343

4444
fixed_input = FixedInputTable(name="test_fixed_input", value="False")
4545

46-
product_block = test_product_block_one_nested_db_in_use_by_block
46+
pb_1 = test_product_block_one_nested_db_in_use_by_block
47+
pb_2, pb_3 = generic_product_block_chain
4748
product.fixed_inputs = [fixed_input]
48-
product.product_blocks = [product_block]
49+
product.product_blocks = [pb_1, pb_2, pb_3]
4950

5051
db.session.add(product)
5152
db.session.commit()

test/unit_tests/graphql/test_product.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_all_product_names_query(
8585
query ProductQuery($filterBy: [GraphqlFilter!]) {
8686
products(filterBy: $filterBy) {
8787
page {
88-
allPbNames
88+
allProductBlockNames
8989
}
9090
pageInfo {
9191
endCursor
@@ -226,18 +226,18 @@ def test_product_query(
226226
}
227227

228228

229-
def test_all_product_block_names(test_client, generic_product_4):
230-
filter_by = {"filter_by": {"field": "name", "value": "Product 4"}}
229+
def test_all_product_block_names(test_client, test_product_one_nested):
230+
filter_by = {"filter_by": {"field": "name", "value": "TestProductOneNested"}}
231231
data = get_all_product_names_query(**filter_by)
232232
response: Response = test_client.post("/api/graphql", content=data, headers={"Content-Type": "application/json"})
233233

234234
assert HTTPStatus.OK == response.status_code
235235
result = response.json()
236236
products_data = result["data"]["products"]
237237
products = products_data["page"]
238-
names = products[0]["allPbNames"]
238+
names = products[0]["allProductBlockNames"]
239239

240-
assert len(names) == 2
240+
assert names == ["PB_Chained_1", "PB_Chained_2", "ProductBlockOneNestedForTest"]
241241

242242

243243
def test_product_has_previous_page(test_client, generic_product_1, generic_product_2, generic_product_3):

0 commit comments

Comments
 (0)