22# Copyright 2026 Atlan Pte. Ltd.
33"""Async integration tests for OAuth client CRUD operations."""
44
5+ import asyncio
56import time
6- from typing import AsyncGenerator , Optional
7+ from typing import AsyncGenerator , List , Optional
78
89import pytest
910import pytest_asyncio
2223OAUTH_CLIENT_ROLE = "Admin" # Role description
2324DATA_ASSETS_PERSONA_NAME = "Data Assets" # Pre-existing persona
2425
26+ # Pagination test constants
27+ PAGINATION_CLIENT_COUNT = 5
28+ PAGINATION_CLIENT_NAME_PREFIX = f"{ MODULE_NAME } _pagination_client"
29+
2530
2631async def delete_oauth_client_async (client : AsyncAtlanClient , client_id : str ) -> None :
2732 """Helper to delete an OAuth client."""
@@ -40,6 +45,41 @@ async def persona_qualified_name(client: AsyncAtlanClient) -> str:
4045 return persona_qn
4146
4247
48+ @pytest_asyncio .fixture (scope = "module" )
49+ async def pagination_oauth_clients (
50+ client : AsyncAtlanClient ,
51+ ) -> AsyncGenerator [List [OAuthClientCreateResponse ], None ]:
52+ """
53+ Fixture to create multiple OAuth clients for pagination testing.
54+ Creates 5 OAuth clients and yields their responses.
55+ Cleans up by deleting all created OAuth clients after tests.
56+ """
57+ created_clients : List [OAuthClientCreateResponse ] = []
58+
59+ # Create 5 OAuth clients for pagination testing
60+ for i in range (PAGINATION_CLIENT_COUNT ):
61+ response = await client .oauth_client .create (
62+ name = f"{ PAGINATION_CLIENT_NAME_PREFIX } _{ i } " ,
63+ role = OAUTH_CLIENT_ROLE ,
64+ description = f"Pagination test OAuth client { i } " ,
65+ )
66+ assert response is not None
67+ assert response .client_id is not None
68+ created_clients .append (response )
69+ # Small delay to ensure distinct createdAt timestamps for sorting
70+ await asyncio .sleep (0.5 )
71+
72+ yield created_clients
73+
74+ # Cleanup: delete all created OAuth clients
75+ for oauth_client in created_clients :
76+ if oauth_client .client_id :
77+ try :
78+ await delete_oauth_client_async (client , oauth_client .client_id )
79+ except Exception :
80+ pass # Ignore cleanup errors
81+
82+
4383@pytest_asyncio .fixture (scope = "module" )
4484async def oauth_client_response (
4585 client : AsyncAtlanClient ,
@@ -128,25 +168,50 @@ async def test_oauth_client_get_by_id(
128168@pytest .mark .order (after = "test_oauth_client_get_by_id" )
129169async def test_oauth_client_get_with_pagination (
130170 client : AsyncAtlanClient ,
131- oauth_client_response : OAuthClientCreateResponse ,
132- persona_qualified_name : str ,
171+ pagination_oauth_clients : List [OAuthClientCreateResponse ],
133172):
134- """Test retrieving OAuth clients with pagination and async iteration."""
135- assert oauth_client_response .client_id is not None
173+ """Test retrieving OAuth clients with pagination and async iteration.
174+
175+ This test creates 5 OAuth clients and uses limit=1 to ensure
176+ the pagination logic is properly exercised across multiple API calls.
177+ """
178+ # Verify we have the expected number of test clients
179+ assert len (pagination_oauth_clients ) == PAGINATION_CLIENT_COUNT
180+
181+ # Get the client IDs we created for verification
182+ created_client_ids = {c .client_id for c in pagination_oauth_clients }
136183
137- response = await client .oauth_client .get (limit = 10 , offset = 0 , sort = "-createdAt" )
184+ # Use limit=1 to force multiple API calls for pagination
185+ response = await client .oauth_client .get (limit = 1 , offset = 0 , sort = "createdAt" )
138186 assert response is not None
139187 assert response .total_record is not None
140- assert response .total_record >= 1
188+ # Should have at least our 5 created clients
189+ assert response .total_record >= PAGINATION_CLIENT_COUNT
190+
191+ # Store the initial total record count
192+ initial_total = response .total_record
141193
142194 # Test async iteration over the paginated response
143- found = False
195+ # This should make multiple API calls (one per page with limit=1)
196+ found_client_ids : set = set ()
197+ total_iterated = 0
198+
144199 async for oauth_client in response :
145- if oauth_client .client_id == oauth_client_response .client_id :
146- found = True
147- _assert_oauth_client (oauth_client , persona_qn = persona_qualified_name )
148- break
149- assert found , f"OAuth client { oauth_client_response .client_id } not found"
200+ total_iterated += 1
201+ if oauth_client .client_id in created_client_ids :
202+ found_client_ids .add (oauth_client .client_id )
203+
204+ # Verify we iterated through all records
205+ assert total_iterated == initial_total , (
206+ f"Expected to iterate through { initial_total } records, "
207+ f"but only iterated through { total_iterated } "
208+ )
209+
210+ # Verify we found all our created clients
211+ assert found_client_ids == created_client_ids , (
212+ f"Expected to find all { PAGINATION_CLIENT_COUNT } created clients. "
213+ f"Found: { len (found_client_ids )} , Missing: { created_client_ids - found_client_ids } "
214+ )
150215
151216
152217@pytest .mark .order (after = "test_oauth_client_get_with_pagination" )
0 commit comments