def test_normal_inline_ops(self): prettify = Prettifier(indent=4, max_len=50, inline_ops=True) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""")
def test_with_unknown_op_nested(self): prettify = Prettifier(indent=8, max_len=20) tree = OrOperation( UnknownOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz")), Word("fooooooooooo")) self.assertEqual( "\n" + prettify(tree), """ baaaaaaaaaar baaaaaaaaaaaaaz OR fooooooooooo""")
def test_with_unknown_op(self): prettify = Prettifier(indent=8, max_len=20) tree = UnknownOperation( Group( UnknownOperation(Word("baaaaaaaaaar"), Word("baaaaaaaaaaaaaz"))), Word("fooooooooooo")) self.assertEqual( "\n" + prettify(tree), """ ( baaaaaaaaaar baaaaaaaaaaaaaz ) fooooooooooo""")
def test_small(self): prettify = Prettifier(indent=8, max_len=20) self.assertEqual( "\n" + prettify(self.big_tree), """ ( baaaaaaaaaar OR baaaaaaaaaaaaaz ) AND fooooooooooo""") self.assertEqual( "\n" + prettify(self.fat_tree), """ subject: ( fiiiiiiiiiiz OR baaaaaaaaaar AND baaaaaaaaaaaaaz ) AND fooooooooooo AND wiiiiiiiiiz""")
def q2filter(tree) -> Dict[str, object]: print('******** search query', prettify(tree)) result = {} def dt2timestamp(s, timePortion): if not 'T' in s: s += f'T{timePortion}' return int(isodate.parse_datetime(s).timestamp()) if isinstance(tree, t.AndOperation): for op in tree.operands: result.update(q2filter(op)) if isinstance(tree, t.SearchField): name = str(tree.name) if isinstance(tree.expr, t.Range): low_value = str(tree.expr.low) if tree.expr.include_low else None high_value = str( tree.expr.high) if tree.expr.include_high else None if name == 'filterDate': result = { name: { 'dateFrom': dt2timestamp(low_value, '00:00'), 'dateTo': dt2timestamp(high_value, '23:59') } } if name == 'filterTotal': expr = None if tree.expr.include_low and tree.expr.include_high and low_value == high_value: expr = f'equal-{low_value}' elif low_value != '*': expr = f'more-{low_value}' elif high_value != '*': expr = f'less-{high_value}' result = {name: expr} if isinstance(tree.expr, t.Word): result = {name: str(tree.expr.value)} if isinstance(tree.expr, t.Phrase): result = {name: str(tree.expr.value).strip('"')} return result
def test_one_liner(self): tree = AndOperation(Group(OrOperation(Word("bar"), Word("baz"))), Word("foo")) self.assertEqual(prettify(tree), "( bar OR baz ) AND foo")