def test_convert_simple_value_boolean_query_to_and_boolean_queries():
    parse_tree = \
        parser.SimpleQuery(
            parser.SpiresKeywordQuery(
                parser.InspireKeyword('author'),
                parser.Value(
                    parser.SimpleValueBooleanQuery(
                        parser.SimpleValue('foo'),
                        parser.And(),
                        parser.SimpleValueBooleanQuery(
                            parser.SimpleValue('bar'),
                            parser.Or(),
                            parser.SimpleValueNegation(parser.SimpleValue('foobar'))
                        )
                    )
                )
            )
        )

    expected_parse_tree = \
        AndOp(
            KeywordOp(Keyword('author'), Value('foo')),
            OrOp(
                KeywordOp(Keyword('author'), Value('bar')),
                NotOp(KeywordOp(Keyword('author'), Value('foobar')))
            )
        )

    restructuring_visitor = RestructuringVisitor()
    parse_tree = parse_tree.accept(restructuring_visitor)

    assert parse_tree == expected_parse_tree
    def visit_boolean_query(self, node):
        """Convert BooleanRule into AndOp or OrOp nodes."""
        left = node.left.accept(self)
        right = node.right.accept(self)

        is_journal_keyword_op = isinstance(
            left, KeywordOp) and left.left == Keyword('journal')

        if is_journal_keyword_op:
            journal_and_volume_conjunction = _restructure_if_volume_follows_journal(
                left, right)

            if journal_and_volume_conjunction:
                return journal_and_volume_conjunction

        return AndOp(left, right) if isinstance(node.bool_op, And) else OrOp(
            left, right)
Ejemplo n.º 3
0
 def visit_boolean_query(self, node):
     """Convert BooleanRule into AndOp or OrOp nodes."""
     left = node.left.accept(self)
     right = node.right.accept(self)
     return AndOp(left, right) if isinstance(node.bool_op, And) else OrOp(
         left, right)
         KeywordOp(Keyword('dotted.keyword'), PartialMatchValue('bar'))),

        # Boolean operator testing (And/Or)
        ('author ellis and title \'boson\'',
         AndOp(KeywordOp(Keyword('author'), Value('ellis')),
               KeywordOp(Keyword('title'), PartialMatchValue('boson')))),
        ('f a appelquist and date 1983',
         AndOp(KeywordOp(Keyword('author'), Value('appelquist')),
               KeywordOp(Keyword('date'), Value('1983')))),
        ('fin a henneaux and citedby a nicolai',
         AndOp(
             KeywordOp(Keyword('author'), Value('henneaux')),
             NestedKeywordOp(Keyword('citedby'),
                             KeywordOp(Keyword('author'), Value('nicolai'))))),
        ('au ellis | title \'boson\'',
         OrOp(KeywordOp(Keyword('author'), Value('ellis')),
              KeywordOp(Keyword('title'), PartialMatchValue('boson')))),
        ('-author ellis OR title \'boson\'',
         OrOp(NotOp(KeywordOp(Keyword('author'), Value('ellis'))),
              KeywordOp(Keyword('title'), PartialMatchValue('boson')))),
        ('author ellis & title \'boson\'',
         AndOp(KeywordOp(Keyword('author'), Value('ellis')),
               KeywordOp(Keyword('title'), PartialMatchValue('boson')))),

        # Implicit And
        ('author ellis elastic.keyword:\'boson\'',
         AndOp(
             KeywordOp(Keyword('author'), Value('ellis')),
             KeywordOp(Keyword('elastic.keyword'), PartialMatchValue('boson')))
         ),
        ('find cn atlas not tc c',
         AndOp(KeywordOp(Keyword('collaboration'), Value('atlas')),