InterpreTalk / backend /tests /unit_test.py
benjolo's picture
Uploading completed backend
ddc5bbd verified
raw
history blame
7.95 kB
import os
from dotenv import dotenv_values
from fastapi import FastAPI
from pymongo import MongoClient
from main import requests
import uuid
import pytest
from dotenv import load_dotenv
import requests
import json
# Test Root endpoint
def test_root_pass():
response = requests.get("http://127.0.0.1:8080/")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to InterpreTalk!"}
# POST /user/
# Test DB user record creation including response validation
def test_create_user_pass():
payload = {
"name": "Tester1",
"user_id": "testerID",
"email": "tester1@gmail.com"
}
response = requests.post("http://127.0.0.1:8080/user/", json=payload)
assert response.status_code == 201
'''Test User Endpoints'''
# GET /user/
# Test finding DB user record based on user ID
def test_find_user_pass():
response = requests.get("http://localhost:8080/user/ozpHhyum3sayTdxIKUAtF51uvWJ2") # existing user record
assert response.status_code == 200
assert response.json() == {
"user_id": "ozpHhyum3sayTdxIKUAtF51uvWJ2",
"name": "Benjamin",
"email": "benjolounchained@gmail.com"
}
def test_find_user_fail():
response = requests.get(f"http://127.0.0.1:8080/users/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# PUT /user/{user_id}
# Updating DB user record based on user ID
def test_update_user_pass():
payload = {
"name": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/testerID", json=payload)
assert response.status_code == 202
assert response.json() == {
"name": "TesterNewName",
"user_id": "testerID",
"email": "tester1@gmail.com"
}
# Test with non-existing user ID
def test_update_user_fail():
payload = {
"name": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/falseID", json=payload)
assert response.status_code == 404
# DELETE /user/{user_id}
def test_delete_user_pass():
response = requests.delete(f"http://127.0.0.1:8080/users/testerID")
assert response.status_code == 200
def test_delete_user_fail():
response = requests.delete(f"http://127.0.0.1:8080/users/fakeID")
assert response.status_code == 404
# GET /user/find-name-id/{user_ud}
def test_find_name_id_pass():
response = requests.get("http://127.0.0.1:8080/user/find-name-id/ozpHhyum3sayTdxIKUAtF51uvWJ2")
assert response.status_code == 201
assert response.json == {
'name': "Benjamin"
}
def test_find_name_id_fail():
response = requests.get("http://127.0.0.1:8080/user/find-name-id/falseID")
assert response.status_code == 404
'''Test Call endpoints'''
# POST /call/create-call
# Test creating call record
def test_create_call_pass():
payload = {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
response = requests.post("http://127.0.0.1:8080/call/create-call", json=payload)
assert response.status_code == 201
# GET /call/find-call
# Test finding DB call record based on call ID
def test_find_call_pass():
response = requests.get(f"http://127.0.0.1:8080/call/test001") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_find_call_fail():
response = requests.get(f"http://127.0.0.1:8080/call/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/find-user-call
# Test finding DB call record based on user ID
def test_find_user_call_pass():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/tester01") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_find_user_call_fail():
response = requests.get(f"http://127.0.0.1:8080/calls/fakeID") # non-existing user record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/get-captions
# Test finding DB call record based on user ID
def test_get_captions_pass():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test001/tester01") # existing user record
assert response.status_code == 200
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
def test_get_captions_fail():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test001/tester00") # fake user record
# check if response is inteded error code
assert response.status_code == 404
def test_get_captions_fail():
response = requests.get(f"http://127.0.0.1:8080/call/find-user-calls/test000/tester01") # fake call record
# check if response is inteded error code
assert response.status_code == 404
# GET /call/update-call/{call_id}
# test updating call record based on id
def test_update_call_pass():
payload = {
"callee_id": "TesterNewName"
}
response = requests.patch(f"http://127.0.0.1:8080/call/update-call/tester02", json=payload)
assert response.status_code == 202
assert response.json() == {
"call_id": "test001",
"caller_id": "tester01",
"callee_id": "tester02",
"captions": [
{
"author_id": "tester01",
"author_username": "tester",
"original_text": "It is a test",
"translated_text": "Es un prueba",
}
]
}
# Test with non-existing user ID
def test_update_call_fail():
payload = {
"callee_id": "testName"
}
response = requests.patch(f"http://127.0.0.1:8080/users/falseID", json=payload)
assert response.status_code == 404
# GET /call/update-captions/{call_id}
# test updating caption record based on id
def test_update_caption_pass():
payload = {
"author_username": "testerNew"
}
response = requests.patch(f"http://127.0.0.1:8080/call/update-caption/tester01", json=payload)
assert response.status_code == 202
# Test with non-existing user ID
def test_update_call_fail():
payload = {
"callee_id": "testName"
}
response = requests.patch(f"http://127.0.0.1:8080/update-caption/falseID", json=payload)
assert response.status_code == 404
# DELETE /call/delete-call/{call_id}
def test_delete_user_pass():
response = requests.delete(f"http://127.0.0.1:8080//call/delete-call/test001")
assert response.status_code == 200
def test_delete_user_fail():
response = requests.delete(f"http://127.0.0.1:8080//call/delete-call/test009")
assert response.status_code == 404