99
1010from zappend .fsutil .fileobj import FileObj
1111from zappend .fsutil .transaction import Transaction
12- from zappend .fsutil .transaction import LOCK_EXT
1312from zappend .fsutil .transaction import ROLLBACK_FILE
1413from ..helpers import clear_memory_fs
1514
@@ -21,12 +20,15 @@ def setUp(self):
2120 clear_memory_fs ()
2221
2322 def test_transaction_success (self ):
24- self ._run_transaction_test (fail = False )
23+ self ._run_transaction_test (fail = False , rollback = True )
2524
26- def test_transaction_with_rollback (self ):
27- self ._run_transaction_test (fail = True )
25+ def test_transaction_failure_with_rollback (self ):
26+ self ._run_transaction_test (fail = True , rollback = True )
2827
29- def _run_transaction_test (self , fail : bool ):
28+ def test_transaction_failure_without_rollback (self ):
29+ self ._run_transaction_test (fail = True , rollback = False )
30+
31+ def _run_transaction_test (self , fail : bool , rollback : bool ):
3032
3133 test_root = FileObj ("memory://test" )
3234 test_root .mkdir ()
@@ -42,7 +44,8 @@ def _run_transaction_test(self, fail: bool):
4244 self .assertFalse (test_file_3 .exists ())
4345
4446 temp_dir = FileObj ("memory://temp" )
45- transaction = Transaction (test_root , temp_dir )
47+ transaction = Transaction (test_root , temp_dir ,
48+ disable_rollback = not rollback )
4649
4750 self .assertEqual (test_root , transaction .target_dir )
4851
@@ -69,25 +72,26 @@ def create_test_folder(rollback_cb: Callable):
6972
7073 try :
7174 with transaction as rollback_cb :
72- self .assertTrue (rollback_file .exists ())
7375 self .assertTrue (lock_file .exists ())
76+ self .assertEqual (rollback , rollback_file .exists ())
7477
7578 change_test_file_1 (rollback_cb )
7679 create_test_file_2 (rollback_cb )
7780 create_test_folder (rollback_cb )
7881
79- rollback_data = rollback_file .read (mode = "rt" )
80- rollback_records = [line .split ()[:2 ]
81- for line in rollback_data .split ("\n " )]
82- self .assertEqual (
83- [
84- ["replace_file" , "file-1.txt" ],
85- ["delete_file" , "file-2.txt" ],
86- ["delete_dir" , "folder" ],
87- []
88- ],
89- rollback_records
90- )
82+ if rollback :
83+ rollback_data = rollback_file .read (mode = "rt" )
84+ rollback_records = [line .split ()[:2 ]
85+ for line in rollback_data .split ("\n " )]
86+ self .assertEqual (
87+ [
88+ ["replace_file" , "file-1.txt" ],
89+ ["delete_file" , "file-2.txt" ],
90+ ["delete_dir" , "folder" ],
91+ []
92+ ],
93+ rollback_records
94+ )
9195
9296 if fail :
9397 raise OSError ("disk full" )
@@ -101,7 +105,7 @@ def create_test_folder(rollback_cb: Callable):
101105 self .assertFalse (lock_file .exists ())
102106 self .assertFalse (rollback_dir .exists ())
103107
104- if fail :
108+ if fail and rollback :
105109 self .assertTrue (test_root .exists ())
106110 self .assertTrue (test_file_1 .exists ()) # replace_file by rollback
107111 self .assertEqual (b"A-B-C" , test_file_1 .read ())
0 commit comments