Add unit tests for answer schema, request data schema, base controller, and history manager

This commit is contained in:
sHa
2024-03-22 00:50:05 +02:00
parent 187d405186
commit 56125f0dbb
5 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import unittest
from schema.answer import Answer
from schema.status import Status
class TestAnswerSchema(unittest.TestCase):
def test_answer_creation(self):
status = Status.success
message = "Test message"
answer = Answer(status=status, message=message)
self.assertEqual(answer.status, status)
self.assertEqual(answer.message, message)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,34 @@
import unittest
from controller.base_controller import BaseController
class TestBaseController(unittest.TestCase):
def setUp(self):
self.controller = BaseController()
def test_check_namespace_restricted(self):
restricted_namespaces = [
"__history",
"__last_request",
"__last",
"__clear",
"docs",
"redoc",
"api",
]
for namespace in restricted_namespaces:
result = self.controller.check_namespace(namespace)
self.assertFalse(result)
def test_check_namespace_allowed(self):
allowed_namespaces = [
"test",
"example",
"namespace",
"custom",
]
for namespace in allowed_namespaces:
result = self.controller.check_namespace(namespace)
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,122 @@
import unittest
from fastapi import Response, status
from schema.answer import Answer
from schema.request_data import RequestData
from manager.history import History
from controller.history_controller import HistoryController
from schema.status import Status
from unittest import IsolatedAsyncioTestCase
from schema.methods import Methods
from typing import Any
class TestHistoryController(IsolatedAsyncioTestCase):
def setUp(self):
self.namespace = "test"
self.namespace_invalid = "api"
self.request_data: dict[Any, Any] = {
"data": {},
"method": Methods.GET,
"url": "https://example.com",
"headers": {"Content-Type": "application/json"},
"time": "2024-01-01 12:00:00",
}
self.controller = HistoryController()
self.history = History(namespace=self.namespace)
async def test_history_invalid_namespace(self):
response = Response()
result = await self.controller.history(
response=response, namespace=self.namespace_invalid
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
result,
Answer(status=Status.error, message="Invalid namespace name provided."),
)
async def test_history_with_id_existing_request(self):
self.history.add(self.request_data)
response = Response()
result = await self.controller.history(response, id=0, namespace=self.namespace)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(result, RequestData(**self.request_data))
async def test_history_with_id_non_existing_request(self):
self.history.clear()
response = Response()
result = await self.controller.history(
response=response, id=0, namespace=self.namespace
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
result, Answer(status=Status.error, message="No requests found.")
)
async def test_history_without_id(self):
self.history.clear()
self.history.add(self.request_data)
response = Response()
result = await self.controller.history(
response=response, namespace=self.namespace
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(result, [RequestData(**self.request_data)])
async def test_last_requests_invalid_namespace(self):
response = Response()
result = await self.controller.last_requests(
response, namespace=self.namespace_invalid
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
result,
Answer(status=Status.error, message="Invalid namespace name provided."),
)
async def test_last_requests_existing_request(self):
self.history.clear()
self.history.add(self.request_data)
response = Response()
result = await self.controller.last_requests(
response=response, namespace=self.namespace
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(result, RequestData(**self.request_data))
async def test_last_requests_non_existing_request(self):
self.history.clear()
response = Response()
result = await self.controller.last_requests(
response=response, namespace=self.namespace
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
result, Answer(status=Status.error, message="No requests found.")
)
async def test_clear_history_invalid_namespace(self):
response = Response()
result = await self.controller.clear_history(
response=response, namespace=self.namespace_invalid
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
result,
Answer(status=Status.error, message="Invalid namespace name provided."),
)
async def test_clear_history(self):
response = Response()
self.history.add(self.request_data)
result = await self.controller.clear_history(
response=response, namespace=self.namespace
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
result, Answer(status=Status.success, message="History cleared.")
)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,86 @@
import unittest
from manager.history import History
from schema.methods import Methods
from typing import Any
class TestHistoryManager(unittest.TestCase):
def setUp(self):
self.namespace = "test"
self.request_data: dict[Any, Any] = {
"data": {},
"method": Methods.GET,
"url": "https://example.com",
"headers": {"Content-Type": "application/json"},
"time": "2024-01-01 12:00:00",
}
self.history = History(namespace=self.namespace)
def tearDown(self):
self.history.clear()
def test_load_empty_history(self):
self.history.load()
self.assertEqual(self.history.data, [])
def test_save_and_load_history(self):
self.history.add(self.request_data)
self.history.save()
self.history.load()
self.assertEqual(self.history.data, [self.request_data])
def test_clear_history(self):
self.history.add(self.request_data)
self.history.clear()
self.assertEqual(self.history.data, [])
def test_get_existing_request(self):
self.history.add(self.request_data)
result = self.history.get(0)
self.assertEqual(result, self.request_data)
def test_get_non_existing_request(self):
result = self.history.get(0)
self.assertIsNone(result)
def test_get_request_with_index_out_of_range(self):
self.history.add(self.request_data)
result = self.history.get(1)
self.assertIsNone(result)
def test_last_existing_request(self):
self.history.add(self.request_data)
result = self.history.last()
self.assertEqual(result, self.request_data)
def test_last_non_existing_request(self):
result = self.history.last()
self.assertIsNone(result)
def test_last_request_with_limit_reached(self):
for i in range(History.HISTORY_LIMIT + 1):
self.history.add(self.request_data)
result = self.history.last()
self.assertEqual(result, self.request_data)
def test_add_request(self):
self.history.clear()
self.history.add(self.request_data)
self.assertEqual(self.history.data, [self.request_data])
def test_add_request_with_limit_reached(self):
for i in range(History.HISTORY_LIMIT + 1):
self.history.add(self.request_data)
self.assertEqual(len(self.history.data), History.HISTORY_LIMIT)
def test_all_requests(self):
for i in range(History.HISTORY_LIMIT):
self.history.add(self.request_data)
result = self.history.all()
self.assertEqual(result, [self.request_data for i in range(History.HISTORY_LIMIT)])
def test_all_requests_empty(self):
result = self.history.all()
self.assertIsNone(result)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,22 @@
import unittest
from schema.request_data import RequestData
from schema.methods import Methods
class TestRequestDataSchema(unittest.TestCase):
def test_request_data_creation(self):
data = {"key": "value"}
method = Methods.GET
url = "https://example.com"
headers = {"Content-Type": "application/json"}
time = "2022-01-01 12:00:00"
request_data = RequestData(data=data, method=method, url=url, headers=headers, time=time)
self.assertEqual(request_data.data, data)
self.assertEqual(request_data.method, method)
self.assertEqual(request_data.url, url)
self.assertEqual(request_data.headers, headers)
self.assertEqual(request_data.time, time)
if __name__ == '__main__':
unittest.main()