def _createDocument(self): d = document.Document(uuid.uuid4()) d.setTestMode(True) docUrl = self._buildUrl() d.load(docUrl) return d
def test_get_xml(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() result = x.get('first/firstChild2', 'application/xml') self.assertEqual(result.strip(), '<firstChild2 attr="value" />') self.assertEqual(d._count(), DOCUMENT_COUNT)
def test_get_json(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() result = x.get('first/firstChild2', 'application/json') result = json.loads(result) self.assertEqual(result, dict(attr="value")) self.assertEqual(d._count(), DOCUMENT_COUNT)
def test_xpath_namespaces(self): d = document.Document(uuid.uuid4()) docUrl = self._buildUrl('_namespaces') d.load(docUrl) for e in d.tree.getroot().iter(): p = d._getXPath(e) e2 = d._getElementByPath(p) self.assertIs(e, e2)
def test_saveDocument(self): d = document.Document(uuid.uuid4()) docUrl = self._buildUrl() d.load(docUrl) newDocUrl = self._buildUrl('_tmp') d.save(newDocUrl) oldData = urllib.request.urlopen(docUrl).read().strip() newData = urllib.request.urlopen(newDocUrl).read().strip() self.assertEqual(newData, oldData)
def test_put_json(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() path = x.paste('third', 'begin', 'thirdChild', '{"depth":"2"}', 'application/json') self.assertEqual(d._count(), DOCUMENT_COUNT + 1) self.assertEqual(path, '/testDocument/third[1]/thirdChild[1]') grandData = x.get('third/thirdChild', 'application/json') grandData = json.loads(grandData) self.assertEqual(grandData, dict(depth="2"))
def test_put_xml(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() path = x.paste( 'third', 'begin', None, '<thirdChild><thirdGrandChild depth="3" /></thirdChild>', 'application/xml') self.assertEqual(d._count(), DOCUMENT_COUNT + 2) self.assertEqual(path, '/testDocument/third[1]/thirdChild[1]') grandData = x.get('third/thirdChild/thirdGrandChild', 'application/json') grandData = json.loads(grandData) self.assertEqual(grandData, dict(depth="3"))
def test_move(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() newpath = x.move('second/second2', 'before', 'second/second3') self.assertEqual(newpath, '/testDocument/second[1]/second3[1]') newpath = x.move('second/second2', 'after', 'second/second1') self.assertEqual(newpath, '/testDocument/second[1]/second1[1]') secondData = x.get('second', 'application/xml') secondData = secondData.strip() self.assertEqual(secondData, '<second><second3 /><second2 /><second1 /></second>') self.assertEqual(d._count(), DOCUMENT_COUNT)
def test_put_where(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) x = d.xml() path = x.paste('third', 'begin', 'third3', '{}', 'application/json') x.paste(path, 'before', 'third2', '{}', 'application/json') x.paste(path, 'after', 'third4', '{}', 'application/json') x.paste('third', 'begin', 'third1', '{}', 'application/json') x.paste('third', 'end', 'third5', '{}', 'application/json') self.assertEqual(d._count(), DOCUMENT_COUNT + 5) thirdData = x.get('third', 'application/xml') thirdData = thirdData.strip() self.assertEqual( thirdData, '<third><third1 /><third2 /><third3 /><third4 /><third5 /></third>' )
def test_createDocument2(self): d = document.Document(uuid.uuid4()) docUrl = self._buildUrl() d.load(docUrl) self.assertEqual(d._count(), DOCUMENT_COUNT)
def test_createDocument(self): d = document.Document(uuid.uuid4()) d.loadXml(DOCUMENT.strip()) self.assertEqual(d._count(), DOCUMENT_COUNT)