Skip to content

Commit c07e657

Browse files
committed
Migrate test_json to pytest engine
1 parent 75f0aa9 commit c07e657

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_json.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import flask
2+
import pytest
3+
4+
from flask_mongoengine import MongoEngine
5+
6+
7+
@pytest.fixture()
8+
def extended_db(app):
9+
app.json_encoder = DummyEncoder
10+
app.config["MONGODB_HOST"] = "mongodb://localhost:27017/flask_mongoengine_test_db"
11+
test_db = MongoEngine(app)
12+
db_name = test_db.connection.get_database("flask_mongoengine_test_db").name
13+
14+
if not db_name.endswith("_test_db"):
15+
raise RuntimeError(
16+
f"DATABASE_URL must point to testing db, not to master db ({db_name})"
17+
)
18+
19+
# Clear database before tests, for cases when some test failed before.
20+
test_db.connection.drop_database(db_name)
21+
22+
yield test_db
23+
24+
# Clear database after tests, for graceful exit.
25+
test_db.connection.drop_database(db_name)
26+
27+
28+
class DummyEncoder(flask.json.JSONEncoder):
29+
"""
30+
An example encoder which a user may create and override
31+
the apps json_encoder with.
32+
This class is a NO-OP, but used to test proper inheritance.
33+
"""
34+
35+
36+
@pytest.mark.usefixtures("extended_db")
37+
def test_inheritance(app):
38+
assert issubclass(app.json_encoder, DummyEncoder)
39+
json_encoder_name = app.json_encoder.__name__
40+
41+
# Since the class is dynamically derrived, must compare class names
42+
# rather than class objects.
43+
assert json_encoder_name == "MongoEngineJSONEncoder"

0 commit comments

Comments
 (0)