summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Romanovich <anthony.romanovich@gmail.com>2013-02-04 23:45:11 +0600
committerAnton Romanovich <anthony.romanovich@gmail.com>2013-02-04 23:45:11 +0600
commitf31da1caac95a61af88dea31cb41b5cca3d7916b (patch)
treed43b9e8d8c597a3fe2e8f042a23a4b87a20f7730
parent029834f4d866f75dfa897bbfcafe2482a3675662 (diff)
downloadpython-magic-f31da1caac95a61af88dea31cb41b5cca3d7916b.tar.gz
Refactor tests
-rw-r--r--test.py96
1 files changed, 47 insertions, 49 deletions
diff --git a/test.py b/test.py
index 0a14b65..a1d450d 100644
--- a/test.py
+++ b/test.py
@@ -4,61 +4,59 @@ import unittest
import magic
-testfile = [
- ("magic.pyc", b"python 2.4 byte-compiled", b"application/octet-stream"),
- ("test.pdf", b"PDF document, version 1.2", b"application/pdf"),
- ("test.gz", b'gzip compressed data, was "test", from Unix, last modified: '
- b'Sun Jun 29 07:32:52 2008', b"application/x-gzip"),
- ("text.txt", b"ASCII text", b"text/plain"),
- # is there no better way to encode a unicode literal across python2/3.[01]/3.3?
- # (b"\xce\xbb".decode('utf-8'), b"empty", b"application/x-empty")
-]
+class MagicTest(unittest.TestCase):
+ TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata')
-testFileEncoding = [('text-iso8859-1.txt', b'iso-8859-1')]
+ def assert_values(self, m, expected_values):
+ for filename, expected_value in expected_values.items():
+ filename = os.path.join(self.TESTDATA_DIR, filename)
-
-class TestMagic(unittest.TestCase):
-
- mime = False
-
- def setUp(self):
- self.m = magic.Magic(mime=self.mime)
-
- def testFileTypes(self):
- for filename, desc, mime in testfile:
- filename = os.path.join(os.path.dirname(__file__), "testdata", filename)
- if self.mime:
- target = mime
- else:
- target = desc
with open(filename, 'rb') as f:
- self.assertEqual(target, self.m.from_buffer(f.read(1024)))
- self.assertEqual(target, self.m.from_file(filename), filename)
-
-
- def testErrors(self):
- self.assertRaises(IOError, self.m.from_file, "nonexistent")
- self.assertRaises(magic.MagicException, magic.Magic, magic_file="noneexistent")
- os.environ['MAGIC'] = '/nonexistetn'
+ value = m.from_buffer(f.read(1024))
+ self.assertEqual(value, expected_value)
+
+ value = m.from_file(filename)
+ self.assertEqual(value, expected_value)
+
+ def test_mime_types(self):
+ m = magic.Magic(mime=True)
+ self.assert_values(m, {
+ 'magic.pyc': b'application/octet-stream',
+ 'test.pdf': b'application/pdf',
+ 'test.gz': b'application/x-gzip',
+ 'text.txt': b'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': b'python 2.4 byte-compiled',
+ 'test.pdf': b'PDF document, version 1.2',
+ 'test.gz': b'gzip compressed data, was "test", from Unix, '
+ b'last modified: Sun Jun 29 01:32:52 2008',
+ 'text.txt': b'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': b'iso-8859-1',
+ 'text.txt': b'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'
self.assertRaises(magic.MagicException, magic.Magic)
del os.environ['MAGIC']
-class TestMagicMime(TestMagic):
- mime = True
-
-
-class TestMagicMimeEncoding(unittest.TestCase):
- def setUp(self):
- self.m = magic.Magic(mime_encoding=True)
-
- def testFileEncoding(self):
- for filename, encoding in testFileEncoding:
- filename = os.path.join(os.path.dirname(__file__), "testdata", filename)
- with open(filename, 'rb') as f:
- self.assertEqual(encoding, self.m.from_buffer(f.read(1024)))
- self.assertEqual(encoding, self.m.from_file(filename), filename)
-
-
if __name__ == '__main__':
unittest.main()