File: test_core.py

package info (click to toggle)
python-flask-marshmallow 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: python: 1,104; makefile: 169; sh: 17
file content (61 lines) | stat: -rwxr-xr-x 1,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json

from flask import Flask, url_for
from werkzeug.wrappers import Response

from flask_marshmallow import Marshmallow


def test_deferred_initialization():
    app = Flask(__name__)
    m = Marshmallow()
    m.init_app(app)

    assert "flask-marshmallow" in app.extensions


def test_schema(app, schemas, mockauthor):
    s = schemas.AuthorSchema()
    result = s.dump(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_jsonify_instance(app, schemas, mockauthor):
    s = schemas.AuthorSchema()
    resp = s.jsonify(mockauthor)
    assert isinstance(resp, Response)
    assert resp.content_type == "application/json"
    obj = json.loads(resp.get_data(as_text=True))
    assert isinstance(obj, dict)


def test_jsonify_collection(app, schemas, mockauthorlist):
    s = schemas.AuthorSchema()
    resp = s.jsonify(mockauthorlist, many=True)
    assert isinstance(resp, Response)
    assert resp.content_type == "application/json"
    obj = json.loads(resp.get_data(as_text=True))
    assert isinstance(obj, list)


def test_jsonify_collection_via_schema_attr(app, schemas, mockauthorlist):
    s = schemas.AuthorSchema(many=True)
    resp = s.jsonify(mockauthorlist)
    assert isinstance(resp, Response)
    assert resp.content_type == "application/json"
    obj = json.loads(resp.get_data(as_text=True))
    assert isinstance(obj, list)


def test_links_within_nested_object(app, schemas, mockbook):
    s = schemas.BookSchema()
    result = s.dump(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")