def test_write_to_ro_file(self): write_test_file = "dummy.lzma" assert not os.path.exists(write_test_file) with self.assertRaises(IOError): c = CompressedFile(write_test_file, mode="r") c.write("testing...") assert not os.path.exists(write_test_file)
def compress_one_file(self, filetype, force_popen): base_dir = self.get_test_dir() write_test_file = os.path.join(base_dir, "write_test." + filetype) assert not os.path.exists(write_test_file) c = CompressedFile(write_test_file, mode="w", force_popen=force_popen) # Write data to file.lzma lines = self.get_test_data() for line in lines: c.write(line + "\n") c.close() # Read it back after = [] c = CompressedFile(write_test_file, mode="r", force_popen=force_popen) for line in c: after.append(line.strip()) # make sure it looks ok self.assertEqual(len(lines), len(after)) for i in range(len(lines)): self.assertEqual(lines[i], after[i]) # all is well, remove the file. os.remove(write_test_file)
def light_versus_heavy(self, file_type, force_popen): base_dir = self.get_test_dir() test_lines = [] for i in range(2000): test_lines.append("Hello there {0}!".format(i)) test_contents = "\n".join(test_lines) # Write it with a little compression. write_light = os.path.join( base_dir, "clevel_test.1.{}.{}".format(force_popen, file_type)) assert not os.path.exists(write_light) c = CompressedFile(write_light, mode="w", force_popen=force_popen, compression_level=1) c.write(test_contents) c.close() # Check the resulting size light_compression_size = os.stat(write_light).st_size os.remove(write_light) # Write it with max compression. write_heavy = os.path.join( base_dir, "clevel_test.9.{}.{}".format(force_popen, file_type)) assert not os.path.exists(write_heavy) c = CompressedFile(write_heavy, mode="w", force_popen=force_popen, compression_level=9) c.write(test_contents) c.close() # Check the size again. heavy_compression_size = os.stat(write_heavy).st_size os.remove(write_heavy) common_msg = "size should be less than raw size for type {0} " \ "(popen={1})".format(file_type, force_popen) self.assertTrue(light_compression_size < len(test_contents), msg="Lightly Compressed " + common_msg) self.assertTrue(heavy_compression_size < len(test_contents), msg="Heavily Compressed " + common_msg) #print "{0}, popen={1} - raw: {2}, light: {3}, heavy: {4}".format( # t, popen, len(test_contents), light_compression_size, # heavy_compression_size) self.assertTrue(light_compression_size > heavy_compression_size, msg="Light compression ({0}) should be larger " \ "than heavy compression ({1}) for type {2} " \ "(popen={3})".format(light_compression_size, heavy_compression_size, file_type, force_popen))
def light_versus_heavy(self, file_type, force_popen): base_dir = self.get_test_dir() test_lines = [] for i in range(2000): test_lines.append("Hello there {0}!".format(i)) test_contents = "\n".join(test_lines) # Write it with a little compression. write_light = os.path.join(base_dir, "clevel_test.1.{}.{}".format(force_popen, file_type)) assert not os.path.exists(write_light) c = CompressedFile(write_light, mode="w", force_popen=force_popen, compression_level=1) c.write(test_contents) c.close() # Check the resulting size light_compression_size = os.stat(write_light).st_size os.remove(write_light) # Write it with max compression. write_heavy = os.path.join(base_dir, "clevel_test.9.{}.{}".format(force_popen, file_type)) assert not os.path.exists(write_heavy) c = CompressedFile(write_heavy, mode="w", force_popen=force_popen, compression_level=9) c.write(test_contents) c.close() # Check the size again. heavy_compression_size = os.stat(write_heavy).st_size os.remove(write_heavy) common_msg = "size should be less than raw size for type {0} " "(popen={1})".format(file_type, force_popen) self.assertTrue(light_compression_size < len(test_contents), msg="Lightly Compressed " + common_msg) self.assertTrue(heavy_compression_size < len(test_contents), msg="Heavily Compressed " + common_msg) # print "{0}, popen={1} - raw: {2}, light: {3}, heavy: {4}".format( # t, popen, len(test_contents), light_compression_size, # heavy_compression_size) self.assertTrue( light_compression_size > heavy_compression_size, msg="Light compression ({0}) should be larger " "than heavy compression ({1}) for type {2} " "(popen={3})".format(light_compression_size, heavy_compression_size, file_type, force_popen), )