mirror of
https://github.com/shadoll/scatcher.git
synced 2025-12-20 03:26:59 +00:00
Add unit tests for app and history controller
This commit is contained in:
72
tests/test_app.py
Normal file
72
tests/test_app.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import unittest
|
||||
from fastapi.testclient import TestClient
|
||||
from app import app
|
||||
from unittest import IsolatedAsyncioTestCase
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
class TestApp(IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
self.methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"]
|
||||
self.namespace = "test"
|
||||
self.namespace_invalid = "api"
|
||||
|
||||
def test_catch_endpoint(self):
|
||||
for method in self.methods:
|
||||
response = client.request(method, "/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
response_namespace = client.request(method, f"/{self.namespace}")
|
||||
self.assertEqual(response_namespace.status_code, 200)
|
||||
|
||||
def test_catch_invalid_namespace(self):
|
||||
response = client.get("/api")
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_last_requests_endpoint(self):
|
||||
response = client.get("/api/__last_request")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_last_requests_namespace(self):
|
||||
response = client.get(f"/api/__last_request/{self.namespace}")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_last_requests_invalid_namespace(self):
|
||||
response = client.get(f"/api/__last_request/{self.namespace_invalid}")
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_last_endpoint(self):
|
||||
response = client.get("/api/__last")
|
||||
self.assertEqual(response.status_code, 200) # WTF?
|
||||
|
||||
def test_last_namespace(self):
|
||||
response = client.get(f"/api/__last/{self.namespace}")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_history_endpoint(self):
|
||||
response = client.get("/api/__history")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_history_id_endpoint(self):
|
||||
response_id = client.get("/api/__history/1")
|
||||
self.assertEqual(response_id.status_code, 200)
|
||||
|
||||
def test_history_namespace(self):
|
||||
response_namespace = client.get(f"/api/__history/{self.namespace}")
|
||||
self.assertEqual(response_namespace.status_code, 200)
|
||||
|
||||
def test_history_namespace_id(self):
|
||||
response_namespace_id = client.get(f"/api/__history/{self.namespace}/1")
|
||||
self.assertEqual(response_namespace_id.status_code, 200)
|
||||
|
||||
def test_clear_history_endpoint(self):
|
||||
response = client.get("/api/__clear")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_clear_history_namespace(self):
|
||||
response = client.get(f"/api/__clear/{self.namespace}")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -9,6 +9,7 @@ from unittest import IsolatedAsyncioTestCase
|
||||
from schema.methods import Methods
|
||||
from typing import Any
|
||||
|
||||
|
||||
class TestHistoryController(IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
self.namespace = "test"
|
||||
@@ -38,7 +39,11 @@ class TestHistoryController(IsolatedAsyncioTestCase):
|
||||
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)
|
||||
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))
|
||||
|
||||
@@ -46,7 +51,9 @@ class TestHistoryController(IsolatedAsyncioTestCase):
|
||||
self.history.clear()
|
||||
response = Response()
|
||||
result = await self.controller.history(
|
||||
response=response, id=0, namespace=self.namespace
|
||||
response=response,
|
||||
id=0,
|
||||
namespace=self.namespace,
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(
|
||||
@@ -63,6 +70,17 @@ class TestHistoryController(IsolatedAsyncioTestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(result, [RequestData(**self.request_data)])
|
||||
|
||||
async def test_history_without_id_no_requests(self):
|
||||
self.history.clear()
|
||||
response = Response()
|
||||
result = await self.controller.history(
|
||||
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_last_requests_invalid_namespace(self):
|
||||
response = Response()
|
||||
result = await self.controller.last_requests(
|
||||
|
||||
Reference in New Issue
Block a user