def testStoredFiles(self): Environment.cleanupTestFolder() targetFolder = Environment.targetFolder() # Create some files in target folder if not os.path.exists(targetFolder): os.makedirs(targetFolder) filename = os.path.join(targetFolder, 'myName_20180304_1200.zip') filename2 = os.path.join(targetFolder, 'otherName_20180304_1200.zip') with open(filename, 'w') as f: f.write('Simple test file for BRIT') with open(filename2, 'w') as f: f.write('Simple test file for BRIT') newConfig = Configuration() newConfig.definitions.append( Definition('Name1', 'dir', Environment.examplesFolder(), './C/temp1')) newTask = Task('myName', ['Name1'], targetFolder=Environment.targetFolder()) newConfig.addTask(newTask) fileNames = [] for fName in newTask.storedFiles(): fileNames.append(fName) self.assert_(len(fileNames) == 1, "Only one file should be found") self.assert_(fileNames[0] == filename, "Wrong file found")
def test_DefinitionChanged(self): ''' I set a new definition to the dialog and check, that all GUI elements change their value accordingly''' newdefinition = Definition('new_name', 'file', 'new_from', 'new_to') self.dlg.definition = newdefinition self.dlg._definitionChanged() self.assert_(self.dlg.editName.text() == newdefinition.name, 'Name not OK') self.assert_(self.dlg.editSource.text() == newdefinition.fromPlace, 'fromPlace not OK') self.assert_(self.dlg.editTarget.text() == newdefinition.toPlace, 'toPlace not OK') self.assert_(self.dlg.cbbType.itemData(self.dlg.cbbType.currentIndex()) == 'file', 'Type not OK')
def testWriteToReadFrom(self): newConfig = Configuration() newConfig.addTask(Task('TestTest1', ['config1', 'config2'])) newConfig.addTask(Task('TestTest2', ['config1', 'config2'])) newConfig.definitions.append( Definition('name1', 'dir', 'C:/temp1', './C/temp1')) newConfig.definitions.append( Definition('name2', 'dir', 'C:/temp2', './C/temp2')) newConfig.definitions.append( Definition('name3', 'dir', 'C:/temp3', './C/temp3')) newConfig.retainStrategies.append(RetainStrategy('reatinDefault')) newConfig.backupDirectory = 'c:/temp/backup' Environment.cleanupTestFolder() Environment.setupExamplesDir() testFile = os.path.join(Environment.examplesFolder(), 'test_config.brit') newConfig.writeTo(testFile) self.assert_(os.path.isfile(testFile), 'No config file written') newConfig2 = Configuration.readFrom(testFile) self.assert_(newConfig2 <> None, 'No configuration has been read from Json') self.assert_( len(newConfig2.tasks) == 2, 'Length of list of tasks not OK') self.assert_(newConfig2.tasks[0].name == 'TestTest1', 'Name of first task not OK') self.assert_(newConfig2.tasks[0].configuration == newConfig2, 'Configuration of task not set') self.assert_( len(newConfig2.definitions) == 3, 'Length of list of Definitions not OK') self.assert_( len(newConfig2.retainStrategies) == 1, 'Length of list of RetainStrategies not OK') self.assert_(newConfig2.backupDirectory == 'c:/temp/backup', 'backupDirectory not OK') Environment.cleanupTestFolder()
def testUpdateFrom(self): oldDef = Definition('myName', 'dir', 'C:/temp', './C/temp', ['*.txt'], ['old1*', '1bad*']) newDef = Definition('myNameNew', 'file', 'C:/tempNew', './C/tempNew', ['*.tyt'], ['old2*', '2bad*'], ['wanted'], ['unwanted']) oldDef.updateFrom(newDef) self.assert_(oldDef.name == 'myNameNew', 'name not OK') self.assert_(oldDef.backupType == 'file', 'backupType not OK') self.assert_(oldDef.fromPlace == 'C:/tempNew', 'fromPlace not OK') self.assert_(oldDef.toPlace == './C/tempNew', 'toPlace not OK') self.assert_(oldDef.includeFilePattern[0] == '*.tyt', 'includeFilePattern not OK') self.assert_(oldDef.excludeFilePattern[1] == '2bad*', 'includePattern not OK') self.assert_(oldDef.includeDirPattern == ['wanted'], 'includeDirPattern not OK') self.assert_(oldDef.excludeDirPattern == ['unwanted'], 'excludeDirPattern not OK')
def testfilesSize(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() someFiles = Environment.extendExamplesDir() someFiles.append(exampleFile) myDef = Definition('Name1', 'dir', Environment.examplesFolder(), 'Test/Dir') self.assert_(myDef.filesSize() == 514, 'FilesSize not OK') Environment.cleanupTestFolder()
def testDefinitions(self): # Let's prepare a configuration newConfig = Configuration() newConfig.definitions.append( Definition('name1', 'dir', 'C:/temp1', './C/temp1')) newConfig.definitions.append( Definition('name2', 'dir', 'C:/temp2', './C/temp2')) newConfig.definitions.append( Definition('name3', 'dir', 'C:/temp3', './C/temp3')) # And now the task newTask = Task('myName', ['name1', 'name3', 'badConf']) newConfig.tasks.append(newTask) newTask.configuration = newConfig # Now the actual test defs = newTask.defintions() self.assert_(defs <> None, 'No defs returned') self.assert_(len(defs) == 2, 'Nb of found defs not OK') self.assert_(defs[0].name == 'name1', 'Name of first def not OK') self.assert_(defs[1].name == 'name3', 'Name of second def not OK')
def test_dirIsExcluded(self): myDef = Definition('myName', 'dir', 'C:/temp', './C/temp', [], [], [], []) self.assert_(not myDef._dirIsExcluded('c:/temp/anydir'), 'Dir excluded on empty exclude list') myDef.excludeDirPattern = ['*/asd*', '*/path/*'] self.assert_(myDef._dirIsExcluded('c:/temp/path/anydir'), 'Dir not excluded that is in include list') self.assert_(myDef._dirIsExcluded('c:/temp/asd_whatEverDir'), 'Dir not excluded that is in include list2') self.assert_(not myDef._dirIsExcluded('c:/temp/anydir'), 'Dir excluded when not in include list')
def test_fileIsExcluded(self): myDef = Definition('myName', 'dir', 'C:/temp', './C/temp', [], [], [], []) self.assert_(not myDef._fileIsExcluded('Test.txt'), 'File excluded on empty exclude list') myDef.excludeFilePattern = ['*Test*', '*all*'] self.assert_(myDef._fileIsExcluded('Test.txt'), 'File not excluded on matching exclude list') self.assert_(myDef._fileIsExcluded('fileOnCall.txt'), 'File not excluded on matching exclude list, item2') self.assert_(not myDef._fileIsExcluded('Bad.file'), 'File excluded on not matching exclude list')
def test_dirIsIncluded(self): myDef = Definition('myName', 'dir', 'C:/temp', './C/temp', [], [], [], []) self.assert_(myDef._dirIsIncluded('anydir'), 'Dir not included on empty include list') myDef.includeDirPattern = ['asd*', 'path'] self.assert_(myDef._dirIsIncluded('path'), 'Dir not included that is in include list') self.assert_(myDef._dirIsIncluded('asd_whatEverDir'), 'Dir not included that is in include list2') self.assert_(not myDef._dirIsIncluded('anydir'), 'Dir included when not in include list')
def testFileInfos_forFile(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() myDef = Definition('Name1', 'file', exampleFile, 'Test/example.txt') infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(len(infos) == 1, 'There should be exactly one fileInfo') self.assert_(infos[0]['source'] == exampleFile, 'Source not OK') self.assert_(infos[0]['target'] == 'Test/example.txt', 'Target not OK') Environment.cleanupTestFolder()
def testCreate(self): myDef = Definition('myName', 'dir', 'C:/temp', './C/temp', ['*.txt'], ['old*', 'bad*'], ['wanted_dir*'], ['bad_dir*']) self.assert_(myDef <> None, 'No definition created') self.assert_(myDef.name == 'myName', 'name not OK') self.assert_(myDef.backupType == 'dir', 'backupType not OK') self.assert_(myDef.fromPlace == 'C:/temp', 'fromPlace not OK') self.assert_(myDef.toPlace == './C/temp', 'toPlace not OK') self.assert_(myDef.includeFilePattern[0] == '*.txt', 'includeFilePattern not OK') self.assert_(myDef.excludeFilePattern[1] == 'bad*', 'excludeFilePattern not OK') self.assert_(myDef.includeDirPattern[0] == 'wanted_dir*', 'includeDirPattern not OK') self.assert_(myDef.excludeDirPattern[0] == 'bad_dir*', 'excludeDirPattern not OK')
def testCopy(self): oldDef = Definition('myName', 'dir', 'C:/temp', './C/temp', ['*.txt'], ['old*', 'bad*'], [], ['unwanted']) newDef = oldDef.copy() self.assert_(newDef <> None, 'No definition created') self.assert_(newDef.name == 'myName', 'name not OK') self.assert_(newDef.backupType == 'dir', 'backupType not OK') self.assert_(newDef.fromPlace == 'C:/temp', 'fromPlace not OK') self.assert_(newDef.toPlace == './C/temp', 'toPlace not OK') self.assert_(newDef.includeFilePattern[0] == '*.txt', 'includeFilePattern not OK') self.assert_(newDef.excludeFilePattern[1] == 'bad*', 'excludeFilePattern not OK') self.assert_(newDef.includeDirPattern == [], 'includeDirPattern not OK') self.assert_(newDef.excludeDirPattern[0] == 'unwanted', 'excludeDirPattern not OK')
def testToJson(self): myDef = Definition('myName', 'dir', 'C:/temp', './C/temp', ['*.txt'], ['old*', 'bad*'], ['wanted'], ['unwanted']) json = myDef.toJson() self.assert_(json <> None, 'No json created') self.assert_(json['class_name'] == 'Definition', 'class_name not OK') self.assert_(json['name'] == 'myName', 'name not OK') self.assert_(json['backupType'] == 'dir', 'backupType not OK') self.assert_(json['fromPlace'] == 'C:/temp', 'fromPlace not OK') self.assert_(json['toPlace'] == './C/temp', 'toPlace not OK') self.assert_(json['includeFilePattern'] == ['*.txt'], 'includeFilePattern not OK') self.assert_(json['excludeFilePattern'] == ['old*', 'bad*'], 'excludeFilePattern not OK') self.assert_(json['includeDirPattern'] == ['wanted'], 'includeDirPattern not OK') self.assert_(json['excludeDirPattern'] == ['unwanted'], 'excludeDirPattern not OK')
def testRun(self): Environment.cleanupTestFolder() Environment.setupExamplesDir() newConfig = Configuration() newConfig.definitions.append( Definition('Name1', 'dir', Environment.examplesFolder(), './C/temp1')) newTask = Task('myName', ['Name1'], targetFolder=Environment.targetFolder()) newConfig.addTask(newTask) filename = newTask.run() self.assert_(os.path.exists(Environment.targetFolder()), 'Target folder not created') self.assert_(filename <> None and filename <> '', 'No file name returned on run') Environment.cleanupTestFolder()
def test_doArchiveFileFail(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test # I open and lock the example file to provoke an I/O error file_ = open(filename, 'a') if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_LOCK, 0x7fffffff) with LogCapture(level=logging.WARNING) as l: task._doArchiveFile(archiveFile, filename, zipTargetFile) l.check(( 'root', 'WARNING', 'File could not be read: C:\\ProgramData\\brit_test\\examples\\example.txt Reason: I/O error(13): Permission denied' )) archiveFile.close() # Unlock and close the file if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_UNLCK, 0x7fffffff) file_.close() self.assert_(not self.fileIsZipped(zipTargetFile, targetfile), 'File placed in zip file even though it was locked') Environment.cleanupTestFolder()
def testfileInfos(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() myDef = Definition('Name1', 'dir', Environment.examplesFolder(), 'Test/Dir') infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(infos <> None, 'No fileInfos created') self.assert_( infos[0]['source'] == os.path.join(Environment.examplesFolder(), 'example.txt'), 'Example source file not OK') self.assert_( infos[0]['target'] == os.path.join('Test/Dir', 'examples', 'example.txt'), 'Example source file not OK') someFiles = Environment.extendExamplesDir() someFiles.append(exampleFile) infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(len(infos) == len(someFiles), 'Not all files found') myDef.includeDirPattern = ['subfolder'] infos = [] for info in myDef.fileInfos(): infos.append(info) # Note that I get all files of the base dir, independent if that matches included dirrs! self.assert_(len(infos) == 4, 'Nb of files not OK for include dirs') Environment.cleanupTestFolder()
def test_doArchiveFile(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test task._doArchiveFile(archiveFile, filename, zipTargetFile) archiveFile.close() self.assert_(self.fileIsZipped(zipTargetFile, targetfile), 'File not placed in zip file') Environment.cleanupTestFolder()
def setUp(self): self.app = QtGui.QApplication([]) self.definition = Definition('name', 'dir', 'from', 'to') self.dlg = DefinitionDialog(None, self.definition) self.dlg.show()