def test_date_field_returns_none_when_xpathed_value_for_the_node_is_none( self): xml_string = '<root><kiddie><value></value></kiddie></root>' xml = xpath.domify(xml_string) field = DateField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEqual(None, response)
def test_bool_field_returns_true_when_xpathed_value_for_the_node_is_true( self): xml_string = '<root><kiddie1><value>true</value></kiddie1></root>' xml = xpath.domify(xml_string) field = BoolField(xpath='/root/kiddie1/value') response = field.parse(xml, None) self.assertEqual(True, response)
def test_date_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>2008-06-21T10:36:12</value></kiddie></root>' xml = xpath.domify(xml_string) field = DateField(xpath='/root/kiddie/value') response = field.parse(xml, None) date = datetime.datetime(2008, 0o6, 21, 10, 36, 12) self.assertEqual(date, response)
def test_date_field_strips_utc_offset_from_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>2008-06-21T10:36:12-06:00</value></kiddie></root>' xml = xpath.domify(xml_string) field = DateField(xpath='/root/kiddie/value') response = field.parse(xml,None) date = datetime.datetime(2008,06,21,10,36,12) self.assertEquals(date, response)
def test_date_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = "<root><kiddie><value>2008-06-21T10:36:12</value></kiddie></root>" xml = xpath.domify(xml_string) field = DateField(xpath="/root/kiddie/value") response = field.parse(xml, None) date = datetime.datetime(2008, 06, 21, 10, 36, 12) self.assertEquals(date, response)
def _get_xml(self): if self._dom is None: try : self._dom = xpath.domify(self._xml) except Exception, e: print self._xml print str(e) raise e
def _get_xml(self): if self._dom is None: try: self._dom = xpath.domify(self._xml) except Exception, e: print self._xml print str(e) raise e
def test_int_field_raises_exception_when_non_int_value_is_parsed(self): xml_string = '<root><kiddie><value>NaN</value></kiddie></root>' xml = xpath.domify(xml_string) field = IntField(xpath='/root/kiddie/value') try: response = field.parse(xml, None) self.fail('Should have raised an exception') except: pass
def parse(self, xml, namespace): matches = xpath.find_all(xml, self.xpath, namespace) if not xml_models.BaseField in self.field_type.__bases__: results = CollectionList(type=self.field_type) for match in matches: results.append(self.field_type(xml=match)) else: field = self.field_type(xpath = '.') results = [field.parse(xpath.domify(match), namespace) for match in matches] if self.order_by: results.sort(lambda a,b : cmp(getattr(a, self.order_by), getattr(b, self.order_by))) return results
def parse(self, xml, namespace): matches = xpath.find_all(xml, self.xpath, namespace) if not xml_models.BaseField in self.field_type.__bases__: results = CollectionList(type=self.field_type) for match in matches: results.append(self.field_type(xml=match)) else: field = self.field_type(xpath='.') results = [ field.parse(xpath.domify(match), namespace) for match in matches ] if self.order_by: results.sort(lambda a, b: cmp(getattr(a, self.order_by), getattr(b, self.order_by))) return results
def test_int_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>123</value></kiddie></root>' xml = xpath.domify(xml_string) field = IntField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEquals(123, response)
def test_char_field_reads_from_attribute(self): xml_string = '<Listing><Details><Bathrooms comment="Nice Baths">6</Bathrooms></Details></Listing>' xml = xpath.domify(xml_string) field = CharField(xpath='/Listing/Details/Bathrooms/@comment') response = field.parse(xml, None) self.assertEquals('Nice Baths', response)
def test_char_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>Muppets rock</value></kiddie></root>' xml = xpath.domify(xml_string) field = CharField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEquals('Muppets rock', response)
def test_bool_field_returns_true_when_xpathed_value_for_the_node_is_true(self): xml_string = '<root><kiddie1><value>true</value></kiddie1></root>' xml = xpath.domify(xml_string) field = BoolField(xpath='/root/kiddie1/value') response = field.parse(xml, None) self.assertEquals(True, response)
def test_date_field_returns_none_when_xpathed_value_for_the_node_is_none(self): xml_string = '<root><kiddie><value></value></kiddie></root>' xml = xpath.domify(xml_string) field = DateField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEquals(None, response)
def test_bool_field_returns_false_when_xpathed_value_for_the_node_is_false(self): xml_string = "<root><kiddie1><value1>false</value1></kiddie1></root>" xml = xpath.domify(xml_string) field = BoolField(xpath="/root/kiddie1/value1") response = field.parse(xml, None) self.assertEquals(False, response)
def test_char_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>Muppets rock</value></kiddie></root>' xml = xpath.domify(xml_string) field = CharField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEqual('Muppets rock', response)
def test_int_field_returns_xpathed_value_for_the_node_passed_in(self): xml_string = '<root><kiddie><value>123</value></kiddie></root>' xml = xpath.domify(xml_string) field = IntField(xpath='/root/kiddie/value') response = field.parse(xml, None) self.assertEqual(123, response)
def test_char_field_reads_from_attribute(self): xml_string = '<Listing><Details><Bathrooms comment="Nice Baths">6</Bathrooms></Details></Listing>' xml = xpath.domify(xml_string) field = CharField(xpath='/Listing/Details/Bathrooms/@comment') response = field.parse(xml, None) self.assertEqual('Nice Baths', response)
def test_int_field_reads_from_attribute(self): xml_string = '<Listing id="123"/>' xml = xpath.domify(xml_string) field = IntField(xpath='/Listing/@id') response = field.parse(xml, None) self.assertEquals(123, response)
def test_int_field_reads_from_attribute(self): xml_string = '<Listing id="123"/>' xml = xpath.domify(xml_string) field = IntField(xpath='/Listing/@id') response = field.parse(xml, None) self.assertEqual(123, response)