def test_Save_LocationIsNotString(self, notStringLocation): trainingLog = Logger(False) trainingLog._content = "Ford Sierra II 2.0 DOHC 125KM" trainingLog.Save(notStringLocation) pathToLogFile = os.path.join(str(notStringLocation), "%s.log" % trainingLog._fileName) doesLogFileExist = os.path.exists(pathToLogFile) self.assertFalse(doesLogFileExist)
def test_Save_LocationDoesNotExist(self): nonExistentLocation = "TEST_NON_EXISTENT_LOCATION" self.assertFalse(os.path.exists(nonExistentLocation)) trainingLog = Logger(False) trainingLog._content = "Ford Sierra II 2.0 DOHC 125KM" trainingLog.Save(nonExistentLocation) pathToLogFile = os.path.join(nonExistentLocation, "%s.log" % trainingLog._fileName) doesLogFileExist = os.path.exists(pathToLogFile) self.assertFalse(doesLogFileExist)
def test_Append_InvalidParameters(self, isLogVerbose, invalidInfoParameter): trainingLog = Logger(isLogVerbose) logContentBeforeCall = "Ford Sierra II 2.0 DOHC 125KM\n" trainingLog._content = logContentBeforeCall with patch('sys.stdout', new=StringIO()) as fakeOutput: trainingLog.Append(invalidInfoParameter) expectedPrintMessage = "" actualPrintMessage = fakeOutput.getvalue() self.assertEqual(actualPrintMessage, expectedPrintMessage) logContentAfterCall = trainingLog._content self.assertEqual(logContentAfterCall, logContentBeforeCall)
def test_Save_LocationIsNotDir(self): nonDirectoryLocation = "TEST_NON_DIRECTORY_LOCATION.log" with open(nonDirectoryLocation, "w") as tempFile: tempFile.write("kanapka") self.assertTrue(os.path.exists(nonDirectoryLocation)) self.assertFalse(os.path.isdir(nonDirectoryLocation)) trainingLog = Logger(False) trainingLog._content = "Ford Sierra II 2.0 DOHC 125KM" trainingLog.Save(nonDirectoryLocation) pathToLogFile = os.path.os.path.join(nonDirectoryLocation, "%s.log" % trainingLog._fileName) doesLogFileExist = os.path.exists(pathToLogFile) self.assertFalse(doesLogFileExist) os.remove(nonDirectoryLocation) self.assertFalse(os.path.exists(nonDirectoryLocation))
def test_Append(self, isLogVerbose, infoToAppend, expectedPrintMessage, mock_datetime): mock_datetime.now.return_value = datetime(1995, 7, 4, 17, 15, 0) trainingLog = Logger(isLogVerbose) logContentBeforeCall = "Ford Sierra II 2.0 DOHC 125KM\n" trainingLog._content = logContentBeforeCall with patch('sys.stdout', new=StringIO()) as fakeOutput: trainingLog.Append(infoToAppend) actualPrintMessage = fakeOutput.getvalue() self.assertEqual(actualPrintMessage, expectedPrintMessage) expectedLogContentAfterCall = \ logContentBeforeCall + "[ 1995-07-04 17:15:00 ] " + \ infoToAppend + "\n" actualLogContentAfterCall = trainingLog._content self.assertEqual(actualLogContentAfterCall, expectedLogContentAfterCall)
def test_Save_LogContentIsEmptyOrOnlyWhitespaces(self, logContent): testLocation = "TEST_LOCATION_FOR_LOG_FILE" if os.path.isdir(testLocation): rmtree(testLocation) os.mkdir(testLocation) self.assertTrue(os.path.isdir(testLocation)) trainingLog = Logger(False) trainingLog._content = logContent trainingLog.Save(testLocation) pathToLogFile = os.path.join(testLocation, "{0}.log".format(trainingLog._fileName)) self.assertFalse(os.path.exists(pathToLogFile)) rmtree(testLocation) self.assertFalse(os.path.isdir(testLocation))
def test_Save_OK(self): testLocation = "TEST_LOCATION_FOR_LOG_FILE" if os.path.isdir(testLocation): rmtree(testLocation) os.mkdir(testLocation) self.assertTrue(os.path.isdir(testLocation)) trainingLog = Logger(False) trainingLog._content = "Ford Sierra II 2.0 DOHC 125KM" trainingLog.Save(testLocation) pathToLogFile = os.path.join(testLocation, "{0}.log".format(trainingLog._fileName)) self.assertTrue(os.path.isfile(pathToLogFile)) with open(pathToLogFile, "r") as logFile: expectedLogFileContent = trainingLog._content actualLogFileContent = logFile.read() self.assertEqual(actualLogFileContent, expectedLogFileContent) rmtree(testLocation) self.assertFalse(os.path.isdir(testLocation))