Skip to content

Commit c644346

Browse files
committed
fix tests
1 parent 8a1a9b0 commit c644346

File tree

4 files changed

+18
-32
lines changed

4 files changed

+18
-32
lines changed

flask_pymongo/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
DriverInfo = None
4343

4444
from flask_pymongo._version import __version__
45-
from flask_pymongo.helpers import BSONObjectIdConverter, JSONEncoder
45+
from flask_pymongo.helpers import BSONObjectIdConverter, BSONProvider
4646
from flask_pymongo.wrappers import MongoClient
4747

4848
DESCENDING = pymongo.DESCENDING
@@ -69,7 +69,7 @@ class PyMongo(object):
6969
def __init__(self, app=None, uri=None, json_options=None, *args, **kwargs):
7070
self.cx = None
7171
self.db = None
72-
self._json_encoder = partial(JSONEncoder, json_options=json_options)
72+
self._json_provider = BSONProvider(json_options, app)
7373

7474
if app is not None:
7575
self.init_app(app, uri, *args, **kwargs)
@@ -123,7 +123,7 @@ def init_app(self, app, uri=None, *args, **kwargs):
123123
self.db = self.cx[database_name]
124124

125125
app.url_map.converters["ObjectId"] = BSONObjectIdConverter
126-
app.json_encoder = self._json_encoder
126+
app.json = self._json_provider
127127

128128
# view helpers
129129
def send_file(self, filename, base="fs", version=-1, cache_for=31536000):

flask_pymongo/helpers.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
# POSSIBILITY OF SUCH DAMAGE.
2525

2626

27-
__all__ = ("BSONObjectIdConverter", "JSONEncoder")
27+
__all__ = ("BSONObjectIdConverter", "BSONProvider")
2828

29-
from bson import json_util, SON
29+
from bson import json_util
3030
from bson.errors import InvalidId
3131
from bson.objectid import ObjectId
3232
from flask import abort
33-
from json import JSONEncoder
34-
from six import iteritems, string_types
33+
from flask.json.provider import JSONProvider
3534
from werkzeug.routing import BaseConverter
3635
import pymongo
3736

@@ -84,7 +83,7 @@ def to_url(self, value):
8483
return str(value)
8584

8685

87-
class JSONEncoder(JSONEncoder):
86+
class BSONProvider(JSONProvider):
8887

8988
"""A JSON encoder that uses :mod:`bson.json_util` for MongoDB documents.
9089
@@ -121,35 +120,22 @@ def json_route(cart_id):
121120
122121
"""
123122

124-
def __init__(self, json_options, *args, **kwargs):
123+
def __init__(self, json_options, app):
125124
if json_options is None:
126125
json_options = DEFAULT_JSON_OPTIONS
127126
if json_options is not None:
128127
self._default_kwargs = {"json_options": json_options}
129128
else:
130129
self._default_kwargs = {}
131130

132-
super(JSONEncoder, self).__init__(*args, **kwargs)
131+
super().__init__(app)
133132

134-
def default(self, obj):
133+
def dumps(self, obj):
135134
"""Serialize MongoDB object types using :mod:`bson.json_util`.
135+
"""
136+
return json_util.dumps(obj)
136137

137-
Falls back to Flask's default JSON serialization for all other types.
138-
139-
This may raise ``TypeError`` for object types not recognized.
140-
141-
.. versionadded:: 2.4.0
142-
138+
def loads(self, str_obj):
139+
"""Deserialize MongoDB object types using :mod:`bson.json_util`.
143140
"""
144-
if hasattr(obj, "iteritems") or hasattr(obj, "items"):
145-
return SON((k, self.default(v)) for k, v in iteritems(obj))
146-
elif hasattr(obj, "__iter__") and not isinstance(obj, string_types):
147-
return [self.default(v) for v in obj]
148-
else:
149-
try:
150-
return json_util.default(obj, **self._default_kwargs)
151-
except TypeError:
152-
# PyMongo couldn't convert into a serializable object, and
153-
# the Flask default JSONEncoder won't; so we return the
154-
# object itself and let stdlib json handle it if possible
155-
return obj
141+
return json_util.loads(str_obj)

flask_pymongo/tests/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_config_with_uri_in_flask_conf_var(self):
4444

4545
_wait_until_connected(mongo)
4646
assert mongo.db.name == self.dbname
47-
assert ("localhost", self.port) == mongo.cx.address
47+
assert ("localhost", self.port) == mongo.cx.address or ("127.0.0.1", self.port) == mongo.cx.address
4848

4949
def test_config_with_uri_passed_directly(self):
5050
uri = "mongodb://localhost:{}/{}".format(self.port, self.dbname)
@@ -53,7 +53,7 @@ def test_config_with_uri_passed_directly(self):
5353

5454
_wait_until_connected(mongo)
5555
assert mongo.db.name == self.dbname
56-
assert ("localhost", self.port) == mongo.cx.address
56+
assert ("localhost", self.port) == mongo.cx.address or ("127.0.0.1", self.port) == mongo.cx.address
5757

5858
def test_it_fails_with_no_uri(self):
5959
self.app.config.pop("MONGO_URI", None)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
platforms="any",
3131
packages=find_packages(),
3232
install_requires=[
33-
"Flask>=1.0",
33+
"Flask>=3.0",
3434
"PyMongo>=3.11",
3535
"six",
3636
],

0 commit comments

Comments
 (0)