forked from ahupp/python-magic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pythonic_library.py
More file actions
80 lines (61 loc) · 3.07 KB
/
test_pythonic_library.py
File metadata and controls
80 lines (61 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Test cases for the wrapped C library.
"""
import os
import unittest
from magic.wrapper import MAGIC_MIME, \
MAGIC_MIME_ENCODING, MAGIC_COMPRESS, MAGIC_MIME_TYPE
from magic.pythonic import Magic, Magic2
from ._test_data import TEST_DATA_DIR, TEST_FILES, TEST_FILES_COMPRESSED, MagicTestCaseMixin
class TestMagicPythonic(MagicTestCaseMixin, unittest.TestCase):
def test_files(self):
m_desc = Magic()
m_mime_type = Magic(MAGIC_MIME_TYPE)
m_mime_encoding = Magic(MAGIC_MIME_ENCODING)
m_mime = Magic(MAGIC_MIME)
for filename, expected in sorted(TEST_FILES.items()):
mime_type, mime_encoding, desc, mime = expected
file_path = os.path.join(TEST_DATA_DIR, filename)
read_mime_type = m_mime_type.from_file(file_path)
read_mime_encoding = m_mime_encoding.from_file(file_path)
read_desc = m_desc.from_file(file_path)
read_mime = m_mime.from_file(file_path)
self.assertMatches(read_mime_type, mime_type)
self.assertMatches(read_mime_encoding, mime_encoding)
self.assertMatches(read_mime, mime)
self.assertMatches(read_desc, desc)
def test_compressed_files(self):
m_desc = Magic(MAGIC_COMPRESS)
m_mime_type = Magic(MAGIC_MIME_TYPE | MAGIC_COMPRESS)
m_mime_encoding = Magic(MAGIC_MIME_ENCODING | MAGIC_COMPRESS)
m_mime = Magic(MAGIC_MIME | MAGIC_COMPRESS)
for filename, expected in sorted(TEST_FILES_COMPRESSED.items()):
mime_type, mime_encoding, desc, mime = expected
file_path = os.path.join(TEST_DATA_DIR, filename)
read_mime_type = m_mime_type.from_file(file_path)
read_mime_encoding = m_mime_encoding.from_file(file_path)
read_desc = m_desc.from_file(file_path)
read_mime = m_mime.from_file(file_path)
self.assertMatches(read_mime_type, mime_type)
self.assertMatches(read_mime_encoding, mime_encoding)
self.assertMatches(read_mime, mime)
self.assertMatches(read_desc, desc)
class TestMagic2(MagicTestCaseMixin, unittest.TestCase):
def test_files(self):
for filename, expected in sorted(TEST_FILES.items()):
mime_type, mime_encoding, desc, mime = expected
file_path = os.path.join(TEST_DATA_DIR, filename)
m = Magic2.from_file(file_path)
self.assertMatches(m.mimetype, mime_type)
self.assertMatches(m.encoding, mime_encoding)
self.assertMatches(m.mime, mime)
self.assertMatches(m.description, desc)
def test_compressed_files(self):
for filename, expected in sorted(TEST_FILES_COMPRESSED.items()):
mime_type, mime_encoding, desc, mime = expected
file_path = os.path.join(TEST_DATA_DIR, filename)
m = Magic2.from_file(file_path, MAGIC_COMPRESS)
self.assertMatches(m.mimetype, mime_type)
self.assertMatches(m.encoding, mime_encoding)
self.assertMatches(m.mime, mime)
self.assertMatches(m.description, desc)