def test_predicate4(self): """test a path expression with multiple predicates (written ops)""" xpb = XPathBuilder() xp = xpb.foo.bar.where(xpb.attr('name').not_equals('abc')) xp = xp.where(xpb.attr('x').equals('foo')) exp = '/foo/bar[@name != "abc"][@x = "foo"]' self.assertEqual(xp.tostring(), exp)
def test_predicate12(self): """test a "chained" predicate (written ops)""" xpb = XPathBuilder() pred = (xpb.attr('d').equals('e').log_and( xpb.foo.where(xpb.attr('z').equals('abc')))) xp = xpb.a.b.c.where(pred) exp = '/a/b/c[@d = "e" and /foo[@z = "abc"]]' self.assertEqual(xp.tostring(), exp)
def test_predicate8(self): """test a predicate with more conditions (written ops)""" xpb = XPathBuilder() pred = (xpb.attr('name').equals('foo').log_and( xpb.attr('x').equals('x'))) xp = xpb.foo.bar.where(pred) exp = '/foo/bar[@name = "foo" and @x = "x"]' self.assertEqual(xp.tostring(), exp)
def test_predicate12(self): """test a "chained" predicate (written ops)""" xpb = XPathBuilder() pred = (xpb.attr('d').equals('e') .log_and(xpb.foo.where(xpb.attr('z').equals('abc')))) xp = xpb.a.b.c.where(pred) exp = '/a/b/c[@d = "e" and /foo[@z = "abc"]]' self.assertEqual(xp.tostring(), exp)
def test_predicate8(self): """test a predicate with more conditions (written ops)""" xpb = XPathBuilder() pred = (xpb.attr('name').equals('foo') .log_and(xpb.attr('x').equals('x'))) xp = xpb.foo.bar.where(pred) exp = '/foo/bar[@name = "foo" and @x = "x"]' self.assertEqual(xp.tostring(), exp)
def test_predicate10(self): """test a predicate with attribute and path expression (written ops)""" xpb = XPathBuilder() pred = xpb.attr('foo').equals('bar').log_or(xpb.foobar) xp = xpb.foo.bar.where(pred) exp = '/foo/bar[@foo = "bar" or /foobar]' self.assertEqual(xp.tostring(), exp)
def test_generator1(self): """test a non generator (everything happens in place)""" xpb = XPathBuilder() xp = xpb.foo xp = xp.bar xp = xp.baz[xpb.attr('x') == 'y'] xp = xp[1] exp = '/foo/bar/baz[@x = "y"][1]' self.assertEqual(xp.tostring(), exp)
def test_generator2(self): """test a non generator (everything happens in place) (written ops)""" xpb = XPathBuilder() xp = xpb.foo xp = xp.bar xp = xp.baz.where(xpb.attr('x').equals('y')) xp = xp.where(1) exp = '/foo/bar/baz[@x = "y"][1]' self.assertEqual(xp.tostring(), exp)
def test_exception1(self): """test invalid expression tree""" xpb = XPathBuilder() pred = xpb.attr('foo') == 'bar' path = xpb.foo.bar pred_expr = path[pred] self.assertEqual(pred_expr.tostring(), '/foo/bar[@foo = "bar"]') pred.reparent(None) self.assertRaises(XPathSyntaxError, pred_expr.tostring)
def test_dummy2(self): """test dummy method 2""" xpb = XPathBuilder() xp = xpb.dummy() self.assertFalse(xp) xp = xp & (xpb.attr('foo') == 'xyz') self.assertTrue(xp) exp = '@foo = "xyz"' self.assertEqual(xp.tostring(), exp)
def test_request2(self): """test find_request (with validation)""" RequestCollection.SCHEMA = self.fixture_file('collection_request.xsd') xpb = XPathBuilder() xp = xpb.state[xpb.attr('name') == 'new'] collection = find_request(xp) self.assertTrue(len(collection.request[:]) == 1) self.assertEqual(collection.get('matches'), '1') self.assertEqual(collection.request[0].action.get('type'), 'submit') self.assertEqual(collection.request.action.get('type'), 'submit')
def test_generator4(self): """test a xpath generator (written ops)""" xpb = XPathBuilder() xp1 = xp2 = None base_xp = xpb.base.foo.where(xpb.attr('abc').equals('x')) with base_xp as b: xp1 = b().bar.text().equals('foo') xp2 = b().x.y.z.where(42) base_exp = '/base/foo[@abc = "x"]' xp1_exp = '/base/foo[@abc = "x"]/bar/text() = "foo"' xp2_exp = '/base/foo[@abc = "x"]/x/y/z[42]' self.assertEqual(base_xp.tostring(), base_exp) self.assertEqual(xp1.tostring(), xp1_exp) self.assertEqual(xp2.tostring(), xp2_exp)
def test_generator3(self): """test a xpath generator""" xpb = XPathBuilder() xp1 = xp2 = None base_xp = xpb.base.foo[xpb.attr('abc') == 'x'] with base_xp as b: xp1 = b().bar.text() == 'foo' xp2 = b().x.y.z[42] base_exp = '/base/foo[@abc = "x"]' xp1_exp = '/base/foo[@abc = "x"]/bar/text() = "foo"' xp2_exp = '/base/foo[@abc = "x"]/x/y/z[42]' self.assertEqual(base_xp.tostring(), base_exp) self.assertEqual(xp1.tostring(), xp1_exp) self.assertEqual(xp2.tostring(), xp2_exp)
def test_request1(self): """test find_request""" xpb = XPathBuilder() xp = xpb.state[(xpb.attr('name') == 'new') | (xpb.attr('name') == 'review')] xp = xp & (xpb.action.target[xpb.attr('project') == 'prj'] | xpb.action.source[xpb.attr('project') == 'prj'] ).parenthesize() collection = find_request(xp) self.assertTrue(len(collection.request[:]) == 3) self.assertEqual(collection.request[0].get('id'), '1') self.assertEqual(collection.request[0].action.source.get('project'), 'foo') self.assertEqual(collection.request[1].get('id'), '42') self.assertEqual(collection.request[1].action.get('type'), 'submit') self.assertEqual(collection.request[2].get('id'), '108') self.assertEqual(collection.request[2].review[2].get('by_group'), 'autobuild-team') # test __iter__ method of the collection ids = ['1', '42', '108'] for r in collection: self.assertEqual(r.get('id'), ids.pop(0)) self.assertTrue(len(ids) == 0)
def test_request1(self): """test find_request""" xpb = XPathBuilder() xp = xpb.state[(xpb.attr('name') == 'new') | (xpb.attr('name') == 'review')] xp = xp & ( xpb.action.target[xpb.attr('project') == 'prj'] | xpb.action.source[xpb.attr('project') == 'prj']).parenthesize() collection = find_request(xp) self.assertTrue(len(collection.request[:]) == 3) self.assertEqual(collection.request[0].get('id'), '1') self.assertEqual(collection.request[0].action.source.get('project'), 'foo') self.assertEqual(collection.request[1].get('id'), '42') self.assertEqual(collection.request[1].action.get('type'), 'submit') self.assertEqual(collection.request[2].get('id'), '108') self.assertEqual(collection.request[2].review[2].get('by_group'), 'autobuild-team') # test __iter__ method of the collection ids = ['1', '42', '108'] for r in collection: self.assertEqual(r.get('id'), ids.pop(0)) self.assertTrue(len(ids) == 0)
def test_predicate1(self): """test a path expression with simple predicate""" xpb = XPathBuilder() xp = xpb.action.source[xpb.attr('project') == 'bar'] exp = '/action/source[@project = "bar"]' self.assertEqual(xp.tostring(), exp)
def test_predicate2(self): """test a path expression with simple predicate (written ops)""" xpb = XPathBuilder() xp = xpb.action.source.where(xpb.attr('project').equals('bar')) exp = '/action/source[@project = "bar"]' self.assertEqual(xp.tostring(), exp)
def test_predicate13(self): """test contains function""" xpb = XPathBuilder() xp = xpb.foo.bar[xpb.attr('x').contains('foo')] exp = '/foo/bar[contains(@x, "foo")]' self.assertEqual(xp.tostring(), exp)
def test_predicate3(self): """test a path expression with multiple predicates""" xpb = XPathBuilder() xp = xpb.foo.bar[xpb.attr('name') != 'abc'][xpb.attr('x') == 'foo'] exp = '/foo/bar[@name != "abc"][@x = "foo"]' self.assertEqual(xp.tostring(), exp)
def test_request4(self): """test find_request (validation fails)""" RequestCollection.SCHEMA = self.fixture_file('collection_request.xsd') xpb = XPathBuilder() xp = xpb.state[xpb.attr('name') == 'declined'] self.assertRaises(etree.DocumentInvalid, find_request, xp)
def test_predicate9(self): """test a predicate with attribute and path expression""" xpb = XPathBuilder() xp = xpb.foo.bar[(xpb.attr('foo') == 'bar') | xpb.foobar] exp = '/foo/bar[@foo = "bar" or /foobar]' self.assertEqual(xp.tostring(), exp)
def test_predicate7(self): """test a predicate with more conditions""" xpb = XPathBuilder() xp = xpb.foo.bar[(xpb.attr('name') == 'foo') & (xpb.attr('x') == 'x')] exp = '/foo/bar[@name = "foo" and @x = "x"]' self.assertEqual(xp.tostring(), exp)
def test_predicate11(self): """test a "chained" predicate""" xpb = XPathBuilder() xp = xpb.a.b.c[(xpb.attr('d') == 'e') & xpb.foo[xpb.attr('z') == 'ab']] exp = '/a/b/c[@d = "e" and /foo[@z = "ab"]]' self.assertEqual(xp.tostring(), exp)
def find(self, name): xpb = XPathBuilder() xp = xpb.descendant(self._tag)[xpb.attr('name') == name] return self._xml.find(xp.tostring())