def categories(request): if request.method == "POST": title = request.POST['title'] Category(title=title).save() return redirect('/categories') categories = Category.objects.all() return render(request, "category.html", {"categories": categories})
async def test_query_book(mocker): class AsyncMock(MagicMock): async def __call__(self, *args, **kwargs): return super(AsyncMock, self).__call__(*args, **kwargs) book_id = '1' double = mocker.patch.object(PostgresPersistence, 'filter', new_callable=AsyncMock) author = Author(name='martin fowler') category = Category(title='software engineering') book = Book(id=1, external_id=book_id, title='refactoring', subtitle='improving the design of existing code', authors=[author], categories=[category], published_year=1999, editor=Editor(name='addison wesley'), description='whatever', saved=True) double.return_value = [book] book_DTO = BookDTO( external_id=book_id, title='refactoring', subtitle='improving the design of existing code', authors=[{ 'name': 'martin fowler' }], categories=[{ 'title': 'software engineering' }], published_year=1999, editor={'name': 'addison wesley'}, description='whatever', ) insert_controller = InsertBookController(data=book_DTO) await insert_controller.insert() controller = QueryBookController() books = await controller.filter(external_id=book_id) book = books[0] double.assert_called_once_with(external_id=book_id) assert book.id assert book.external_id == book_id assert book.title == 'refactoring' assert book.subtitle == 'improving the design of existing code' assert book.authors[0].name == 'martin fowler' assert book.categories[0].title == 'software engineering' assert book.published_year == 1999 assert book.editor.name == 'addison wesley' assert book.description == 'whatever' assert book.saved is True
def setUp(self): self.author = Author(name='John Doe') self.author.save() self.category = Category(description='Comedy', ) self.category.save() self.book = Book(title='Any book', author=self.author, category=self.category, pages=900, description='', published_in=datetime.today()) self.book.save()
async def filter(self, external_id): author = Author(name='martin fowler') category = Category(title='software engineering') book = Book( id=1, external_id=external_id, title='refactoring', subtitle='improving the design of existing code', authors=[author], categories=[category], published_year=1999, editor=Editor(name='addison wesley'), description='whatever', saved=True ) return [book]
def test_query_book(loop): def book_api(request): pass routes = [Route('/book', book_api)] app = Starlette(routes=routes, debug=True) client = TestClient(app) book_id = 1 book = Book(id=book_id, title='refactoring', subtitle='improving the design of existing code', author=[Author(name='martin fowler')], category=[Category(title='software engineering')], published_year=1999, editor='addison wesley', description='whatever', saved=True) loop.run_until_complete(book.save()) query_response = client.get('/books', params={'title': 'refactoring'}) result = query_response.json()[0] assert result == [{ 'id': book_id, 'title': 'refactoring', 'subtitle': 'improving the design of existing code', 'author': [{ 'name': 'martin fowler' }], 'category': [{ 'title': 'software engineering' }], 'published_year': 1999, 'editor': 'addison wesley', 'description': 'whatever', 'saved': True }]
def setUp(self): self.category = Category(description='Comedy') self.category.save()