Example #1
0
def to_yaml_string(value, indent=None, allow_unicode=True):
    value = db_util.mongodb_to_python_types(value)

    options = {'default_flow_style': False}

    if indent is not None:
        options['indent'] = indent

    if allow_unicode is not None:
        options['allow_unicode'] = allow_unicode

    return yaml.safe_dump(value, **options)
Example #2
0
def to_json_string(value, indent=None, sort_keys=False, separators=(',', ': ')):
    value = db_util.mongodb_to_python_types(value)

    options = {}

    if indent is not None:
        options['indent'] = indent

    if sort_keys is not None:
        options['sort_keys'] = sort_keys

    if separators is not None:
        options['separators'] = separators

    return json.dumps(value, **options)
Example #3
0
def to_json_string(value,
                   indent=None,
                   sort_keys=False,
                   separators=(",", ": ")):
    value = db_util.mongodb_to_python_types(value)

    options = {}

    if indent is not None:
        options["indent"] = indent

    if sort_keys is not None:
        options["sort_keys"] = sort_keys

    if separators is not None:
        options["separators"] = separators

    return json.dumps(value, **options)
Example #4
0
    def test_nested_mongdb_to_python_types(self):
        data = {
            'a':
            mongoengine.base.datastructures.BaseList([1, 2, 3], None, 'a'),
            'b':
            mongoengine.base.datastructures.BaseDict({
                'a': 1,
                'b': 2
            }, None, 'b'),
            'c': {
                'd':
                mongoengine.base.datastructures.BaseList([4, 5, 6], None, 'd'),
                'e':
                mongoengine.base.datastructures.BaseDict({
                    'c': 3,
                    'd': 4
                }, None, 'e')
            },
            'f':
            mongoengine.base.datastructures.BaseList([
                mongoengine.base.datastructures.BaseDict({'e': 5}, None, 'f1'),
                mongoengine.base.datastructures.BaseDict({'f': 6}, None, 'f2')
            ], None, 'f'),
            'g':
            mongoengine.base.datastructures.BaseDict(
                {
                    'h':
                    mongoengine.base.datastructures.BaseList([
                        mongoengine.base.datastructures.BaseDict({'g': 7},
                                                                 None, 'h1'),
                        mongoengine.base.datastructures.BaseDict({'h': 8},
                                                                 None, 'h2')
                    ], None, 'h'),
                    'i':
                    mongoengine.base.datastructures.BaseDict({
                        'j': 9,
                        'k': 10
                    }, None, 'i')
                }, None, 'g'),
        }

        expected = {
            'a': [1, 2, 3],
            'b': {
                'a': 1,
                'b': 2
            },
            'c': {
                'd': [4, 5, 6],
                'e': {
                    'c': 3,
                    'd': 4
                }
            },
            'f': [{
                'e': 5
            }, {
                'f': 6
            }],
            'g': {
                'h': [{
                    'g': 7
                }, {
                    'h': 8
                }],
                'i': {
                    'j': 9,
                    'k': 10
                }
            }
        }

        self.assertDictEqual(db_util.mongodb_to_python_types(data), expected)
Example #5
0
    def test_mongodb_baselist_to_list(self):
        data = [2, 4, 6]

        obj = mongoengine.base.datastructures.BaseList(data, None, 'foobar')

        self.assertListEqual(db_util.mongodb_to_python_types(obj), data)
Example #6
0
    def test_mongodb_basedict_to_dict(self):
        data = {'a': 1, 'b': 2}

        obj = mongoengine.base.datastructures.BaseDict(data, None, 'foobar')

        self.assertDictEqual(db_util.mongodb_to_python_types(obj), data)
Example #7
0
    def test_noop_mongodb_to_python_types(self):
        data = [123, 999.99, True, [10, 20, 30], {'a': 1, 'b': 2}, None]

        for item in data:
            self.assertEqual(db_util.mongodb_to_python_types(item), item)
Example #8
0
    def test_nested_mongdb_to_python_types(self):
        data = {
            "a":
            mongoengine.base.datastructures.BaseList([1, 2, 3], None, "a"),
            "b":
            mongoengine.base.datastructures.BaseDict({
                "a": 1,
                "b": 2
            }, None, "b"),
            "c": {
                "d":
                mongoengine.base.datastructures.BaseList([4, 5, 6], None, "d"),
                "e":
                mongoengine.base.datastructures.BaseDict({
                    "c": 3,
                    "d": 4
                }, None, "e"),
            },
            "f":
            mongoengine.base.datastructures.BaseList(
                [
                    mongoengine.base.datastructures.BaseDict({"e": 5}, None,
                                                             "f1"),
                    mongoengine.base.datastructures.BaseDict({"f": 6}, None,
                                                             "f2"),
                ],
                None,
                "f",
            ),
            "g":
            mongoengine.base.datastructures.BaseDict(
                {
                    "h":
                    mongoengine.base.datastructures.BaseList(
                        [
                            mongoengine.base.datastructures.BaseDict(
                                {"g": 7}, None, "h1"),
                            mongoengine.base.datastructures.BaseDict(
                                {"h": 8}, None, "h2"),
                        ],
                        None,
                        "h",
                    ),
                    "i":
                    mongoengine.base.datastructures.BaseDict({
                        "j": 9,
                        "k": 10
                    }, None, "i"),
                },
                None,
                "g",
            ),
        }

        expected = {
            "a": [1, 2, 3],
            "b": {
                "a": 1,
                "b": 2
            },
            "c": {
                "d": [4, 5, 6],
                "e": {
                    "c": 3,
                    "d": 4
                }
            },
            "f": [{
                "e": 5
            }, {
                "f": 6
            }],
            "g": {
                "h": [{
                    "g": 7
                }, {
                    "h": 8
                }],
                "i": {
                    "j": 9,
                    "k": 10
                }
            },
        }

        self.assertDictEqual(db_util.mongodb_to_python_types(data), expected)