Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/experts): test fixtures delete the templates and hires they create (#14556)

Reinier van der Leerdb50144f44f3
diff --git a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py
index 5c642ee40b..eedefedd10 100644
--- a/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py
+++ b/autogpt_platform/backend/backend/api/features/experts/experts_db_test.py
@@ -75,6 +75,69 @@ def mock_embedding_functions():
yield
+_seeded_template_ids: list[str] = []
+_seeded_user_ids: list[str] = []
+
+
[email protected](autouse=True)
+async def delete_rows_this_test_seeded():
+ """Delete the templates, hires and seed users the test created.
+
+ Without it a run against the shared dev database leaves every template and
+ hire behind; 4,689 stray templates had accumulated by 2026-09-11.
+ """
+ yield
+ template_ids, user_ids = list(_seeded_template_ids), list(_seeded_user_ids)
+ # Clear only once the deletion succeeded: a raise here keeps the ids so the
+ # next teardown retries them, and re-deleting a gone row is a no-op.
+ await _delete_seeded_rows(template_ids, user_ids)
+ _seeded_template_ids.clear()
+ _seeded_user_ids.clear()
+
+
+async def _delete_seeded_rows(template_ids: list[str], user_ids: list[str]) -> None:
+ if template_ids:
+ # Hires outlive their template (sourceTemplateId is SET NULL), so they go first.
+ await prisma.models.Expert.prisma().delete_many(
+ where={"sourceTemplateId": {"in": template_ids}}
+ )
+ await prisma.models.Expert.prisma().delete_many(
+ where={"id": {"in": template_ids}}
+ )
+ if not user_ids:
+ return
+ # Every FK to User that is not ON DELETE CASCADE has to be cleared by hand,
+ # deepest first: a listing still pointing at a seed user's graph blocks the
+ # cascade that would delete that graph.
+ graphs = await prisma.models.AgentGraph.prisma().find_many(
+ where={"userId": {"in": user_ids}}
+ )
+ graph_ids = [graph.id for graph in graphs]
+ if graph_ids:
+ await prisma.models.StoreListing.prisma().delete_many(
+ where={"agentGraphId": {"in": graph_ids}}
+ )
+ await prisma.models.StoreListingReview.prisma().delete_many(
+ where={"reviewByUserId": {"in": user_ids}}
+ )
+ await prisma.models.StoreListing.prisma().delete_many(
+ where={"owningUserId": {"in": user_ids}}
+ )
+ await prisma.models.CreditTransaction.prisma().delete_many(
+ where={"userId": {"in": user_ids}}
+ )
+ await prisma.models.AgentPreset.prisma().delete_many(
+ where={"userId": {"in": user_ids}}
+ )
+ await prisma.models.LibraryAgent.prisma().delete_many(
+ where={"userId": {"in": user_ids}}
+ )
+ await prisma.models.IntegrationWebhook.prisma().delete_many(
+ where={"userId": {"in": user_ids}}
+ )
+ await prisma.models.User.prisma().delete_many(where={"id": {"in": user_ids}})
+
+
@pytest.fixture
async def test_user():
return await _create_seed_user()
@@ -103,13 +166,15 @@ def _marketplace_skill(listing_id: str) -> list[RaiseAttachment]:
async def _create_seed_user():
suffix = uuid.uuid4().hex[:8]
- return await get_or_create_user(
+ user = await get_or_create_user(
{
"sub": str(uuid.uuid4()),
"email": f"expert-seed-{suffix}@example.com",
"name": "Seed Owner",
}
)
+ _seeded_user_ids.append(user.id)
+ return user
async def _seed_store_listing(
@@ -292,6 +357,7 @@ async def _seed_template(
"isTemplate": True,
}
)
+ _seeded_template_ids.append(template.id)
for slv_id in preload_listings:
await prisma.models.ExpertWorkflow.prisma().create(
data={
@@ -2051,15 +2117,11 @@ async def test_update_soul_if_current_raises_when_the_row_vanishes_after_the_wri
@pytest.mark.asyncio(loop_scope="session")
async def test_hire_copies_soul_fields_from_template(server: SpinTestServer, test_user):
- template = await prisma.models.Expert.prisma().create(
- data={
- "name": f"Otto {uuid.uuid4().hex[:8]}",
- "role": "Writer",
- "identity": "You are Otto, a playful writer.",
- "voicePreferences": "Direct, playful, and concise.",
- "boundaries": "Never publish without approval.",
- "isTemplate": True,
- }
+ template = await _template(
+ f"Otto {uuid.uuid4().hex[:8]}",
+ identity="You are Otto, a playful writer.",
+ voicePreferences="Direct, playful, and concise.",
+ boundaries="Never publish without approval.",
)
hired = await experts_db.hire_expert(test_user.id, template.id, None)
@@ -2082,14 +2144,10 @@ async def test_hire_from_template_with_samples_stores_plain_voice(
VoiceSample(label="Warm", text="Let's start with a story."),
],
)
- template = await prisma.models.Expert.prisma().create(
- data={
- "name": f"Vox {uuid.uuid4().hex[:8]}",
- "role": "Writer",
- "identity": "You are Vox.",
- "voicePreferences": envelope,
- "isTemplate": True,
- }
+ template = await _template(
+ f"Vox {uuid.uuid4().hex[:8]}",
+ identity="You are Vox.",
+ voicePreferences=envelope,
)
listed = next(t for t in await experts_db.list_templates() if t.id == template.id)
@@ -4655,7 +4713,7 @@ async def test_expert_skill_name_write_gives_up_as_a_conflict_after_losing_every
async def _template(name: str, **fields) -> prisma.models.Expert:
- return await prisma.models.Expert.prisma().create(
+ template = await prisma.models.Expert.prisma().create(
data={
"name": name,
"role": fields.pop("role", "Writer"),
@@ -4664,6 +4722,8 @@ async def _template(name: str, **fields) -> prisma.models.Expert:
**fields,
}
)
+ _seeded_template_ids.append(template.id)
+ return template
@pytest.mark.asyncio(loop_scope="session")