示例#1
0
 def test_multiple_nested_wildcards_with_list_values(self):
     # foo[].bar[].baz
     parsed = ast.Projection(
         ast.Flatten(
             ast.Projection(ast.Flatten(ast.Field('foo')),
                            ast.Field('bar'))), ast.Field('baz'))
     data = {
         "foo": [
             {
                 "bar": [{
                     "baz": [1]
                 }, {
                     "baz": [2]
                 }]
             },
             {
                 "bar": [{
                     "baz": [3]
                 }, {
                     "baz": [4]
                 }]
             },
         ]
     }
     self.assertEqual(parsed.search(data), [[1], [2], [3], [4]])
示例#2
0
 def test_multiple_projections(self):
     # foo[*].bar[*].baz
     field_foo = ast.Field('foo')
     field_bar = ast.Field('bar')
     field_baz = ast.Field('baz')
     second_projection = ast.Projection(field_bar, field_baz)
     first_projection = ast.Projection(field_foo, second_projection)
     data = {
         'foo': [
             {
                 'bar': [{
                     'baz': 1
                 }, {
                     'baz': 2
                 }, {
                     'baz': 3
                 }],
                 'other': 1
             },
             {
                 'bar': [{
                     'baz': 4
                 }, {
                     'baz': 5
                 }, {
                     'baz': 6
                 }],
                 'other': 2
             },
         ]
     }
     self.assertEqual(first_projection.search(data), [[1, 2, 3], [4, 5, 6]])
示例#3
0
 def test_multiple_nested_wildcards(self):
     # foo[].bar[].baz
     parsed = ast.Projection(
         ast.Flatten(
             ast.Projection(ast.Flatten(ast.Field('foo')),
                            ast.Field('bar'))), ast.Field('baz'))
     data = {
         "foo": [
             {
                 "bar": [{
                     "baz": 1
                 }, {
                     "baz": 2
                 }]
             },
             {
                 "bar": [{
                     "baz": 3
                 }, {
                     "baz": 4
                 }]
             },
         ]
     }
     self.assertEqual(parsed.search(data), [1, 2, 3, 4])
示例#4
0
 def test_base_projection_on_invalid_type(self):
     # [*]
     data = {'foo': [{'bar': 1}, {'bar': 2}, {'bar': 3}]}
     projection = ast.Projection(ast.Identity(), ast.Identity())
     # search() should return None because the evaluated
     # type is a dict, not a list.
     self.assertIsNone(projection.search(data))
示例#5
0
 def test_projection_simple(self):
     # foo[*].bar
     field_foo = ast.Field('foo')
     field_bar = ast.Field('bar')
     projection = ast.Projection(field_foo, field_bar)
     data = {'foo': [{'bar': 1}, {'bar': 2}, {'bar': 3}]}
     self.assertEqual(projection.search(data), [1, 2, 3])
示例#6
0
 def test_projection_no_right(self):
     # foo[*]
     field_foo = ast.Field('foo')
     projection = ast.Projection(field_foo, ast.Identity())
     data = {'foo': [{'bar': 1}, {'bar': 2}, {'bar': 3}]}
     self.assertEqual(projection.search(data), [{
         'bar': 1
     }, {
         'bar': 2
     }, {
         'bar': 3
     }])
示例#7
0
 def test_flattened_multiselect_with_none(self):
     # foo[].[bar,baz]
     field_bar = ast.Field('bar')
     field_baz = ast.Field('baz')
     multiselect = ast.MultiFieldList([field_bar, field_baz])
     projection = ast.Projection(ast.Flatten(ast.Field('foo')), multiselect)
     self.assertEqual(
         projection.search({'bar': [{
             'bar': 1,
             'baz': 2,
             'qux': 3
         }]}), None)
示例#8
0
 def _token_led_lbracket(self, left):
     token = self._lookahead_token(0)
     if token['type'] == 'number':
         self._match('number')
         right = ast.Index(token['value'])
         self._match('rbracket')
         return ast.IndexExpression(left, right)
     else:
         # We have a projection
         self._match('star')
         self._match('rbracket')
         right = self._parse_projection_rhs(self.BINDING_POWER['star'])
         return ast.Projection(left, right)
示例#9
0
 def _token_nud_lbracket(self, token):
     if self._current_token() == 'number':
         node = ast.Index(self._lookahead_token(0)['value'])
         self._advance()
         self._match('rbracket')
         return node
     elif self._current_token() == 'star' and self._lookahead(1) == 'rbracket':
         self._advance()
         self._advance()
         right = self._parse_projection_rhs(self.BINDING_POWER['star'])
         return ast.Projection(ast.Identity(), right)
     else:
         return self._parse_multi_select_list()
示例#10
0
 def test_nested_filter_projection(self):
     data = {
         "reservations": [{
             "instances": [{
                 "foo": 1,
                 "bar": 2
             }, {
                 "foo": 1,
                 "bar": 3
             }, {
                 "foo": 1,
                 "bar": 2
             }, {
                 "foo": 2,
                 "bar": 1
             }]
         }]
     }
     projection = ast.Projection(
         ast.Flatten(ast.Field('reservations')),
         ast.FilterProjection(
             ast.Field('instances'), ast.Identity(),
             ast.OPEquals(ast.Field('bar'), ast.Literal(1))))
     self.assertEqual(projection.search(data), [[{'bar': 1, 'foo': 2}]])
示例#11
0
 def test_bare_projection(self):
     # [*]
     projection = ast.Projection(ast.Identity(), ast.Identity())
     data = [{'bar': 1}, {'bar': 2}, {'bar': 3}]
     self.assertEqual(projection.search(data), data)
示例#12
0
 def test_projection_no_left(self):
     # [*].bar
     field_bar = ast.Field('bar')
     projection = ast.Projection(ast.Identity(), field_bar)
     data = [{'bar': 1}, {'bar': 2}, {'bar': 3}]
     self.assertEqual(projection.search(data), [1, 2, 3])
示例#13
0
 def test_flattened_wildcard(self):
     # foo[].bar
     parsed = ast.Projection(ast.Flatten(ast.Field('foo')),
                             ast.Field('bar'))
     data = {'foo': [{'bar': 1}, {'bar': 2}, {'bar': 3}]}
     self.assertEqual(parsed.search(data), [1, 2, 3])
示例#14
0
 def test_wildcard_branches_with_index(self):
     # foo[*].bar
     child = ast.Projection(ast.Field('foo'), ast.Field('bar'))
     match = child.search({'foo': [{'bar': 'one'}, {'bar': 'two'}]})
     self.assertTrue(isinstance(match, list))
     self.assertEqual(match, ['one', 'two'])
示例#15
0
 def _token_led_flatten(self, left):
     left = ast.Flatten(left)
     right = self._parse_projection_rhs(
         self.BINDING_POWER['flatten'])
     return ast.Projection(left, right)
示例#16
0
 def _token_nud_flatten(self, token):
     left = ast.Flatten(ast.Identity())
     right = self._parse_projection_rhs(
         self.BINDING_POWER['flatten'])
     return ast.Projection(left, right)