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.assert_('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())
def test_namespaces(self): xq = Xquery(xpath='/foo:el', namespaces={'foo': 'urn:foo#'}) ns_declaration = '''declare namespace foo='urn:foo#';''' # xpath-only xquery should have namespace declaration self.assert_(ns_declaration in xq.getQuery()) # full FLOWR xquery should also have declaration xq.return_only({'id': '@id'}) self.assert_(ns_declaration in xq.getQuery())
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.assert_('return <el>' in xq_return) self.assert_('<field>{$n/@id}</field>' in xq_return) self.assert_('<field>{$n/name}</field>' in xq_return) self.assert_('<field>{substring($n/@n,1,1)}</field>' in xq_return) self.assert_('</el>' in xq_return) xq = Xquery(xpath='/some/el/notroot') xq.return_only({'id': '@id'}) self.assert_('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.assert_('return <node>' in xq_return) xq = Xquery(xpath='/foo/bar/*') xq.return_only({'myid': '@id'}) xq_return = xq._constructReturn() self.assert_('return <node>' in xq_return)
def test_return_only__fulltext_score(self): xq = Xquery(xpath='/el') xq.xq_var = '$n' xq.return_only({'fulltext_score': ''}) self.assert_('let $fulltext_score := ft:score($n)' in xq.getQuery()) self.assert_('<fulltext_score>{$fulltext_score}</fulltext_score>' in xq._constructReturn())