tests.py 983 B

1234567891011121314151617181920212223242526272829303132
  1. # coding: utf-8
  2. import unittest
  3. import magic
  4. class MagicTestCase(unittest.TestCase):
  5. filename = 'magic.py'
  6. expected_mime_type = 'text/x-script.python'
  7. expected_encoding = 'us-ascii'
  8. expected_name = 'Python script, ASCII text executable'
  9. def assert_result(self, result):
  10. self.assertEqual(result.mime_type, self.expected_mime_type)
  11. self.assertEqual(result.encoding, self.expected_encoding)
  12. self.assertEqual(result.name, self.expected_name)
  13. def test_detect_from_filename(self):
  14. result = magic.detect_from_filename(self.filename)
  15. self.assert_result(result)
  16. def test_detect_from_fobj(self):
  17. with open(self.filename, "rb") as fobj:
  18. result = magic.detect_from_fobj(fobj)
  19. self.assert_result(result)
  20. def test_detect_from_content(self):
  21. with open(self.filename, "rb") as fobj:
  22. result = magic.detect_from_content(fobj.read(8192))
  23. self.assert_result(result)