def test_TimestampIndex(self): """ Tests indexing of timestamps. """ # w/ microseconds dt = UTCDateTime(2008, 10, 23, 11, 53, 12, 54000) idx = XmlIndex(self.rt1, "/station/creation_date", index.TIMESTAMP_INDEX) timestr = "%f" % dt.timestamp doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.datetime) # w/o microseconds dt = UTCDateTime(2008, 10, 23, 11, 53, 12) idx = XmlIndex(self.rt1, "/station/creation_date", index.TIMESTAMP_INDEX) timestr = "%f" % dt.timestamp doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.datetime) # negative timestamp works too dt = UTCDateTime(1969, 12, 31, 23, 36, 39, 500000) idx = XmlIndex(self.rt1, "/station/creation_date", index.TIMESTAMP_INDEX) timestr = "%f" % dt.timestamp doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.datetime)
def test_registerProcessorIndex(self): self.env.enableComponent(TestIndex) indexes = self.env.catalog.index_catalog.getIndexes( package_id='processorindextest', resourcetype_id='testtype') self.assertEqual(len(indexes), 1) idx = indexes[0] self.assertEqual(idx.resourcetype.package.package_id, 'processorindextest') self.assertEqual(idx.resourcetype.resourcetype_id, 'testtype') self.assertEqual(idx.type, index.PROCESSOR_INDEX) self.assertEqual(idx.options, TestIndex.__module__ + '.' + \ TestIndex.__name__) test_doc = newXMLDocument(RAW_XML1) res = idx.eval(test_doc, self.env) self.assertEqual(len(res), 3) self.assertEqual(type(res[0]), index.FloatIndexElement) self.assertEqual(type(res[1]), index.FloatIndexElement) self.assertEqual(type(res[2]), index.FloatIndexElement) self.assertEqual(res[0].key, 1) self.assertEqual(res[1].key, 2) self.assertEqual(res[2].key, 3) self.env.disableComponent(TestIndex) # cleanup self.env.catalog.deleteIndex(idx)
def test_flushIndex(self): # set up index1 = XmlIndex(self.rt1, "/station/station_code", label="code") index2 = XmlIndex(self.rt1, "/station/XY/paramXY", label="paramXY") self.catalog.registerIndex(index1) self.catalog.registerIndex(index2) res = Resource(self.rt1, document=newXMLDocument(RAW_XML2)) self.xmldb.addResource(res) self.catalog.indexResource(res) # index1 and index2 contain data el = self.catalog.dumpIndex(index1) self.assertEquals(len(el), 1) el = self.catalog.dumpIndex(index2) self.assertEquals(len(el), 3) # flush index1 self.catalog.flushIndex(index1) # index1 data has gone el = self.catalog.dumpIndex(index1) self.assertEquals(len(el), 0) # index2 data still there el = self.catalog.dumpIndex(index2) self.assertEquals(len(el), 3) # flush index2 self.catalog.flushIndex(index2) el = self.catalog.dumpIndex(index2) self.assertEquals(len(el), 0) # clean up: self.catalog.deleteIndex(index1) self.catalog.deleteIndex(index2) self.xmldb.deleteResource(res)
def testGetResourceList(self): # add some test resources first: res1 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data)) res2 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(res1) self.xmldbm.addResource(res2) l = self.xmldbm.getAllResources(self.test_package.package_id) assert len(l) == 2 for res in l: self.assertEqual(res.package.package_id, self.test_package.package_id) self.assertEqual(res.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) # delete test resource: self.xmldbm.deleteResource(res1) self.xmldbm.deleteResource(res2)
def testNumericIndex(self): test_doc = newXMLDocument(RAW_XML1) idx = XmlIndex(self.rt1, "/station/lon", index.NUMERIC_INDEX) res = idx.eval(test_doc, self.env)[0] self.assertEquals(type(res), NumericIndexElement) # self.assertEquals(type(res.key), float) self.assertEquals(res.key, '12.51200') # elements with wrong data type are ignored idx = XmlIndex(self.rt1, "/station/XY/paramXY", index.NUMERIC_INDEX) res = idx.eval(test_doc, self.env) self.assertEquals(len(res), 2)
def testTextIndex(self): test_doc = newXMLDocument(RAW_XML1) idx = XmlIndex(self.rt1, "/station/lon", index.TEXT_INDEX) res = idx.eval(test_doc, self.env)[0] self.assertEquals(type(res), index.TextIndexElement) self.assertEquals(type(res.key), unicode) self.assertEquals(res.key, '12.51200') # index which is empty on test resource idx = XmlIndex(self.rt1, "/station/empty", index.TEXT_INDEX) res = idx.eval(test_doc, self.env) self.assertEquals(len(res), 1)
def testBooleanIndex(self): idx = XmlIndex(self.rt1, "/station/bool", index.BOOLEAN_INDEX) doc = newXMLDocument(RAW_XML1 % ("", "True")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, True) doc = newXMLDocument(RAW_XML1 % ("", "False")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, False) doc = newXMLDocument(RAW_XML1 % ("", "1")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, True) doc = newXMLDocument(RAW_XML1 % ("", "0")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, False) doc = newXMLDocument(RAW_XML1 % ("", "something")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, True)
def modifyResource(self, resource, xml_data, uid=None): """ Modify the XML document of an already existing resource. In case of a version controlled resource a new revision is created. """ new_resource = Resource(resourcetype=resource.resourcetype, document=newXMLDocument(xml_data), name=resource.name) self.validateResource(new_resource) self.xmldb.modifyResource(resource, new_resource, uid) # we only keep indexes for the newest revision self.index_catalog.flushResource(resource) self.index_catalog.indexResource(new_resource)
def test_indexResource(self): # set up res = Resource(self.rt1, document=newXMLDocument(RAW_XML2)) self.xmldb.addResource(res) index1 = XmlIndex(self.rt1, "/station/station_code", label='idx1') index2 = XmlIndex(self.rt1, "/station/XY/paramXY", label='idx2') index3 = XmlIndex(self.rt1, "/station/test_date", FLOAT_INDEX) self.catalog.registerIndex(index1) self.catalog.registerIndex(index2) self.catalog.registerIndex(index3) # index resource r = self.catalog.indexResource(res) self.assertEquals(len(r), 5) # dump first index el = self.catalog.dumpIndex(index1) self.assertEquals(len(el), 1) self.assertEquals(el[0].key, "GENF") self.assertEquals(el[0].document.data, res.document.data) # dump second index el = self.catalog.dumpIndex(index2) self.assertEqual(len(el), 3) keys = ["0", "2.5", "99"] for e in el: assert e.key in keys keys.remove(e.key) self.assertEquals(e.document.data, res.document.data) # dumpIndexByResource el = self.catalog.dumpIndexByResource(res) self.assertEqual(len(el), 5) self.assertEquals(el[0].key, "GENF") self.assertEquals(el[0].document.data, res.document.data) self.assertEquals(el[0].index.xpath, "/station/station_code") self.assertTrue(el[1].key in ["0", "2.5", "99"]) self.assertEquals(el[1].document.data, res.document.data) self.assertEquals(el[1].index.xpath, "/station/XY/paramXY") self.assertTrue(el[2].key in ["0", "2.5", "99"]) self.assertEquals(el[2].document.data, res.document.data) self.assertEquals(el[2].index.xpath, "/station/XY/paramXY") self.assertTrue(el[3].key in ["0", "2.5", "99"]) self.assertEquals(el[3].document.data, res.document.data) self.assertEquals(el[3].index.xpath, "/station/XY/paramXY") self.assertEquals(el[4].key, 2008.123456789) self.assertEquals(el[4].document.data, res.document.data) self.assertEquals(el[4].index.xpath, "/station/test_date") # clean up self.catalog.deleteIndex(index1) self.catalog.deleteIndex(index2) self.catalog.deleteIndex(index3) self.xmldb.deleteResource(res)
def test_DateIndex(self): """ Tests indexing of dates. """ dt = datetime(2008, 10, 10, 11, 53, 0, 54000) # ISO 8601 w/o minus idx = XmlIndex(self.rt1, "/station/creation_date", index.DATE_INDEX) timestr = dt.strftime("%Y%m%d") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.date()) # ISO 8601 w/ minus idx = XmlIndex(self.rt1, "/station/creation_date", index.DATE_INDEX) timestr = dt.strftime("%Y-%m-%d") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.date()) # custom format idx = XmlIndex(self.rt1, "/station/creation_date", index.DATE_INDEX, options="%d.%m.%Y") timestr = dt.strftime("%d.%m.%Y") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.date())
def test_StylesheetRegistry(self): # with resourcetype self.env.registry.stylesheets.register('testpackage0', 'weapon', 'xhtml', TEST_STYLESHEET) # without resourcetype self.env.registry.stylesheets.register('testpackage0', None, 'xhtml', TEST_STYLESHEET) stylesheet = self.env.registry.stylesheets.get\ (package_id='testpackage0', resourcetype_id='weapon') self.assertEqual(len(stylesheet), 1) self.assertEqual(stylesheet[0].package.package_id, 'testpackage0') self.assertEqual(stylesheet[0].resourcetype.resourcetype_id, 'weapon') self.assertEqual(stylesheet[0].type, 'xhtml') self.assertEquals(stylesheet[0].content_type, 'application/json') stylesheet_nort = self.env.registry.stylesheets.get(package_id=\ 'testpackage0') self.assertEqual(len(stylesheet_nort), 1) self.assertEqual(stylesheet_nort[0].package.package_id, 'testpackage0') self.assertEqual(stylesheet_nort[0].resourcetype.resourcetype_id, None) # transformations res_list = Resource(document=newXMLDocument(TEST_RESLIST)) self.assertEquals(stylesheet[0].transform(res_list), '{"mapping":["/seishub/schema/browser"],"resource"' + \ ':["/seishub/schema/3","/seishub/schema/4"],}') self.assertEquals(stylesheet[0].transform(TEST_RESLIST), '{"mapping":["/seishub/schema/browser"],"resource"' + \ ':["/seishub/schema/3","/seishub/schema/4"],}') # get stylesheet resource res = stylesheet[0].resource self.assertEqual(res.document.data, TEST_STYLESHEET) self.assertEqual(res.package.package_id, 'seishub') self.assertEqual(res.resourcetype.resourcetype_id, 'stylesheet') # remove self.env.registry.stylesheets.delete( stylesheet[0].package.package_id, stylesheet[0].resourcetype.resourcetype_id, stylesheet[0].type) stylesheet = self.env.registry.stylesheets.get\ (package_id='testpackage0', resourcetype_id='weapon') self.assertEqual(len(stylesheet), 0) self.env.registry.stylesheets.delete('testpackage0', None, 'xhtml') stylesheet_nort = self.env.registry.stylesheets.get(package_id=\ 'testpackage0') self.assertEqual(len(stylesheet_nort), 0)
def test_StylesheetRegistry(self): # with resourcetype self.env.registry.stylesheets.register('testpackage0', 'weapon', 'xhtml', TEST_STYLESHEET) # without resourcetype self.env.registry.stylesheets.register('testpackage0', None, 'xhtml', TEST_STYLESHEET) stylesheet = self.env.registry.stylesheets.get\ (package_id='testpackage0', resourcetype_id='weapon') self.assertEqual(len(stylesheet), 1) self.assertEqual(stylesheet[0].package.package_id, 'testpackage0') self.assertEqual(stylesheet[0].resourcetype.resourcetype_id, 'weapon') self.assertEqual(stylesheet[0].type, 'xhtml') self.assertEquals(stylesheet[0].content_type, 'application/json') stylesheet_nort = self.env.registry.stylesheets.get(package_id=\ 'testpackage0') self.assertEqual(len(stylesheet_nort), 1) self.assertEqual(stylesheet_nort[0].package.package_id, 'testpackage0') self.assertEqual(stylesheet_nort[0].resourcetype.resourcetype_id, None) # transformations res_list = Resource(document=newXMLDocument(TEST_RESLIST)) self.assertEquals(stylesheet[0].transform(res_list), '{"mapping":["/seishub/schema/browser"],"resource"' + \ ':["/seishub/schema/3","/seishub/schema/4"],}') self.assertEquals(stylesheet[0].transform(TEST_RESLIST), '{"mapping":["/seishub/schema/browser"],"resource"' + \ ':["/seishub/schema/3","/seishub/schema/4"],}') # get stylesheet resource res = stylesheet[0].resource self.assertEqual(res.document.data, TEST_STYLESHEET) self.assertEqual(res.package.package_id, 'seishub') self.assertEqual(res.resourcetype.resourcetype_id, 'stylesheet') # remove self.env.registry.stylesheets.delete(stylesheet[0].package.package_id, stylesheet[0].resourcetype.resourcetype_id, stylesheet[0].type) stylesheet = self.env.registry.stylesheets.get\ (package_id='testpackage0', resourcetype_id='weapon') self.assertEqual(len(stylesheet), 0) self.env.registry.stylesheets.delete('testpackage0', None, 'xhtml') stylesheet_nort = self.env.registry.stylesheets.get(package_id=\ 'testpackage0') self.assertEqual(len(stylesheet_nort), 0)
def test_indexResourceWithGrouping(self): # set up res = Resource(self.rt1, document=newXMLDocument(RAW_XML4)) self.xmldb.addResource(res) index = XmlIndex(self.rt1, "/station/XY/Z/value", group_path="/station/XY") self.catalog.registerIndex(index) r = self.catalog.indexResource(res) self.assertEquals(len(r), 2) el = self.catalog.dumpIndex(index) self.assertEquals(len(el), 2) self.assertEquals(el[0].key, "3") self.assertEquals(el[0].group_pos, 0) self.assertEquals(el[0].document.data, res.document.data) self.assertEquals(el[1].key, "6") self.assertEquals(el[1].group_pos, 1) self.assertEquals(el[1].document.data, res.document.data) # clean up self.catalog.deleteIndex(index) self.xmldb.deleteResource(res)
def testIndexCommon(self): # index with single node key result: si = XmlIndex(self.rt1, xpath="/station/station_code") # index with multiple nodes key result: mi = XmlIndex(self.rt1, xpath="/station/XY/paramXY") # index which is empty on test resource ei = XmlIndex(self.rt1, xpath="/station/empty") # # index which does not exists on test resource # ni = XmlIndex(self.rt1, xpath="/station/XXX") test_doc = newXMLDocument(RAW_XML1) class Foo(object): pass # pass a Foo: (which does not implement IXmlDoc) self.assertRaises(TypeError, si.eval, Foo()) # pass an empty XmlDoc: self.assertRaises(SeisHubError, si.eval, XmlDocument()) res = si.eval(test_doc, self.env) self.assertEquals(len(res), 1) self.assertEquals(res[0].index, si) self.assertEquals(res[0].document, test_doc) self.assertEquals(res[0].key, 'BERN') res = mi.eval(test_doc, self.env) self.assertEquals(len(res), 3) self.assertEquals(res[0].index, mi) self.assertEquals(res[0].document, test_doc) self.assertEquals(res[0].key, '20.5') self.assertEquals(res[1].index, mi) self.assertEquals(res[1].document, test_doc) self.assertEquals(res[1].key, '11.5') self.assertEquals(res[2].index, mi) self.assertEquals(res[2].document, test_doc) self.assertEquals(res[2].key, 'blah') res = ei.eval(test_doc, self.env) self.assertEquals(len(res), 1)
def addResource(self, package_id, resourcetype_id, xml_data, uid=None, name=None): """ Add a new resource to the database. @param package_id: package id @param resourcetype_id: resourcetype id @param xml_data: xml data @param uid: user id of creator @param name: optional resource name, defaults to unique integer id @return: Resource object """ _, resourcetype = self.env.registry.objects_from_id(package_id, resourcetype_id) res = Resource(resourcetype, document=newXMLDocument(xml_data, uid=uid), name=name) # get xml_doc to ensure the document is parsed res.document.xml_doc self.validateResource(res) self.xmldb.addResource(res) self.index_catalog.indexResource(res) return res
def testIndexGrouping(self): doc = newXMLDocument(RAW_XML2) idx1 = XmlIndex(self.rt1, "/station/XY/X", index.NUMERIC_INDEX, group_path="/station/XY") res = idx1.eval(doc, self.env) self.assertEqual(len(res), 2) self.assertEqual(res[0].key, '1') self.assertEqual(res[0].group_pos, 0) self.assertEqual(res[1].key, '4') self.assertEqual(res[1].group_pos, 1) idx2 = XmlIndex(self.rt1, "/station/XY/Y", index.NUMERIC_INDEX, group_path="/station/XY") res = idx2.eval(doc, self.env) self.assertEqual(len(res), 2) self.assertEqual(res[0].key, '2') self.assertEqual(res[0].group_pos, 0) self.assertEqual(res[1].key, '5') self.assertEqual(res[1].group_pos, 1) idx3 = XmlIndex(self.rt1, "/station/XY/Y/@id", index.NUMERIC_INDEX, group_path="/station/XY") res = idx3.eval(doc, self.env) self.assertEqual(len(res), 2) self.assertEqual(res[0].key, '1') self.assertEqual(res[0].group_pos, 0) self.assertEqual(res[1].key, '2') self.assertEqual(res[1].group_pos, 1) idx4 = XmlIndex(self.rt1, "/station/XY/Z/value", index.NUMERIC_INDEX, group_path="/station/XY") res = idx4.eval(doc, self.env) self.assertEqual(len(res), 2) self.assertEqual(res[0].key, '3') self.assertEqual(res[0].group_pos, 0) self.assertEqual(res[1].key, '6') self.assertEqual(res[1].group_pos, 1)
def testXml_data(self): test_res = newXMLDocument(TEST_XML) xml_data = test_res.getData() self.assertEquals(xml_data, TEST_XML) test_res.data = TEST_BAD_XML self.assertRaises(InvalidObjectError, test_res.getXml_doc)
def testUnversionedResource(self): #====================================================================== # addResource() #====================================================================== # add empty resource empty = Resource(document=XmlDocument()) self.assertRaises(InvalidParameterError, self.xmldbm.addResource, empty) res1 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data, uid='testuser')) otherpackage = self.env.registry.db_registerPackage("otherpackage") othertype = self.env.registry.db_registerResourceType( "otherpackage", "testml") res2 = Resource(othertype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(res1) self.xmldbm.addResource(res2) # try to add a resource with same id res3 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data), id=res2.id) self.assertRaises(DuplicateObjectError, self.xmldbm.addResource, res3) #====================================================================== # getResource() #====================================================================== result = self.xmldbm.getResource(id=res1.id) # check lazyness of Resource.data: assert isinstance(result.document._data, DbAttributeProxy) self.assertEquals(result.name, str(res1.id)) self.assertEquals(result.document.data, self.test_data) self.assertTrue(result.document.meta.datetime) self.assertEquals(result.document.meta.size, len(self.test_data) + XML_DECLARATION_LENGTH) self.assertEquals(result.document.meta.hash, hash(self.test_data)) self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) result = self.xmldbm.getResource(id=res2.id) self.assertEquals(result.document.data, self.test_data) self.assertEquals(result.document.meta.uid, None) self.assertEquals(result.package.package_id, otherpackage.package_id) self.assertEquals(result.resourcetype.resourcetype_id, othertype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # modifyResource() #====================================================================== modres = Resource(res1.resourcetype, res1.id, document=newXMLDocument(self.test_data_mod)) self.xmldbm.modifyResource(res1, modres) result = self.xmldbm.getResource(res1.package.package_id, res1.resourcetype.resourcetype_id, id=res1.id) self.assertEquals(result.document.data, self.test_data_mod) # user id is still the same self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # deleteResource() #====================================================================== # by object self.xmldbm.deleteResource(res1) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # add again self.xmldbm.addResource(res1) # there again? self.assertTrue(self.xmldbm.getResource(id=res1.id)) # now by resource_id self.xmldbm.deleteResource(resource_id=res1._id) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # now for res2 self.xmldbm.deleteResource(res2) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res2.id) # cleanup self.env.registry.db_deleteResourceType(otherpackage.package_id, othertype.resourcetype_id) self.env.registry.db_deletePackage(otherpackage.package_id)
def testVersionControlledResource(self): testres = Resource(self.vc_resourcetype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(testres) result = self.xmldbm.getResource(id=testres.id) self.assertEquals(result.document.data, self.test_data) self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.vc_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, True) self.assertEquals(result.document.revision, 1) # modify resource / a new resource with same id testres_v2 = Resource(self.vc_resourcetype, document=newXMLDocument(self.test_data % 'r2'), id=result.id) self.xmldbm.modifyResource(testres, testres_v2) # get latest revision rev2 = self.xmldbm.getResource(testres.package.package_id, testres.resourcetype.resourcetype_id, id=testres.id) self.assertEquals(rev2.document.revision, 2) self.assertEquals(rev2.document._id, testres_v2.document._id) self.assertEquals(rev2.document.data, self.test_data % 'r2') # get previous revision rev1 = self.xmldbm.getResource(testres.package.package_id, testres.resourcetype.resourcetype_id, id=testres.id, revision=1) self.assertEquals(rev1.document.revision, 1) self.assertEquals(rev1._id, rev2._id) self.assertEquals(rev1.document.data, self.test_data) # get version history res2 = self.xmldbm.getRevisions(id=testres.id) self.assertEqual(len(res2.document), 2) self.assertEqual(res2.document[0].revision, 1) self.assertEqual(res2.document[0].data, self.test_data) self.assertEqual(res2.document[1].revision, 2) self.assertEqual(res2.document[1].data, self.test_data % 'r2') # add a third revision testres_v3 = Resource(self.vc_resourcetype, document=newXMLDocument(self.test_data % 'r3')) self.xmldbm.modifyResource(testres, testres_v3) res = self.xmldbm.getRevisions(id=testres.id) self.assertEqual(len(res.document), 3) # delete revision 2 self.xmldbm.deleteRevision(res2, 2) self.assertRaises(NotFoundError, self.xmldbm.getResource, testres.package.package_id, testres.resourcetype.resourcetype_id, None, 2, None, testres.id) res = self.xmldbm.getRevisions(id=testres.id) self.assertEqual(len(res.document), 2) # revert revision 1 self.xmldbm.revertResource(id=testres.id, revision=1) res = self.xmldbm.getResource(id=testres.id) self.assertEquals(res.document.revision, 1) self.assertEquals(res.document.data, self.test_data) # only one revision is left => res.document is not a list res = self.xmldbm.getRevisions(id=testres.id) self.assertEqual(res.document.revision, 1) # delete resource self.xmldbm.deleteResource(testres) # try to get latest revision (deleted) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=testres.id)
def testUnversionedResource(self): #====================================================================== # addResource() #====================================================================== # add empty resource empty = Resource(document=XmlDocument()) self.assertRaises(InvalidParameterError, self.xmldbm.addResource, empty) res1 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data, uid='testuser')) otherpackage = self.env.registry.db_registerPackage("otherpackage") othertype = self.env.registry.db_registerResourceType("otherpackage", "testml") res2 = Resource(othertype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(res1) self.xmldbm.addResource(res2) # try to add a resource with same id res3 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data), id=res2.id) self.assertRaises(DuplicateObjectError, self.xmldbm.addResource, res3) #====================================================================== # getResource() #====================================================================== result = self.xmldbm.getResource(id=res1.id) # check lazyness of Resource.data: assert isinstance(result.document._data, DbAttributeProxy) self.assertEquals(result.name, str(res1.id)) self.assertEquals(result.document.data, self.test_data) self.assertTrue(result.document.meta.datetime) self.assertEquals(result.document.meta.size, len(self.test_data) + XML_DECLARATION_LENGTH) self.assertEquals(result.document.meta.hash, hash(self.test_data)) self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) result = self.xmldbm.getResource(id=res2.id) self.assertEquals(result.document.data, self.test_data) self.assertEquals(result.document.meta.uid, None) self.assertEquals(result.package.package_id, otherpackage.package_id) self.assertEquals(result.resourcetype.resourcetype_id, othertype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # modifyResource() #====================================================================== modres = Resource(res1.resourcetype, res1.id, document=newXMLDocument(self.test_data_mod)) self.xmldbm.modifyResource(res1, modres) result = self.xmldbm.getResource(res1.package.package_id, res1.resourcetype.resourcetype_id, id=res1.id) self.assertEquals(result.document.data, self.test_data_mod) # user id is still the same self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # deleteResource() #====================================================================== # by object self.xmldbm.deleteResource(res1) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # add again self.xmldbm.addResource(res1) # there again? self.assertTrue(self.xmldbm.getResource(id=res1.id)) # now by resource_id self.xmldbm.deleteResource(resource_id=res1._id) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # now for res2 self.xmldbm.deleteResource(res2) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res2.id) # cleanup self.env.registry.db_deleteResourceType(otherpackage.package_id, othertype.resourcetype_id) self.env.registry.db_deletePackage(otherpackage.package_id)
def test_DateTimeIndex(self): """ Tests indexing of datetimes. """ # setup dt = datetime(2008, 10, 23, 11, 53, 12, 54000) dt2 = datetime(2008, 10, 23, 11, 53, 12) dt3 = datetime(2008, 10, 23) dt4 = datetime(2008, 10, 23, 11) dt5 = datetime(2008, 10, 23, 11 , 53) # ISO 8601 idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt.strftime("%Y%m%dT%H:%M:%S") + ".054000" doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt) # ISO 8601 w/ minus idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt.strftime("%Y-%m-%dT%H:%M:%S") + ".054000" doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt) # ISO 8601 w/ time zone idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt.strftime("%Y%m%dT%H:%M:%S") + ".054000Z" doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt) # ISO 8601 w/o T idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt.strftime("%Y%m%d %H:%M:%S") + ".054000" doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt) # ISO 8601 w/o milliseconds idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt2.strftime("%Y%m%dT%H:%M:%S") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt2) # ISO 8601 w/o time - defaults to 00:00:00 idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt3.strftime("%Y-%m-%d") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt3) # ISO 8601 w/o minutes - defaults to :00:00 idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt4.strftime("%Y-%m-%dT%H") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt4) # ISO 8601 w/o seconds - defaults to :00 idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX) timestr = dt5.strftime("%Y-%m-%dT%H:%M") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt5) # with custom format # microseconds are ignored since not supported by strftime() idx = XmlIndex(self.rt1, "/station/creation_date", index.DATETIME_INDEX, "%H:%M:%S - %Y%m%d") timestr = dt.strftime("%H:%M:%S - %Y%m%d") doc = newXMLDocument(RAW_XML1 % (timestr, "")) res = idx.eval(doc, self.env)[0] self.assertEqual(res.key, dt.replace(microsecond=0))