Skip to content

Commit 67072ef

Browse files
committed
add filters to SDK client count method
1 parent f883e36 commit 67072ef

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added possibility to use filters for the SDK client's count method

infrahub_sdk/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,21 @@ async def count(
547547
at: Timestamp | None = None,
548548
branch: str | None = None,
549549
timeout: int | None = None,
550+
**kwargs: Any,
550551
) -> int:
551552
"""Return the number of nodes of a given kind."""
553+
filters = kwargs
552554
schema = await self.schema.get(kind=kind, branch=branch)
553555

554556
branch = branch or self.default_branch
555557
if at:
556558
at = Timestamp(at)
557559

558560
response = await self.execute_graphql(
559-
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
561+
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
562+
branch_name=branch,
563+
at=at,
564+
timeout=timeout,
560565
)
561566
return int(response.get(schema.kind, {}).get("count", 0))
562567

@@ -1651,16 +1656,21 @@ def count(
16511656
at: Timestamp | None = None,
16521657
branch: str | None = None,
16531658
timeout: int | None = None,
1659+
**kwargs: Any,
16541660
) -> int:
16551661
"""Return the number of nodes of a given kind."""
1662+
filters = kwargs
16561663
schema = self.schema.get(kind=kind, branch=branch)
16571664

16581665
branch = branch or self.default_branch
16591666
if at:
16601667
at = Timestamp(at)
16611668

16621669
response = self.execute_graphql(
1663-
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
1670+
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
1671+
branch_name=branch,
1672+
at=at,
1673+
timeout=timeout,
16641674
)
16651675
return int(response.get(schema.kind, {}).get("count", 0))
16661676

tests/unit/sdk/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ async def test_method_count(clients, mock_query_repository_count, client_type):
8383
assert count == 5
8484

8585

86+
@pytest.mark.parametrize("client_type", client_types)
87+
async def test_method_count_with_filter(clients, mock_query_repository_count, client_type): # pylint: disable=unused-argument
88+
if client_type == "standard":
89+
count = await clients.standard.count(kind="CoreRepository", name__value="test")
90+
else:
91+
count = clients.sync.count(kind="CoreRepository", name__value="test")
92+
93+
assert count == 5
94+
95+
8696
@pytest.mark.parametrize("client_type", client_types)
8797
async def test_method_get_version(clients, mock_query_infrahub_version, client_type): # pylint: disable=unused-argument
8898
if client_type == "standard":

0 commit comments

Comments
 (0)