Skip to content

Commit af797cd

Browse files
committed
Migrate test_basic_app to pytest engine
1 parent 07b833d commit af797cd

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

tests/conftest.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from datetime import datetime
2+
13
import mongoengine
24
import pytest
35
from flask import Flask
46

7+
from flask_mongoengine import MongoEngine
8+
59

610
@pytest.fixture()
711
def app():
@@ -14,12 +18,32 @@ def app():
1418
mongoengine.connection.disconnect_all()
1519

1620

21+
@pytest.fixture()
22+
def db(app):
23+
app.config["MONGODB_HOST"] = "mongodb://localhost:27017/flask_mongoengine_test_db"
24+
test_db = MongoEngine(app)
25+
db_name = test_db.connection.get_database("flask_mongoengine_test_db").name
26+
27+
if not db_name.endswith("_test_db"):
28+
raise RuntimeError(
29+
f"DATABASE_URL must point to testing db, not to master db ({db_name})"
30+
)
31+
32+
# Clear database before tests, for cases when some test failed before.
33+
test_db.connection.drop_database(db_name)
34+
35+
yield test_db
36+
37+
# Clear database after tests, for graceful exit.
38+
test_db.connection.drop_database(db_name)
39+
40+
1741
@pytest.fixture()
1842
def todo(db):
1943
class Todo(db.Document):
20-
# meta = {"db_alias": alias}
2144
title = mongoengine.StringField(max_length=60)
2245
text = mongoengine.StringField()
2346
done = mongoengine.BooleanField(default=False)
47+
pub_date = mongoengine.DateTimeField(default=datetime.utcnow)
2448

2549
return Todo

tests/test_basic_app.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)