helpers.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from datetime import datetime
  2. from darc.helpers import Location, format_timedelta, IncludePattern
  3. from darc.testsuite import DarcTestCase
  4. class LocationTestCase(DarcTestCase):
  5. def test(self):
  6. self.assert_equal(
  7. repr(Location('ssh://user@host:1234/some/path::archive')),
  8. "Location(proto='ssh', user='user', host='host', port=1234, path='/some/path', archive='archive')"
  9. )
  10. self.assert_equal(
  11. repr(Location('file:///some/path::archive')),
  12. "Location(proto='file', user=None, host=None, port=None, path='/some/path', archive='archive')"
  13. )
  14. self.assert_equal(
  15. repr(Location('user@host:/some/path::archive')),
  16. "Location(proto='ssh', user='user', host='host', port=22, path='/some/path', archive='archive')"
  17. )
  18. self.assert_equal(
  19. repr(Location('mybackup.darc::archive')),
  20. "Location(proto='file', user=None, host=None, port=None, path='mybackup.darc', archive='archive')"
  21. )
  22. self.assert_equal(
  23. repr(Location('/some/absolute/path::archive')),
  24. "Location(proto='file', user=None, host=None, port=None, path='/some/absolute/path', archive='archive')"
  25. )
  26. self.assert_equal(
  27. repr(Location('some/relative/path::archive')),
  28. "Location(proto='file', user=None, host=None, port=None, path='some/relative/path', archive='archive')"
  29. )
  30. class FormatTimedeltaTestCase(DarcTestCase):
  31. def test(self):
  32. t0 = datetime(2001, 1, 1, 10, 20, 3, 0)
  33. t1 = datetime(2001, 1, 1, 12, 20, 4, 100000)
  34. self.assert_equal(
  35. format_timedelta(t1 - t0),
  36. '2 hours 1.10 seconds'
  37. )
  38. class PatternTestCase(DarcTestCase):
  39. def test(self):
  40. py = IncludePattern('*.py')
  41. foo = IncludePattern('/foo')
  42. self.assert_equal(py.match('/foo/foo.py'), True)
  43. self.assert_equal(py.match('/bar/foo.java'), False)
  44. self.assert_equal(foo.match('/foo/foo.py'), True)
  45. self.assert_equal(foo.match('/bar/foo.java'), False)
  46. self.assert_equal(foo.match('/foobar/foo.py'), False)
  47. self.assert_equal(foo.match('/foo'), True)