def test_editEntryS(self): createTablesBig() err, s = tos.ScriptTable().getByID(2) # change some of s's attributes s.name = "newName" s.fileName = "newFilename.sh" s.author = 1 s.desc = "New desc" s.size = 200.0 s.isAdmin = False s.dtModified = dt.now() # write these edits to table err, s2 = tos.ScriptTable().editEntry(s) errExp = pref.getError(pref.ERROR_SUCCESS) # checks self.assertEqual(err, errExp) self.assertEqual(s.ID, s2.ID) self.assertEqual(s.name, s2.name) self.assertEqual(s.fileName, s2.fileName) self.assertEqual(s.author, s2.author) self.assertEqual(s.desc, s2.desc) self.assertEqual(s.dtCreated, s2.dtCreated) self.assertEqual(s.dtModified, s2.dtModified) self.assertEqual(s.size, s2.size) self.assertEqual(s.isAdmin, s2.isAdmin)
def test_addEntrySL(self): createEmptyTables() # need user entry first for foreign key u = tou.UserTable().createEntry("rbroders", "hella_secure_hashed_password", True) err = tou.UserTable().add(u) # uID will be 1 # need script entry for foreign key s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 1, "emptry script used for testing", False) err = tos.ScriptTable().add(s) # need computer entry for foreign key c = toc.ComputerTable().createEntry( 1, "RachelsSurface", "Raquels Computer", "Rachel's wonderful awful computer", "rbroders", "idk how IPs are formatted ya yeet", False) err = toc.ComputerTable().add(c) # scriptLog entry sl = tosl.ScriptLogTable().createEntry(1, 1, 1, False) err = tosl.ScriptLogTable().add(sl) errExp = pref.getError(pref.ERROR_SUCCESS) # check stdout/stderr file creation outPath = pref.getNoCheck(pref.CONFIG_SCRIPT_LOG_PATH) + sl.stdoutFile errPath = pref.getNoCheck(pref.CONFIG_SCRIPT_LOG_PATH) + sl.stderrFile self.assertEqual(os.path.exists(outPath), True) self.assertEqual(os.path.exists(errPath), True) # check error and scriptlog ID self.assertEqual(err, errExp) self.assertEqual(sl.ID, 1)
def createTables(): ''' Create tables (scriptLog, script, computer and user) with one record inserted into each of them @param None. @return None. ''' createEmptyTables() # user entry u = tou.UserTable().createEntry("rbroders", "hella_secure_hashed_password", True) err = tou.UserTable().add(u) # uID will be 1 # script entry s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 1, "empty script used for testing", False) err = tos.ScriptTable().add(s) # computer entry c = toc.ComputerTable().createEntry(1, "RachelsSurface", "Raquels Computer", "Rachel's wonderful awful computer", "rbroders", "idk how IPs are formatted ya yeet", False) err = toc.ComputerTable().add(c) # scriptLog entry sl = tosl.ScriptLogTable().createEntry(1, 1, 1, False) err = tosl.ScriptLogTable().add(sl)
def createTablesBig(): ''' Create tables (scriptLog, script, computer and user) with multiple records inserted into each of them @param None. @return None. ''' createEmptyTables() # user entries u = tou.UserTable().createEntry("rbroders", "hella_secure_hashed_password", True) err = tou.UserTable().add(u) u = tou.UserTable().createEntry("lmilne", "ya_yeet", False) err = tou.UserTable().add(u) u = tou.UserTable().createEntry("jbusch", "beer-brand", True) err = tou.UserTable().add(u) # script entries s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 1, "empty script used for testing", False) err = tos.ScriptTable().add(s) s = tos.ScriptTable().createEntry("create_happiness", "reboot.sh", 1, "doggies and kitties", True) err = tos.ScriptTable().add(s) s = tos.ScriptTable().createEntry("solve_world_hunger", "shutdown.sh", 1, "crazy scritpt", False) err = tos.ScriptTable().add(s) s = tos.ScriptTable().createEntry("leprechan_script", "sleepScript copy.sh", 1, "found at end of rainbow", True) err = tos.ScriptTable().add(s) # computer entries c = toc.ComputerTable().createEntry(1, "RachelsSurface", "Raquels Computer", "Rachel's wonderful awful computer", "rbroders", "idk how IPs are formatted ya yeet", False) err = toc.ComputerTable().add(c) c = toc.ComputerTable().createEntry( 1, "Rachels Air", "Old computer", "dusty computer in closet", "rbroders22", "idk how IPs are formatted ya yeet... we can dream", True) err = toc.ComputerTable().add(c) c = toc.ComputerTable().createEntry( 2, "Larry's Pi", "Raspberry Pie", "yum love pie", "lmilne", "idk how IPs are formatted ya yeet ... maybe one day", False) err = toc.ComputerTable().add(c) c = toc.ComputerTable().createEntry( 3, "James' Computer", "JB Computer", "its a computer what can I say", "jbusch", "idk how IPs are formatted ya yeet...still", False) err = toc.ComputerTable().add(c) # scriptLog entries sl = tosl.ScriptLogTable().createEntry(1, 1, 3, False) err = tosl.ScriptLogTable().add(sl) sl = tosl.ScriptLogTable().createEntry(2, 2, 4, False) err = tosl.ScriptLogTable().add(sl) sl = tosl.ScriptLogTable().createEntry(3, 4, 2, False) err = tosl.ScriptLogTable().add(sl) sl = tosl.ScriptLogTable().createEntry(1, 3, 1, False) err = tosl.ScriptLogTable().add(sl)
def test_addEntryS(self): createEmptyTables() # need user entry first for foreign key u = tou.UserTable().createEntry("rbroders", "hella_secure_hashed_password", True) err = tou.UserTable().add(u) # uID will be 1 # script entry s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 1, "emptry script used for testing", False) err = tos.ScriptTable().add(s) errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp) self.assertEqual(s.ID, 1)
def test_deleteTableU_S(self): createTables() err = tosl.ScriptLogTable().deleteTable() err = tos.ScriptTable().deleteTable() err = toc.ComputerTable().deleteTable() err = tou.UserTable().deleteTable() errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp)
def test_editEntryS_F(self): createTablesBig() err, s = tos.ScriptTable().getByID(2) # change some of s's attributes s.ID = None # write these edits to table err, s2 = tos.ScriptTable().editEntry(s) errExp = pref.getError(pref.ERROR_NO_ID_PROVIDED, args=("editEntry", "Script")) # checks self.assertEqual(err, errExp) self.assertEqual(s.ID, s2.ID) self.assertEqual(s.name, s2.name) self.assertEqual(s.fileName, s2.fileName) self.assertEqual(s.author, s2.author) self.assertEqual(s.desc, s2.desc) self.assertEqual(s.dtCreated, s2.dtCreated) self.assertEqual(s.dtModified, s2.dtModified) self.assertEqual(s.size, s2.size) self.assertEqual(s.isAdmin, s2.isAdmin)
def test_createEntryS(self): createEmptyTables() s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 0, "emptry script used for testing", False) self.assertEqual(s.name, "test_script_name") self.assertEqual(s.fileName, "test_script_name.sh") self.assertEqual(s.author, 0) self.assertEqual(s.desc, "emptry script used for testing") self.assertEqual(s.isAdmin, False)
def test_getAttrByIDS(self): # s = tos.ScriptTable().createEntry("test_script_name", "test_script_name.sh", 1, "empty script used for testing", False) createTables() errExp = pref.getError(pref.ERROR_SUCCESS) err, sID = tos.ScriptTable().getAttrByID("ID", 1) self.assertEqual(err, errExp) self.assertEqual(sID, 1) err, sName = tos.ScriptTable().getAttrByID("name", 1) self.assertEqual(err, errExp) self.assertEqual(sName, "test_script_name") err, sSize = tos.ScriptTable().getAttrByID("size", 1) self.assertEqual(err, errExp) self.assertEqual(sSize, 0.0) err, sAdmin = tos.ScriptTable().getAttrByID("isAdmin", 1) self.assertEqual(err, errExp) self.assertEqual(sAdmin, False)
def test_getByIDS(self): createTables() err, s = tos.ScriptTable().getByID(1) errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp) self.assertEqual(s.ID, 1) self.assertEqual(s.name, "test_script_name") self.assertEqual(s.fileName, "test_script_name.sh") self.assertEqual(s.author, 1) self.assertEqual(s.desc, "empty script used for testing") self.assertEqual(s.size, 0.0) self.assertEqual(s.isAdmin, False)
def add(entry: ScriptLog): ''' Takes a scriptLog object (which has not yet been added to the scriptLog SQL table), adds it to the table and updates scriptLog object's ID (ID is automatically generated using sqlite AUTOINCREMENT) This function is meant to take a scriptLog object generated from a call to the createEntry function. @param entry - object of class ScriptLog @return e - most recent error when executing function or Success if no error occurs ''' ID = None command = """ INSERT INTO sl (id, scriptID, userID, compID, startTime, endTime, returnVal, errorCode, stdoutFile, stderrFile, asAdmin) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""" data = entry.paramToList() e, ID = sqlFuncs.insert(command, data, "add", "ScriptLog") entry.ID = ID # access ID through entry object after executing this function ######### create names/files for stdoutFile, stderrFile - {STDOUT/STDERR}_SCRIPT_ID.log ######### # (1) Add names to entry object if (e == pref.getError(pref.ERROR_SUCCESS)): e, scriptName = tos.ScriptTable().getAttrByID( "name", entry.scriptID) if e == pref.getError(pref.ERROR_SUCCESS): entry.stdoutFile = "STDOUT_" + str(scriptName) + "_" + str( ID ) + ".log" # access stdoutFile through entry object after executing this function entry.stderrFile = "STDERR_" + str(scriptName) + "_" + str( ID ) + ".log" # access stderrFile through entry object after executing this function # (2) Write names to sql entry command2 = """UPDATE sl SET stdoutFile = \"""" + str( entry.stdoutFile) + """\", stderrFile = \"""" + str( entry.stderrFile) + """\" WHERE id = """ + str( ID) + """;""" e = sqlFuncs.exeCommand(command2, "add", "ScriptLog") # (3) Create files if (e == pref.getError(pref.ERROR_SUCCESS)): e = createFile( e, pref.getNoCheck(pref.CONFIG_SCRIPT_LOG_PATH), entry.stdoutFile) if (e == pref.getError(pref.ERROR_SUCCESS)): e = createFile( e, pref.getNoCheck(pref.CONFIG_SCRIPT_LOG_PATH), entry.stderrFile) if ( e != pref.getError(pref.ERROR_SUCCESS) ): # if any errors occured along the way, revert any potential changes (catch all) entry.stdoutFile = None entry.stderrFile = None return e #FIXME - should actions be undone if any errors occur along the way *thinking* - for loop
def clearTables(): ''' Drops all SQL tables in database (scriptLog, script, computer and user) @param None. @return None. @Notes All rows of a given table are deleted upon a drop table operation. If any of these deletions trigger errors due to foreign key constraints or other issues, an error will be raised. No error will be raised for dropping empty tables regardless of foreign key constraints on them. ''' e = tosl.ScriptLogTable().deleteTable() e = tos.ScriptTable().deleteTable() e = toc.ComputerTable().deleteTable() e = tou.UserTable().deleteTable()
def createEmptyTables(): ''' Creates empty SQL tables for scriptLog, script, computer and user @param None. @return None. @Notes Foreign key constraints are not checked when a table is created. There is nothing stopping the user from creating a foreign key definition that refers to a parent table that does not exist therefore creation order does not matter. ''' clearTables() e = tosl.ScriptLogTable().createTable() e = tos.ScriptTable().createTable() e = toc.ComputerTable().createTable() e = tou.UserTable().createTable()
def test_deleteS2(self): createTablesBig() open( pref.getNoCheck(pref.CONFIG_SCRIPT_PATH) + "sleepScript copy.sh", "a") # create file to be deleted existsBefore = path.exists( pref.getNoCheck(pref.CONFIG_SCRIPT_PATH) + "sleepScript copy.sh") err = tos.ScriptTable().delete(4) errExp = pref.getError(pref.ERROR_SUCCESS) err2, s = tos.ScriptTable.getByID(4) errExp2 = pref.getError(pref.ERROR_SQL_RETURN_MISSING_ATTR, args=("getByID", "Script", 0, 9)) existsAfter = path.exists( pref.getNoCheck(pref.CONFIG_SCRIPT_PATH) + "sleepScript copy.sh") self.assertEqual(existsBefore, True) self.assertEqual(err, errExp) self.assertEqual(err2, errExp2) self.assertEqual(existsAfter, False) self.assertEqual(s, None) open( pref.getNoCheck(pref.CONFIG_SCRIPT_PATH) + "sleepScript copy.sh", "a") # recreate deleted file
def test_deleteS_F(self): createTablesBig() err = tos.ScriptTable().delete(99) # delete non-existent script errExp = pref.getError(pref.ERROR_SQL_RETURN_MISSING_ATTR, args=("getAttrByID", "Script", 0, 9)) self.assertEqual(err, errExp)
def test_deleteTableEmptyS(self): createEmptyTables() err = tos.ScriptTable().deleteTable() errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp)
def test_createTableS(self): clearTables() err = tos.ScriptTable().createTable() errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp)
def test_getAllS(self): createTablesBig() err, s = tos.ScriptTable().getAll() errExp = pref.getError(pref.ERROR_SUCCESS) self.assertEqual(err, errExp)