def test_should_return_object(self): expression = "//Address[1]" field = XmlField(expression, pytype=TestXmlFieldObject.Address) address = field.object(self.doc) self.assertEqual("Ellen Adams", address.name) self.assertEqual("Mill Valley", address.city) self.assertEqual("123 Maple Street", address.street)
def test_should_return_default_if_absent(self): expression = "//does_not_exists" default = TestXmlFieldObject.Address() field = XmlField(expression, pytype=TestXmlFieldObject.Address, default=default) self.assertIsInstance(field.object(self.doc), TestXmlFieldObject.Address)
def test_should_throw_if_strict_when_nothing_found(self): expression = "//does_not_exists" field = XmlField(expression, strict=True) self.assertRaises(NotFoundException, lambda: field.value(self.doc))
def test_should_return_default_if_absent(self): expression = "//does_not_exists" field = XmlField(expression, default="default value") self.assertEqual("default value", field.value(self.doc))
def test_should_return_value(self): expression = "//Address[1]/Name" field = XmlField(expression) self.assertEqual("Ellen Adams", field.value(self.doc))
def test_should_throw_if_incorrect_expression(self): expression = "///incorrect_expression[" field = XmlField(expression) self.assertRaises(XPathSyntaxError, lambda: field.exec_query(self.doc))
def test_should_return_empty_list_when_nothing_found(self): expression = "//Home/Name" field = XmlField(expression) find = etree.XPath(expression) self.assertEqual(0, len(field.exec_query(self.doc))) self.assertEqual(find(self.doc), field.exec_query(self.doc))
def test_should_return_all_elements(self): expression = "//Address" field = XmlField(expression) self.assertEqual(2, len(field.exec_query(self.doc)))
def test_should_return_one_element(self): expression = "//Address[1]/Name" field = XmlField(expression) find = etree.XPath(expression) self.assertEqual(1, len(field.exec_query(self.doc))) self.assertEqual(find(self.doc), field.exec_query(self.doc))
def test_should_return_first_of_found(self): expression = "//Address" field = XmlField(expression, pytype=TestXmlFieldObject.Address) found = etree.XPath("//Address/Name")(self.doc) self.assertEqual(2, len(found)) self.assertEqual(found[0].text, field.object(self.doc).name)
def test_should_return_default_if_absent(self): expression = "//does_not_exists" field = XmlField(expression, default=[]) self.assertListEqual([], field.values_list(self.doc).all())
def test_should_return_list_of_values(self): expression = "//Address/Name" field = XmlField(expression) self.assertListEqual(["Ellen Adams", "Tai Yee"], field.values_list(self.doc).all())
def test_should_return_first_of_found(self): expression = "//Address/Name" field = XmlField(expression) found = etree.XPath(expression)(self.doc) self.assertEqual(2, len(found)) self.assertEqual(found[0].text, field.value(self.doc))