Skip to content

Commit 6a00e5e

Browse files
authored
Merge pull request #232 from opsmill/wvd-20240110-add-filters-for-count-method
Add possibility to use filters in the SDK client's count method
2 parents efcf472 + 922ed7f commit 6a00e5e

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-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/integration/test_infrahub_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ async def test_create_branch_async(self, client: InfrahubClient, base_dataset):
142142
task_id = await client.branch.create(branch_name="new-branch-2", wait_until_completion=False)
143143
assert isinstance(task_id, str)
144144

145+
async def test_count(self, client: InfrahubClient, base_dataset):
146+
count = await client.count(kind=TESTING_PERSON)
147+
assert count == 3
148+
149+
async def test_count_with_filter(self, client: InfrahubClient, base_dataset):
150+
count = await client.count(kind=TESTING_PERSON, name__values=["Liam Walker", "Ethan Carter"])
151+
assert count == 2
152+
145153
# async def test_get_generic_filter_source(self, client: InfrahubClient, base_dataset):
146154
# admin = await client.get(kind="CoreAccount", name__value="admin")
147155

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)