changeset 8670:bf2286a3fc7e

tests: simplify test_lib mocking of routing.url Mock internals of the url generator to avoid import tricks.
author Mads Kiilerich <mads@kiilerich.com>
date Sun, 11 Oct 2020 17:10:38 +0200
parents 07cb7b42057e
children 9685f50a69d0
files kallithea/lib/helpers.py kallithea/tests/other/test_libs.py
diffstat 2 files changed, 15 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Sat Oct 10 20:33:27 2020 +0200
+++ b/kallithea/lib/helpers.py	Sun Oct 11 17:10:38 2020 +0200
@@ -985,18 +985,15 @@
     if email_address == _def:
         return default
 
-    # re-import url so tests can mock it
-    from kallithea.config.routing import url
     from kallithea.model.db import User
 
     parsed_url = urllib.parse.urlparse(url.current(qualified=True))
-    url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
+    return (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \
                .replace('{email}', email_address) \
                .replace('{md5email}', hashlib.md5(safe_bytes(email_address).lower()).hexdigest()) \
                .replace('{netloc}', parsed_url.netloc) \
                .replace('{scheme}', parsed_url.scheme) \
                .replace('{size}', str(size))
-    return url
 
 
 def changed_tooltip(nodes):
@@ -1125,15 +1122,14 @@
     """
 
     def _replace(match_obj):
-        url = match_obj.group('url')
-        if url is not None:
-            return '<a href="%(url)s">%(url)s</a>' % {'url': url}
+        match_url = match_obj.group('url')
+        if match_url is not None:
+            return '<a href="%(url)s">%(url)s</a>' % {'url': match_url}
         mention = match_obj.group('mention')
         if mention is not None:
             return '<b>%s</b>' % mention
         hash_ = match_obj.group('hash')
         if hash_ is not None and repo_name is not None:
-            from kallithea.config.routing import url  # doh, we need to re-import url to mock it later
             return '<a class="changeset_hash" href="%(url)s">%(hash)s</a>' % {
                  'url': url('changeset_home', repo_name=repo_name, revision=hash_),
                  'hash': hash_,
--- a/kallithea/tests/other/test_libs.py	Sat Oct 10 20:33:27 2020 +0200
+++ b/kallithea/tests/other/test_libs.py	Sun Oct 11 17:10:38 2020 +0200
@@ -71,26 +71,6 @@
 ]
 
 
-class FakeUrlGenerator(object):
-
-    def __init__(self, current_url=None, default_route=None, **routes):
-        """Initialize using specified 'current' URL template,
-        default route template, and all other aguments describing known
-        routes (format: route=template)"""
-        self.current_url = current_url
-        self.default_route = default_route
-        self.routes = routes
-
-    def __call__(self, route_name, *args, **kwargs):
-        if route_name in self.routes:
-            return self.routes[route_name] % kwargs
-
-        return self.default_route % kwargs
-
-    def current(self, *args, **kwargs):
-        return self.current_url % kwargs
-
-
 class TestLibs(base.TestController):
 
     @base.parametrize('test_url,expected,expected_creds', TEST_URLS)
@@ -239,8 +219,7 @@
 
             return _c
 
-        fake_url = FakeUrlGenerator(current_url='https://example.com')
-        with mock.patch('kallithea.config.routing.url', fake_url):
+        with mock.patch('kallithea.config.routing.url.current', lambda *a, **b: 'https://example.com'):
             fake = fake_tmpl_context(_url='http://example.com/{email}')
             with mock.patch('tg.tmpl_context', fake):
                     from kallithea.config.routing import url
@@ -338,8 +317,10 @@
     ])
     def test_urlify_text(self, sample, expected):
         expected = self._quick_url(expected)
-        fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
-        with mock.patch('kallithea.config.routing.url', fake_url):
+
+        with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+            lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+        ):
             from kallithea.lib.helpers import urlify_text
             assert urlify_text(sample, 'repo_name') == expected
 
@@ -393,8 +374,9 @@
     def test_urlify_test(self, sample, expected, url_):
         expected = self._quick_url(expected,
                                    tmpl="""<a href="%s">%s</a>""", url_=url_)
-        fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
-        with mock.patch('kallithea.config.routing.url', fake_url):
+        with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+            lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+        ):
             from kallithea.lib.helpers import urlify_text
             assert urlify_text(sample, 'repo_name', stylize=True) == expected
 
@@ -406,8 +388,9 @@
        """<a class="message-link" href="#the-link"> yo</a>"""),
     ])
     def test_urlify_link(self, sample, expected):
-        fake_url = FakeUrlGenerator(changeset_home='/%(repo_name)s/changeset/%(revision)s')
-        with mock.patch('kallithea.config.routing.url', fake_url):
+        with mock.patch('kallithea.config.routing.UrlGenerator.__call__',
+            lambda self, name, **kwargs: dict(changeset_home='/%(repo_name)s/changeset/%(revision)s')[name] % kwargs,
+        ):
             from kallithea.lib.helpers import urlify_text
             assert urlify_text(sample, 'repo_name', link_='#the-link') == expected