adding unit tests for filter function
authorEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 08:55:44 +0000 (10:55 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Thu, 1 Aug 2013 08:55:44 +0000 (10:55 +0200)
testing/test_deltatar.py

index 4368ba8..b844beb 100644 (file)
@@ -321,6 +321,86 @@ class DeltaTarTest(BaseTest):
         assert not os.path.exists("./source_dir/test/huge")
         assert not os.path.exists("./source_dir/big")
 
+    def test_create_filter_func(self):
+        '''
+        Tests create backup basic filtering.
+        '''
+        visited_paths = []
+        def filter_func(visited_paths, path):
+            if path not in visited_paths:
+                visited_paths.append(path)
+            return True
+
+        filter_func = partial(filter_func, visited_paths)
+        deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+                            logger=self.consoleLogger,
+                            included_files=["/test", "/small"],
+                            excluded_files=["/test/huge"],
+                            filter_func=filter_func)
+
+        # create first backup
+        deltatar.create_full_backup(
+            source_path="source_dir",
+            backup_path="backup_dir")
+
+        assert os.path.exists("backup_dir")
+        shutil.rmtree("source_dir")
+
+        tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+        tar_path = os.path.join("backup_dir", tar_filename)
+
+        deltatar.restore_backup(target_path="source_dir",
+                                backup_tar_path=tar_path)
+        assert visited_paths == [
+                '/small',
+                '/test',
+                '/test/huge2',
+                '/test/test2'
+            ]
+
+    def test_create_filter_out_func(self):
+        '''
+        Tests create backup basic filtering.
+        '''
+        visited_paths = []
+        def filter_func(visited_paths, path):
+            '''
+            Filter out everything
+            '''
+            if path not in visited_paths:
+                visited_paths.append(path)
+            return False
+
+        filter_func = partial(filter_func, visited_paths)
+        deltatar = DeltaTar(mode=self.MODE, password=self.PASSWORD,
+                            logger=self.consoleLogger,
+                            included_files=["/test", "/small"],
+                            excluded_files=["/test/huge"],
+                            filter_func=filter_func)
+
+        # create first backup
+        deltatar.create_full_backup(
+            source_path="source_dir",
+            backup_path="backup_dir")
+
+        assert os.path.exists("backup_dir")
+        shutil.rmtree("source_dir")
+
+        tar_filename = deltatar.volume_name_func('backup_dir', True, 0)
+        tar_path = os.path.join("backup_dir", tar_filename)
+
+        deltatar.restore_backup(target_path="source_dir",
+                                backup_tar_path=tar_path)
+        assert visited_paths == [
+                '/small',
+                '/test'
+            ]
+
+        # check that effectively no file was backed up
+        assert not os.path.exists("./source_dir/small")
+        assert not os.path.exists("./source_dir/big")
+        assert not os.path.exists("./source_dir/test")
+
 class DeltaTar2Test(DeltaTarTest):
     '''
     Same as DeltaTar but with specific ":" mode