test_formats.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /usr/bin/env python3
  2. from base_test import DakTestCase
  3. import unittest
  4. from daklib.formats import parse_format, validate_changes_format
  5. from daklib.dak_exceptions import UnknownFormatError
  6. class ParseFormatTestCase(DakTestCase):
  7. def assertParse(self, format, expected):
  8. self.assertEqual(parse_format(format), expected)
  9. def assertParseFail(self, format):
  10. self.assertRaises(
  11. UnknownFormatError,
  12. lambda: parse_format(format)
  13. )
  14. def testParse(self):
  15. self.assertParse('1.0', (1, 0))
  16. def testEmpty(self):
  17. self.assertParseFail('')
  18. self.assertParseFail(' ')
  19. self.assertParseFail(' ')
  20. def textText(self):
  21. self.assertParse('1.2 (three)', (1, 2, 'three'))
  22. self.assertParseFail('0.0 ()')
  23. class ValidateChangesFormat(DakTestCase):
  24. def assertValid(self, changes, field='files'):
  25. validate_changes_format(changes, field)
  26. def assertInvalid(self, *args, **kwargs):
  27. self.assertRaises(
  28. UnknownFormatError,
  29. lambda: self.assertValid(*args, **kwargs)
  30. )
  31. ##
  32. def testBinary(self):
  33. self.assertValid((1, 5))
  34. self.assertValid((1, 8))
  35. self.assertInvalid((1, 0))
  36. def testRange(self):
  37. self.assertInvalid((1, 3))
  38. self.assertValid((1, 5))
  39. self.assertValid((1, 8))
  40. self.assertInvalid((1, 9))
  41. def testFilesField(self):
  42. self.assertInvalid((1, 7), field='notfiles')
  43. self.assertValid((1, 8), field='notfiles')
  44. if __name__ == '__main__':
  45. unittest.main()