import os.path
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")
]

testFileEncoding = [('text-iso8859-1.txt', b'iso-8859-1')]


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'
        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()

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():
            filename = os.path.join(self.TESTDATA_DIR, filename)

            with open(filename, 'rb') as f:
                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']


if __name__ == '__main__':
    unittest.main()

Refactor tests