def test_update_book_update(): updated_book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='Fahrenheit 451', author='Ray Bradbury', year=1984, language='English', is_available=True, reader=None) repo = mock.Mock() repo.update.return_value = updated_book book_update_use_case = books.BookUpdateUseCase(repo) request_object = request_books.BookUpdateRequestObject.from_dict({ 'pk': 1, 'patch': { 'title': 'Fahrenheit 451', 'author': 'Ray Bradbury' } }) response_object = book_update_use_case.execute(request_object) assert bool(response_object) is True repo.update.assert_called_with(patch={ 'title': 'Fahrenheit 451', 'author': 'Ray Bradbury' }, pk=1) assert response_object.value == updated_book
def test_give_book_use_case(): patch = { 'reader': { 'code': 'f853578c-fc0f-4e65-81b8-566c5dffa355', 'full_name': 'VS', 'reg_date': '01-01-2000' } } book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='Fahrenheit 451', author='Ray Bradbury', year=1984, language='English', is_available=False, reader=patch.get('reader')) repo = mock.Mock() repo.give.return_value = book book_give_use_case = books.BookGiveUseCase(repo) request_object = request_books.BookGiveRequestObject.from_dict({ 'pk': 1, 'patch': patch }) response_object = book_give_use_case.execute(request_object) assert bool(response_object) is True repo.give.assert_called_with(pk=1, patch=patch) assert response_object.value == book
def test_with_good_arguments(self, mocked_use_case): book = Book.from_dict({ 'code': '3251a5bd-86be-428d-8ae9-6e51a8048c33', 'title': '1984', 'author': 'George Orwell', 'year': 1984, 'language': 'English', 'is_available': True, 'reader': None }) mocked_use_case().execute.return_value = ro.ResponseSuccess(book) response = self.c.post(reverse('books_list'), { 'code': '3251a5bd-86be-428d-8ae9-6e51a8048c33', 'title': '1984', 'author': 'George Orwell', 'year': 1984, 'language': 'English', 'is_available': True, 'reader': None }) self.assertEqual(response.status_code, 201) self.assertEqual(json.loads(json.dumps(book, cls=books.BookEncoder)), json.loads(response.content.decode('utf-8')))
def test_book_update_with_bad_patch_parameters(): updated_book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='Fahrenheit 451', author='Ray Bradbury', year=1984, language='English', is_available=True, reader=None) repo = mock.Mock() repo.update.return_value = updated_book book_update_use_case = books.BookUpdateUseCase(repo) request_object = request_books.BookUpdateRequestObject.from_dict({ 'pk': 1, 'patch': { 'ololo': 'ololo' } }) response_object = book_update_use_case.execute(request_object) assert bool(response_object) is False assert response_object.value == { 'patch': ['parameters in patch are wrong'] }
def test_book_model_init(): code = uuid.uuid4() book = Book(code=code, title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) assert book.code == code assert book.title == '1984' assert book.author == 'George Orwell' assert book.year == 1984 assert book.language == 'English' assert book.is_available is True assert book.reader is None
def _convert_to_domain(cls, book): domain_book = DomainBook.from_dict({ 'code': book.code, 'title': book.title, 'author': book.author, 'year': book.year, 'language': book.language, 'is_available': book.is_available, 'reader': book.reader }) return domain_book
def test_book_details_without_key(): book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) repo = mock.Mock() repo.details.return_value = book book_details_use_case = books.BookDetailsUseCase(repo) request_object = request_books.BookDetailsRequestObject.from_dict({'blah': 'ololo'}) response_object = book_details_use_case.execute(request_object) assert bool(response_object) is False assert response_object.value == {'primary key': ['has to pass primary key']}
def test_get_book_details(): book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) repo = mock.Mock() repo.details.return_value = book book_details_use_case = books.BookDetailsUseCase(repo) request_object = request_books.BookDetailsRequestObject.from_dict({'pk': 1}) response_object = book_details_use_case.execute(request_object) assert bool(response_object) is True repo.details.assert_called_with(pk=1) assert response_object.value == book
def test_repository_return(self, mocked_use_case): updated_book = Book.from_dict({'code': '3251a5bd-86be-428d-8ae9-6e51a8048c33', 'title': 'Fahrenheit 451', 'author': 'Ray Bradbury', 'year': 1984, 'language': 'English', 'is_available': True, 'reader': None }) mocked_use_case().execute.return_value = ro.ResponseSuccess(updated_book) response = self.c.patch('/books/1/', {"reader": None, 'action': 'take'}) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(json.dumps(updated_book, cls=books.BookEncoder)), json.loads(response.content.decode('utf-8')))
def test_book_addition(adict): code = 'f853578c-fc0f-4e65-81b8-566c5dffa35a' book = Book(code=code, title=adict['title'], author=adict['author'], year=adict['year'], language=adict['language'], is_available=True, reader=None) repo = mock.Mock() repo.from_dict.return_value = book book_list_add_case = books.BookAddUseCase(repo) request_object = request_books.BookAddRequestObject.from_dict(adict) response_object = book_list_add_case.execute(request_object) assert bool(response_object) is True repo.from_dict.assert_called_with(adict) assert response_object.value == book
def test_book_model_from_dict(): code = uuid.uuid4() book = Book.from_dict( { 'code': code, 'title': '1984', 'author': 'George Orwell', 'year': 1984, 'language': 'English', 'is_available': True, 'reader': None } ) assert book.code == code assert book.title == '1984' assert book.author == 'George Orwell' assert book.year == 1984 assert book.language == 'English' assert book.is_available is True assert book.reader is None
def test_repository_give(self, mocked_use_case): reader = Reader.from_dict({ 'code': 'r2rwr3re-bdfc-e2ww-5644-hd94id04kd9r', 'full_name': 'John Smith', 'reg_date': datetime.date(2017, 2, 13) }) updated_book = Book.from_dict({'code': '3251a5bd-86be-428d-8ae9-6e51a8048c33', 'title': 'Fahrenheit 451', 'author': 'Ray Bradbury', 'year': 1984, 'language': 'English', 'is_available': False, 'reader': reader }) mocked_use_case().execute.return_value = ro.ResponseSuccess(updated_book) response = self.c.patch('/books/1/', {"reader": reader, 'action': 'give'}) self.assertEqual(response.status_code, 200) self.assertEqual(json.loads(json.dumps(updated_book, cls=books.BookEncoder)), json.loads(response.content.decode('utf-8')))
def test_serialize_domain_book(): book = Book(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) expected_json = """ { "code": "f853578c-fc0f-4e65-81b8-566c5dffa35a", "title": "1984", "author": "George Orwell", "year": 1984, "language": "English", "is_available": true, "reader": "null" } """ assert json.loads(json.dumps( book, cls=books.BookEncoder)) == json.loads(expected_json)
from unittest import mock from domains.book import Book from shared import response_object as res dictionary = { 'code': '3251a5bd-86be-428d-8ae9-6e51a8048c33', 'title': '1984', 'author': 'George Orwell', 'year': 1984, 'language': 'English', 'is_available': True, 'reader': None } book = Book.from_dict(dictionary) books = [book] @mock.patch('flask_interface.library.books.book.BookListUseCase') def test_get(mock_use_case, client): mock_use_case().execute.return_value = res.ResponseSuccess(books) http_response = client.get('/books') assert json.loads(http_response.data) == [dictionary] assert http_response.status_code == 200 assert http_response.mimetype == 'application/json'