test_utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import unittest
  2. import os
  3. from tubeup.utils import sanitize_identifier, check_is_file_empty
  4. class UtilsTest(unittest.TestCase):
  5. def test_preserve_valid_identifiers(self):
  6. valid = [
  7. 'youtube--QBwhSklJks',
  8. 'youtube-_--M04_mN-M',
  9. 'youtube-Xy2jZABDB40'
  10. ]
  11. clean = [sanitize_identifier(x) for x in valid]
  12. self.assertListEqual(valid, clean)
  13. def test_sanitize_bad_identifiers(self):
  14. bad = [
  15. 'twitch:vod-v181464551',
  16. 'twitch:clips-1003820974',
  17. 'twitter:card-1192732384065708032'
  18. ]
  19. expect = [
  20. 'twitch-vod-v181464551',
  21. 'twitch-clips-1003820974',
  22. 'twitter-card-1192732384065708032'
  23. ]
  24. clean = [sanitize_identifier(x) for x in bad]
  25. self.assertListEqual(expect, clean)
  26. def test_check_is_file_empty_when_file_is_empty(self):
  27. # Create a file for the test
  28. with open('testemptyfile.txt', 'w'):
  29. pass
  30. self.assertTrue(check_is_file_empty('testemptyfile.txt'))
  31. os.remove('testemptyfile.txt')
  32. def test_check_is_file_empty_when_file_is_not_empty(self):
  33. with open('testfilenotempty.txt', 'w') as not_empty_file:
  34. not_empty_file.write('just a text')
  35. self.assertFalse(check_is_file_empty('testfilenotempty.txt'))
  36. os.remove('testfilenotempty.txt')
  37. def test_check_is_file_empty_when_file_doesnt_exist(self):
  38. with self.assertRaisesRegex(
  39. FileNotFoundError,
  40. r"^Path 'file_that_doesnt_exist.txt' doesn't exist$"):
  41. check_is_file_empty('file_that_doesnt_exist.txt')