def test_elem_match(self): mspec = mim.match({'foo': { '$elemMatch': { 'bar': 1, 'baz': 2 } } }, {'foo': [ { 'bar': 1, 'baz': 2 } ] }) self.assertIsNotNone(mspec) mspec = mim.match({'foo': { '$elemMatch': { 'bar': 1, 'baz': 2 } } }, {'foo': [ { 'bar': 1, 'baz': 1 }, { 'bar': 2, 'baz': 2 } ] }) self.assertIsNone(mspec)
def test_regex_match_inside(self): doc = {'a': 'bar', 'b': 'bat', 'c': None} regex = re.compile(r'ar') self.assertIsNotNone(mim.match({'a': regex}, doc)) self.assertIsNone(mim.match({'b': regex}, doc)) self.assertIsNone(mim.match({'c': regex}, doc)) self.assertIsNone(mim.match({'d': regex}, doc))
def test_regex_match_begin(self): doc = {'a': 'bar', 'b': 'bet', 'c': None} regex = re.compile(r'^ba') self.assertIsNotNone(mim.match({'a': regex}, doc)) self.assertIsNone(mim.match({'b': regex}, doc)) self.assertIsNone(mim.match({'c': regex}, doc)) self.assertIsNone(mim.match({'d': regex}, doc))
def test_or(self): doc = { 'd': 2 } self.assertIsNotNone(mim.match( {'$or': [ { 'd': 1 }, { 'd': 2 } ] }, doc)) self.assertIsNone(mim.match( {'$or': [ { 'd': 1 }, { 'd': 3 } ] }, doc))
def test_regex_match_array(self): doc = { 'a': ['hello world'], 'b': ['good night'], 'c': ['this is hello world'], 'd': ['one', 'two', 'hello three']} regex = re.compile(r'^hello') self.assertIsNotNone(mim.match( {'a': regex}, doc)) self.assertIsNone(mim.match( {'b': regex}, doc)) self.assertIsNone(mim.match( {'c': regex}, doc)) self.assertIsNotNone(mim.match( {'d': regex}, doc))
def test_list_match(self): mspec = mim.match({'foo.bar': 4}, {'foo': {'bar': [1, 2, 3, 4, 5]}}) self.assertEqual( mspec, mim.MatchDoc({ 'foo': mim.MatchDoc({'bar': mim.MatchList([1, 2, 3, 4, 5], pos=3)}) })) self.assertEqual(mspec.getvalue('foo.bar.$'), 4)
def test_subdoc_exact(self): doc = {'a': {'b': 1}} self.assertIsNotNone(mim.match({'a': {'b': 1}}, doc)) self.assertIsNone(mim.match({'a': {'b': 2}}, doc)) self.assertIsNone(mim.match({'a': {'b': 1, 'c': 1}}, doc))
def test_dotted_match(self): mspec = mim.match({'foo.bar': 4}, { 'foo': { 'bar': 4 } }) self.assertEqual(mspec, mim.MatchDoc({'foo': mim.MatchDoc({'bar': 4}) } ))
def test_subdoc_partial(self): doc = {'a': {'b': 1, 'c': 1}} self.assertIsNotNone(mim.match({'a.b': 1}, doc)) self.assertIsNone(mim.match({'a.b': 2}, doc))
def test_exists(self): doc = { 'd': 2 } self.assertIsNotNone(mim.match({'d': { '$exists': 1 } }, doc)) self.assertIsNone(mim.match({'d': { '$exists': 0 } }, doc)) self.assertIsNone(mim.match({'e': { '$exists': 1 } }, doc)) self.assertIsNotNone(mim.match({'e': { '$exists': 0 } }, doc))
def test_list_match(self): mspec = mim.match({'foo.bar': 4}, { 'foo': { 'bar': [1,2,3,4,5] } }) self.assertEqual(mspec, mim.MatchDoc({ 'foo': mim.MatchDoc({'bar': mim.MatchList([1,2,3,4,5], pos=3) } ) })) self.assertEqual(mspec.getvalue('foo.bar.$'), 4)
def test_traverse_none(self): doc = {'a': None} self.assertIsNone(mim.match({'a.b.c': 1}, doc))
def test_dotted_match(self): mspec = mim.match({'foo.bar': 4}, {'foo': {'bar': 4}}) self.assertEqual(mspec, mim.MatchDoc({'foo': mim.MatchDoc({'bar': 4})}))
def test_lte(self): spec = {'d': {'$lte': 2}} self.assertIsNotNone(mim.match(spec, {'d': 1})) self.assertIsNotNone(mim.match(spec, {'d': 2})) self.assertIsNone(mim.match(spec, {'d': 3}))
def test_regex_match(self): doc = { 'a': 'bar', 'b': 'bat' } regex = re.compile(r'ba[rz]') self.assertIsNotNone(mim.match( {'a': regex}, doc)) self.assertIsNone(mim.match( {'b': regex}, doc))
def test_traverse_list(self): doc = { 'a': [ { 'b': 1 }, { 'b': 2 } ] } self.assertIsNotNone(mim.match( {'a.b': 1 }, doc))
def test_lte(self): spec = { 'd': { '$lte': 2 } } self.assertIsNotNone(mim.match(spec, { 'd': 1 } )) self.assertIsNotNone(mim.match(spec, { 'd': 2 } )) self.assertIsNone(mim.match(spec, { 'd': 3 } ))
def test_all(self): doc = { 'c': [ 1, 2 ] } self.assertIsNotNone(mim.match({'c': {'$all': [] } }, doc)) self.assertIsNotNone(mim.match({'c': {'$all': [1] } }, doc)) self.assertIsNotNone(mim.match({'c': {'$all': [1, 2] } }, doc)) self.assertIsNone(mim.match({'c': {'$all': [1, 2, 3] } }, doc))
def test_subdoc_deep(self): doc = {'a': {'b': {'c': 1}}} self.assertIsNotNone(mim.match({'a': {'b': {'c': 1}}}, doc))
def test_simple_match(self): mspec = mim.match({'foo': 4}, { 'foo': 4 }) self.assertEqual(mspec, mim.MatchDoc({'foo': 4}))
def test_subdoc_deep_list(self): doc = {'a': [0, [1, 1, 1], 2, 3]} self.assertIsNotNone(mim.match({'a': [0, [1, 1, 1], 2, 3]}, doc))
def test_all(self): doc = {'c': [1, 2]} self.assertIsNotNone(mim.match({'c': {'$all': []}}, doc)) self.assertIsNotNone(mim.match({'c': {'$all': [1]}}, doc)) self.assertIsNotNone(mim.match({'c': {'$all': [1, 2]}}, doc)) self.assertIsNone(mim.match({'c': {'$all': [1, 2, 3]}}, doc))
def test_simple_match(self): mspec = mim.match({'foo': 4}, {'foo': 4}) self.assertEqual(mspec, mim.MatchDoc({'foo': 4}))
def test_traverse_list(self): doc = {'a': [{'b': 1}, {'b': 2}]} self.assertIsNotNone(mim.match({'a.b': 1}, doc))
def test_range(self): doc = { 'd': 2 } self.assertIsNotNone(mim.match({'d': { '$gt': 1, '$lt': 3 } }, doc)) self.assertIsNone(mim.match({'d': { '$gt': 1, '$lt': 2 } }, doc)) self.assertIsNotNone(mim.match({'d': { '$gt': 1, '$lte': 2 } }, doc))
def test_range(self): doc = {'d': 2} self.assertIsNotNone(mim.match({'d': {'$gt': 1, '$lt': 3}}, doc)) self.assertIsNone(mim.match({'d': {'$gt': 1, '$lt': 2}}, doc)) self.assertIsNotNone(mim.match({'d': {'$gt': 1, '$lte': 2}}, doc))
def test_exists(self): doc = {'d': 2} self.assertIsNotNone(mim.match({'d': {'$exists': 1}}, doc)) self.assertIsNone(mim.match({'d': {'$exists': 0}}, doc)) self.assertIsNone(mim.match({'e': {'$exists': 1}}, doc)) self.assertIsNotNone(mim.match({'e': {'$exists': 0}}, doc))