lib.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Provide base classes for the test system"""
  2. from unittest import TestCase
  3. import os
  4. import tempfile
  5. __all__ = ['TestBase', 'FileCreator']
  6. #{ Utilities
  7. class FileCreator:
  8. """A instance which creates a temporary file with a prefix and a given size
  9. and provides this info to the user.
  10. Once it gets deleted, it will remove the temporary file as well."""
  11. __slots__ = ("_size", "_path")
  12. def __init__(self, size, prefix=''):
  13. assert size, "Require size to be larger 0"
  14. self._path = tempfile.mktemp(prefix=prefix)
  15. self._size = size
  16. with open(self._path, "wb") as fp:
  17. fp.seek(size - 1)
  18. fp.write(b'1')
  19. assert os.path.getsize(self.path) == size
  20. def __del__(self):
  21. try:
  22. os.remove(self.path)
  23. except OSError:
  24. pass
  25. # END exception handling
  26. def __enter__(self):
  27. return self
  28. def __exit__(self, exc_type, exc_value, traceback):
  29. self.__del__()
  30. @property
  31. def path(self):
  32. return self._path
  33. @property
  34. def size(self):
  35. return self._size
  36. #} END utilities
  37. class TestBase(TestCase):
  38. """Foundation used by all tests"""
  39. #{ Configuration
  40. k_window_test_size = 1000 * 1000 * 8 + 5195
  41. #} END configuration
  42. #{ Overrides
  43. @classmethod
  44. def setUpAll(cls):
  45. # nothing for now
  46. pass
  47. # END overrides
  48. #{ Interface
  49. #} END interface