Skip to content

Commit

Permalink
Api fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nupur Khare committed Oct 26, 2023
1 parent e0a997d commit 5eef626
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
12 changes: 6 additions & 6 deletions kairon/api/app/routers/bot/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ async def save_cognition_data(
}


@router.put("/cognition/{cognition_id}", response_model=Response)
@router.put("/cognition/{row_id}", response_model=Response)
async def update_cognition_data(
cognition_id: str,
row_id: str,
cognition: CognitiveDataRequest,
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS),
):
Expand All @@ -130,7 +130,7 @@ async def update_cognition_data(
"message": "Record updated!",
"data": {
"_id": cognition_processor.update_cognition_data(
cognition_id,
row_id,
cognition.dict(),
current_user.get_user(),
current_user.get_bot(),
Expand All @@ -139,15 +139,15 @@ async def update_cognition_data(
}


@router.delete("/cognition/{cognition_id}", response_model=Response)
@router.delete("/cognition/{row_id}", response_model=Response)
async def delete_cognition_data(
cognition_id: str,
row_id: str,
current_user: User = Security(Authentication.get_current_user_and_bot, scopes=DESIGNER_ACCESS),
):
"""
Deletes cognition content of the bot
"""
cognition_processor.delete_cognition_data(cognition_id, current_user.get_bot())
cognition_processor.delete_cognition_data(row_id, current_user.get_bot())
return {
"message": "Record deleted!"
}
Expand Down
15 changes: 8 additions & 7 deletions kairon/shared/cognition/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ def save_cognition_data(self, payload: Dict, user: Text, bot: Text):
payload_id = payload_obj.save().to_mongo().to_dict()["_id"].__str__()
return payload_id

def update_cognition_data(self, payload_id: str, payload: Dict, user: Text, bot: Text):
def update_cognition_data(self, row_id: str, payload: Dict, user: Text, bot: Text):
from kairon import Utility

data = payload['data']
content_type = payload['content_type']
if payload.get('content_type') == CognitionDataType.text.value and len(payload.get('data').split()) < 10:
raise AppException("Content should contain atleast 10 words.")
Utility.is_exist(CognitionData, bot=bot, id__ne=payload_id, data=data,
Utility.is_exist(CognitionData, bot=bot, id__ne=row_id, data=data,
exp_message="Payload data already exists!")
if payload.get('collection') and not Utility.is_exist(CognitionSchema, bot=bot, collection_name=payload.get('collection'), raise_error=False):
raise AppException('Collection does not exist!')
try:
payload_obj = CognitionData.objects(bot=bot, id=payload_id).get()
payload_obj = CognitionData.objects(bot=bot, id=row_id).get()
if content_type == CognitionDataType.json.value:
CognitionDataProcessor.validate_metadata_and_payload(bot, payload)
payload_obj.data = data
Expand All @@ -166,9 +166,9 @@ def update_cognition_data(self, payload_id: str, payload: Dict, user: Text, bot:
except DoesNotExist:
raise AppException("Payload with given id not found!")

def delete_cognition_data(self, payload_id: str, bot: Text):
def delete_cognition_data(self, row_id: str, bot: Text):
try:
payload = CognitionData.objects(bot=bot, id=payload_id).get()
payload = CognitionData.objects(bot=bot, id=row_id).get()
payload.delete()
except DoesNotExist:
raise AppException("Payload does not exists!")
Expand All @@ -186,10 +186,11 @@ def list_cognition_data(self, bot: Text, start_idx: int = 0, page_size: int = 10
search = kwargs.pop('data', None)
kwargs.pop('start_idx', None)
kwargs.pop('page_size', None)
cognition_data = CognitionData.objects(**kwargs)
collection = kwargs.pop('collection', None)
cognition_data = CognitionData.objects(**kwargs).filter(Q(collection=collection))
if search:
cognition_data = cognition_data.search_text(search)
for value in cognition_data.skip(start_idx).limit(page_size):
for value in cognition_data.skip(start_idx).limit(page_size).order_by('-id'):
final_data = {}
item = value.to_mongo().to_dict()
data = item.pop("data")
Expand Down
35 changes: 17 additions & 18 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ def test_content_update_api():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.content_id_text}",
json={
"cognition_id": pytest.content_id_text,
"row_id": pytest.content_id_text,
"data": "AWS Fargate is a serverless compute engine for containers that allows you to run "
"Docker containers without having to manage the underlying EC2 instances. With Fargate, "
"you can focus on developing and deploying your applications rather than managing the infrastructure.",
Expand All @@ -1682,7 +1682,7 @@ def test_content_update_api_collection_does_not_exist():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.content_id_text}",
json={
"cognition_id": pytest.content_id_text,
"row_id": pytest.content_id_text,
"data": "Docker containers without having to manage the underlying EC2 instances.",
"collection": "test_content_update_api_collection_does_not_exist",
"content_type": "text"
Expand All @@ -1700,7 +1700,7 @@ def test_content_update_api_invalid():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.content_id_text}",
json={
"cognition_id": pytest.content_id_text,
"row_id": pytest.content_id_text,
"data": "AWS Fargate is a serverless compute engine.",
"collection": "details",
"content_type": "text"
Expand All @@ -1720,7 +1720,7 @@ def test_content_update_api_already_exist():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{content_id}",
json={
"cognition_id": content_id,
"row_id": content_id,
"data": "AWS Fargate is a serverless compute engine for containers that allows you to run "
"Docker containers without having to manage the underlying EC2 instances. With Fargate, "
"you can focus on developing and deploying your applications rather than managing the infrastructure.",
Expand All @@ -1742,7 +1742,7 @@ def test_content_update_api_id_not_found():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{content_id}",
json={
"cognition_id": content_id,
"row_id": content_id,
"data": "Artificial intelligence (AI) involves using computers to do things that traditionally require human "
"intelligence. AI can process large amounts of data in ways that humans cannot. The goal for AI is "
"to be able to do things like recognize patterns, make decisions, and judge like humans.",
Expand Down Expand Up @@ -1792,16 +1792,15 @@ def test_get_content_without_data():
assert actual["success"]
assert actual["error_code"] == 0
assert actual["data"]
assert actual["data"][0]['collection'] == 'details'
assert actual["data"][0]['data'] == 'AWS Fargate is a serverless compute engine for containers that allows you to run Docker containers without having to manage the underlying EC2 instances. With Fargate, you can focus on developing and deploying your applications rather than managing the infrastructure.'
assert actual["data"][1]['data'] == 'Blockchain technology is an advanced database mechanism that allows transparent information sharing within a business network.'
assert actual["data"][0]['collection'] == None
assert actual["data"][0]['data'] == 'Blockchain technology is an advanced database mechanism that allows transparent information sharing within a business network.'


def test_delete_content():
response_one = client.delete(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.content_id_no_collection}",
json={
"cognition_id": pytest.content_id_no_collection,
"row_id": pytest.content_id_no_collection,
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
Expand All @@ -1814,7 +1813,7 @@ def test_delete_content():
response = client.delete(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.content_id_text}",
json={
"cognition_id": pytest.content_id_text,
"row_id": pytest.content_id_text,
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
Expand All @@ -1830,7 +1829,7 @@ def test_delete_content_does_not_exist():
response = client.delete(
url=f"/api/bot/{pytest.bot}/data/cognition/{content_id}",
json={
"cognition_id": content_id,
"row_id": content_id,
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
Expand Down Expand Up @@ -1973,7 +1972,7 @@ def test_payload_updated_api_collection_does_not_exists():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json={
"cognition_id": pytest.payload_id,
"row_id": pytest.payload_id,
"data": {"details": "data science"},
"collection": "test_payload_updated_api_collection_does_not_exists",
"content_type": "json"
Expand Down Expand Up @@ -2010,7 +2009,7 @@ def test_payload_updated_api():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json={
"cognition_id": pytest.payload_id,
"row_id": pytest.payload_id,
"data": {"details": "data science"},
"collection": "Details",
"content_type": "json"
Expand All @@ -2033,7 +2032,7 @@ def _mock_get_bot_settings(*args, **kwargs):
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{payload_id}",
json={
"cognition_id": payload_id,
"row_id": payload_id,
"data": {"details": "data science"},
"content_type": "json",
},
Expand All @@ -2053,7 +2052,7 @@ def test_payload_content_update_api_id_not_found():
response = client.put(
url=f"/api/bot/{pytest.bot}/data/cognition/{payload_id}",
json={
"cognition_id": payload_id,
"row_id": payload_id,
"data": {"details": "data"},
"content_type": "json",
},
Expand All @@ -2070,7 +2069,7 @@ def test_payload_content_update_api_id_not_found():

def test_get_payload_content():
response = client.get(
url=f"/api/bot/{pytest.bot}/data/cognition",
url=f"/api/bot/{pytest.bot}/data/cognition?collection=Details",
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
actual = response.json()
Expand All @@ -2086,7 +2085,7 @@ def test_delete_payload_content():
response = client.delete(
url=f"/api/bot/{pytest.bot}/data/cognition/{pytest.payload_id}",
json={
"cognition_id": pytest.payload_id,
"row_id": pytest.payload_id,
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
Expand All @@ -2103,7 +2102,7 @@ def test_delete_payload_content_does_not_exist():
response = client.delete(
url=f"/api/bot/{pytest.bot}/data/cognition/{payload_id}",
json={
"cognition_id": payload_id,
"row_id": payload_id,
},
headers={"Authorization": pytest.token_type + " " + pytest.access_token}
)
Expand Down
6 changes: 4 additions & 2 deletions tests/unit_test/data_processor/data_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14614,7 +14614,7 @@ def _list_cognition_data(*args, **kwargs):
"content_type": "text",
"collection": collection}
pytest.content_id_unit = processor.save_cognition_data(payload, user, bot)
kwargs = {'data': 'Unit testing'}
kwargs = {'collection': 'testing', 'data': 'Unit testing'}
data = list(processor.list_cognition_data(bot, **kwargs))
assert data[0][
'data'] == 'Unit testing is a software testing technique in which individual units or components of a ' \
Expand All @@ -14623,6 +14623,7 @@ def _list_cognition_data(*args, **kwargs):
assert data[0]['collection'] == 'testing'
kwargs = {}
actual = list(processor.list_cognition_data(bot, **kwargs))
print(actual)
assert actual[0][
'data'] == 'Unit testing is a software testing technique in which individual units or components of a ' \
'software application are tested in isolation to ensure that each unit functions as expected. '
Expand Down Expand Up @@ -14790,7 +14791,8 @@ def test_get_payload_content(self):
processor = CognitionDataProcessor()
bot = 'test'
user = 'testUser'
data = list(processor.list_cognition_data(bot))
kwargs = {'collection': 'test_save_payload_content'}
data = list(processor.list_cognition_data(bot, **kwargs))
print(data)
assert data[0][
'data'] == {"name": "Digite", "city": "Mumbai"}
Expand Down

0 comments on commit 5eef626

Please sign in to comment.