Skip to content

Commit

Permalink
Delete community test
Browse files Browse the repository at this point in the history
  • Loading branch information
NolanTrem committed Nov 29, 2024
1 parent 2a4c8e6 commit 555f732
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 36 deletions.
18 changes: 18 additions & 0 deletions js/sdk/__tests__/GraphsIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ describe("r2rClient V3 Graphs Integration Tests", () => {
expect(response.results.predicate).toBe("marries");
});

test("Delete the community", async () => {
const response = await client.graphs.deleteCommunity({
collectionId: collectionId,
communityId: communityId,
});

expect(response.results).toBeDefined();
});

test("Check that the community was deleted", async () => {
const response = await client.graphs.listCommunities({
collectionId: collectionId,
});

expect(response.results).toBeDefined();
expect(response.results.entries).toHaveLength(0);
});

test("Reset the graph", async () => {
const response = await client.graphs.reset({
collectionId: collectionId,
Expand Down
46 changes: 40 additions & 6 deletions py/core/main/api/v3/graph_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,37 @@ async def delete_relationship(
client = R2RClient("http://localhost:7272")
# when using auth, do client.login(...)
result = client.graphs.communities.create(collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1", communities=[community1, community2])
result = client.graphs.create_community(
collection_id="9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
name="My Community",
summary="A summary of the community",
findings=["Finding 1", "Finding 2"],
rating=5,
rating_explanation="This is a rating explanation",
)
"""
),
},
{
"lang": "JavaScript",
"source": textwrap.dedent(
"""
const { r2rClient } = require("r2r-js");
const client = new r2rClient("http://localhost:7272");
function main() {
const response = await client.graphs.createCommunity({
collectionId: "9fbe403b-c11c-5aae-8ade-ef22980c3ad1",
name: "My Community",
summary: "A summary of the community",
findings: ["Finding 1", "Finding 2"],
rating: 5,
ratingExplanation: "This is a rating explanation",
});
}
main();
"""
),
},
Expand Down Expand Up @@ -1414,14 +1444,18 @@ async def delete_community(
),
auth_user=Depends(self.providers.auth.auth_wrapper),
):
if not auth_user.is_superuser:
if (
not auth_user.is_superuser
and collection_id not in auth_user.graph_ids
):
raise R2RException(
"Only superusers can access this endpoint.", 403
"The currently authenticated user does not have access to the specified graph.",
403,
)
await self.services["kg"].delete_community_v3(
graph_id=collection_id,

await self.services["kg"].delete_community(
parent_id=collection_id,
community_id=community_id,
auth_user=auth_user,
)
return GenericBooleanResponse(success=True) # type: ignore

Expand Down
17 changes: 6 additions & 11 deletions py/core/main/services/kg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,8 @@ async def create_relationship(
@telemetry_event("delete_relationship_v3")
async def delete_relationship_v3(
self,
level: DataLevel,
id: UUID,
relationship_id: UUID,
**kwargs,
):
return (
await self.providers.database.graph_handler.relationships.delete(
Expand Down Expand Up @@ -437,18 +435,15 @@ async def update_community(
rating_explanation=rating_explanation,
)

@telemetry_event("delete_community_v3")
async def delete_community_v3(
@telemetry_event("delete_community")
async def delete_community(
self,
graph_id: UUID,
parent_id: UUID,
community_id: UUID,
auth_user: Any,
**kwargs,
):
return await self.providers.database.graph_handler.communities.delete(
graph_id=graph_id,
) -> None:
await self.providers.database.graph_handler.communities.delete(
parent_id=parent_id,
community_id=community_id,
auth_user=auth_user,
)

@telemetry_event("list_communities_v3")
Expand Down
55 changes: 36 additions & 19 deletions py/core/providers/database/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,22 +885,28 @@ async def create(
description_embedding,
]

result = await self.connection_manager.fetchrow_query(
query=query,
params=params,
)
try:
result = await self.connection_manager.fetchrow_query(
query=query,
params=params,
)

return Community(
id=result["id"],
community_id=result["community_id"],
name=result["name"],
summary=result["summary"],
findings=result["findings"],
rating=result["rating"],
rating_explanation=result["rating_explanation"],
created_at=result["created_at"],
updated_at=result["updated_at"],
)
return Community(
id=result["id"],
community_id=result["community_id"],
name=result["name"],
summary=result["summary"],
findings=result["findings"],
rating=result["rating"],
rating_explanation=result["rating_explanation"],
created_at=result["created_at"],
updated_at=result["updated_at"],
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An error occurred while creating the community: {e}",
)

async def update(
self,
Expand Down Expand Up @@ -950,14 +956,25 @@ async def update(

async def delete(
self,
graph_id: UUID,
parent_id: UUID,
community_id: UUID,
) -> None:
table_name = "graph_community"

QUERY = f"""
DELETE FROM {self._get_table_name("graph_community")} WHERE id = $1
query = f"""
DELETE FROM {self._get_table_name(table_name)}
WHERE id = $1 AND graph_id = $2
"""
await self.connection_manager.execute_query(QUERY, [community_id])

params = [community_id, parent_id]

try:
await self.connection_manager.execute_query(query, params)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An error occurred while deleting the community: {e}",
)

async def get(
self,
Expand Down

0 comments on commit 555f732

Please sign in to comment.