def test_diff_records(filetree): filetree.dir("source_data") \ .file("junkA", deferred_content=["different things"]) \ .file("junkB", deferred_content=["different things B"]) \ .file("junkC") \ .build() mr = MetaRecord.create_new(filetree.path) rec = Record(filetree.path, "Rec A", "Rec A Data") rec.add_file(filetree.relpath("source_data/junkA"), filetree.relpath("source_data/junkA")) rec.add_file(filetree.relpath("source_data/junkB"), filetree.relpath("source_data/junkB")) rec.add_file(filetree.relpath("source_data/junkC"), filetree.relpath("source_data/junkC")) rec.save(mr) mr.save() filetree.root().do_deferred() rec2 = Record(filetree.path, "Rec A - 2", "Rec A Data") rec2.add_file(filetree.relpath("source_data/junkA"), filetree.relpath("source_data/junkA")) rec2.add_file(filetree.relpath("source_data/junkB"), filetree.relpath("source_data/junkB")) rec2.add_file(filetree.relpath("source_data/junkC"), filetree.relpath("source_data/junkC")) rec2.save(mr) mr.save() add, chg, rm, uchg = rec.file_diff(rec2) assert len(add) == 0 assert len(chg) == 2 assert len(rm) == 0 assert len(uchg) == 1 assert chg[0][1] == rec2.files[0] assert chg[1][1] == rec2.files[1]
def test_gen_records(filetree): dirs = ["Dir A", "Dir B", "Dir C"] for d in dirs: filetree \ .dir(d) \ .file(f"testfile-{randint(1, 100)}.txt") \ .file(f"testfile-{randint(1, 100)}.txt") filetree.build() log.info(f"Created:\t{dirs}") mr = MetaRecord.create_new(filetree.path) mr.save() rb = Rebuilder(mr) for i, r in enumerate(rb.records()): ts = r.timestamp + datetime.timedelta(seconds=i * 10) date_safe = ts.strftime('%Y-%m-%d_%H-%M-%S') rb.configure_directory(r.folder, timestamp=ts, name=f"Backup for {date_safe}") rb.generate_records() rb.wait_for_completion() for r in rb.records(): path = f"records/{r.name}{record_ext}" filetree.exists(path) loaded_rec = Record.load_from(filetree.path, r.name) assert loaded_rec == r
def test_load_record(filetree): filetree.dir("source_data").file("junkA").file("junkB").build() mr = MetaRecord.create_new(filetree.path) rec = Record(filetree.path, "Rec A", "Rec A Data") rec.add_file(filetree.relpath("source_data/junkA"), filetree.relpath("source_data/junkA")) rec.add_file(filetree.relpath("source_data/junkB"), filetree.relpath("source_data/junkB")) rec.save(mr) mr.save() newrec = Record.load_from(rec.path, rec.name) assert rec == newrec
def test_load_metarecord(filetree): mr = MetaRecord.create_new(filetree.path) rec = Record(filetree.path, "Rec A", "Rec A Data") rec.save(mr) mr.save() newmr = MetaRecord.load_from(filetree.path) assert mr.path == newmr.path assert len(mr.records) == len(newmr.records) for i, r in enumerate(mr.records): assert r.name == newmr.records[i].name assert r.timestamp == newmr.records[i].timestamp
def test_create_record(filetree): mr = MetaRecord.create_new(filetree.path) new_rec_folder = "Rec A Data" new_rec_name = "Rec A" new_rec_full_path = os.path.join(record_folder, f"{new_rec_name}{record_ext}") rec = Record(filetree.path, new_rec_name, new_rec_folder) rec.save(mr) mr.save() assert filetree.exists(new_rec_full_path) assert len(filetree.content(new_rec_full_path)) > 0 assert filetree.exists(mr_path) assert len(filetree.content(mr_path)) > 0
def choose_folder(self): dest = filedialog.askdirectory(title="Choose Destination") if MetaRecord.is_managed(dest): self.model.var_metarecord.set(MetaRecord.load_from(dest)) elif dest is not None: res = askyesno( "Folder Manager - Choose Folder", "This folder is not managed yet.\nWould you like to manage this folder?" ) if res: mr = MetaRecord.create_new(dest) mr.save() self.model.var_metarecord.set(mr) else: self.model.var_metarecord.set(None)
def test_discover_dirs(filetree): dirs = ["Dir A", "Dir B", "Dir C"] for d in dirs: filetree.dir(d) filetree.build() log.info(f"Created:\t{dirs}") mr = MetaRecord.create_new(filetree.path) mr.save() rb = Rebuilder(mr) rbdirs = list(map(lambda x: x.folder, rb.records())) log.info(f"Found: \t{rbdirs}") for d in rbdirs: assert d in dirs dirs.remove(d) assert len(dirs) == 0
def test_delete(filetree): filetree \ .dir("rec_a_data") \ .dir("Dir A") \ .file("tfa_1.txt", value="testfile_data_1") \ .file("tfa_2.txt", value="testfile_data_1") \ .up() \ .dir("Dir B") \ .file("tfb_1.txt", value="testfile_data_2") \ .root() \ .dir("rec_b_data") \ .dir("Dir A") \ .file("tfa_1.txt", value="testfile_data_1_chg") \ .file("tfa_2.txt", value="testfile_data_1") \ .up() \ .dir("Dir B") \ .file("tfb_1.txt", value="testfile_data_2") \ .build() mr = MetaRecord.create_new(filetree.path) recA = Record(filetree.path, "Rec A", "rec_a_data") recA.add_file(filetree.relpath("rec_a_data/Dir A/tfa_1.txt"), "Dir A/tfa_1.txt") recA.add_file(filetree.relpath("rec_a_data/Dir A/tfa_2.txt"), "Dir A/tfa_2.txt") recA.add_file(filetree.relpath("rec_a_data/Dir B/tfb_1.txt"), "Dir B/tfb_1.txt") recA.save(mr) recB = Record(filetree.path, "Rec B", "rec_b_data") recB.add_file(filetree.relpath("rec_b_data/Dir A/tfa_1.txt"), "Dir A/tfa_1.txt") recB.add_file(filetree.relpath("rec_b_data/Dir A/tfa_2.txt"), "Dir A/tfa_2.txt") recB.add_file(filetree.relpath("rec_b_data/Dir B/tfb_1.txt"), "Dir B/tfb_1.txt") recB.save(mr) mr.save() cln = Cleaner(mr) cln.generate_diffs() cln.wait_for_completion() cln.perform_clean() cln.wait_for_completion() assert os.path.exists(filetree.relpath("rec_a_data/Dir A/tfa_1.txt")) assert os.path.exists(filetree.relpath("rec_a_data/Dir A/tfa_2.txt")) assert os.path.exists(filetree.relpath("rec_a_data/Dir B/tfb_1.txt")) assert os.path.exists(filetree.relpath("rec_b_data/Dir A/tfa_1.txt")) assert not os.path.exists(filetree.relpath("rec_b_data/Dir A/tfa_2.txt")) assert not os.path.exists(filetree.relpath("rec_b_data/Dir B/tfb_1.txt"))
def test_generate_diffs(filetree): filetree \ .dir("rec_a_data") \ .dir("Dir A") \ .file("tfa_1.txt", value="testfile_data_1") \ .file("tfa_2.txt", value="testfile_data_1") \ .up() \ .dir("Dir B") \ .file("tfb_1.txt", value="testfile_data_2") \ .root() \ .dir("rec_b_data") \ .dir("Dir A") \ .file("tfa_1.txt", value="testfile_data_1_chg") \ .file("tfa_2.txt", value="testfile_data_1") \ .up() \ .dir("Dir B") \ .file("tfb_1.txt", value="testfile_data_2_chg") \ .build() mr = MetaRecord.create_new(filetree.path) recA = Record(filetree.path, "Rec A", "rec_a_data") recA.add_file(filetree.relpath("rec_a_data/Dir A/tfa_1.txt"), "Dir A/tfa_1.txt") recA.add_file(filetree.relpath("rec_a_data/Dir A/tfa_2.txt"), "Dir A/tfa_2.txt") recA.add_file(filetree.relpath("rec_a_data/Dir B/tfb_1.txt"), "Dir B/tfb_1.txt") recA.save(mr) recB = Record(filetree.path, "Rec B", "rec_b_data") recB.add_file(filetree.relpath("rec_b_data/Dir A/tfa_1.txt"), "Dir A/tfa_1.txt") recB.add_file(filetree.relpath("rec_b_data/Dir A/tfa_2.txt"), "Dir A/tfa_2.txt") recB.add_file(filetree.relpath("rec_b_data/Dir B/tfb_1.txt"), "Dir B/tfb_1.txt") recB.save(mr) mr.save() cln = Cleaner(mr) cln.generate_diffs() cln.wait_for_completion() diffs = cln.to_delete assert len(diffs) == 1 diff_rec, files = diffs[0] assert diff_rec.name == recB.name assert len(files) == 1 assert files[0] == "Dir A/tfa_2.txt"
def test_load_latest_record(filetree): # Records are timestamped on CREATION NOT SAVE. This test should reflect that the first to be *instantiated* # is the oldest, not the first to be saved! mr = MetaRecord.create_new(filetree.path) recA = Record(filetree.path, "Rec A", "Rec A Data") # Oldest sleep(2) recB = Record(filetree.path, "Rec B", "Rec B Data") # Newest recB.save(mr) recA.save(mr) mr.save() latest = mr.load_latest_record() assert latest == recB sleep(2) recC = Record(filetree.path, "Rec C", "Rec C Data") # New Newest recC.save(mr) mr.save() latest = mr.load_latest_record() assert latest == recC
def test_create_metarecord(filetree): mr = MetaRecord.create_new(filetree.path) mr.save() assert filetree.exists(mr_path) assert len(filetree.content(mr_path)) > 0