def add_or(self, or_instance): assert isinstance(or_instance, OR), type(or_instance) with AliasFieldCtx( or_instance.with_config(self.config).lex(), 'cnd' ).with_config(self.config) as lex: if lex and lex not in self.or_fields: self.or_fields.append(lex)
def lex(self): raw_field, raw_alias = self.raw_attrs with AliasFieldCtx(raw_field, raw_alias).called_by( self.__class__).with_config(self.config) as lex: return '' if not lex else self._agg_template.format( field=raw_field, alias=raw_alias, )
def lex(self): with AliasFieldCtx( self.field, self.alias ).called_by( self.__class__ ).with_config( self.config ) as lex: return '' if not lex else self._template.format( field=self.field, alias=self.alias ) return
def lex(self): raw_field, raw_alias, raw_item = self.raw_attrs with AliasFieldCtx(raw_field, raw_alias).called_by( self.__class__).with_config(self.config) as lex: template = self._template if raw_alias == 'num': raw_alias = self._alias_template.format(field=raw_field) # allow use of lists # eg. IN(personal.bought, 3753083, 7418876) if type(raw_item) == type([]): raw_item = ", ".join([unicode(_item) for _item in raw_item]) return '' if not lex else template.format( field=raw_field, alias=raw_alias, item=raw_item)
def lex(self): raw_field, raw_alias = self.raw_attrs with AliasFieldCtx(raw_field, raw_alias).called_by( self.__class__).with_config(self.config) as lex: if raw_field == '*': template = self._star_agg_template else: template = self._agg_template if raw_alias == 'num': raw_alias = self._alias_template.format(field=raw_field) return '' if not lex else template.format(field=raw_field, alias=raw_alias)
def add_alias(self, field, alias): with AliasFieldCtx(field, alias).with_config(self.config) as lex: if lex and lex not in self.fields: self.fields.append(lex)
with MatchQueryCtxSoft(' ') as value: self.assertIsNone(value) def test_invalid_query_strict(self): self.assertRaises( SphinxQLSyntaxException, lambda: MatchQueryCtxStrict(42).__enter__() ) with MatchQueryCtxStrict('') as value: self.assertIsNone(value) with MatchQueryCtxStrict(' ') as value: self.assertIsNone(value) AliasFieldCtxSoft = lambda x, y: AliasFieldCtx(x, y).with_config(ProductionConfig) AliasFieldCtxStrict = lambda x, y: AliasFieldCtx(x, y).with_config(DebugConfig) class TestAliasFieldCtx(unittest.TestCase): def test_valid_attrs(self): with AliasFieldCtxSoft('name', 'another_name') as value: self.assertEqual(value, 'name AS another_name') def test_valid_attrs_strict(self): with AliasFieldCtxStrict('name', 'another_name') as value: self.assertEqual(value, 'name AS another_name') def test_invalid_attrs(self): with AliasFieldCtxSoft('name', 42) as value: self.assertIsNone(value)