def test_zip_no_append(self): """It should not append files by default.""" util.zip_directory(self.compressdir, self.tmpdir, delete_original=True) self.create_file() zip_path = util.zip_directory(self.compressdir, self.tmpdir) with zipfile.ZipFile(zip_path) as f: self.assertEqual(len(f.namelist()), 1)
def test_zip_append(self): """It should append files if specified.""" zip_path = util.zip_directory(self.compressdir, self.tmpdir, delete_original=True, append=True) with zipfile.ZipFile(zip_path) as f: self.assertEqual(len(f.namelist()), 1) self.create_file() self.create_file() self.create_file() zip_path = util.zip_directory(self.compressdir, self.tmpdir, append=True) with zipfile.ZipFile(zip_path) as f: self.assertEqual(len(f.namelist()), 4)
def test_zip_multi_level(self): """It should support multiple directory levels.""" self.create_file('test1/test.txt') self.create_file('test1/testsubdir/test.txt') self.create_file('test2/test.txt') zip_path = util.zip_directory(self.compressdir, self.tmpdir) with zipfile.ZipFile(zip_path) as f: names = f.namelist() self.assertIn('test/test1/test.txt', names) self.assertIn('test/test1/testsubdir/test.txt', names) self.assertIn('test/test2/test.txt', names)
def test_zip(self): """It should compress a file and be able to decompress.""" zip_path = util.zip_directory(self.compressdir, self.tmpdir) # Zip file should be smaller self.assertLess(os.path.getsize(self.filepath), os.path.getsize(zip_path)) # Zip file should be able to decompress and have same contents as # before. checkdir = os.path.join(self.tmpdir, 'check/') os.mkdir(checkdir) check_filename = os.path.join(checkdir, 'test/{}'.format(self.filename)) with zipfile.ZipFile(zip_path) as f: f.extractall(checkdir) with open(check_filename) as f: s = f.read() self.assertEqual(s, 'This string will be compressed')
def test_zip_delete_original(self): """It should delete the original files if specified.""" util.zip_directory(self.compressdir, self.tmpdir, delete_original=True) self.assertFalse(os.path.exists(self.filepath))
def test_zip_no_delete_original(self): """It should not delete the original files by default.""" util.zip_directory(self.compressdir, self.tmpdir) self.assertTrue(os.path.exists(self.filepath))