示例#1
0
    def test_return_also_raw(self):
        xq = Xquery(xpath='/el')
        xq.xq_var = '$n'
        xq._raw_prefix = 'r_'
        xq.return_also({'myid': 'count(util:expand(%(xq_var)s/@id))'},
                       raw=True)
        self.assertTrue('<r_myid>{count(util:expand($n/@id))}</r_myid>' in
                        xq._constructReturn())

        xq = Xquery(xpath='/el')
        xq.xq_var = '$n'
        xq._raw_prefix = 'r_'
        xq.return_also({'myid': '@id'}, raw=True)
        self.assertTrue('<r_myid>{@id}</r_myid>' in xq._constructReturn())
示例#2
0
    def test_set_limits(self):
        # subsequence with xpath
        xq = Xquery(xpath='/el')
        xq.xq_var = '$n'
        xq.set_limits(low=0, high=4)
        self.assertEqual('subsequence(/el, 1, 4)', xq.getQuery())
        # subsequence with FLWR query
        xq.return_only({'name': 'name'})
        self.assertTrue('subsequence(for $n in' in xq.getQuery())

        # additive limits
        xq = Xquery(xpath='/el')
        xq.set_limits(low=2, high=10)
        xq.set_limits(low=1, high=5)
        self.assertEqual('subsequence(/el, 4, 4)', xq.getQuery())

        # no high specified
        xq = Xquery(xpath='/el')
        xq.set_limits(low=10)
        self.assertEqual('subsequence(/el, 11, )', xq.getQuery())

        # no low
        xq = Xquery(xpath='/el')
        xq.set_limits(high=15)
        self.assertEqual('subsequence(/el, 1, 15)', xq.getQuery())
示例#3
0
 def test_return_also__fulltext_score(self):
     xq = Xquery(xpath='/el')
     xq.xq_var = '$n'
     xq.return_also({'fulltext_score': ''})
     self.assertTrue('let $fulltext_score := ft:score($n)' in xq.getQuery())
     self.assertTrue('<fulltext_score>{$fulltext_score}</fulltext_score>' in
                     xq._constructReturn())
示例#4
0
    def test_return_only(self):
        xq = Xquery(xpath='/el')
        xq.xq_var = '$n'
        xq.return_only({
            'myid': '@id',
            'some_name': 'name',
            'first_letter': 'substring(@n,1,1)'
        })
        xq_return = xq._constructReturn()
        self.assertTrue('return <el>' in xq_return)
        self.assertTrue('<field>{$n/@id}</field>' in xq_return)
        self.assertTrue('<field>{$n/name}</field>' in xq_return)
        self.assertTrue('<field>{substring($n/@n,1,1)}</field>' in xq_return)
        self.assertTrue('</el>' in xq_return)

        xq = Xquery(xpath='/some/el/notroot')
        xq.return_only({'id': '@id'})
        self.assertTrue('return <notroot>' in xq._constructReturn())

        # case where node test can't be the return element
        xq = Xquery(xpath='/foo/bar/node()')
        xq.return_only({'myid': '@id'})
        xq_return = xq._constructReturn()
        self.assertTrue('return <node>' in xq_return)

        xq = Xquery(xpath='/foo/bar/*')
        xq.return_only({'myid': '@id'})
        xq_return = xq._constructReturn()
        self.assertTrue('return <node>' in xq_return)
示例#5
0
    def test_prep_xpath(self):
        xq = Xquery()
        xq.xq_var = '$n'
        # handle attributes
        self.assertEqual('<field>{$n/@id}</field>',
                         xq.prep_xpath('@id', return_field=True))
        self.assertEqual('<field>{$n/../@id}</field>',
                         xq.prep_xpath('../@id', return_field=True))
        self.assertEqual('<field>{$n/parent::root/@id}</field>',
                         xq.prep_xpath('parent::root/@id', return_field=True))
        # handle regular nodes
        self.assertEqual('<field>{$n/title}</field>',
                         xq.prep_xpath('title', return_field=True))
        # function call - regular node
        self.assertEqual('substring($n/title,1,1)',
                         xq.prep_xpath('substring(title,1,1)'))
        # function call - abbreviated step
        self.assertEqual('substring($n/.,1,1)',
                         xq.prep_xpath('substring(.,1,1)'))

        # xpath with OR - absolute paths
        self.assertEqual('<field>{$n/name|$n/title}</field>',
                         xq.prep_xpath('/name|/title', return_field=True))
        # xpath with OR - relative paths
        self.assertEqual('<field>{$n/name|$n/title}</field>',
                         xq.prep_xpath('name|title', return_field=True))
        # xpath with OR - mixed absolute and relative paths
        self.assertEqual('<field>{$n/name|$n/title}</field>',
                         xq.prep_xpath('/name|title', return_field=True))
        # multiple ORs
        self.assertEqual(
            '<field>{$n/name|$n/title|$n/@year}</field>',
            xq.prep_xpath('/name|/title|@year', return_field=True))

        # .//node inside a function call
        self.assertEqual(
            '<field>{normalize-space($n/.//name)}</field>',
            xq.prep_xpath('normalize-space($n/.//name)', return_field=True))

        # node|node inside a function call
        self.assertEqual('fn:lower-case($n/name|$n/title)',
                         xq.prep_xpath('fn:lower-case(name|title)'))

        # node|node inside a nested function call
        self.assertEqual(
            'fn:lower-case(normalize-space($n/name|$n/title))',
            xq.prep_xpath('fn:lower-case(normalize-space(name|title))'))
示例#6
0
    def test_sort(self):
        xq = Xquery(collection="mycoll")
        xq.xq_var = '$n'
        xq.sort('@id')
        self.assertTrue('order by $n/@id ascending' in xq.getQuery())
        self.assertTrue('collection("/db/mycoll")' in xq.getQuery())

        # prep_xpath function should clean up more complicated xpaths
        xq.sort('name|@id')
        self.assertTrue('order by $n/name|$n/@id' in xq.getQuery())

        # sort descending
        xq.sort('@id', ascending=False)
        self.assertTrue('order by $n/@id descending' in xq.getQuery())

        # sort case-insensitive
        xq.sort('@id', case_insensitive=True)
        self.assertTrue(
            'order by fn:lower-case($n/@id) ascending' in xq.getQuery())

        # case-insensitive and descending
        xq.sort('@id', case_insensitive=True, ascending=False)
        self.assertTrue(
            'order by fn:lower-case($n/@id) descending' in xq.getQuery())
示例#7
0
 def test_return_also__highlight(self):
     xq = Xquery(xpath='/el')
     xq.xq_var = '$n'
     xq.return_also({'fulltext_score': ''})
     xq.add_filter('.', 'highlight', 'dog star')
     self.assertTrue('(/el[ft:query(., "dog star")]|/el)' in xq.getQuery())
示例#8
0
 def test_return_also(self):
     xq = Xquery(xpath='/el')
     xq.xq_var = '$n'
     xq.return_also({'myid': '@id', 'some_name': 'name'})
     self.assertTrue('{$n}' in xq._constructReturn())
     self.assertTrue('<field>{$n/@id}</field>' in xq._constructReturn())