Skip to content

Commit 5e1cc6e

Browse files
committed
add tester json parse in case of pymongo
add json parser in case of aggregation add tester json parse in case of aggregation
1 parent e8b8f08 commit 5e1cc6e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

flask_mongoengine/json.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from flask.json import JSONEncoder
33
from mongoengine.base import BaseDocument
44
from mongoengine.queryset import QuerySet
5+
from pymongo.command_cursor import CommandCursor
56

67

78
def _make_encoder(superclass):
@@ -16,6 +17,8 @@ def default(self, obj):
1617
return json_util._json_convert(obj.to_mongo())
1718
elif isinstance(obj, QuerySet):
1819
return json_util._json_convert(obj.as_pymongo())
20+
elif isinstance(obj, CommandCursor):
21+
return json_util._json_convert(obj)
1922
elif isinstance(obj, DBRef):
2023
return obj.id
2124
elif isinstance(obj, ObjectId):

tests/test_json_app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ def setup_endpoints(app, todo):
1111
def index():
1212
return flask.jsonify(result=Todo.objects())
1313

14+
@app.route("/as_pymongo")
15+
def as_pymongo():
16+
return flask.jsonify(result=Todo.objects().as_pymongo())
17+
18+
@app.route("/aggregate")
19+
def aggregate():
20+
return flask.jsonify(
21+
result=Todo.objects().aggregate({"$match": {"title": {"$ne": "lksdjh"}}})
22+
)
23+
1424
@app.route("/add", methods=["POST"])
1525
def add():
1626
form = flask.request.form
@@ -57,7 +67,24 @@ def test_basic_insert(app):
5767
client = app.test_client()
5868
client.post("/add", data={"title": "First Item", "text": "The text"})
5969
client.post("/add", data={"title": "2nd Item", "text": "The text"})
70+
6071
rv = client.get("/")
6172
result = flask.json.loads(rv.data).get("result")
73+
assert len(result) == 2
74+
for i in result:
75+
assert "title" in i
76+
assert "text" in i
77+
78+
rv = client.get("/as_pymongo")
79+
result = flask.json.loads(rv.data).get("result")
80+
assert len(result) == 2
81+
for i in result:
82+
assert "title" in i
83+
assert "text" in i
6284

85+
rv = client.get("/aggregate")
86+
result = flask.json.loads(rv.data).get("result")
87+
for i in result:
88+
assert "title" in i
89+
assert "text" in i
6390
assert len(result) == 2

0 commit comments

Comments
 (0)