Skip to content

Commit 60d001e

Browse files
authored
Add count method to clients (#225)
1 parent 73fb502 commit 60d001e

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

changelog/158.added.me

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `count` method to both sync and async clients to retrieve the number of objects of a given kind

infrahub_sdk/client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,25 @@ async def _process_nodes_and_relationships(
525525

526526
return ProcessRelationsNode(nodes=nodes, related_nodes=related_nodes)
527527

528+
async def count(
529+
self,
530+
kind: str | type[SchemaType],
531+
at: Timestamp | None = None,
532+
branch: str | None = None,
533+
timeout: int | None = None,
534+
) -> int:
535+
"""Return the number of nodes of a given kind."""
536+
schema = await self.schema.get(kind=kind, branch=branch)
537+
538+
branch = branch or self.default_branch
539+
if at:
540+
at = Timestamp(at)
541+
542+
response = await self.execute_graphql(
543+
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
544+
)
545+
return int(response.get(schema.kind, {}).get("count", 0))
546+
528547
@overload
529548
async def all(
530549
self,
@@ -1549,6 +1568,25 @@ def execute_graphql(
15491568

15501569
# TODO add a special method to execute mutation that will check if the method returned OK
15511570

1571+
def count(
1572+
self,
1573+
kind: str | type[SchemaType],
1574+
at: Timestamp | None = None,
1575+
branch: str | None = None,
1576+
timeout: int | None = None,
1577+
) -> int:
1578+
"""Return the number of nodes of a given kind."""
1579+
schema = self.schema.get(kind=kind, branch=branch)
1580+
1581+
branch = branch or self.default_branch
1582+
if at:
1583+
at = Timestamp(at)
1584+
1585+
response = self.execute_graphql(
1586+
query=Query(query={schema.kind: {"count": None}}).render(), branch_name=branch, at=at, timeout=timeout
1587+
)
1588+
return int(response.get(schema.kind, {}).get("count", 0))
1589+
15521590
@overload
15531591
def all(
15541592
self,

tests/unit/sdk/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,12 @@ async def mock_query_corenode_page1_1(httpx_mock: HTTPXMock, client: InfrahubCli
16801680
return httpx_mock
16811681

16821682

1683+
@pytest.fixture
1684+
async def mock_query_repository_count(httpx_mock: HTTPXMock, client: InfrahubClient, mock_schema_query_01) -> HTTPXMock:
1685+
httpx_mock.add_response(method="POST", json={"data": {"CoreRepository": {"count": 5}}})
1686+
return httpx_mock
1687+
1688+
16831689
@pytest.fixture
16841690
async def mock_query_repository_page1_empty(
16851691
httpx_mock: HTTPXMock, client: InfrahubClient, mock_schema_query_01

tests/unit/sdk/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ async def test_get_repositories(
6666
}
6767

6868

69+
@pytest.mark.parametrize("client_type", client_types)
70+
async def test_method_count(clients, mock_query_repository_count, client_type): # pylint: disable=unused-argument
71+
if client_type == "standard":
72+
count = await clients.standard.count(kind="CoreRepository")
73+
else:
74+
count = clients.sync.count(kind="CoreRepository")
75+
76+
assert count == 5
77+
78+
6979
@pytest.mark.parametrize("client_type", client_types)
7080
async def test_method_all_with_limit(clients, mock_query_repository_page1_2, client_type): # pylint: disable=unused-argument
7181
if client_type == "standard":

0 commit comments

Comments
 (0)