示例#1
0
    def test_pull(self):
        m = Mangle({'$pull': {'foo': 'bar', 'bar': {'i': {'$gt': 5}}}})

        doc = {
            'foo': ['bar', 'baz'],
            'bar': [{
                'i': 1
            }, {
                'i': 2
            }, {
                'i': 3
            }, {
                'i': 4
            }, {
                'i': 5
            }, {
                'i': 6
            }, {
                'i': 7
            }]
        }
        got = m(doc)

        self.assertTrue('bar' not in got['foo'])
        self.assertTrue('baz' in got['foo'])

        for item in got['bar']:
            self.assertTrue(item['i'] <= 5)
示例#2
0
    def test_unset(self):
        m = Mangle({'$unset': {'foo': 1}})

        doc = {'foo': 'bar'}
        got = m(doc)

        self.assertTrue('foo' not in got)
示例#3
0
    def test_set(self):
        m = Mangle({'$set': {'foo': 'bar'}})

        doc = {}
        got = m(doc)

        self.assertTrue('foo' in got)
        self.assertEqual(got['foo'], 'bar')
示例#4
0
    def test_pullAll(self):
        m = Mangle({'$pullAll': {'foo': ['bar', 'baz']}})

        doc = {'foo': ['bar', 'baz', 'biz']}
        got = m(doc)

        self.assertTrue('bar' not in got['foo'])
        self.assertTrue('baz' not in got['foo'])
        self.assertTrue('biz' in got['foo'])
示例#5
0
    def test_rename(self):
        m = Mangle({'$rename': {'foo': 'bar'}})

        doc = {'foo': 'baz'}
        got = m(doc)

        self.assertTrue('foo' not in got)
        self.assertTrue('bar' in got)
        self.assertEqual(got['bar'], 'baz')
示例#6
0
    def test_pop(self):
        m = Mangle({'$pop': {'i': 1, 'j': -1}})

        doc = {'i': [1, 2], 'j': [1, 2]}
        got = m(doc)

        self.assertTrue(1 not in got['i'])
        self.assertTrue(2 in got['i'])
        self.assertTrue(1 in got['j'])
        self.assertTrue(2 not in got['j'])
示例#7
0
    def test_max(self):
        m = Mangle({'$max': {'i': 5}})

        doc = {'i': 1}
        got = m(doc)
        self.assertEqual(got['i'], 5)

        doc = {'i': 10}
        got = m(doc)
        self.assertEqual(got['i'], 10)
示例#8
0
    def test_addToSet(self):
        m = Mangle({'$addToSet': {'i': 4, 'foo': {'$each': ['bar', 'baz']}}})

        doc = {'i': [1, 2, 3]}
        got = m(doc)

        self.assertTrue(1 in got['i'])
        self.assertTrue(2 in got['i'])
        self.assertTrue(3 in got['i'])
        self.assertTrue(4 in got['i'])
        self.assertTrue('foo' in got)
        self.assertTrue('bar' in got['foo'])
        self.assertTrue('baz' in got['foo'])
示例#9
0
    def test_currentDate(self):
        m = Mangle({
            '$currentDate': {
                'foo': {
                    '$type': 'date'
                },
                'bar': {
                    '$type': 'timestamp'
                },
                'baz': True
            }
        })

        doc = {}
        got = m(doc)

        self.assertTrue('foo' in got)
        self.assertTrue('bar' in got)
        self.assertTrue('baz' in got)

        self.assertTrue(isinstance(got['foo'], datetime))
        self.assertTrue(isinstance(got['bar'], float))
        self.assertTrue(isinstance(got['baz'], datetime))
示例#10
0
    def test_bit(self):
        m = Mangle({
            '$bit': {
                'foo': {
                    'and': 4
                },
                'bar': {
                    'or': 4
                },
                'baz': {
                    'xor': 4
                },
                'biz': {
                    'and': 4
                },
                'buz': {
                    'or': 4
                },
                'boz': {
                    'xor': 4
                }
            }
        })

        doc = {'foo': 3, 'bar': 3, 'baz': 3}
        got = m(doc)

        self.assertEqual(got['foo'], 0)
        self.assertEqual(got['bar'], 7)
        self.assertEqual(got['baz'], 7)

        self.assertTrue('biz' in got)
        self.assertEqual(got['biz'], 0)
        self.assertTrue('buz' in got)
        self.assertEqual(got['buz'], 4)
        self.assertTrue('boz' in got)
        self.assertEqual(got['boz'], 4)
示例#11
0
    def test_push(self):
        m = Mangle({
            '$push': {
                'foo': 'bar',
                'bar': {
                    '$each': ['baz', 'biz']
                },
                'baz': {
                    '$each': ['biz', 'buz'],
                    '$sort': -1
                },
                'biz': {
                    '$each': ['biz', 'buz'],
                    '$sort': 1
                },
                'buz': {
                    '$each': [{
                        'i': 1
                    }, {
                        'i': 5
                    }],
                    '$sort': {
                        'i': 1
                    }
                },
                'boz': {
                    '$each': ['buz', 'boz'],
                    '$slice': 1
                }
            }
        })

        doc = {
            'bar': ['bar'],
            'baz': ['foo'],
            'biz': ['foo'],
            'buz': [{
                'i': 8
            }, {
                'i': 4
            }]
        }
        got = m(doc)

        self.assertTrue('foo' in got)
        self.assertTrue('bar' in got['foo'])

        self.assertTrue('bar' in got['bar'])
        self.assertTrue('baz' in got['bar'])
        self.assertTrue('biz' in got['bar'])

        self.assertEqual(got['baz'], ['foo', 'buz', 'biz'])
        self.assertEqual(got['biz'], ['biz', 'buz', 'foo'])

        prev = None

        for item in got['buz']:
            if prev is not None:
                self.assertTrue(prev['i'] <= item['i'])

            prev = item

        self.assertTrue('boz' in got)
        self.assertTrue('buz' in got['boz'])
        self.assertTrue('boz' not in got['boz'])
        self.assertEqual(len(got['boz']), 1)
示例#12
0
    def test_mul(self):
        m = Mangle({'$mul': {'i': 5}})

        doc = {'i': 1}
        got = m(doc)
        self.assertEqual(got['i'], 5)
示例#13
0
    def test_inc(self):
        m = Mangle({'$inc': {'i': 5}})

        doc = {'i': 0}
        got = m(doc)
        self.assertEqual(got['i'], 5)