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
Ejemplo n.º 2
0
    def _create_keyword_op_node(value_node):
        """Creates a KeywordOp node, with the given Keyword argument and the given Value node of the closure."""
        if isinstance(value_node, NotOp):
            keyword_op_node = NotOp(KeywordOp(keyword, value_node.op))
        else:
            keyword_op_node = KeywordOp(keyword, value_node)

        return keyword_op_node
    def _create_operator_node(value_node):
        """Creates a KeywordOp or a ValueOp node."""
        base_node = value_node.op if isinstance(value_node,
                                                NotOp) else value_node
        updated_base_node = KeywordOp(
            keyword, base_node) if keyword else ValueOp(base_node)

        return NotOp(updated_base_node) if isinstance(
            value_node, NotOp) else updated_base_node
        ('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')),
               NotOp(KeywordOp(Keyword('type-code'), Value('c'))))),
        ('author:ellis j title:\'boson\' reference:M.N.1',