コード例 #1
0
ファイル: books_nm.py プロジェクト: devdil/bookapi
def post_book():
    request_json = request.get_json()
    book_service = BookService()
    return book_service.create_book(request_json)
コード例 #2
0
ファイル: test_book_service.py プロジェクト: devdil/bookapi
    def test_create_book(self, book_p):
        with app.test_request_context():
            mobject = MagicMock(spec=BookService)
            request_body = {
                "authors": ["George R. R. Martin"],
                "country": "United States",
                "isbn": "978-24123",
                "name": "Diljit Books 8",
                "number_of_pages": 768,
                "publisher": "Bantam Books",
                "release_date": "1999-02"
            }
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')

            request_body = {
                "_id": "123",
                "authors": ["George R. R. Martin"],
                "country": "United States",
                "isbn": "978-24123",
                "name": "Diljit Books 8",
                "number_of_pages": 768,
                "publisher": "Bantam Books",
                "release_date": "1999-02-02"
            }
            a = MagicMock()
            a.save = MagicMock(side_effect=ValidationError)
            book_p.return_value = a
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')
            a = MagicMock()
            a.save = MagicMock(side_effect=FieldDoesNotExist)
            book_p.return_value = a
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')
            a.save = MagicMock(side_effect=NotUniqueError)
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')
            a.save = MagicMock(side_effect=Exception)
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')

            book = MagicMock()
            s_obj = MagicMock()
            s_obj.to_json = MagicMock(return_value=json.dumps(request_body))
            book.save = MagicMock(return_value=s_obj)
            book_p.return_value = book
            error_resp = json.loads(
                BookService.create_book(mobject,
                                        request_body).get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 201)
            request_body.pop('_id')
            assert error_resp['data'][0].keys()[0] == 'book'
            self.assertEqual(error_resp['status'], 'success')
            error_resp = json.loads(
                BookService.create_book(mobject, '').get_data(as_text=True))
            self.assertEqual(error_resp['status_code'], 400)
            self.assertEqual(error_resp['data'], [])
            self.assertEqual(error_resp['status'], 'failed')