File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 22from flask .json import JSONEncoder
33from mongoengine .base import BaseDocument
44from mongoengine .queryset import QuerySet
5+ from pymongo .command_cursor import CommandCursor
56
67
78def _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 ):
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments