From 4869c6ff1a2d47e1bf4dd2c686857434a473bb38 Mon Sep 17 00:00:00 2001 From: Gabriel Borges Date: Fri, 17 May 2024 12:43:14 -0300 Subject: [PATCH] fix: adjust unit tests --- backend/tests/__init__.py | 5 --- backend/tests/conftest.py | 18 ++++++++ .../controllers/test_author_controller.py | 45 ++++++++++++++----- .../tests/controllers/test_book_controller.py | 23 ++++------ backend/tests/mocks/author_mock.py | 8 +++- backend/tests/services/__init__.py | 0 .../tests/services/test_management_service.py | 23 ---------- 7 files changed, 68 insertions(+), 54 deletions(-) create mode 100644 backend/tests/conftest.py delete mode 100644 backend/tests/services/__init__.py delete mode 100644 backend/tests/services/test_management_service.py diff --git a/backend/tests/__init__.py b/backend/tests/__init__.py index 23721cb..e69de29 100644 --- a/backend/tests/__init__.py +++ b/backend/tests/__init__.py @@ -1,5 +0,0 @@ -from unittest.mock import Mock -import sys - -sys.modules["backend.app.models.model"] = Mock() -from backend.main import app diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py new file mode 100644 index 0000000..2aec0be --- /dev/null +++ b/backend/tests/conftest.py @@ -0,0 +1,18 @@ +from flask import Flask + +from backend.app.handlers.http_error_handler import handle_exception + +from backend.app.models.model import db + +from backend.app.controllers.book_controller import blueprint as books_bp +from backend.app.controllers.author_controller import blueprint as authors_bp + + +app = Flask(__name__) +app.config["TESTING"] = True +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False +db.init_app(app) +app.register_blueprint(books_bp) +app.register_blueprint(authors_bp) +app.register_error_handler(400, handle_exception) diff --git a/backend/tests/controllers/test_author_controller.py b/backend/tests/controllers/test_author_controller.py index b922ddd..107919b 100644 --- a/backend/tests/controllers/test_author_controller.py +++ b/backend/tests/controllers/test_author_controller.py @@ -1,27 +1,52 @@ import unittest from unittest.mock import patch -from backend.tests import app +from backend.tests.conftest import app +from backend.tests.mocks.author_mock import author_create_without_name_mock, author_create_mock -class TestAuthorController(unittest.TestCase): - +class TestGetAuthors(unittest.TestCase): @patch("backend.app.services.management_service.ManagementService.get_authors") def test_get_all(self, mock_get_authors): - books_mock_result = [{"name": "Author 1"}] - mock_get_authors.return_value = books_mock_result + authors_mock_result = [{"id": 1, "name": "Author 1"}] + mock_get_authors.return_value = authors_mock_result - with app.test_client() as client: - response = client.get("/authors/") + with app.test_client() as testing_client: + response = testing_client.get("/authors/") self.assertEqual(response.status_code, 200) self.assertEqual( response.json, - books_mock_result, + authors_mock_result, ) mock_get_authors.assert_called_once() -if __name__ == "__main__": - unittest.main() +class TestCreateAuthor(unittest.TestCase): + @patch("backend.app.services.management_service.ManagementService.create_author") + def test_should_return_bad_request_when_author_is_empty(self, mock_create_author): + authors_mock_result = {"title": "Author 1"} + mock_create_author.return_value = authors_mock_result + + with app.test_client() as client: + response = client.post("/authors/", json=author_create_without_name_mock) + + self.assertEqual(response.status_code, 400) + + @patch("backend.app.services.management_service.ManagementService.create_author") + def test_should_succeed(self, mock_create_author): + authors_mock_result = {"title": "Author 1"} + mock_create_author.return_value = authors_mock_result + + with app.test_client() as client: + response = client.post("/authors/", json=author_create_mock) + + self.assertEqual(response.status_code, 201) + + self.assertEqual( + response.json, + authors_mock_result, + ) + + mock_create_author.assert_called_once() diff --git a/backend/tests/controllers/test_book_controller.py b/backend/tests/controllers/test_book_controller.py index 83664cb..3e73c66 100644 --- a/backend/tests/controllers/test_book_controller.py +++ b/backend/tests/controllers/test_book_controller.py @@ -1,17 +1,16 @@ import unittest from unittest.mock import patch -from backend.tests import app +from backend.tests.conftest import app from backend.tests.mocks.books_mock import ( book_create_with_invalid_author_mock, book_create_with_existent_author_id_mock, ) -class TestBookController(unittest.TestCase): - +class TestGetBooks(unittest.TestCase): @patch("backend.app.services.management_service.ManagementService.get_books") - def test_get_all_should_succeed(self, mock_get_books): + def test_should_succeed(self, mock_get_books): books_mock_result = [{"title": "Book 1"}, {"title": "Book 2"}] mock_get_books.return_value = books_mock_result @@ -27,10 +26,10 @@ def test_get_all_should_succeed(self, mock_get_books): mock_get_books.assert_called_once() + +class TestCreateBook(unittest.TestCase): @patch("backend.app.services.management_service.ManagementService.create_book") - def test_create_book_should_return_bad_request_when_author_is_empty( - self, mock_create_book - ): + def test_should_return_bad_request_when_author_is_empty(self, mock_create_book): books_mock_result = {"title": "Book 1"} mock_create_book.return_value = books_mock_result @@ -44,14 +43,12 @@ def test_create_book_should_return_bad_request_when_author_is_empty( ) @patch("backend.app.services.management_service.ManagementService.create_book") - def test_create_book_should_succeed(self, mock_create_book): + def test_should_succeed(self, mock_create_book): books_mock_result = {"title": "Book 1"} mock_create_book.return_value = books_mock_result with app.test_client() as client: - response = client.post( - "/books/", json=book_create_with_existent_author_id_mock - ) + response = client.post("/books/", json=book_create_with_existent_author_id_mock) self.assertEqual(response.status_code, 201) @@ -61,7 +58,3 @@ def test_create_book_should_succeed(self, mock_create_book): ) mock_create_book.assert_called_once() - - -if __name__ == "__main__": - unittest.main() diff --git a/backend/tests/mocks/author_mock.py b/backend/tests/mocks/author_mock.py index 5a38603..bf5c6e7 100644 --- a/backend/tests/mocks/author_mock.py +++ b/backend/tests/mocks/author_mock.py @@ -1,6 +1,12 @@ -author_mock = { +author_create_mock = { "name": "str", "email": "email@test.com", "nationality": "Brazil", "birthDate": "2001-01-01", } + +author_create_without_name_mock = { + "email": "email@test.com", + "nationality": "Brazil", + "birthDate": "2001-01-01", +} diff --git a/backend/tests/services/__init__.py b/backend/tests/services/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/backend/tests/services/test_management_service.py b/backend/tests/services/test_management_service.py deleted file mode 100644 index ebb9f73..0000000 --- a/backend/tests/services/test_management_service.py +++ /dev/null @@ -1,23 +0,0 @@ -import unittest -from unittest.mock import patch - - -from backend.app.dtos.author_dto import AuthorResponseDto -from backend.app.services.management_service import ManagementService -from backend.tests.mocks.author_mock import author_mock - - -class TestManagementService(unittest.TestCase): - - @patch("backend.app.models.author_model.AuthorModel.query", create=True) - def test_get_authors_should_succeed(self, mock_query): - books_mock_result = [ - AuthorResponseDto(**author_mock), - AuthorResponseDto(**author_mock), - ] - mock_query.all.return_value = books_mock_result - - response = ManagementService().get_authors() - - self.assertEqual(len(response), len(books_mock_result)) - mock_query.all.assert_called_once()