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))
def _token_nud_star(self, token): left = ast.Identity() if self._current_token() == 'rbracket': right = ast.Identity() else: right = self._parse_projection_rhs(self.BINDING_POWER['star']) return ast.ValueProjection(left, right)
def test_root_value_projection(self): # * projection = ast.ValueProjection(ast.Identity(), ast.Identity()) data = { 'a': 1, 'b': 2, 'c': 3, } result = list(sorted(projection.search(data))) self.assertEqual(result, [1, 2, 3])
def _token_led_filter(self, left): # Filters are projections. condition = self._expression(0) self._match('rbracket') if self._current_token() == 'flatten': right = ast.Identity() else: right = self._parse_projection_rhs(self.BINDING_POWER['filter']) return ast.FilterProjection(left, right, condition)
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 }])
def test_no_right_node_value_projection(self): # foo.* field_foo = ast.Field('foo') projection = ast.ValueProjection(field_foo, ast.Identity()) data = { 'foo': { 'a': 1, 'b': 2, 'c': 3, } } result = list(sorted(projection.search(data))) self.assertEqual(result, [1, 2, 3])
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()
def test_no_left_node_value_projection(self): # *.bar field_bar = ast.Field('bar') projection = ast.ValueProjection(ast.Identity(), field_bar) data = { 'a': { 'bar': 1 }, 'b': { 'bar': 2 }, 'c': { 'bar': 3 }, } result = list(sorted(projection.search(data))) self.assertEqual(result, [1, 2, 3])
def _parse_projection_rhs(self, binding_power): # Parse the right hand side of the projection. if self.BINDING_POWER[self._current_token()] < 10: # BP of 10 are all the tokens that stop a projection. right = ast.Identity() elif self._current_token() == 'lbracket': right = self._expression(binding_power) elif self._current_token() == 'filter': right = self._expression(binding_power) elif self._current_token() == 'dot': self._match('dot') right = self._parse_dot_rhs(binding_power) else: t = self._lookahead_token(0) lex_position = t['start'] actual_value = t['value'] actual_type = t['type'] raise exceptions.ParseError(lex_position, actual_value, actual_type, 'syntax error') return right
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}]])
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)
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])
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)
def _token_nud_filter(self, token): return self._token_led_filter(ast.Identity())