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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
"""Pytest fixtures for the test suite."""
import pytest
from flask import Flask
from flask_marshmallow import Marshmallow
_app = Flask(__name__)
_app.testing = True
class Bunch(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
# Models
class Author(Bunch):
pass
class Book(Bunch):
pass
@pytest.fixture
def mockauthor():
author = Author(id=123, name="Fred Douglass")
return author
@pytest.fixture
def mockauthorlist():
a1 = Author(id=1, name="Alice")
a2 = Author(id=2, name="Bob")
a3 = Author(id=3, name="Carol")
return [a1, a2, a3]
@pytest.fixture
def mockbook(mockauthor):
book = Book(id=42, author=mockauthor, title="Legend of Bagger Vance")
return book
@_app.route("/author/<int:id>")
def author(id):
return "Steven Pressfield"
@_app.route("/authors/")
def authors():
return "Steven Pressfield, Chuck Paluhniuk"
@_app.route("/books/")
def books():
return "Legend of Bagger Vance, Fight Club"
@_app.route("/books/<id>")
def book(id):
return "Legend of Bagger Vance"
@pytest.fixture(scope="function")
def app():
ctx = _app.test_request_context()
ctx.push()
yield _app
ctx.pop()
@pytest.fixture(scope="function")
def ma(app):
return Marshmallow(app)
@pytest.fixture
def schemas(ma):
class AuthorSchema(ma.Schema):
id = ma.Integer()
name = ma.String()
absolute_url = ma.AbsoluteURLFor("author", values={"id": "<id>"})
links = ma.Hyperlinks(
{
"self": ma.URLFor("author", values={"id": "<id>"}),
"collection": ma.URLFor("authors"),
}
)
class BookSchema(ma.Schema):
id = ma.Integer()
title = ma.String()
author = ma.Nested(AuthorSchema)
links = ma.Hyperlinks(
{
"self": ma.URLFor("book", values={"id": "<id>"}),
"collection": ma.URLFor("books"),
}
)
# So we can access schemas.AuthorSchema, etc.
return Bunch(**locals())
|