-
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.
SQlite for unit testing, depreciated methods replaced and unit tests …
…for service layer (#2) * test: create base test class * feat: removed depreciated methods and fixed nested session issues * test: setup sqlite, tests for management_service
- Loading branch information
1 parent
5a4b5e9
commit ba16206
Showing
8 changed files
with
124 additions
and
14 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
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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"birthDate": "2001-01-01", | ||
} | ||
|
||
|
||
author_create_without_name_mock = { | ||
"email": "[email protected]", | ||
"nationality": "Brazil", | ||
|
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,11 +1,21 @@ | ||
from backend.tests.mocks.author_mock import author_create_mock | ||
|
||
book_create_with_invalid_author_mock = { | ||
"title": "Aboovk", | ||
"authors": [{"nonExistentProperty": "123"}], | ||
"pages": 100, | ||
} | ||
|
||
book_create_with_existent_author_id_mock = { | ||
book_create_mock = { | ||
"title": "Aboovk", | ||
"authors": [{"existentAuthorId": "123"}], | ||
"authors": [{"authorCreationPayload": author_create_mock}], | ||
"pages": 100, | ||
} | ||
|
||
|
||
def get_book_with_authors(authors): | ||
return { | ||
"title": "Aboovk", | ||
"authors": authors, | ||
"pages": 100, | ||
} |
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,9 @@ | ||
import copy | ||
|
||
|
||
def get_mock_with_custom_args(mock_dict, **kwargs): | ||
custom_mock = copy.deepcopy(mock_dict) | ||
for key, value in kwargs.items(): | ||
custom_mock[key] = value | ||
|
||
return custom_mock |
Empty file.
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,71 @@ | ||
from datetime import date | ||
from backend.tests.conftest import BaseTestCase | ||
from backend.tests.mocks.author_mock import author_create_mock, author_create_without_name_mock | ||
from backend.tests.mocks.mock_utils import get_mock_with_custom_args | ||
from backend.tests.mocks.books_mock import ( | ||
book_create_with_invalid_author_mock, | ||
get_book_with_authors, | ||
) | ||
|
||
from backend.app.services.management_service import ManagementService | ||
|
||
management_service = ManagementService() | ||
|
||
author_mock = get_mock_with_custom_args(author_create_mock, birthDate=date.today()) | ||
book_mock = get_book_with_authors([{"authorCreationPayload": author_mock}]) | ||
|
||
|
||
class TestCreateBook(BaseTestCase): | ||
def test_should_create_book_and_its_author(self): | ||
self.assertEqual(len(management_service.get_authors()), 0) | ||
|
||
book = management_service.create_book(book_mock) | ||
|
||
self.assertEqual(len(management_service.get_authors()), 1) | ||
|
||
self.assertEqual(book.get("title"), book_mock.get("title")) | ||
self.assertEqual(book.get("pages"), book_mock.get("pages")) | ||
|
||
def test_should_create_book_and_find_a_created_author(self): | ||
self.assertEqual(len(management_service.get_authors()), 0) | ||
|
||
author = management_service.create_author(author_mock) | ||
|
||
self.assertIsNotNone(author.get("id")) | ||
book = management_service.create_book( | ||
get_book_with_authors([{"existentAuthorId": author.get("id")}]) | ||
) | ||
|
||
self.assertEqual(len(management_service.get_authors()), 1) | ||
|
||
self.assertEqual(book.get("title"), book_mock.get("title")) | ||
self.assertEqual(book.get("pages"), book_mock.get("pages")) | ||
|
||
def test_should_fail_when_book_does_not_have_required_field(self): | ||
with self.assertRaises(Exception): | ||
management_service.create_book(book_create_with_invalid_author_mock) | ||
|
||
|
||
class TestCreateAuthor(BaseTestCase): | ||
def test_should_create_author(self): | ||
author = management_service.create_author(author_mock) | ||
self.assertEqual(author.get("name"), author_mock.get("name")) | ||
self.assertEqual(author.get("email"), author_mock.get("email")) | ||
|
||
def test_should_fail_when_author_does_not_have_required_field(self): | ||
with self.assertRaises(Exception): | ||
management_service.create_author(author_create_without_name_mock) | ||
|
||
|
||
class TestGetAuthors(BaseTestCase): | ||
def test_count_should_start_being_zero(self): | ||
authors = management_service.get_authors() | ||
self.assertEqual(len(authors), 0) | ||
|
||
def test_should_have_one_author_after_creation(self): | ||
|
||
author = management_service.create_author(author_mock) | ||
|
||
authors = management_service.get_authors() | ||
self.assertEqual(len(authors), 1) | ||
self.assertEqual(author.get("name"), authors[0].get("name")) |