def testRun(self): """Tests creation of a detached util""" params = {"TopSessionPath": [self.__sessiontop]} reqobj = InputRequest(params) reqobj.printIt() du = DetachUtils(reqobj, verbose=True) ok = du.set(workerObj=self, workerMethod="_runTest") self.assertTrue(ok) du.runDetach() sph = reqobj.getValue("semaphore") print("Semaphore %s" % sph) cntmax = 10 cnt = 0 for cnt in range(cntmax): if du.semaphoreExists(sph): break time.sleep(1) print("SLEEP %s" % cnt) self.assertNotEqual(cnt, cntmax - 1, "Count exceeded") self.assertTrue(du.semaphoreExists(sph), "Semaphore file missing") # Test broken semaphore reference val = du.getSemaphore("non_existent_semaphore") self.assertEqual(val, "FAIL") val = du.getSemaphore(sph) self.assertEqual(val, "OK", "")
def __setup(self, defSiteId=None, sessionId=None): """Simulate the web application environment for managing session storage of temporaty data files.""" self.__siteId = getSiteId(defaultSiteId=defSiteId) # self.__cI = ConfigInfo(self.__siteId) self.__topPath = self.__cI.get("SITE_WEB_APPS_TOP_PATH") self.__topSessionPath = self.__cI.get( "SITE_WEB_APPS_TOP_SESSIONS_PATH") # self.__reqObj = InputRequest({}, verbose=self.__verbose, log=self.__lfh) self.__reqObj.setValue("TopSessionPath", self.__topSessionPath) self.__reqObj.setValue("TopPath", self.__topPath) self.__reqObj.setValue("WWPDB_SITE_ID", self.__siteId) # self.__reqObj.setValue("SITE_DA_INTERNAL_DB_USER", os.environ["SITE_DA_INTERNAL_DB_USER"]) self.__reqObj.setValue("SITE_DA_INTERNAL_DB_PASSWORD", os.environ["SITE_DA_INTERNAL_DB_PASSWORD"]) os.environ["WWPDB_SITE_ID"] = self.__siteId if sessionId is not None: self.__reqObj.setValue("sessionid", sessionId) # retained due to side effects _sessionObj = self.__reqObj.newSessionObj() # noqa: F841 self.__reqObj.printIt(ofh=self.__lfh)
def testBadArg(self): """Tests passing bad argument to set()""" params = {"TopSessionPath": [self.__sessiontop]} reqobj = InputRequest(params) reqobj.printIt() du = DetachUtils(reqobj, verbose=True) ok = du.set(workerObj=self, workerMethod="_badrunTest") self.assertFalse(ok)
def testWebappWorkerUpload(self): """Tests WebAppWorker upload file""" reqObj = InputRequest(self.__paramDict) app = MyWebAppWorker(reqObj, verbose=True) self.assertIsNotNone(app.newSessionOp()) # File uploaded sObj = reqObj.getSessionObj() sesspath = sObj.getPath() app.uploadFile() # Ensure present dst = os.path.join(sesspath, "WebAppWorkerBaseTests.py") self.assertTrue(os.path.exists(dst)) self.assertTrue(filecmp.cmp(dst, self.__reffile))
def testWebappWorkerParameter(self): """Tests WebAppWorker parameter setting""" reqObj = InputRequest(self.__paramDict) app = MyWebAppWorker(reqObj, verbose=True) self.assertIsNotNone(app.newSessionOp()) self.assertTrue( app.saveSessionParameter("test", "5", { "value1": 2, "value2": 3 })) self.assertEqual(app.getSessionParameter("test"), "5") self.assertEqual(app.getSessionParameter("value1"), 2) self.assertEqual(app.getSessionParameter("value2"), 3)
def testWebappWorkerSemaphore(self): """Tests WebAppWorker semaphore""" reqObj = InputRequest(self.__paramDict) app = MyWebAppWorker(reqObj, verbose=True) self.assertIsNotNone(app.newSessionOp()) # Semaphore testing app.setSemaphore() self.assertFalse(app.semaphoreExists()) # This redirects class self._lfh app.openSemaphoreLog() app.postSemaphore(value="Working") self.assertTrue(app.semaphoreExists()) self.assertEqual(app.getSemaphore(), "Working") app.closeSemaphoreLog()
def testInstantiate(self): _vc = WebRequest() # noqa: F841 params = {"TopSessionPath": [self.__sessiontop]} reqobj = InputRequest(params) # Needs a valid reqobj _vc = UtilDataStore(reqobj) # noqa: F841 _vc = WebAppWorkerBase(reqobj) # noqa: F841 _vc = WebDownloadUtils(reqobj, verbose=True) # noqa: F841 _vc = WebUploadUtils(reqobj, verbose=True) # noqa: F841 _vc = SessionManager() # noqa: F841 _vc = FileUtils("xxxx", reqobj) # noqa: F841 _vc = WwPdbResponse() # noqa: F841 body = b"input" INPUT = BytesIO(body) environ = { "wsgi.input": INPUT, "CONTENT_LENGTH": len(body), "REQUEST_METHOD": "POST", } with WwPdbRequest(environ) as _vc: # noqa: F841 pass
def testInputRequest(self): """Tests InputRequest access""" paramDict = { "TopSessionPath": [self.__sessiontop], "request_path": ["service/testpath"] } ir = InputRequest(paramDict) # Test return format self.assertEqual(ir.getReturnFormat(), "") ir.setDefaultReturnFormat("html") ir.setReturnFormat("html") self.assertEqual(ir.getReturnFormat(), "html") self.assertEqual(ir.getRequestPath(), "service/testpath") sObj = ir.newSessionObj() self.assertNotEqual("", ir.getSessionId()) self.assertIsNotNone(ir.getSessionPath()) self.assertIsNotNone(ir.getTopSessionPath()) # No semaphore available in this interface self.assertEqual(ir.getSemaphore(), "") sid = sObj.getId() self.assertIsNotNone(sid) sObj = ir.getSessionObj() self.assertEqual(sid, sObj.getId()) sObj = ir.newSessionObj(forceNew=True) sid = sObj.getId() self.assertIsNotNone(sid)
def testResponseConent(self): """Tests WebRequest access""" reqObj = InputRequest(self.__paramDict) rc = ResponseContent(reqObj) rc.setData(["test"]) rc.setReturnFormat("jsonData") self.assertEqual(rc.get(), { "CONTENT_TYPE": "application/json", "RETURN_STRING": '["test"]' }) # ResponseContent d = datetime.now() rc.set("date", d, asJson=True) rc.set("Other", "value") rc.setReturnFormat("json") self.assertNotEqual(rc.get(), "") # Misc adds rc.appendHtmlList(["<p>Maybe</p>", "<p>There</p>"]) rc.setHtmlList(["<p>Hello</p>", "<p>There</p>"]) rc.appendHtmlList(["<p>Added</p>"]) rc.setHtmlText("Some text") rc.setLocation("https://wwpdb.org") rc.setReturnFormat("html") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("text") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("json") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("jsonText") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("jsonData") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("location") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("jsonp") self.assertNotEqual(rc.get(), "") rc.setReturnFormat("jsonText") self.assertNotEqual(rc.get(), "") # Error handling rc.setStatus("ok") self.assertFalse(rc.isError()) rc.setError("failed to compute") self.assertTrue(rc.isError()) rc.setStatusCode("ok") # Templates rc.setHtmlTextFromTemplate(os.path.join(self.__HERE, "template.txt"), self.__HERE, parameterDict={"T1": 2}) sys.stderr.write("%s\n" % rc.dump()) # Should error rc.setHtmlTextFromTemplate(os.path.join(self.__HERE, "missingtemplate.txt"), self.__HERE, parameterDict={"T1": 2}) # Files rc.setTextFile(os.path.join(self.__HERE, "template.txt")) rc.setHtmlContentPath("https://wwpdb.org") self.assertEqual( ("text/plain", None), rc.getMimetypeAndEncoding(os.path.join(self.__HERE, "template.txt"))) rc.setBinaryFile(os.path.join(self.__HERE, "template.txt")) rc.wrapFileAsJsonp(os.path.join(self.__HERE, "template.txt"), "") rc.setReturnFormat("binary") rc.get() self.assertIn("dump", rc.dump()[0])
class StatusHistoryExec(object): def __init__(self, defSiteId="WWWDPB_INTERNAL_RU", sessionId=None, verbose=True, log=sys.stderr): self.__lfh = log self.__verbose = verbose self.__setup(defSiteId=defSiteId, sessionId=sessionId) def __setup(self, defSiteId=None, sessionId=None): """Simulate the web application environment for managing session storage of temporaty data files.""" self.__siteId = getSiteId(defaultSiteId=defSiteId) # self.__cI = ConfigInfo(self.__siteId) self.__topPath = self.__cI.get("SITE_WEB_APPS_TOP_PATH") self.__topSessionPath = self.__cI.get( "SITE_WEB_APPS_TOP_SESSIONS_PATH") # self.__reqObj = InputRequest({}, verbose=self.__verbose, log=self.__lfh) self.__reqObj.setValue("TopSessionPath", self.__topSessionPath) self.__reqObj.setValue("TopPath", self.__topPath) self.__reqObj.setValue("WWPDB_SITE_ID", self.__siteId) # self.__reqObj.setValue("SITE_DA_INTERNAL_DB_USER", os.environ["SITE_DA_INTERNAL_DB_USER"]) self.__reqObj.setValue("SITE_DA_INTERNAL_DB_PASSWORD", os.environ["SITE_DA_INTERNAL_DB_PASSWORD"]) os.environ["WWPDB_SITE_ID"] = self.__siteId if sessionId is not None: self.__reqObj.setValue("sessionid", sessionId) # retained due to side effects _sessionObj = self.__reqObj.newSessionObj() # noqa: F841 self.__reqObj.printIt(ofh=self.__lfh) # def doCreateStatusHistory(self, numProc=1, overWrite=False): """ """ try: shu = StatusHistoryUtils(reqObj=self.__reqObj, verbose=self.__verbose, log=self.__lfh) entryIdList = shu.getEntryIdList() if numProc > 1: rL = shu.createHistoryMulti(entryIdList, numProc=numProc, overWrite=overWrite) else: rL = shu.createHistory(entryIdList, overWrite=overWrite) self.__lfh.write( "StatusHistoryExec.doCreateStatusHistory() %d status files created.\n\n" % len(rL)) except: # noqa: E722 pylint: disable=bare-except traceback.print_exc(file=self.__lfh) def doLoadStatusHistory(self, numProc=1, newTable=False): """ """ try: shu = StatusHistoryUtils(reqObj=self.__reqObj, verbose=self.__verbose, log=self.__lfh) if numProc > 1: return shu.loadStatusHistoryMulti(numProc, newTable=newTable) else: return shu.loadStatusHistory(newTable=newTable) except: # noqa: E722 pylint: disable=bare-except traceback.print_exc(file=self.__lfh) return False def doLoadEntryStatusHistory(self, entryId): """Load/reload status history file for the input entryId""" try: shu = StatusHistoryUtils(reqObj=self.__reqObj, verbose=self.__verbose, log=self.__lfh) return shu.loadEntryStatusHistory(entryIdList=[entryId]) except: # noqa: E722 pylint: disable=bare-except traceback.print_exc(file=self.__lfh) return False def doCreateEntryStatusHistory(self, entryId, overWrite=False): """ """ try: shu = StatusHistoryUtils(reqObj=self.__reqObj, verbose=self.__verbose, log=self.__lfh) rL = shu.createHistory([entryId], overWrite=overWrite) self.__lfh.write( "StatusHistoryExec.doCreateEntryStatusHistory() %d status files created.\n\n" % len(rL)) except: # noqa: E722 pylint: disable=bare-except traceback.print_exc(file=self.__lfh) def doCreateStatusHistorySchema(self): """Create/recreate status history schema -""" try: shu = StatusHistoryUtils(reqObj=self.__reqObj, verbose=self.__verbose, log=self.__lfh) return shu.createStatusHistorySchema() except: # noqa: E722 pylint: disable=bare-except traceback.print_exc(file=self.__lfh) return False