-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f80ec2
commit 4869c6f
Showing
7 changed files
with
68 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.