|
1 | | -import datetime |
2 | | - |
3 | 1 | import flask |
| 2 | +import pytest |
4 | 3 | from bson import ObjectId |
5 | 4 |
|
6 | | -from flask_mongoengine import MongoEngine |
7 | | -from tests import FlaskMongoEngineTestCase |
8 | | - |
9 | | - |
10 | | -class BasicAppTestCase(FlaskMongoEngineTestCase): |
11 | | - def setUp(self): |
12 | | - super(BasicAppTestCase, self).setUp() |
13 | | - db = MongoEngine() |
14 | | - |
15 | | - class Todo(db.Document): |
16 | | - title = db.StringField(max_length=60) |
17 | | - text = db.StringField() |
18 | | - done = db.BooleanField(default=False) |
19 | | - pub_date = db.DateTimeField(default=datetime.datetime.now) |
20 | | - |
21 | | - db.init_app(self.app) |
22 | | - |
23 | | - Todo.drop_collection() |
24 | | - self.Todo = Todo |
25 | | - |
26 | | - @self.app.route("/") |
27 | | - def index(): |
28 | | - return "\n".join(x.title for x in self.Todo.objects) |
29 | 5 |
|
30 | | - @self.app.route("/add", methods=["POST"]) |
31 | | - def add(): |
32 | | - form = flask.request.form |
33 | | - todo = self.Todo(title=form["title"], text=form["text"]) |
34 | | - todo.save() |
35 | | - return "added" |
| 6 | +@pytest.fixture(autouse=True) |
| 7 | +def setup_endpoints(app, todo): |
| 8 | + Todo = todo |
36 | 9 |
|
37 | | - @self.app.route("/show/<id>/") |
38 | | - def show(id): |
39 | | - todo = self.Todo.objects.get_or_404(id=id) |
40 | | - return "\n".join([todo.title, todo.text]) |
| 10 | + @app.route("/") |
| 11 | + def index(): |
| 12 | + return "\n".join(x.title for x in Todo.objects) |
41 | 13 |
|
42 | | - self.db = db |
| 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" |
43 | 20 |
|
44 | | - def test_connection_default(self): |
45 | | - self.app.config["MONGODB_SETTINGS"] = {} |
46 | | - self.app.config["TESTING"] = True |
| 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]) |
47 | 25 |
|
48 | | - db = MongoEngine() |
49 | | - # Disconnect to drop connection from setup. |
50 | | - db.disconnect() |
51 | | - db.init_app(self.app) |
52 | 26 |
|
53 | | - def test_with_id(self): |
54 | | - c = self.app.test_client() |
55 | | - resp = c.get("/show/%s/" % ObjectId()) |
56 | | - self.assertEqual(resp.status_code, 404) |
| 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 |
57 | 32 |
|
58 | | - c.post("/add", data={"title": "First Item", "text": "The text"}) |
| 33 | + client.post("/add", data={"title": "First Item", "text": "The text"}) |
59 | 34 |
|
60 | | - resp = c.get("/show/%s/" % self.Todo.objects.first_or_404().id) |
61 | | - self.assertEqual(resp.status_code, 200) |
62 | | - self.assertEqual(resp.data.decode("utf-8"), "First Item\nThe text") |
| 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" |
63 | 38 |
|
64 | | - def test_basic_insert(self): |
65 | | - c = self.app.test_client() |
66 | | - c.post("/add", data={"title": "First Item", "text": "The text"}) |
67 | | - c.post("/add", data={"title": "2nd Item", "text": "The text"}) |
68 | | - rv = c.get("/") |
69 | | - self.assertEqual(rv.data.decode("utf-8"), "First Item\n2nd Item") |
70 | 39 |
|
71 | | - def test_request_context(self): |
72 | | - with self.app.test_request_context(): |
73 | | - todo = self.Todo(title="Test", text="test") |
74 | | - todo.save() |
75 | | - self.assertEqual(self.Todo.objects.count(), 1) |
| 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