diff options
Diffstat (limited to 'tests/test_util')
-rw-r--r-- | tests/test_util/__init__.py | 0 | ||||
-rw-r--r-- | tests/test_util/test_datetimeutil.py | 135 | ||||
-rw-r--r-- | tests/test_util/test_mimeparse.py | 235 | ||||
-rw-r--r-- | tests/test_util/test_quoting.py | 28 |
4 files changed, 398 insertions, 0 deletions
diff --git a/tests/test_util/__init__.py b/tests/test_util/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/test_util/__init__.py diff --git a/tests/test_util/test_datetimeutil.py b/tests/test_util/test_datetimeutil.py new file mode 100644 index 0000000..45d96c7 --- /dev/null +++ b/tests/test_util/test_datetimeutil.py @@ -0,0 +1,135 @@ +# (c) 2005 Clark C. Evans and contributors +# This module is part of the Python Paste Project and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php +# Some of this code was funded by: http://prometheusresearch.com +from time import localtime +from datetime import date +from paste.util.datetimeutil import * + +def test_timedelta(): + assert('' == normalize_timedelta("")) + assert('0.10' == normalize_timedelta("6m")) + assert('0.50' == normalize_timedelta("30m")) + assert('0.75' == normalize_timedelta("45m")) + assert('1.00' == normalize_timedelta("60 min")) + assert('1.50' == normalize_timedelta("90min")) + assert('1.50' == normalize_timedelta("1.50")) + assert('4.50' == normalize_timedelta("4 : 30")) + assert('1.50' == normalize_timedelta("1h 30m")) + assert('1.00' == normalize_timedelta("1")) + assert('1.00' == normalize_timedelta("1 hour")) + assert('8.00' == normalize_timedelta("480 mins")) + assert('8.00' == normalize_timedelta("8h")) + assert('0.50' == normalize_timedelta("0.5")) + assert('0.10' == normalize_timedelta(".1")) + assert('0.50' == normalize_timedelta(".50")) + assert('0.75' == normalize_timedelta("0.75")) + +def test_time(): + assert('03:00 PM' == normalize_time("3p", ampm=True)) + assert('03:00 AM' == normalize_time("300", ampm=True)) + assert('03:22 AM' == normalize_time("322", ampm=True)) + assert('01:22 PM' == normalize_time("1322", ampm=True)) + assert('01:00 PM' == normalize_time("13", ampm=True)) + assert('12:00 PM' == normalize_time("noon", ampm=True)) + assert("06:00 PM" == normalize_time("6", ampm=True)) + assert("01:00 PM" == normalize_time("1", ampm=True)) + assert("07:00 AM" == normalize_time("7", ampm=True)) + assert("01:00 PM" == normalize_time("1 pm", ampm=True)) + assert("03:30 PM" == normalize_time("3:30 pm", ampm=True)) + assert("03:30 PM" == normalize_time("3 30 pm", ampm=True)) + assert("03:30 PM" == normalize_time("3 30 P.M.", ampm=True)) + assert("12:00 PM" == normalize_time("0", ampm=True)) + assert("12:00 AM" == normalize_time("1200 AM", ampm=True)) + +def test_date(): + tm = localtime() + yr = tm[0] + mo = tm[1] + assert(date(yr,4,11) == parse_date("411")) + assert(date(yr,4,11) == parse_date("APR11")) + assert(date(yr,4,11) == parse_date("11APR")) + assert(date(yr,4,11) == parse_date("4 11")) + assert(date(yr,4,11) == parse_date("11 APR")) + assert(date(yr,4,11) == parse_date("APR 11")) + assert(date(yr,mo,11) == parse_date("11")) + assert(date(yr,4,1) == parse_date("APR")) + assert(date(yr,4,11) == parse_date("4/11")) + assert(date.today() == parse_date("today")) + assert(date.today() == parse_date("now")) + assert(None == parse_date("")) + assert('' == normalize_date(None)) + + assert('2001-02-03' == normalize_date("20010203")) + assert('1999-04-11' == normalize_date("1999 4 11")) + assert('1999-04-11' == normalize_date("1999 APR 11")) + assert('1999-04-11' == normalize_date("APR 11 1999")) + assert('1999-04-11' == normalize_date("11 APR 1999")) + assert('1999-04-11' == normalize_date("4 11 1999")) + assert('1999-04-01' == normalize_date("1999 APR")) + assert('1999-04-01' == normalize_date("1999 4")) + assert('1999-04-01' == normalize_date("4 1999")) + assert('1999-04-01' == normalize_date("APR 1999")) + assert('1999-01-01' == normalize_date("1999")) + + assert('1999-04-01' == normalize_date("1APR1999")) + assert('2001-04-01' == normalize_date("1APR2001")) + + assert('1999-04-18' == normalize_date("1999-04-11+7")) + assert('1999-04-18' == normalize_date("1999-04-11 7")) + assert('1999-04-01' == normalize_date("1 apr 1999")) + assert('1999-04-11' == normalize_date("11 apr 1999")) + assert('1999-04-11' == normalize_date("11 Apr 1999")) + assert('1999-04-11' == normalize_date("11-apr-1999")) + assert('1999-04-11' == normalize_date("11 April 1999")) + assert('1999-04-11' == normalize_date("11 APRIL 1999")) + assert('1999-04-11' == normalize_date("11 april 1999")) + assert('1999-04-11' == normalize_date("11 aprick 1999")) + assert('1999-04-11' == normalize_date("APR 11, 1999")) + assert('1999-04-11' == normalize_date("4/11/1999")) + assert('1999-04-11' == normalize_date("4-11-1999")) + assert('1999-04-11' == normalize_date("1999-4-11")) + assert('1999-04-11' == normalize_date("19990411")) + + assert('1999-01-01' == normalize_date("1 Jan 1999")) + assert('1999-02-01' == normalize_date("1 Feb 1999")) + assert('1999-03-01' == normalize_date("1 Mar 1999")) + assert('1999-04-01' == normalize_date("1 Apr 1999")) + assert('1999-05-01' == normalize_date("1 May 1999")) + assert('1999-06-01' == normalize_date("1 Jun 1999")) + assert('1999-07-01' == normalize_date("1 Jul 1999")) + assert('1999-08-01' == normalize_date("1 Aug 1999")) + assert('1999-09-01' == normalize_date("1 Sep 1999")) + assert('1999-10-01' == normalize_date("1 Oct 1999")) + assert('1999-11-01' == normalize_date("1 Nov 1999")) + assert('1999-12-01' == normalize_date("1 Dec 1999")) + + assert('1999-04-30' == normalize_date("1999-4-30")) + assert('2000-02-29' == normalize_date("29 FEB 2000")) + assert('2001-02-28' == normalize_date("28 FEB 2001")) + assert('2004-02-29' == normalize_date("29 FEB 2004")) + assert('2100-02-28' == normalize_date("28 FEB 2100")) + assert('1900-02-28' == normalize_date("28 FEB 1900")) + + def assertError(val): + try: + normalize_date(val) + except (TypeError,ValueError): + return + raise ValueError("type error expected", val) + + assertError("2000-13-11") + assertError("APR 99") + assertError("29 FEB 1900") + assertError("29 FEB 2100") + assertError("29 FEB 2001") + assertError("1999-4-31") + assertError("APR 99") + assertError("20301") + assertError("020301") + assertError("1APR99") + assertError("1APR01") + assertError("1 APR 99") + assertError("1 APR 01") + assertError("11/5/01") + diff --git a/tests/test_util/test_mimeparse.py b/tests/test_util/test_mimeparse.py new file mode 100644 index 0000000..9b9b675 --- /dev/null +++ b/tests/test_util/test_mimeparse.py @@ -0,0 +1,235 @@ +# (c) 2010 Ch. Zwerschke and contributors +# This module is part of the Python Paste Project and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php + +from paste.util.mimeparse import * + +def test_parse_mime_type(): + parse = parse_mime_type + assert parse('*/*') == ('*', '*', {}) + assert parse('text/html') == ('text', 'html', {}) + assert parse('audio/*; q=0.2') == ('audio', '*', {'q': '0.2'}) + assert parse('text/x-dvi;level=1') == ('text', 'x-dvi', {'level': '1'}) + assert parse('image/gif; level=2; q=0.4') == ( + 'image', 'gif', {'level': '2', 'q': '0.4'}) + assert parse('application/xhtml;level=3;q=0.5') == ( + 'application', 'xhtml', {'level': '3', 'q': '0.5'}) + assert parse('application/xml') == ('application', 'xml', {}) + assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'}) + assert parse('application/xml ; q=1;b=other') == ( + 'application', 'xml', {'q': '1', 'b': 'other'}) + assert parse('application/xml ; q=2;b=other') == ( + 'application', 'xml', {'q': '2', 'b': 'other'}) + assert parse('application/xhtml;q=0.5') == ( + 'application', 'xhtml', {'q': '0.5'}) + assert parse('application/xhtml;q=0.5;ver=1.2') == ( + 'application', 'xhtml', {'q': '0.5', 'ver': '1.2'}) + +def test_parse_illformed_mime_type(): + parse = parse_mime_type + assert parse('*') == ('*', '*', {}) + assert parse('text') == ('text', '*', {}) + assert parse('text/') == ('text', '*', {}) + assert parse('/plain') == ('*', 'plain', {}) + assert parse('/') == ('*', '*', {}) + assert parse('text/plain;') == ('text', 'plain', {}) + assert parse(';q=0.5') == ('*', '*', {'q': '0.5'}) + assert parse('*; q=.2') == ('*', '*', {'q': '.2'}) + assert parse('image; q=.7; level=3') == ( + 'image', '*', {'q': '.7', 'level': '3'}) + assert parse('*;q=1') == ('*', '*', {'q': '1'}) + assert parse('*;q=') == ('*', '*', {}) + assert parse('*;=0.5') == ('*', '*', {}) + assert parse('*;q=foobar') == ('*', '*', {'q': 'foobar'}) + assert parse('image/gif; level=2; q=2') == ( + 'image', 'gif', {'level': '2', 'q': '2'}) + assert parse('application/xml;q=') == ('application', 'xml', {}) + assert parse('application/xml ;q=') == ('application', 'xml', {}) + assert parse(' *; q =;') == ('*', '*', {}) + assert parse(' *; q=.2') == ('*', '*', {'q': '.2'}) + +def test_parse_media_range(): + parse = parse_media_range + assert parse('application/*;q=0.5') == ('application', '*', {'q': '0.5'}) + assert parse('text/plain') == ('text', 'plain', {'q': '1'}) + assert parse('*') == ('*', '*', {'q': '1'}) + assert parse(';q=0.5') == ('*', '*', {'q': '0.5'}) + assert parse('*;q=0.5') == ('*', '*', {'q': '0.5'}) + assert parse('*;q=1') == ('*', '*', {'q': '1'}) + assert parse('*;q=') == ('*', '*', {'q': '1'}) + assert parse('*;q=-1') == ('*', '*', {'q': '1'}) + assert parse('*;q=foobar') == ('*', '*', {'q': '1'}) + assert parse('*;q=0.0001') == ('*', '*', {'q': '0.0001'}) + assert parse('*;q=1000.0') == ('*', '*', {'q': '1'}) + assert parse('*;q=0') == ('*', '*', {'q': '0'}) + assert parse('*;q=0.0000') == ('*', '*', {'q': '0.0000'}) + assert parse('*;q=1.0001') == ('*', '*', {'q': '1'}) + assert parse('*;q=2') == ('*', '*', {'q': '1'}) + assert parse('*;q=1e3') == ('*', '*', {'q': '1'}) + assert parse('image/gif; level=2') == ( + 'image', 'gif', {'level': '2', 'q': '1'}) + assert parse('image/gif; level=2; q=0.5') == ( + 'image', 'gif', {'level': '2', 'q': '0.5'}) + assert parse('image/gif; level=2; q=2') == ( + 'image', 'gif', {'level': '2', 'q': '1'}) + assert parse('application/xml') == ('application', 'xml', {'q': '1'}) + assert parse('application/xml;q=1') == ('application', 'xml', {'q': '1'}) + assert parse('application/xml;q=') == ('application', 'xml', {'q': '1'}) + assert parse('application/xml ;q=') == ('application', 'xml', {'q': '1'}) + assert parse('application/xml ; q=1;b=other') == ( + 'application', 'xml', {'q': '1', 'b': 'other'}) + assert parse('application/xml ; q=2;b=other') == ( + 'application', 'xml', {'q': '1', 'b': 'other'}) + assert parse(' *; q =;') == ('*', '*', {'q': '1'}) + assert parse(' *; q=.2') == ('*', '*', {'q': '.2'}) + +def test_fitness_and_quality_parsed(): + faq = fitness_and_quality_parsed + assert faq('*/*;q=0.7', [ + ('foo', 'bar', {'q': '0.5'})]) == (0, 0.5) + assert faq('foo/*;q=0.7', [ + ('foo', 'bar', {'q': '0.5'})]) == (100, 0.5) + assert faq('*/bar;q=0.7', [ + ('foo', 'bar', {'q': '0.5'})]) == (10, 0.5) + assert faq('foo/bar;q=0.7', [ + ('foo', 'bar', {'q': '0.5'})]) == (110, 0.5) + assert faq('text/html;q=0.7', [ + ('foo', 'bar', {'q': '0.5'})]) == (-1, 0) + assert faq('text/html;q=0.7', [ + ('text', 'bar', {'q': '0.5'})]) == (-1, 0) + assert faq('text/html;q=0.7', [ + ('foo', 'html', {'q': '0.5'})]) == (-1, 0) + assert faq('text/html;q=0.7', [ + ('text', '*', {'q': '0.5'})]) == (100, 0.5) + assert faq('text/html;q=0.7', [ + ('*', 'html', {'q': '0.5'})]) == (10, 0.5) + assert faq('text/html;q=0.7', [ + ('*', '*', {'q': '0'}), ('text', 'html', {'q': '0.5'})]) == (110, 0.5) + assert faq('text/html;q=0.7', [ + ('*', '*', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (0, 0.5) + assert faq('audio/mp3;q=0.7', [ + ('*', '*', {'q': '0'}), ('audio', '*', {'q': '0.5'})]) == (100, 0.5) + assert faq('*/mp3;q=0.7', [ + ('foo', 'mp3', {'q': '0.5'}), ('audio', '*', {'q': '0'})]) == (10, 0.5) + assert faq('audio/mp3;q=0.7', [ + ('audio', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5) + assert faq('audio/mp3;q=0.7', [ + ('*', 'ogg', {'q': '0'}), ('*', 'mp3', {'q': '0.5'})]) == (10, 0.5) + assert faq('text/html;q=0.7', [ + ('text', 'plain', {'q': '0'}), + ('plain', 'html', {'q': '0'}), + ('text', 'html', {'q': '0.5'}), + ('html', 'text', {'q': '0'})]) == (110, 0.5) + assert faq('text/html;q=0.7;level=2', [ + ('plain', 'html', {'q': '0', 'level': '2'}), + ('text', '*', {'q': '0.5', 'level': '3'}), + ('*', 'html', {'q': '0.5', 'level': '2'}), + ('image', 'gif', {'q': '0.5', 'level': '2'})]) == (100, 0.5) + assert faq('text/html;q=0.7;level=2', [ + ('text', 'plain', {'q': '0'}), ('text', 'html', {'q': '0'}), + ('text', 'plain', {'q': '0', 'level': '2'}), + ('text', 'html', {'q': '0.5', 'level': '2'}), + ('*', '*', {'q': '0', 'level': '2'}), + ('text', 'html', {'q': '0', 'level': '3'})]) == (111, 0.5) + assert faq('text/html;q=0.7;level=2;opt=3', [ + ('text', 'html', {'q': '0'}), + ('text', 'html', {'q': '0', 'level': '2'}), + ('text', 'html', {'q': '0', 'opt': '3'}), + ('*', '*', {'q': '0', 'level': '2', 'opt': '3'}), + ('text', 'html', {'q': '0', 'level': '3', 'opt': '3'}), + ('text', 'html', {'q': '0.5', 'level': '2', 'opt': '3'}), + ('*', '*', {'q': '0', 'level': '3', 'opt': '3'})]) == (112, 0.5) + +def test_quality_parsed(): + qp = quality_parsed + assert qp('image/gif;q=0.7', [('image', 'jpg', {'q': '0.5'})]) == 0 + assert qp('image/gif;q=0.7', [('image', '*', {'q': '0.5'})]) == 0.5 + assert qp('audio/mp3;q=0.7;quality=100', [ + ('*', '*', {'q': '0', 'quality': '100'}), + ('audio', '*', {'q': '0', 'quality': '100'}), + ('*', 'mp3', {'q': '0', 'quality': '100'}), + ('audio', 'mp3', {'q': '0', 'quality': '50'}), + ('audio', 'mp3', {'q': '0.5', 'quality': '100'}), + ('audio', 'mp3', {'q': '0.5'})]) == 0.5 + +def test_quality(): + assert quality('text/html', + 'text/*;q=0.3, text/html;q=0.75, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75 + assert quality('text/html;level=2', + 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.4 + assert quality('text/plain', + 'text/*;q=0.25, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.25 + assert quality('plain/text', + 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5 + assert quality('text/html;level=1', + 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 1 + assert quality('image/jpeg', + 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.5 + assert quality('text/html;level=2', + 'text/*;q=0.3, text/html;q=0.7, text/html;level=1,' + ' text/html;level=2;q=0.375, */*;q=0.5') == 0.375 + assert quality('text/html;level=3', + 'text/*;q=0.3, text/html;q=0.75, text/html;level=1,' + ' text/html;level=2;q=0.4, */*;q=0.5') == 0.75 + +def test_best_match(): + bm = best_match + assert bm([], '*/*') == '' + assert bm(['application/xbel+xml', 'text/xml'], + 'text/*;q=0.5,*/*; q=0.1') == 'text/xml' + assert bm(['application/xbel+xml', 'audio/mp3'], + 'text/*;q=0.5,*/*; q=0.1') == 'application/xbel+xml' + assert bm(['application/xbel+xml', 'audio/mp3'], + 'text/*;q=0.5,*/mp3; q=0.1') == 'audio/mp3' + assert bm(['application/xbel+xml', 'text/plain', 'text/html'], + 'text/*;q=0.5,*/plain; q=0.1') == 'text/plain' + assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'], + 'text/*;q=0.1,*/xhtml; q=0.5') == 'text/html' + assert bm(['application/xbel+xml', 'text/html', 'text/xhtml'], + '*/html;q=0.1,*/xhtml; q=0.5') == 'text/xhtml' + assert bm(['application/xbel+xml', 'application/xml'], + 'application/xbel+xml') == 'application/xbel+xml' + assert bm(['application/xbel+xml', 'application/xml'], + 'application/xbel+xml; q=1') == 'application/xbel+xml' + assert bm(['application/xbel+xml', 'application/xml'], + 'application/xml; q=1') == 'application/xml' + assert bm(['application/xbel+xml', 'application/xml'], + 'application/*; q=1') == 'application/xbel+xml' + assert bm(['application/xbel+xml', 'application/xml'], + '*/*, application/xml') == 'application/xml' + assert bm(['application/xbel+xml', 'text/xml'], + 'text/*;q=0.5,*/*; q=0.1') == 'text/xml' + assert bm(['application/xbel+xml', 'text/xml'], + 'text/html,application/atom+xml; q=0.9') == '' + assert bm(['application/json', 'text/html'], + 'application/json, text/javascript, */*') == 'application/json' + assert bm(['application/json', 'text/html'], + 'application/json, text/html;q=0.9') == 'application/json' + assert bm(['image/*', 'application/xml'], 'image/png') == 'image/*' + assert bm(['image/*', 'application/xml'], 'image/*') == 'image/*' + +def test_illformed_best_match(): + bm = best_match + assert bm(['image/png', 'image/jpeg', 'image/gif', 'text/html'], + 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/jpeg' + assert bm(['image/png', 'image/jpg', 'image/tif', 'text/html'], + 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'text/html' + assert bm(['image/png', 'image/jpg', 'image/tif', 'audio/mp3'], + 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2') == 'image/png' + +def test_sorted_match(): + dm = desired_matches + assert dm(['text/html', 'application/xml'], + 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,' + 'text/plain;q=0.8,image/png') == ['text/html', 'application/xml'] + assert dm(['text/html', 'application/xml'], + 'application/xml,application/json') == ['application/xml'] + assert dm(['text/xhtml', 'text/plain', 'application/xhtml'], + 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,' + 'text/plain;q=0.8,image/png') == ['text/plain'] diff --git a/tests/test_util/test_quoting.py b/tests/test_util/test_quoting.py new file mode 100644 index 0000000..5f5e0a8 --- /dev/null +++ b/tests/test_util/test_quoting.py @@ -0,0 +1,28 @@ +from paste.util import quoting +import six +import unittest + +class TestQuoting(unittest.TestCase): + def test_html_unquote(self): + self.assertEqual(quoting.html_unquote(b'<hey you>'), + u'<hey\xa0you>') + self.assertEqual(quoting.html_unquote(b''), + u'') + self.assertEqual(quoting.html_unquote(b'&blahblah;'), + u'&blahblah;') + self.assertEqual(quoting.html_unquote(b'\xe1\x80\xa9'), + u'\u1029') + + def test_html_quote(self): + self.assertEqual(quoting.html_quote(1), + '1') + self.assertEqual(quoting.html_quote(None), + '') + self.assertEqual(quoting.html_quote('<hey!>'), + '<hey!>') + if six.PY3: + self.assertEqual(quoting.html_quote(u'<\u1029>'), + u'<\u1029>') + else: + self.assertEqual(quoting.html_quote(u'<\u1029>'), + '<\xe1\x80\xa9>') |