Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete slot mapping fix #1602

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kairon/api/app/routers/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ async def delete_slot_mapping(mapping_id: str = Path(description="Slot Mapping i
"""
Deletes a slot mapping.
"""
mongo_processor.delete_single_slot_mapping(mapping_id, user=current_user.get_user())
mongo_processor.delete_singular_slot_mapping(mapping_id)
return Response(message='Slot mapping deleted')


Expand Down
70 changes: 70 additions & 0 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21438,6 +21438,76 @@ def test_add_http_action_case_insensitivity():
assert actual["success"]


def test_add_slot_mapping_1():
response = client.post(
f"/api/bot/{pytest.bot}/slots",
json={"name": "name_slot", "type": "text"},
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)

actual = response.json()
assert actual["message"] == "Slot added successfully!"
assert actual["success"]
assert actual["error_code"] == 0
response = client.post(
f"/api/bot/{pytest.bot}/slots/mapping",
json={
"slot": "name_slot",
"mapping": {"type": "from_text", "value": "user", "entity": "name_slot"},
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["message"] == "Slot mapping added"
assert actual["success"]
assert actual["error_code"] == 0


def test_delete_slot_mapping_1():
response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
for slot_mapping in actual["data"]:
if slot_mapping['slot'] == "name_slot":
mapping_id = slot_mapping['mapping'][0]['_id']
break
assert len(actual["data"]) == 12

response = client.delete(
f"/api/bot/{pytest.bot}/slots/mapping_id/{mapping_id}",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["message"] == "Slot mapping deleted"
assert actual["error_code"] == 0
assert not actual["data"]

response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["error_code"] == 0
assert len(actual["data"]) == 11

Comment on lines +21466 to +21497
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix brittle test implementation

The test has several issues that could lead to flaky behavior:

  1. Hardcoded assertions for total mappings (12 -> 11) make the test brittle
  2. Missing error handling if mapping_id is not found
  3. Implicit dependency on test data state
-def test_delete_slot_mapping_1():
+def test_delete_slot_mapping_success():
     response = client.get(
         f"/api/bot/{pytest.bot}/slots/mapping",
         headers={"Authorization": pytest.token_type + " " + pytest.access_token},
     )
     actual = response.json()
     assert actual["success"]
+    initial_mapping_count = len(actual["data"])
+    mapping_id = None
     for slot_mapping in actual["data"]:
         if slot_mapping['slot'] == "name_slot":
             mapping_id = slot_mapping['mapping'][0]['_id']
             break
-    assert len(actual["data"]) == 12
+    assert mapping_id is not None, "Required test mapping 'name_slot' not found"

     response = client.delete(
         f"/api/bot/{pytest.bot}/slots/mapping_id/{mapping_id}",
         headers={"Authorization": pytest.token_type + " " + pytest.access_token},
     )
     actual = response.json()
     assert actual["success"]
     assert actual["message"] == "Slot mapping deleted"
     assert actual["error_code"] == 0
     assert not actual["data"]

     response = client.get(
         f"/api/bot/{pytest.bot}/slots/mapping",
         headers={"Authorization": pytest.token_type + " " + pytest.access_token},
     )
     actual = response.json()
     assert actual["success"]
     assert actual["error_code"] == 0
-    assert len(actual["data"]) == 11
+    assert len(actual["data"]) == initial_mapping_count - 1
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def test_delete_slot_mapping_1():
response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
for slot_mapping in actual["data"]:
if slot_mapping['slot'] == "name_slot":
mapping_id = slot_mapping['mapping'][0]['_id']
break
assert len(actual["data"]) == 12
response = client.delete(
f"/api/bot/{pytest.bot}/slots/mapping_id/{mapping_id}",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["message"] == "Slot mapping deleted"
assert actual["error_code"] == 0
assert not actual["data"]
response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["error_code"] == 0
assert len(actual["data"]) == 11
def test_delete_slot_mapping_success():
response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
initial_mapping_count = len(actual["data"])
mapping_id = None
for slot_mapping in actual["data"]:
if slot_mapping['slot'] == "name_slot":
mapping_id = slot_mapping['mapping'][0]['_id']
break
assert mapping_id is not None, "Required test mapping 'name_slot' not found"
response = client.delete(
f"/api/bot/{pytest.bot}/slots/mapping_id/{mapping_id}",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["message"] == "Slot mapping deleted"
assert actual["error_code"] == 0
assert not actual["data"]
response = client.get(
f"/api/bot/{pytest.bot}/slots/mapping",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["error_code"] == 0
assert len(actual["data"]) == initial_mapping_count - 1


def test_delete_slot_mapping_does_not_exist():
response = client.delete(
f"/api/bot/{pytest.bot}/slots/mapping_id/{pytest.bot}",
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert not actual["success"]
assert not actual["data"]
assert actual["message"] == 'No slot mapping exists'
assert actual["error_code"] == 422


def test_get_ui_config_empty():
response = client.get(
url=f"/api/account/config/ui",
Expand Down
Loading