def test_can_declare_model_schemas(self, extma, models, db): class AuthorSchema(extma.ModelSchema): class Meta: model = models.Author class BookSchema(extma.ModelSchema): class Meta: model = models.Book author_schema = AuthorSchema() book_schema = BookSchema() author = models.Author(name='Chuck Paluhniuk') db.session.add(author) db.session.commit() author = models.Author(name='Chuck Paluhniuk') book = models.Book(title='Fight Club', author=author) db.session.add(author) db.session.add(book) db.session.commit() author_result = get_dump_data(author_schema, author) assert 'id' in author_result assert 'name' in author_result assert author_result['name'] == 'Chuck Paluhniuk' assert author_result['books'][0] == book.id book_result = get_dump_data(book_schema, book) assert 'id' in book_result assert book_result['author'] == author.id resp = author_schema.jsonify(author) assert isinstance(resp, BaseResponse)
def test_can_declare_model_schemas(self, extma, models, db): class AuthorSchema(extma.ModelSchema): class Meta: model = models.Author class BookSchema(extma.ModelSchema): class Meta: model = models.Book author_schema = AuthorSchema() book_schema = BookSchema() author = models.Author(name="Chuck Paluhniuk") book = models.Book(title="Fight Club", author=author) author_result = get_dump_data(author_schema, author) assert "id" in author_result assert "name" in author_result assert author_result["name"] == "Chuck Paluhniuk" assert author_result["books"][0] == book.id book_result = get_dump_data(book_schema, book) assert "id" in book_result assert book_result["author"] == author.id resp = author_schema.jsonify(author) assert isinstance(resp, BaseResponse)
def test_hyperlink_related_field_errors(self, extma, models, db, extapp): class BookSchema(extma.ModelSchema): class Meta: model = models.Book author = HyperlinkRelated('author') book_schema = BookSchema() author = models.Author(name='Chuck Paluhniuk') book = models.Book(title='Fight Club', author=author) db.session.add(author) db.session.add(book) db.session.flush() # Deserialization fails on bad endpoint book_result = get_dump_data(book_schema, book) book_result['author'] = book.url deserialized, errors = get_load_data(book_schema, book_result) print(errors) assert 'expected "author"' in errors['author'][0] # Deserialization fails on bad URL key book_result = get_dump_data(book_schema, book) book_schema.fields['author'].url_key = 'pk' deserialized, errors = get_load_data(book_schema, book_result) assert 'URL pattern "pk" not found' in errors['author'][0]
def test_can_declare_table_schemas(self, extma, models, db): class AuthorSchema(extma.TableSchema): class Meta: table = models.Author.__table__ class BookSchema(extma.TableSchema): class Meta: table = models.Book.__table__ author_schema = AuthorSchema() book_schema = BookSchema() author = models.Author(name="Chuck Paluhniuk") book = models.Book(title="Fight Club", author=author) author_result = get_dump_data(author_schema, author) assert "id" in author_result assert "name" in author_result assert author_result["id"] == author.id assert author_result["name"] == "Chuck Paluhniuk" book_result = get_dump_data(book_schema, book) assert "id" in book_result assert "title" in book_result assert book_result["id"] == book.id assert book_result["title"] == book.title resp = author_schema.jsonify(author) assert isinstance(resp, BaseResponse)
def test_links_within_nested_object(app, schemas, mockbook): s = schemas.BookSchema() result = get_dump_data(s, mockbook) assert result['title'] == mockbook.title author = result['author'] assert author['links']['self'] == url_for('author', id=mockbook.author.id) assert author['links']['collection'] == url_for('authors')
def test_links_within_nested_object(app, schemas, mockbook): s = schemas.BookSchema() result = get_dump_data(s, mockbook) assert result["title"] == mockbook.title author = result["author"] assert author["links"]["self"] == url_for("author", id=mockbook.author.id) assert author["links"]["collection"] == url_for("authors")
def test_schema(app, schemas, mockauthor): s = schemas.AuthorSchema() result = get_dump_data(s, mockauthor) assert result['id'] == mockauthor.id assert result['name'] == mockauthor.name assert result['absolute_url'] == url_for('author', id=mockauthor.id, _external=True) links = result['links'] assert links['self'] == url_for('author', id=mockauthor.id) assert links['collection'] == url_for('authors')
def test_hyperlink_related_field_serializes_none(self, extma, models): class BookSchema(extma.ModelSchema): class Meta: model = models.Book author = extma.HyperlinkRelated("author") book_schema = BookSchema() book = models.Book(title="Fight Club", author=None) book_result = get_dump_data(book_schema, book) assert book_result["author"] is None
def test_schema(app, schemas, mockauthor): s = schemas.AuthorSchema() result = get_dump_data(s, mockauthor) assert result["id"] == mockauthor.id assert result["name"] == mockauthor.name assert result["absolute_url"] == url_for("author", id=mockauthor.id, _external=True) links = result["links"] assert links["self"] == url_for("author", id=mockauthor.id) assert links["collection"] == url_for("authors")
def test_hyperlink_related_field_list(self, extma, models, db, extapp): class AuthorSchema(extma.ModelSchema): class Meta: model = models.Author books = extma.List(HyperlinkRelated('book')) author_schema = AuthorSchema() author = models.Author(name='Chuck Paluhniuk') book = models.Book(title='Fight Club', author=author) db.session.add(author) db.session.add(book) db.session.flush() author_result = get_dump_data(author_schema, author) assert author_result['books'][0] == book.url deserialized, errors = get_load_data(author_schema, author_result) assert deserialized.books[0] == book
def test_hyperlink_related_field_external(self, extma, models, db, extapp): class BookSchema(extma.ModelSchema): class Meta: model = models.Book author = HyperlinkRelated('author', external=True) book_schema = BookSchema() author = models.Author(name='Chuck Paluhniuk') book = models.Book(title='Fight Club', author=author) db.session.add(author) db.session.add(book) db.session.flush() book_result = get_dump_data(book_schema, book) assert book_result['author'] == author.absolute_url deserialized, errors = get_load_data(book_schema, book_result) assert deserialized.author == author
def test_hyperlink_related_field(self, extma, models, db, extapp): class BookSchema(extma.ModelSchema): class Meta: model = models.Book author = extma.HyperlinkRelated("author") book_schema = BookSchema() author = models.Author(name="Chuck Paluhniuk") book = models.Book(title="Fight Club", author=author) db.session.add(author) db.session.add(book) db.session.flush() book_result = get_dump_data(book_schema, book) assert book_result["author"] == author.url deserialized, errors = get_load_data(book_schema, book_result) assert deserialized.author == author