diff options
| author | Adam Hupp <adam@hupp.org> | 2015-07-29 21:54:07 -0700 |
|---|---|---|
| committer | Adam Hupp <adam@hupp.org> | 2015-07-29 21:54:50 -0700 |
| commit | a818eda7e3e163d6179f9a6a381856ffb00722ac (patch) | |
| tree | f89650a7cf74a49eecd992a8fb986070fac0239c /test/test.py | |
| parent | e479096d5b08af12f3f6eb47290932c9f813acdd (diff) | |
| download | python-magic-a818eda7e3e163d6179f9a6a381856ffb00722ac.tar.gz | |
Include tests in the sdist tarball
Diffstat (limited to 'test/test.py')
| -rw-r--r-- | test/test.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..f9c9c41 --- /dev/null +++ b/test/test.py @@ -0,0 +1,78 @@ +import os.path +import unittest + +import magic + +class MagicTest(unittest.TestCase): + TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata') + + def assert_values(self, m, expected_values): + for filename, expected_value in expected_values.items(): + try: + filename = os.path.join(self.TESTDATA_DIR, filename) + except TypeError: + filename = os.path.join(self.TESTDATA_DIR.encode('utf-8'), filename) + + with open(filename, 'rb') as f: + value = m.from_buffer(f.read()) + expected_value_bytes = expected_value.encode('utf-8') + self.assertEqual(value, expected_value_bytes) + + value = m.from_file(filename) + self.assertEqual(value, expected_value_bytes) + + def test_mime_types(self): + m = magic.Magic(mime=True) + self.assert_values(m, { + 'magic.pyc': 'application/octet-stream', + 'test.pdf': 'application/pdf', + 'test.gz': 'application/x-gzip', + 'text.txt': 'text/plain', + b'\xce\xbb'.decode('utf-8'): 'text/plain', + b'\xce\xbb': 'text/plain', + }) + + def test_descriptions(self): + m = magic.Magic() + os.environ['TZ'] = 'UTC' # To get the last modified date of test.gz in UTC + try: + self.assert_values(m, { + 'magic.pyc': 'python 2.4 byte-compiled', + 'test.pdf': 'PDF document, version 1.2', + 'test.gz': 'gzip compressed data, was "test", from Unix, ' + 'last modified: Sun Jun 29 01:32:52 2008', + 'text.txt': 'ASCII text', + }) + finally: + del os.environ['TZ'] + + def test_mime_encodings(self): + m = magic.Magic(mime_encoding=True) + self.assert_values(m, { + 'text-iso8859-1.txt': 'iso-8859-1', + 'text.txt': 'us-ascii', + }) + + def test_errors(self): + m = magic.Magic() + self.assertRaises(IOError, m.from_file, 'nonexistent') + self.assertRaises(magic.MagicException, magic.Magic, + magic_file='nonexistent') + os.environ['MAGIC'] = 'nonexistent' + try: + self.assertRaises(magic.MagicException, magic.Magic) + finally: + del os.environ['MAGIC'] + + def test_keep_going(self): + filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg') + + m = magic.Magic(mime=True) + self.assertEqual(m.from_file(filename), + 'application/octet-stream'.encode('utf-8')) + + m = magic.Magic(mime=True, keep_going=True) + self.assertEqual(m.from_file(filename), 'image/jpeg'.encode('utf-8')) + +if __name__ == '__main__': + unittest.main() |
