def test_watch_add(self): models.FileNode.objects.all().delete() filesync.scan_folders(verbose=False) watch = models.WatchFolder.objects.filter(name="test folder")[0] watcher = filesync.Watcher() watcher.watch(blocking=False) os.mkdir(os.path.join(self.path, "test4")) os.mkdir(os.path.join(self.path, "test1", "test5")) open(os.path.join(self.path, "test1", "test5", "file6.txt"), "w").write("ok") watcher.terminate() watch = models.WatchFolder.objects.filter(name="test folder")[0] root_child_nodes = models.FileNode.objects.filter(parent=watch.root_node) names = [node.name for node in root_child_nodes] names.sort() self.assertEquals(names, ["file1.txt", "test1", "test2", "test3", "test4"]) self.assertNotEqual(watch.get_node("test1"), None) self.assertNotEqual(watch.get_node("test2"), None) self.assertNotEqual(watch.get_node("test3"), None) self.assertNotEqual(watch.get_node("test4"), None) self.assertNotEqual(watch.get_node("file1.txt"), None) self.assertNotEqual(watch.get_node("test1/file2.txt"), None) self.assertNotEqual(watch.get_node("test1/test4"), None) self.assertNotEqual(watch.get_node("test1/test5"), None) self.assertNotEqual(watch.get_node("test1/test5/file6.txt"), None) self.assertNotEqual(watch.get_node("test2/file3.txt"), None)
def test_second_scan_mixed_files(self): """Re-Scan Add & Remove Files: Scan test folder, then add & remove some files and scan again. Make sure additions & removals are correctly removed to database""" models.FileNode.objects.all().delete() filesync.scan_folders(verbose=False) os.mkdir(os.path.join(self.path, "test4")) os.mkdir(os.path.join(self.path, "test1", "test5")) open(os.path.join(self.path, "test1", "test5", "file6.txt"), "w").write("ok") shutil.rmtree(os.path.join(self.path, "test1")) os.remove(os.path.join(self.path, "file1.txt")) os.remove(os.path.join(self.path, "test2", "file3.txt")) filesync.scan_folders(verbose=False) watch = models.WatchFolder.objects.filter(name="test folder")[0] root_child_nodes = models.FileNode.objects.filter(parent=watch.root_node) names = [node.name for node in root_child_nodes] names.sort() self.assertEquals(names, ["test2", "test3", "test4"]) self.assertNotEqual(watch.get_node("test2"), None) self.assertNotEqual(watch.get_node("test3"), None) self.assertNotEqual(watch.get_node("test4"), None)
def test_initial_scan(self): """First Scan: Scan test folder from clean database and check everything is found correctly""" models.FileNode.objects.all().delete() filesync.scan_folders(verbose=False) watch = models.WatchFolder.objects.filter(name="test folder")[0] root_child_nodes = models.FileNode.objects.filter(parent=watch.root_node) names = [node.name for node in root_child_nodes] names.sort() self.assertEquals(names, ["file1.txt", "test1", "test2", "test3"]) self.assertNotEqual(watch.get_node("test1"), None) self.assertNotEqual(watch.get_node("test2"), None) self.assertNotEqual(watch.get_node("test3"), None) self.assertNotEqual(watch.get_node("file1.txt"), None) self.assertNotEqual(watch.get_node("test1/file2.txt"), None) self.assertNotEqual(watch.get_node("test1/test4"), None) self.assertNotEqual(watch.get_node("test2/file3.txt"), None) self.assertTrue(watch.get_node("test1").directory) self.assertTrue(watch.get_node("test2").directory) self.assertTrue(watch.get_node("test3").directory) self.assertTrue(watch.get_node("test1/test4").directory) print "\nNode tree:" print_node_tree(watch.root_node)
def test_second_scan_no_change(self): """Re-Scan No Change: Scan test folder, then scan again before any changes to make sure that no erroneous changes are made in the database""" models.FileNode.objects.all().delete() filesync.scan_folders(verbose=False) filesync.scan_folders(verbose=False) watch = models.WatchFolder.objects.filter(name="test folder")[0] root_child_nodes = models.FileNode.objects.filter(parent=watch.root_node) names = [node.name for node in root_child_nodes] names.sort() self.assertEquals(names, ["file1.txt", "test1", "test2", "test3"]) self.assertNotEqual(watch.get_node("test1"), None) self.assertNotEqual(watch.get_node("test2"), None) self.assertNotEqual(watch.get_node("test3"), None) self.assertNotEqual(watch.get_node("file1.txt"), None) self.assertNotEqual(watch.get_node("test1/file2.txt"), None) self.assertNotEqual(watch.get_node("test1/test4"), None) self.assertNotEqual(watch.get_node("test2/file3.txt"), None)
def handle(self, **options): filesync.scan_folders()