File: test_json_app.py

package info (click to toggle)
flask-mongoengine 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 508 kB
  • sloc: python: 2,127; makefile: 109
file content (49 lines) | stat: -rw-r--r-- 1,379 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
import flask
import pytest
from bson import ObjectId


@pytest.fixture(autouse=True)
def setup_endpoints(app, todo):
    Todo = todo

    @app.route("/")
    def index():
        return flask.jsonify(result=Todo.objects())

    @app.route("/add", methods=["POST"])
    def add():
        form = flask.request.form
        todo = Todo(title=form["title"], text=form["text"])
        todo.save()
        return flask.jsonify(result=todo)

    @app.route("/show/<id>/")
    def show(id):
        return flask.jsonify(result=Todo.objects.get_or_404(id=id))


def test_with_id(app, todo):
    Todo = todo
    client = app.test_client()
    response = client.get("/show/%s/" % ObjectId())
    assert response.status_code == 404

    response = client.post("/add", data={"title": "First Item", "text": "The text"})
    assert response.status_code == 200

    response = client.get("/show/%s/" % Todo.objects.first().id)
    assert response.status_code == 200

    result = flask.json.loads(response.data).get("result")
    assert ("title", "First Item") in result.items()


def test_basic_insert(app):
    client = app.test_client()
    client.post("/add", data={"title": "First Item", "text": "The text"})
    client.post("/add", data={"title": "2nd Item", "text": "The text"})
    rv = client.get("/")
    result = flask.json.loads(rv.data).get("result")

    assert len(result) == 2