File: mimeparse_test.py

package info (click to toggle)
python-mimeparse 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112 kB
  • sloc: python: 170; makefile: 6
file content (77 lines) | stat: -rwxr-xr-x 2,663 bytes parent folder | download
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
#!/usr/bin/env python
"""
Python tests for Mime-Type Parser.

This module loads a json file and converts the tests specified therein to a set
of PyUnitTestCases. Then it uses PyUnit to run them and report their status.
"""
import json
import unittest

import mimeparse


__version__ = "0.1"
__author__ = 'Ade Oshineye'
__email__ = "ade@oshineye.com"
__credits__ = ""


class MimeParseTestCase(unittest.TestCase):

    def setUp(self):
        super(MimeParseTestCase, self).setUp()
        with open("testdata.json") as f:
            self.test_data = json.load(f)

    def _test_parse_media_range(self, args, expected):
        expected = tuple(expected)
        result = mimeparse.parse_media_range(args)
        message = "Expected: '%s' but got %s" % (expected, result)
        self.assertEqual(expected, result, message)

    def _test_quality(self, args, expected):
        result = mimeparse.quality(args[0], args[1])
        message = "Expected: '%s' but got %s" % (expected, result)
        self.assertEqual(expected, result, message)

    def _test_best_match(self, args, expected, description):
        if expected is None:
            self.assertRaises(mimeparse.MimeTypeParseException,
                              mimeparse.best_match, args[0], args[1])
        else:
            result = mimeparse.best_match(args[0], args[1])
            message = \
                "Expected: '%s' but got %s. Description for this test: %s" % \
                (expected, result, description)
            self.assertEqual(expected, result, message)

    def _test_parse_mime_type(self, args, expected):
        if expected is None:
            self.assertRaises(mimeparse.MimeTypeParseException,
                              mimeparse.parse_mime_type, args)
        else:
            expected = tuple(expected)
            result = mimeparse.parse_mime_type(args)
            message = "Expected: '%s' but got %s" % (expected, result)
            self.assertEqual(expected, result, message)

    def test_parse_media_range(self):
        for args, expected in self.test_data['parse_media_range']:
            self._test_parse_media_range(args, expected)

    def test_quality(self):
        for args, expected in self.test_data['quality']:
            self._test_quality(args, expected)

    def test_best_match(self):
        for args, expected, description in self.test_data['best_match']:
            self._test_best_match(args, expected, description)

    def test_parse_mime_type(self):
        for args, expected in self.test_data['parse_mime_type']:
            self._test_parse_mime_type(args, expected)


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