Include mimeparse, which is used by subunit/testtools.
[Samba.git] / lib / mimeparse / mimeparse_test.py
blob969cbf35e8f6d0fa5d27ebc55d3ea280b77933a6
1 """
2 Python tests for Mime-Type Parser.
4 This module loads a json file and converts the tests specified therein to a set
5 of PyUnitTestCases. Then it uses PyUnit to run them and report their status.
6 """
7 __version__ = "0.1"
8 __author__ = 'Ade Oshineye'
9 __email__ = "ade@oshineye.com"
10 __credits__ = ""
12 import mimeparse
13 import unittest
14 from functools import partial
15 # Conditional import to support Python 2.5
16 try:
17 import json
18 except ImportError:
19 import simplejson as json
21 def test_parse_media_range(args, expected):
22 expected = tuple(expected)
23 result = mimeparse.parse_media_range(args)
24 message = "Expected: '%s' but got %s" % (expected, result)
25 assert expected == result, message
27 def test_quality(args, expected):
28 result = mimeparse.quality(args[0], args[1])
29 message = "Expected: '%s' but got %s" % (expected, result)
30 assert expected == result, message
32 def test_best_match(args, expected):
33 result = mimeparse.best_match(args[0], args[1])
34 message = "Expected: '%s' but got %s" % (expected, result)
35 assert expected == result, message
37 def test_parse_mime_type(args, expected):
38 expected = tuple(expected)
39 result = mimeparse.parse_mime_type(args)
40 message = "Expected: '%s' but got %s" % (expected, result)
41 assert expected == result, message
43 def add_tests(suite, json_object, func_name, test_func):
44 test_data = json_object[func_name]
45 for test_datum in test_data:
46 args, expected = test_datum[0], test_datum[1]
47 desc = "%s(%s) with expected result: %s" % (func_name, str(args), str(expected))
48 if len(test_datum) == 3:
49 desc = test_datum[2] + " : " + desc
50 func = partial(test_func, *(args, expected))
51 func.__name__ = test_func.__name__
52 testcase = unittest.FunctionTestCase(func, description=desc)
53 suite.addTest(testcase)
55 def run_tests():
56 json_object = json.load(open("testdata.json"))
58 suite = unittest.TestSuite()
59 add_tests(suite, json_object, "parse_media_range", test_parse_media_range)
60 add_tests(suite, json_object, "quality", test_quality)
61 add_tests(suite, json_object, "best_match", test_best_match)
62 add_tests(suite, json_object, "parse_mime_type", test_parse_mime_type)
64 test_runner = unittest.TextTestRunner(verbosity=1)
65 test_runner.run(suite)
67 if __name__ == "__main__":
68 run_tests()