Skip to content

Commit af8bde9

Browse files
authored
Add test to make sure appends create conversation field (#3650)
1 parent ee0ca16 commit af8bde9

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

nucliadb/src/nucliadb/reader/api/v1/resource.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ async def _get_resource_field(
398398
raise HTTPException(status_code=404, detail="Resource does not exist")
399399

400400
resource = ORMResource(txn, storage, kbid, rid)
401+
if not await resource.field_exists(pb_field_id, field_id):
402+
raise HTTPException(status_code=404, detail="Resource field does not exist")
403+
401404
field = await resource.get_field(field_id, pb_field_id, load=True)
402-
if field is None:
403-
raise HTTPException(status_code=404, detail="Knowledge Box does not exist")
404405

405406
resource_field = ResourceField(field_id=field_id, field_type=field_type)
406407

nucliadb/tests/nucliadb/integration/test_conversation.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,85 @@ async def _test_semantic_search(message_text: str, message_id: str, message_vect
884884
# New message is searchable too
885885
await _test_keyword_search(new_message_text, new_message_id)
886886
await _test_semantic_search(new_message_text, new_message_id, new_message_vector)
887+
888+
889+
@pytest.mark.deploy_modes("standalone")
890+
async def test_append_messages_to_non_existent_conversation_field_creates_it(
891+
nucliadb_writer: AsyncClient,
892+
nucliadb_reader: AsyncClient,
893+
standalone_knowledgebox: str,
894+
):
895+
"""Test that appending messages to a non-existent conversation field creates it."""
896+
kbid = standalone_knowledgebox
897+
898+
# Create a resource without any conversation field
899+
resp = await nucliadb_writer.post(
900+
f"/kb/{kbid}/resources",
901+
json={
902+
"slug": "test_resource",
903+
"title": "Test Resource",
904+
},
905+
)
906+
assert resp.status_code == 201
907+
rid = resp.json()["uuid"]
908+
909+
# Try to get the conversation field before appending - should fail
910+
resp = await nucliadb_reader.get(
911+
f"/kb/{kbid}/resource/{rid}/conversation/new_conversation",
912+
params={"page": 1},
913+
)
914+
assert resp.status_code == 404
915+
916+
# Append messages to a non-existent conversation field
917+
resp = await nucliadb_writer.put(
918+
f"/kb/{kbid}/resource/{rid}/conversation/new_conversation/messages",
919+
json=[
920+
{
921+
"to": ["assistant"],
922+
"who": "user",
923+
"timestamp": datetime.now().isoformat(),
924+
"content": {"text": "Hello, how are you?"},
925+
"ident": "1",
926+
"type": MessageType.QUESTION.value,
927+
},
928+
{
929+
"to": ["user"],
930+
"who": "assistant",
931+
"timestamp": datetime.now().isoformat(),
932+
"content": {"text": "I'm doing well, thank you!"},
933+
"ident": "2",
934+
"type": MessageType.ANSWER.value,
935+
},
936+
],
937+
)
938+
assert resp.status_code == 200
939+
assert "seqid" in resp.json()
940+
941+
# Get the conversation field and verify the messages were added
942+
resp = await nucliadb_reader.get(
943+
f"/kb/{kbid}/resource/{rid}/conversation/new_conversation",
944+
params={"page": 1, "show": ["value"]},
945+
)
946+
assert resp.status_code == 200
947+
field_resp = ResourceField.model_validate(resp.json())
948+
msgs = field_resp.value["messages"] # type: ignore
949+
assert len(msgs) == 2
950+
assert msgs[0]["ident"] == "1"
951+
assert msgs[0]["who"] == "user"
952+
assert msgs[0]["content"]["text"] == "Hello, how are you?"
953+
assert msgs[1]["ident"] == "2"
954+
assert msgs[1]["who"] == "assistant"
955+
assert msgs[1]["content"]["text"] == "I'm doing well, thank you!"
956+
957+
# Verify the field exists in the resource summary
958+
resp = await nucliadb_reader.get(
959+
f"/kb/{kbid}/resource/{rid}",
960+
params={"show": ["values"]},
961+
)
962+
assert resp.status_code == 200
963+
res_resp = ResponseResponse.model_validate(resp.json())
964+
assert "new_conversation" in res_resp.data.conversations # type: ignore
965+
conv_field = res_resp.data.conversations["new_conversation"] # type: ignore
966+
assert conv_field.value is not None
967+
assert conv_field.value.total == 2
968+
assert conv_field.value.pages == 1

0 commit comments

Comments
 (0)