Skip to content

Commit 3971c56

Browse files
authored
fix: prevent resource leaks in tests when failures occur (#291)
Add pytest fixtures with guaranteed cleanup to replace inline resource creation/deletion. Ensures tests are idempotent with no side effects.
1 parent 50b4d9c commit 3971c56

File tree

2 files changed

+98
-64
lines changed

2 files changed

+98
-64
lines changed

tests/conftest.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,87 @@ def client(token: Optional[str]):
194194
async def async_client(token: Optional[str]):
195195
async with AsyncClient({"auth": token}) as client:
196196
yield client
197+
198+
199+
@pytest.fixture(scope="function")
200+
def multiple_test_pages(client, parent_page_id):
201+
page_ids = []
202+
for i in range(5):
203+
response = client.pages.create(
204+
parent={"page_id": parent_page_id},
205+
properties={
206+
"title": [
207+
{
208+
"text": {
209+
"content": f"Test Page (test_iterate_paginated_api iteration {i})"
210+
}
211+
}
212+
]
213+
},
214+
children=[],
215+
)
216+
page_ids.append(response["id"])
217+
218+
yield page_ids
219+
220+
for page_id in page_ids:
221+
try:
222+
client.blocks.delete(block_id=page_id)
223+
except Exception:
224+
pass
225+
226+
227+
@pytest.fixture(scope="function")
228+
async def async_multiple_test_pages(async_client, parent_page_id):
229+
page_ids = []
230+
for i in range(5):
231+
response = await async_client.pages.create(
232+
parent={"page_id": parent_page_id},
233+
properties={
234+
"title": [
235+
{
236+
"text": {
237+
"content": f"Test Page (test_async_iterate_paginated_api iteration {i})"
238+
}
239+
}
240+
]
241+
},
242+
children=[],
243+
)
244+
page_ids.append(response["id"])
245+
246+
yield page_ids
247+
248+
for page_id in page_ids:
249+
try:
250+
await async_client.blocks.delete(block_id=page_id)
251+
except Exception:
252+
pass
253+
254+
255+
@pytest.fixture(scope="function")
256+
async def async_test_data_source(async_client, parent_page_id):
257+
database = await async_client.databases.create(
258+
parent={"type": "page_id", "page_id": parent_page_id},
259+
title=[{"type": "text", "text": {"content": "Test DB"}}],
260+
properties={"Name": {"type": "title", "title": {}}},
261+
)
262+
database_id = database["id"]
263+
264+
data_source = await async_client.data_sources.create(
265+
parent={"type": "database_id", "database_id": database_id},
266+
properties={"Name": {"type": "title", "title": {}}},
267+
title=[{"type": "text", "text": {"content": "Test Data Source"}}],
268+
)
269+
data_source_id = data_source["id"]
270+
271+
yield data_source_id
272+
273+
try:
274+
await async_client.data_sources.update(data_source_id, archived=True)
275+
except Exception:
276+
pass
277+
try:
278+
await async_client.blocks.delete(block_id=database_id)
279+
except Exception:
280+
pass

tests/test_helpers.py

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,9 @@ def test_get_url():
7777

7878
@pytest.mark.vcr()
7979
@pytest.mark.timeout(90)
80-
def test_iterate_paginated_api(client, parent_page_id):
81-
def create_page(page_name):
82-
page = client.pages.create(
83-
parent={"page_id": parent_page_id},
84-
properties={"title": [{"text": {"content": page_name}}]},
85-
children=[],
86-
)
87-
return page["id"]
88-
89-
page_ids = []
90-
for i in range(0, 5):
91-
page_id = create_page(f"Test Page (test_iterate_paginated_api iteration {i})")
92-
page_ids.append(page_id)
93-
94-
# give time to Notion to index these pages
80+
def test_iterate_paginated_api(client, multiple_test_pages):
81+
page_ids = multiple_test_pages
82+
9583
time.sleep(20)
9684

9785
generator = iterate_paginated_api(
@@ -132,23 +120,9 @@ def test_collect_paginated_api(client):
132120

133121
@pytest.mark.vcr()
134122
@pytest.mark.timeout(90)
135-
async def test_async_iterate_paginated_api(async_client, parent_page_id):
136-
async def create_page(page_name):
137-
page = await async_client.pages.create(
138-
parent={"page_id": parent_page_id},
139-
properties={"title": [{"text": {"content": page_name}}]},
140-
children=[],
141-
)
142-
return page["id"]
143-
144-
page_ids = []
145-
for i in range(0, 5):
146-
page_id = await create_page(
147-
f"Test Page (test_async_iterate_paginated_api iteration {i})"
148-
)
149-
page_ids.append(page_id)
150-
151-
# give time to Notion to index these pages
123+
async def test_async_iterate_paginated_api(async_client, async_multiple_test_pages):
124+
page_ids = async_multiple_test_pages
125+
152126
await asyncio.sleep(20)
153127

154128
generator = async_iterate_paginated_api(
@@ -549,19 +523,10 @@ def test_collect_data_source_templates(client, data_source_id):
549523

550524

551525
@pytest.mark.vcr()
552-
async def test_async_iterate_data_source_templates(async_client, parent_page_id):
553-
database = await async_client.databases.create(
554-
parent={"type": "page_id", "page_id": parent_page_id},
555-
title=[{"type": "text", "text": {"content": "Async Test DB"}}],
556-
)
557-
database_id = database["id"]
558-
559-
data_source_response = await async_client.data_sources.create(
560-
parent={"type": "database_id", "database_id": database_id},
561-
properties={"Name": {"type": "title", "title": {}}},
562-
title=[{"type": "text", "text": {"content": "Async Test Data Source"}}],
563-
)
564-
data_source_id = data_source_response["id"]
526+
async def test_async_iterate_data_source_templates(
527+
async_client, async_test_data_source
528+
):
529+
data_source_id = async_test_data_source
565530

566531
generator = async_iterate_data_source_templates(
567532
async_client.data_sources.list_templates,
@@ -571,31 +536,16 @@ async def test_async_iterate_data_source_templates(async_client, parent_page_id)
571536
templates = [template async for template in generator]
572537
assert isinstance(templates, list)
573538

574-
await async_client.data_sources.update(data_source_id, archived=True)
575-
await async_client.blocks.delete(block_id=database_id)
576-
577539

578540
@pytest.mark.vcr()
579-
async def test_async_collect_data_source_templates(async_client, parent_page_id):
580-
database = await async_client.databases.create(
581-
parent={"type": "page_id", "page_id": parent_page_id},
582-
title=[{"type": "text", "text": {"content": "Async Test DB 2"}}],
583-
)
584-
database_id = database["id"]
585-
586-
data_source_response = await async_client.data_sources.create(
587-
parent={"type": "database_id", "database_id": database_id},
588-
properties={"Name": {"type": "title", "title": {}}},
589-
title=[{"type": "text", "text": {"content": "Async Test Data Source 2"}}],
590-
)
591-
data_source_id = data_source_response["id"]
541+
async def test_async_collect_data_source_templates(
542+
async_client, async_test_data_source
543+
):
544+
data_source_id = async_test_data_source
592545

593546
templates = await async_collect_data_source_templates(
594547
async_client.data_sources.list_templates,
595548
data_source_id=data_source_id,
596549
)
597550

598551
assert isinstance(templates, list)
599-
600-
await async_client.data_sources.update(data_source_id, archived=True)
601-
await async_client.blocks.delete(block_id=database_id)

0 commit comments

Comments
 (0)