my_zip_tester.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import unittest, shutil, os, tempfile, my_zip_file, zipfile
  2. orig = os.getcwd()
  3. path = os.path.abspath('tester')
  4. class Test_archiver(unittest.TestCase):
  5. def setUp(self):
  6. #set up a tempfile for testing to be deleted on tearDown
  7. self.orig = os.getcwd()
  8. self.path = tempfile.mkdtemp()
  9. self.zip_fn = os.path.basename(self.path) + '.zip'
  10. #Populate Temp Path with files and directories for testing
  11. os.chdir(self.path)
  12. self.files = ['test_file', 'test_file.ext']
  13. for fn in self.files:
  14. open(fn, 'w').close()
  15. self.dirs = ['test_folder', 'bogus_test_folder.ext']
  16. for dir_name in self.dirs:
  17. os.mkdir(dir_name)
  18. os.chdir(self.orig)
  19. def test_func_archiver(self):
  20. my_zip_file.archiver(self.path)
  21. #Observed/Expected
  22. zf = zipfile.ZipFile(self.zip_fn)
  23. observed = zf.namelist()
  24. expected = [os.path.join(os.path.basename(self.path),fn)
  25. .replace('\\', '/') for fn in self.files]
  26. #tests
  27. self.assertEqual(sorted(observed), sorted(expected),
  28. '\nObserved does not equal Expected:\
  29. \nExpected: {0}\
  30. \nObserved: {1}'.
  31. format(expected, observed))
  32. for folder in self.dirs:
  33. self.assertTrue(os.path.join(self.path, folder) not in observed,
  34. 'The subdirectory {0} was archived'.
  35. format(folder))
  36. for fn in self.files:
  37. archived_file = os.path.join(os.path.basename(self.path), fn)
  38. self.assertTrue(archived_file.replace('\\', '/') in observed,
  39. '{0} was not archived'.format(fn))
  40. def tearDown(self):
  41. shutil.rmtree(self.path)
  42. if __name__ == "__main__":
  43. unittest.main()