def initDatabase(basedir, config=None): """ Function for initializing the databse when running Mimir3 with the MTF frontend. Args: basedir (str) : Base direcotry for the databse run in the forntend config (str) : Only necessary if a new database should be initialized Raises: RuntimeError : Raised if no .mimir dir is found in basedir and config is None Returns: databse (DataBase) : Initialized/loaded database initType (str) : Either "new" or "loaded" """ if os.path.exists(basedir + "/.mimir"): logger.debug("Found .mimir dir for %s", basedir) database = DataBase(basedir, "load") initType = "loaded" else: if config is None: raise RuntimeError( "Found no .mimir in %s. Please pass initial config" % basedir) else: logger.debug("Creating new databse") database = DataBase(basedir, "new", config) initType = "new" return database, initType
def test_07_DB_load(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) database.saveMain() loadedDB = DataBase(dbRootPath, "load") assert database == loadedDB assert loadedDB.maxID == len(loadedDB.entries)-1 #Since 0 is a valid ID del database
def test_05_DB_equal(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database1 = DataBase(dbRootPath, "new", config) if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database2 = DataBase(dbRootPath, "new", config) assert database1 == database2 del database1, database2
def test_06_DB_notequal(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database1 = DataBase(dbRootPath, "new", config) os.system("rm "+dir2tests+"/testStructure/newfile.mp4") if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database2 = DataBase(dbRootPath, "new", config) os.system("touch "+dir2tests+"/testStructure/newfile.mp4") database2.findNewFiles() os.system("rm "+dir2tests+"/testStructure/newfile.mp4") assert database1 != database2 del database1, database2
def test_11_DB_removeEntry(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) #Remove by ID databaseID = copy.deepcopy(database) id2remove = 2 entry2Remove = databaseID.getEntryByItemName("ID",str(id2remove))[0] databaseID.remove(id2remove, byID = True) assert not entry2Remove in databaseID.entries #Remove by Name databaseName = copy.deepcopy(database) name2remove = "folder2file1" entry2Remove = databaseName.getEntryByItemName("Name",name2remove)[0] databaseName.remove(name2remove, byName = True) assert not entry2Remove in databaseName.entries #Remove by Path databasePath = copy.deepcopy(database) file2remove = "folder2/folder2file1.mp4" path2remove = dbRootPath+"/"+file2remove entry2Remove = databasePath.getEntryByItemName("Path",file2remove)[0] databasePath.remove(file2remove, byPath = True) assert not entry2Remove in databasePath.entries del database
def test_13_p1_DB_query(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) updatedEntry1 = database.getEntryByItemName("ID", "0")[0] updatedEntry2 = database.getEntryByItemName("ID", "1")[0] updatedEntry1.changeItemValue("SingleItem", "ReplacedValue") updatedEntry1.addItemValue("ListItem", "AddedValue") updatedEntry2.changeItemValue("SingleItem", "ReplacedValue") ######################################################## #First names wrong with pytest.raises(KeyError): database.query(["Blubb", "SingleItem"], "SomeQuery") #Second names wrong with pytest.raises(KeyError): database.query(["SingleItem", "Blubb"], "SomeQuery") ######################################################## resultEntry = database.query(["SingleItem","ListItem"], ["ReplacedValue"]) resultID = database.query(["SingleItem","ListItem"], ["ReplacedValue"], returnIDs = True) found1, found2 = False, False if updatedEntry1 in resultEntry: found1 = True if updatedEntry2 in resultEntry: found2 = True foundEntry = found1 and found2 assert resultID == ["0", "1"] resultID = database.query(["SingleItem","ListItem"], ["AddedValue", "ReplacedValue"], returnIDs = True) assert resultID == ["0"] del database
def test_10_DB_removeEntry_exceptions(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) ############################################## #Raise exception for not specified vector # No vector specified with pytest.raises(RuntimeError): database.remove(1) # More than one vector specified with pytest.raises(RuntimeError): database.remove(1, byID = True, byName = True) ############################################## #Raise exception type # ID with pytest.raises(TypeError): database.remove([], byID = True) with pytest.raises(TypeError): database.remove(1, byID = 1) # Name/Path with pytest.raises(TypeError): database.remove(1, byName = True) ############################################## #Raise exception by ID: out of range with pytest.raises(IndexError): database.remove(1000, byID = True) ############################################## #Raise exception by Name/Path: not in DB with pytest.raises(KeyError): database.remove("RandomName", byName = True) with pytest.raises(KeyError): database.remove("RandomPath", byPath = True) del database
def test_03_DB_raise_RuntimeError_existing_mimirDir(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if not os.path.exists(dbRootPath+"/.mimir"): os.makedirs(dbRootPath+"/.mimir") with pytest.raises(RuntimeError): database = DataBase(dbRootPath, "new", config) del database
def test_17_DB_random_all(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) allIDs = database.getAllValuebyItemName("ID") randID = database.getRandomEntryAll() assert randID in allIDs
def test_18_DB_random_weighted(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) allIDs = database.getAllValuebyItemName("ID") with pytest.raises(NotImplementedError): randID = database.getRandomEntry(chooseFrom = allIDs, weighted = True)
def test_08_DB_getAllValues(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) with pytest.raises(KeyError): database.getAllValuebyItemName("Blubb") values = database.getAllValuebyItemName("Path") filesindbRoot = glob(dbRootPath+"/**/*.mp4", recursive = True) filesindbRoot = [x.replace(dbRootPath+"/", "") for x in filesindbRoot] assert values == set(filesindbRoot) del database
def test_04_DB_save(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) #Check database is save assert database.saveMain() assert database.saveMain() #shutil.copytree(dbRootPath+"/.mimir", dbRootPath+"/.mimir2") #assert validateDatabaseJSON(database, config, database.savepath) #check if backup was created day, month, year = datetime.date.today().day, datetime.date.today().month, datetime.date.today().year fulldate = "{2:02}-{1:02}-{0:02}".format(day, month, year-2000) assert os.path.exists(dbRootPath+"/.mimir/mainDB.{0}.backup".format(fulldate)) == True del database
def test_02_DB_init_new(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) print(database.model.listitems) filesindbRoot = glob(dbRootPath+"/**/*.mp4", recursive = True) filesindbRoot = [x.replace(dbRootPath+"/", "") for x in filesindbRoot] allEntriesSaved = True for entry in database.entries: if entry.Path not in filesindbRoot: allEntriesSaved = False assert allEntriesSaved for item in database.model.allItems: assert not database.cachedValuesChanged[item] del database
def test_09_DB_getEntrybyItemName(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) with pytest.raises(KeyError): database.getEntryByItemName("Blubb", "folder2file") found = False for entry in database.entries: print(entry.getItem("Name").value) if entry.getItem("Name").value == "folder2file1": found = True break assert found entrybyItemName = database.getEntryByItemName("Name", "folder2file1") assert entry in entrybyItemName del database
def test_15_DB_status(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) #DB not saved assert not database.getStatus() #DB saved database.saveMain() assert database.getStatus() #DB changed - new File os.system("touch "+dir2tests+"/testStructure/newfile.mp4") newFiles = database.findNewFiles() os.system("rm "+dir2tests+"/testStructure/newfile.mp4") assert not database.getStatus() database.saveMain() assert database.getStatus()
def test_12_DB_findNewFiles_append(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) lastIDbeforeAppend = database.maxID os.system("touch "+dir2tests+"/testStructure/newfile.mp4") newFiles, pairs = database.findNewFiles() os.system("rm "+dir2tests+"/testStructure/newfile.mp4") assert "newfile.mp4" in newFiles assert len(newFiles) == 1 asEntry = False for entry in database.entries: if entry.Path == "newfile.mp4": asEntry = True newEntry = entry break assert asEntry assert int(newEntry.ID) == lastIDbeforeAppend+1 assert database.maxID == lastIDbeforeAppend+1 del database
def preCreatedDB(): os.system("touch "+dir2tests+"/testStructure/rootFile1") os.system("touch "+dir2tests+"/testStructure/folder2/folder2file1.mp4") os.system("touch "+dir2tests+"/testStructure/folder2/folder2file2.mp4") os.system("touch "+dir2tests+"/testStructure/folder2/folder3/folder3file1.mp4") os.system("touch "+dir2tests+"/testStructure/folder1/folder1file1.mp4") os.system("touch "+dir2tests+"/testStructure/folder1/folder1file2.mp4") config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) ## Set Ratings for furure tests # Expected Order: ["3", "2", "4", "1", "5", "0"] database.modifySingleEntry("1", "Rating", "2", byID = True ) database.modifySingleEntry("2", "Rating", "4", byID = True ) database.modifySingleEntry("3", "Rating", "5", byID = True ) database.modifySingleEntry("4", "Rating", "3", byID = True ) database.modifySingleEntry("5", "Rating", "1", byID = True ) # Expected Order: ["5", "4", "3", "2", "1", "0"] database.modifySingleEntry("0", "SingleItem", "Xi", byID = True ) database.modifySingleEntry("1", "SingleItem", "Tau", byID = True ) database.modifySingleEntry("2", "SingleItem", "Ny", byID = True ) database.modifySingleEntry("3", "SingleItem", "Eta", byID = True ) database.modifySingleEntry("4", "SingleItem", "Bea", byID = True ) database.modifySingleEntry("5", "SingleItem", "Alpha", byID = True ) database.modifyListEntry("0", "ListItem", "Blue", byID = True) database.modifyListEntry("0", "ListItem", "Double Orange", byID = True) database.modifyListEntry("0", "ListItem", "Triple Orange", byID = True) database.modifyListEntry("3", "ListItem", "Lavender", byID = True) database.modifyListEntry("4", "ListItem", "Lavender", byID = True) database.modifyListEntry("4", "ListItem", "Pinkish", byID = True) database.modifyListEntry("4", "ListItem", "Spring", byID = True) Entry0 = database.getEntryByItemName("ID", "0")[0] Entry1 = database.getEntryByItemName("ID", "1")[0] Entry2 = database.getEntryByItemName("ID", "2")[0] Entry3 = database.getEntryByItemName("ID", "3")[0] Entry4 = database.getEntryByItemName("ID", "4")[0] Entry5 = database.getEntryByItemName("ID", "5")[0] # Expected Order: ["0", "2", "3", "5", "1", "4"] Entry0.changeItemValue("Added", "30.01.19|00:00:00") Entry1.changeItemValue("Added", "20.01.19|00:00:00") Entry2.changeItemValue("Added", "29.01.19|00:00:00") Entry3.changeItemValue("Added", "29.01.19|00:00:00")# Same time: Fall back to ID Entry4.changeItemValue("Added", "15.01.19|00:00:00") Entry5.changeItemValue("Added", "26.01.19|00:00:00") # Expected Order: ["0", "3", "4", "5", "1", "2"] Entry0.replaceItemValue("Changed", "24.02.19|00:00:00", Entry0.getItem("Changed").value[0]) Entry1.replaceItemValue("Changed", "10.02.19|00:00:00", Entry1.getItem("Changed").value[0]) Entry2.replaceItemValue("Changed", "23.02.19|00:00:00", Entry2.getItem("Changed").value[0]) Entry3.replaceItemValue("Changed", "22.02.19|00:00:00", Entry3.getItem("Changed").value[0]) Entry4.replaceItemValue("Changed", "21.02.19|00:00:00", Entry4.getItem("Changed").value[0]) Entry5.replaceItemValue("Changed", "20.02.19|00:00:00", Entry5.getItem("Changed").value[0]) Entry0.addItemValue("Changed", "25.03.19|00:00:00") Entry1.addItemValue("Changed", "19.03.19|00:00:00") Entry2.addItemValue("Changed", "23.01.19|00:00:00") Entry3.addItemValue("Changed", "22.03.19|00:00:00") Entry4.addItemValue("Changed", "21.03.19|00:00:00") Entry5.addItemValue("Changed", "20.03.19|00:00:00") database.saveMain() for item in database.model.allItems: database.cacheAllValuebyItemName(item) #shutil.copytree(dbRootPath+"/.mimir", dbRootPath+"/.mimir2") #For testing shutil.rmtree(dbRootPath+"/.mimir") return database
def test_14_DB_modifyEntry(): config = mimir_dir+"/conf/modeltest.json" dbRootPath = dir2tests+"/testStructure" if os.path.exists(dbRootPath+"/.mimir"): shutil.rmtree(dbRootPath+"/.mimir") database = DataBase(dbRootPath, "new", config) thisDate, thisTime = getDataTime() #--------------------- SingleItem ------------------------- #Replace single Item value database.modifySingleEntry("1", "SingleItem", "changedItemValue", byID = True ) changedEntry = database.getEntryByItemName("ID", "1")[0] assert "changedItemValue" in changedEntry.getAllValuesbyName("SingleItem") change_datetime = changedEntry.getAllValuesbyName("Changed") change_datetime = list(change_datetime)[0] assert change_datetime != "emptyChanged" date, time = change_datetime.split("|") assert date == thisDate assert time[0:1] == thisTime[0:1] #Check if Item is present in database with pytest.raises(KeyError): database.modifySingleEntry("1", "BLubbb", "changedItemValue", byID = True ) with pytest.raises(TypeError): database.modifySingleEntry("1", "ListItem", "changedItemValue", byID = True ) #---------------------- ListItem -------------------------- with pytest.raises(TypeError): database.modifyListEntry("1", "SingleItem", "appendedItemValue", "Append", byID = True) #Append but first default schould be remove when appending the fist actual value origEntry = database.getEntryByItemName("ID", "1")[0] database.modifyListEntry("1", "ListItem", "initialValue", "Append", byID = True) changedEntry = database.getEntryByItemName("ID", "1")[0] #print(database.model.getDefaultValue("ListItem")) assert ("initialValue" in changedEntry.getAllValuesbyName("ListItem") and database.model.getDefaultValue("ListItem") not in changedEntry.getAllValuesbyName("ListItem") and len(changedEntry.getAllValuesbyName("ListItem")) == 1) #Append change_datetime = changedEntry.getAllValuesbyName("Changed") change_datetime = list(change_datetime)[0] assert change_datetime != "emptyChanged" date, time = change_datetime.split("|") assert date == thisDate assert time[0:1] == thisTime[0:1] print("-------- Append ----------") origEntry = database.getEntryByItemName("ID", "1")[0] databaseAppend = copy.deepcopy(database) databaseAppend.modifyListEntry("1", "ListItem", "appendedItemValue", "Append", byID = True) changedEntry = databaseAppend.getEntryByItemName("ID", "1")[0] assert ( "appendedItemValue" in changedEntry.getAllValuesbyName("ListItem") and origEntry.getAllValuesbyName("ListItem").issubset(changedEntry.getAllValuesbyName("ListItem")) ) #Replace print("-------- Replace ----------") databaseReplace = copy.deepcopy(databaseAppend) databaseReplace.modifyListEntry("1", "ListItem", "replacedItemValue", "Replace", "initialValue", byID = True) changedEntry = databaseReplace.getEntryByItemName("ID", "1")[0] assert ("replacedItemValue" in changedEntry.getAllValuesbyName("ListItem") and "initialValue" not in changedEntry.getAllValuesbyName("ListItem")) #Remove print("-------- Remove I ----------") databaseAppend.modifyListEntry("1", "ListItem", None, "Remove", "appendedItemValue", byID = True) changedEntry = databaseAppend.getEntryByItemName("ID", "1")[0] assert "appendedItemValue" not in changedEntry.getAllValuesbyName("ListItem") #Remove empty entry print("-------- Remove II ----------") databaseReplace.modifyListEntry("1", "ListItem", None, "Remove", "appendedItemValue", byID = True) databaseReplace.modifyListEntry("1", "ListItem", None, "Remove", "replacedItemValue", byID = True) changedEntry = databaseReplace.getEntryByItemName("ID", "1")[0] assert (set(databaseReplace.model.listitems["ListItem"]["default"]) == changedEntry.getAllValuesbyName("ListItem")) print("-------- Change date for ListItem ----------") database.modifyListEntry("2", "ListItem", "initialValue", "Append", byID = True) changedEntry = database.getEntryByItemName("ID", "2")[0] change_datetime = changedEntry.getAllValuesbyName("Changed") change_datetime = list(change_datetime)[0] assert change_datetime != "emptyChanged" date, time = change_datetime.split("|") assert date == thisDate assert time[0:1] == thisTime[0:1]
def preCreatedDB(): config = mimir_dir + "/conf/modeltest.json" dbRootPath = dir2tests + "/testStructure" if os.path.exists(dbRootPath + "/.mimir"): shutil.rmtree(dbRootPath + "/.mimir") database = DataBase(dbRootPath, "new", config) shutil.copy2(mimir_dir + "/conf/MTF_modeltest.json", dbRootPath + "/.mimir/MTF_model.json") jsonConf = None with open(dbRootPath + "/.mimir/MTF_model.json", "r") as f: jsonConf = json.load(f) jsonConf["General"]["DisplayItems"] = [ "ID", "Name", "SingleItem", "ListItem", "Rating", "Opened", "timesOpened" ] with open(dbRootPath + "/.mimir/MTF_model.json", 'w') as o: json.dump(jsonConf, o, sort_keys=True, indent=4, separators=(',', ': ')) ## Set Ratings for furure tests # Expected Order: ["3", "2", "4", "1", "5", "0"] database.modifySingleEntry("1", "Rating", "2", byID=True) database.modifySingleEntry("2", "Rating", "4", byID=True) database.modifySingleEntry("3", "Rating", "5", byID=True) database.modifySingleEntry("4", "Rating", "3", byID=True) database.modifySingleEntry("5", "Rating", "1", byID=True) # Expected Order: ["5", "4", "3", "2", "1", "0"] database.modifySingleEntry("0", "SingleItem", "Xi", byID=True) database.modifySingleEntry("1", "SingleItem", "Tau", byID=True) database.modifySingleEntry("2", "SingleItem", "Ny", byID=True) database.modifySingleEntry("3", "SingleItem", "Eta", byID=True) database.modifySingleEntry("4", "SingleItem", "Bea", byID=True) database.modifySingleEntry("5", "SingleItem", "Alpha", byID=True) database.modifyListEntry("0", "ListItem", "Blue", byID=True) database.modifyListEntry("0", "ListItem", "Double Orange", byID=True) database.modifyListEntry("0", "ListItem", "Triple Orange", byID=True) database.modifyListEntry("1", "ListItem", "Blue", byID=True) database.modifyListEntry("1", "ListItem", "Red", byID=True) database.modifyListEntry("1", "ListItem", "Orange", byID=True) database.modifyListEntry("1", "ListItem", "Magenta", byID=True) Entry0 = database.getEntryByItemName("ID", "0")[0] Entry1 = database.getEntryByItemName("ID", "1")[0] Entry2 = database.getEntryByItemName("ID", "2")[0] Entry3 = database.getEntryByItemName("ID", "3")[0] Entry4 = database.getEntryByItemName("ID", "4")[0] Entry5 = database.getEntryByItemName("ID", "5")[0] # Expected Order: ["0", "2", "3", "5", "1", "4"] Entry0.changeItemValue("Added", "30.01.19|00:00:00") Entry1.changeItemValue("Added", "20.01.19|00:00:00") Entry2.changeItemValue("Added", "29.01.19|00:00:00") Entry3.changeItemValue("Added", "29.01.19|00:00:00") # Same time: Fall back to ID Entry4.changeItemValue("Added", "15.01.19|00:00:00") Entry5.changeItemValue("Added", "26.01.19|00:00:00") # Expected Order: ["0", "3", "4", "5", "1", "2"] Entry0.replaceItemValue("Changed", "24.02.19|00:00:00", Entry0.getItem("Changed").value[0]) Entry1.replaceItemValue("Changed", "10.02.19|00:00:00", Entry1.getItem("Changed").value[0]) Entry2.replaceItemValue("Changed", "23.02.19|00:00:00", Entry2.getItem("Changed").value[0]) Entry3.replaceItemValue("Changed", "22.02.19|00:00:00", Entry3.getItem("Changed").value[0]) Entry4.replaceItemValue("Changed", "21.02.19|00:00:00", Entry4.getItem("Changed").value[0]) Entry5.replaceItemValue("Changed", "20.02.19|00:00:00", Entry5.getItem("Changed").value[0]) Entry0.addItemValue("Changed", "25.03.19|01:00:00") Entry1.addItemValue("Changed", "19.03.19|01:00:00") Entry2.addItemValue("Changed", "23.01.19|01:00:00") Entry3.addItemValue("Changed", "22.03.19|01:00:00") Entry4.addItemValue("Changed", "21.03.19|01:00:00") Entry5.addItemValue("Changed", "20.03.19|01:00:00") #### Entry0.replaceItemValue("Opened", "27.02.19|00:00:00", Entry0.getItem("Opened").value[0]) database.saveMain() #shutil.copytree(dbRootPath+"/.mimir", dbRootPath+"/.mimir2") #For testing yield database
def test_06_plugin_newEntry_wPlugin(mocker): fileSize = 126730706 fileSizeGB = "{0:.2f}".format(fileSize / 1e9) height = "100" width = "200" duration = "01:02:03.4444" durationExp = "01:02:03" def returnMeta(SomeInput): return {"width": width, "height": height, "duration": duration} mocker.patch("hachoir.metadata.extractMetadata", new=returnMeta) mocker.patch("hachoir.parser.createParser", new=returnMeta) mocker.patch.object(os.path, "getsize", return_value=fileSize) mocker.spy(mimir.backend.plugin, "getPluginValues") ######################################################################## # CREATE A DATABASE config = mimir_dir + "/conf/modeltest.json" with open(config) as f: modelDict = json.load(f) modelDict["Width"] = { "Type": "Item", "default": "-1", "hide": "", "itemType": "str", "plugin": "VideoMetaData:width" } modelDict["Height"] = { "Type": "Item", "default": "-1", "hide": "", "itemType": "str", "plugin": "VideoMetaData:height" } modelDict["Duration"] = { "Type": "Item", "default": "-1", "hide": "", "itemType": "str", "plugin": "VideoMetaData:duration" } modelDict["Size"] = { "Type": "Item", "default": "-1", "hide": "", "itemType": "str", "plugin": "osData:size" } with open("conf/plugin_modeltest.json", 'w') as outfile: json.dump(modelDict, outfile, sort_keys=True, indent=4, separators=(',', ': ')) pluginConf = mimir_dir + "/conf/plugin_modeltest.json" dbRootPath = dir2tests + "/testStructure" if os.path.exists(dbRootPath + "/.mimir"): shutil.rmtree(dbRootPath + "/.mimir") database = DataBase(dbRootPath, "new", pluginConf) database.saveMain() #shutil.copytree(dbRootPath+"/.mimir", dbRootPath+"/.mimir2") #For testing shutil.rmtree(dbRootPath + "/.mimir") ######################################################################## aEntry = database.getEntryByItemName("ID", "0")[0] metadata_height = list(aEntry.getAllValuesbyName("Height"))[0] metadata_width = list(aEntry.getAllValuesbyName("Width"))[0] metadata_duration = list(aEntry.getAllValuesbyName("Duration"))[0] osData_size = list(aEntry.getAllValuesbyName("Size"))[0] assert metadata_height == height assert metadata_width == width assert metadata_duration == durationExp assert osData_size == fileSizeGB