|
| 1 | +import flask |
| 2 | +import pytest |
| 3 | +from bson import ObjectId |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def setup_endpoints(app, todo): |
| 8 | + Todo = todo |
| 9 | + |
| 10 | + @app.route("/") |
| 11 | + def index(): |
| 12 | + return "\n".join(x.title for x in Todo.objects) |
| 13 | + |
| 14 | + @app.route("/add", methods=["POST"]) |
| 15 | + def add(): |
| 16 | + form = flask.request.form |
| 17 | + todo = Todo(title=form["title"], text=form["text"]) |
| 18 | + todo.save() |
| 19 | + return "added" |
| 20 | + |
| 21 | + @app.route("/show/<id>/") |
| 22 | + def show(id): |
| 23 | + todo = Todo.objects.get_or_404(id=id) |
| 24 | + return "\n".join([todo.title, todo.text]) |
| 25 | + |
| 26 | + |
| 27 | +def test_with_id(app, todo): |
| 28 | + Todo = todo |
| 29 | + client = app.test_client() |
| 30 | + response = client.get("/show/%s/" % ObjectId()) |
| 31 | + assert response.status_code == 404 |
| 32 | + |
| 33 | + client.post("/add", data={"title": "First Item", "text": "The text"}) |
| 34 | + |
| 35 | + response = client.get("/show/%s/" % Todo.objects.first_or_404().id) |
| 36 | + assert response.status_code == 200 |
| 37 | + assert response.data.decode("utf-8") == "First Item\nThe text" |
| 38 | + |
| 39 | + |
| 40 | +def test_basic_insert(app): |
| 41 | + client = app.test_client() |
| 42 | + client.post("/add", data={"title": "First Item", "text": "The text"}) |
| 43 | + client.post("/add", data={"title": "2nd Item", "text": "The text"}) |
| 44 | + response = client.get("/") |
| 45 | + assert response.data.decode("utf-8") == "First Item\n2nd Item" |
0 commit comments