<commit_before># -*- coding: utf-8 -*-

import unittest
from urllib import quote
from iktomi.web.reverse import URL
from iktomi.web.url_templates import UrlTemplate
from iktomi.web.url_converters import Converter, ConvertError


class URLTests(unittest.TestCase):

    def test_rendering_without_params(self):
        'Url without params'
        u = URL('/path/to/something')
        self.assertEqual(u, '/path/to/something')
        self.assert_(u in repr(u))

    def test_rendering_with_params(self):
        'Url with params'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=5')

    def test_param_set(self):
        'Set new param in url'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=5')
        u = u.qs_set(page=6)
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=6')
        u = u.qs_set(page=7, title='land')
        self.assertEqual(u, '/path/to/something?id=3&page=7&title=land')

    def test_param_delete(self):
        'Set new param in url'
        u = URL('/path/to/something', query=[('id', 3), ('page', 5), ('page', 6)])
        self.assertEqual(u, '/path/to/something?id=3&page=5&page=6')
        u = u.qs_delete('page')
        self.assertEqual(u, '/path/to/something?id=3')
        u = u.qs_delete('offset')
        self.assertEqual(u, '/path/to/something?id=3')

    def test_params_set_args(self):
        'Use multidict to set params in url'
        url = URL('/')
        self.assertEqual(url.qs_set(a=1, b=2), '/?a=1&b=2')
        url = url.qs_set([('a', '1'), ('a', '2'), ('b', '3')])
        self.assertEqual(url, '/?a=1&a=2&b=3')
        self.assertEqual(url.qs_set([('a', '1'), ('c', '2')]), '/?b=3&a=1&c=2')
        self.assertRaises(TypeError, url.qs_set, [('a', 1)], z=0)

    def test_param_add_args(self):
        'Add param to url'
        url = URL('/')
        self.assertEqual(url.qs_add([('a', 1), ('c', 3)], a=2, b=2), '/?a=1&c=3&a=2&b=2')

    def test_param_get(self):
        'Get param from url'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        page = u.qs_get('page')
        self.assertEqual(page, 5)
        u = u.qs_set(page=7)
        page = u.qs_get('page')
        self.assertEqual(page, 7)
        not_here = u.qs_get('not_here')
        self.assertEqual(not_here, None)

    def test_quote(self):
        u = URL(quote('/path/to/+'))
        self.assertEqual(u, '/path/to/%2B')
        u = u.qs_set(page=7)
        self.assertEqual(u, '/path/to/%2B?page=7')

    def test_iri(self):
        u = URL('/', host=u'example.com')
        self.assertEqual(u, u'http://example.com/')
        u = URL(u'/урл/', host=u'сайт.рф', query={'q': u'поиск'})
        self.assertEqual(u, u'http://xn--80aswg.xn--p1ai/%D1%83%D1%80%D0%BB/?q=%D0%BF%D0%BE%D0%B8%D1%81%D0%BA')

    def test_no_quote(self):
        u = URL(u'/урл/', host=u'сайт.рф', query={'q': u'поиск'})
        self.assertEqual(u.get_readable(), u'http://сайт.рф/урл/?q=поиск')

    def test_from_url(self):
        url = URL.from_url('http://example.com/url?a=1&b=2&b=3', show_host=False)
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, 'example.com')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/url')
        self.assertEqual(url.query.items(), [('a' ,'1'), ('b', '2'), ('b', '3')])
        self.assertEqual(url.show_host, False)

    def test_from_url_unicode(self):
        url = URL.from_url(u'http://сайт.рф/', show_host=False)
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, u'сайт.рф')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/')
        self.assertEqual(url.show_host, False)

    def test_from_url_path(self):
        url = URL.from_url('/url?a=1&b=2&b=3')
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, '')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/url')
        self.assertEqual(url.query.items(), [('a' ,'1'), ('b', '2'), ('b', '3')])

    def test_from_url_idna(self):
        url = URL.from_url('http://xn--80aswg.xn--p1ai/%D1%83%D1%80%D0%BB/?q=%D0%BF%D0%BE%D0%B8%D1%81%D0%BA')
        self.assertEqual(url.get_readable(),
                         u'http://сайт.рф/урл/?q=поиск')

    def test_from_url_broken_unicode(self):
        url = URL.from_url('/search?q=hello%E3%81')
        self.assertEqual(url.get_readable(),
                         u'/search?q=hello�')

    def test_cyrillic_path(self):
        url1 = URL.from_url('http://test.ru/тест') # encoded unicode
        url2 = URL.from_url(u'http://test.ru/тест') # decoded unicode
        # should work both without errors
        self.assertEqual(url1.path, '/%D1%82%D0%B5%D1%81%D1%82')
        self.assertEqual(url1.path, url2.path)

class UrlTemplateTest(unittest.TestCase):
    def test_match(self):
        'Simple match'
        t = UrlTemplate('/')
        self.assertEqual(t.match('/'), ('/', {}))<commit_msg>added failing test<commit_after># -*- coding: utf-8 -*-

import unittest
from urllib import quote
from iktomi.web.reverse import URL
from iktomi.web.url_templates import UrlTemplate
from iktomi.web.url_converters import Converter, ConvertError


class URLTests(unittest.TestCase):

    def test_rendering_without_params(self):
        'Url without params'
        u = URL('/path/to/something')
        self.assertEqual(u, '/path/to/something')
        self.assert_(u in repr(u))

    def test_rendering_with_params(self):
        'Url with params'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=5')

    def test_param_set(self):
        'Set new param in url'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=5')
        u = u.qs_set(page=6)
        self.assertEqual(u, '/path/to/something?title=title&id=3&page=6')
        u = u.qs_set(page=7, title='land')
        self.assertEqual(u, '/path/to/something?id=3&page=7&title=land')

    def test_param_delete(self):
        'Set new param in url'
        u = URL('/path/to/something', query=[('id', 3), ('page', 5), ('page', 6)])
        self.assertEqual(u, '/path/to/something?id=3&page=5&page=6')
        u = u.qs_delete('page')
        self.assertEqual(u, '/path/to/something?id=3')
        u = u.qs_delete('offset')
        self.assertEqual(u, '/path/to/something?id=3')

    def test_params_set_args(self):
        'Use multidict to set params in url'
        url = URL('/')
        self.assertEqual(url.qs_set(a=1, b=2), '/?a=1&b=2')
        url = url.qs_set([('a', '1'), ('a', '2'), ('b', '3')])
        self.assertEqual(url, '/?a=1&a=2&b=3')
        self.assertEqual(url.qs_set([('a', '1'), ('c', '2')]), '/?b=3&a=1&c=2')
        self.assertRaises(TypeError, url.qs_set, [('a', 1)], z=0)

    def test_param_add_args(self):
        'Add param to url'
        url = URL('/')
        self.assertEqual(url.qs_add([('a', 1), ('c', 3)], a=2, b=2), '/?a=1&c=3&a=2&b=2')

    def test_param_get(self):
        'Get param from url'
        u = URL('/path/to/something', query=dict(id=3, page=5, title='title'))
        page = u.qs_get('page')
        self.assertEqual(page, 5)
        u = u.qs_set(page=7)
        page = u.qs_get('page')
        self.assertEqual(page, 7)
        not_here = u.qs_get('not_here')
        self.assertEqual(not_here, None)

    def test_quote(self):
        u = URL(quote('/path/to/+'))
        self.assertEqual(u, '/path/to/%2B')
        u = u.qs_set(page=7)
        self.assertEqual(u, '/path/to/%2B?page=7')

    def test_iri(self):
        u = URL('/', host=u'example.com')
        self.assertEqual(u, u'http://example.com/')
        u = URL(u'/урл/', host=u'сайт.рф', query={'q': u'поиск'})
        self.assertEqual(u, u'http://xn--80aswg.xn--p1ai/%D1%83%D1%80%D0%BB/?q=%D0%BF%D0%BE%D0%B8%D1%81%D0%BA')

    def test_no_quote(self):
        u = URL(u'/урл/', host=u'сайт.рф', query={'q': u'поиск'})
        self.assertEqual(u.get_readable(), u'http://сайт.рф/урл/?q=поиск')

    def test_from_url(self):
        url = URL.from_url('http://example.com/url?a=1&b=2&b=3', show_host=False)
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, 'example.com')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/url')
        self.assertEqual(url.query.items(), [('a' ,'1'), ('b', '2'), ('b', '3')])
        self.assertEqual(url.show_host, False)

    def test_from_url_unicode(self):
        url = URL.from_url(u'http://сайт.рф/', show_host=False)
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, u'сайт.рф')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/')
        self.assertEqual(url.show_host, False)

    def test_from_url_path(self):
        url = URL.from_url('/url?a=1&b=2&b=3')
        self.assertEqual(url.schema, 'http')
        self.assertEqual(url.host, '')
        self.assertEqual(url.port, '')
        self.assertEqual(url.path, '/url')
        self.assertEqual(url.query.items(), [('a' ,'1'), ('b', '2'), ('b', '3')])

    def test_from_url_idna(self):
        url = URL.from_url(b'http://xn--80aswg.xn--p1ai/%D1%83%D1%80%D0%BB/?q=%D0%BF%D0%BE%D0%B8%D1%81%D0%BA')
        self.assertEqual(url.get_readable(),
                         u'http://сайт.рф/урл/?q=поиск')
        url = URL.from_url(u'http://xn--80aswg.xn--p1ai/%D1%83%D1%80%D0%BB/?q=%D0%BF%D0%BE%D0%B8%D1%81%D0%BA')
        self.assertEqual(url.get_readable(),
                         u'http://сайт.рф/урл/?q=поиск')

    def test_from_url_broken_unicode(self):
        url = URL.from_url('/search?q=hello%E3%81')
        self.assertEqual(url.get_readable(),
                         u'/search?q=hello�')

    def test_cyrillic_path(self):
        url1 = URL.from_url('http://test.ru/тест') # encoded unicode
        url2 = URL.from_url(u'http://test.ru/тест') # decoded unicode
        # should work both without errors
        self.assertEqual(url1.path, '/%D1%82%D0%B5%D1%81%D1%82')
        self.assertEqual(url1.path, url2.path)

class UrlTemplateTest(unittest.TestCase):
    def test_match(self):
        'Simple match'
        t = UrlTemplate('/')
        self.assertEqual(t.match('/'), ('/', {}))
{'fraction_non_alphanumeric': 0.19905078310393926, 'fraction_numerical': 0.02524916943521595, 'mean_word_length': 2.828488372093023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4355182', 'n_tokens_mistral': 4091, 'n_tokens_neox': 3968, 'n_words': 592}