Skip to content

Commit

Permalink
fix: adjust unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielborgesdm committed May 17, 2024
1 parent 4f80ec2 commit 4869c6f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 54 deletions.
5 changes: 0 additions & 5 deletions backend/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
from unittest.mock import Mock
import sys

sys.modules["backend.app.models.model"] = Mock()
from backend.main import app
18 changes: 18 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 35 additions & 10 deletions backend/tests/controllers/test_author_controller.py
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 8 additions & 15 deletions backend/tests/controllers/test_book_controller.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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()
8 changes: 7 additions & 1 deletion backend/tests/mocks/author_mock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
author_mock = {
author_create_mock = {
"name": "str",
"email": "[email protected]",
"nationality": "Brazil",
"birthDate": "2001-01-01",
}

author_create_without_name_mock = {
"email": "[email protected]",
"nationality": "Brazil",
"birthDate": "2001-01-01",
}
Empty file removed backend/tests/services/__init__.py
Empty file.
23 changes: 0 additions & 23 deletions backend/tests/services/test_management_service.py

This file was deleted.

0 comments on commit 4869c6f

Please sign in to comment.