<commit_before>import json

from bson import ObjectId
from flask import jsonify

from flask_pymongo.tests.util import FlaskPyMongoTest


class JSONTest(FlaskPyMongoTest):

    def test_it_encodes_json(self):
        resp = jsonify({"foo": "bar"})
        dumped = json.loads(resp.get_data())
        self.assertEqual(dumped, {"foo": "bar"})

    def test_it_handles_pymongo_types(self):
        resp = jsonify({"id": ObjectId("5cf29abb5167a14c9e6e12c4")})
        dumped = json.loads(resp.get_data())
        self.assertEqual(dumped, {"id": {"$oid": "5cf29abb5167a14c9e6e12c4"}})

    def test_it_jsonifies_a_cursor(self):
        self.mongo.db.rows.insert_many([{"foo": "bar"}, {"foo": "baz"}])

        curs = self.mongo.db.rows.find(projection={"_id": False}).sort("foo")

        resp = jsonify(curs)
        dumped = json.loads(resp.get_data())
        self.assertEqual([{"foo": "bar"}, {"foo": "baz"}], dumped)<commit_msg>python 3.5 test compatibility fix<commit_after>import json

from bson import ObjectId
from flask import jsonify
from six import ensure_str

from flask_pymongo.tests.util import FlaskPyMongoTest


class JSONTest(FlaskPyMongoTest):

    def test_it_encodes_json(self):
        resp = jsonify({"foo": "bar"})
        dumped = json.loads(ensure_str(resp.get_data()))
        self.assertEqual(dumped, {"foo": "bar"})

    def test_it_handles_pymongo_types(self):
        resp = jsonify({"id": ObjectId("5cf29abb5167a14c9e6e12c4")})
        dumped = json.loads(ensure_str(resp.get_data()))
        self.assertEqual(dumped, {"id": {"$oid": "5cf29abb5167a14c9e6e12c4"}})

    def test_it_jsonifies_a_cursor(self):
        self.mongo.db.rows.insert_many([{"foo": "bar"}, {"foo": "baz"}])

        curs = self.mongo.db.rows.find(projection={"_id": False}).sort("foo")

        resp = jsonify(curs)
        dumped = json.loads(ensure_str(resp.get_data()))
        self.assertEqual([{"foo": "bar"}, {"foo": "baz"}], dumped)
{'fraction_non_alphanumeric': 0.16142341413099537, 'fraction_numerical': 0.029912325941206807, 'mean_word_length': 3.9743589743589745, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7036481', 'n_tokens_mistral': 733, 'n_tokens_neox': 688, 'n_words': 117}