Repository: fidals/shopelectro

Issue:
Reuse refarm's get_category_path helper in tests
From https://github.com/fidals/stroyprombeton/issues/344

It's required to remove code doubling


Discussion:
`get_category_path` function is not suitable for SE, because the function doesn't handle neither slugs as identifiers nor `offset` url kwargs, but these are required for SE.

Redesign of the function probably will produce terrible interface. I believe we shouldn't reuse the `get_category_path`. @duker33 , what do you think?
@artemiy312 , we should not `slug` instead of `id` in stb urls.
With this approach we can unify urls and this function too.

Offset is not big problem, i think. It'll have some default value, that does not affect urls

Resolving patch:
diff --git a/shopelectro/templatetags/se_extras.py b/shopelectro/templatetags/se_extras.py
--- a/shopelectro/templatetags/se_extras.py
+++ b/shopelectro/templatetags/se_extras.py
@@ -123,8 +123,3 @@ def icon_stars(rating=0):
         'half_icons': range(half_icons),
         'empty_icons': range(empty_icons),
     }
-
-
-@register.filter
-def get_item(dictionary: dict, key: str):
-    return dictionary.get(key)
diff --git a/shopelectro/urls.py b/shopelectro/urls.py
--- a/shopelectro/urls.py
+++ b/shopelectro/urls.py
@@ -58,10 +58,10 @@ def cache_page(arg):  # Ignore PyFlakesBear
     url(r'^categories/(?P<slug>[\w-]+)/(?P<sorting>[0-9]*)/tags/(?P<tags>[\w_-]+)/$',
         views.CategoryPage.as_view(), name='category'),
     # "load more" group
-    url(r'categories/(?P<category_slug>[\w-]+)/load-more/'
+    url(r'categories/(?P<slug>[\w-]+)/load-more/'
         r'(?P<offset>[0-9]+)/(?P<sorting>[0-9]*)/$',
         views.load_more, name='load_more'),
-    url(r'categories/(?P<category_slug>[\w-]+)/load-more/'
+    url(r'categories/(?P<slug>[\w-]+)/load-more/'
         r'(?P<offset>[0-9]+)/(?P<sorting>[0-9]*)/tags/(?P<tags>[\w_-]+)/$',
         views.load_more, name='load_more'),
     # rest of urls
diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py
--- a/shopelectro/views/catalog.py
+++ b/shopelectro/views/catalog.py
@@ -192,13 +192,13 @@ def get_context_data(self, **kwargs):
         }
 
 
-def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None):
+def load_more(request, slug, offset=0, limit=0, sorting=0, tags=None):
     """
     Load more products of a given category.
 
     :param sorting: preferred sorting index from CATEGORY_SORTING tuple
     :param request: HttpRequest object
-    :param category_slug: Slug for a given category
+    :param slug: Slug for a given category
     :param offset: used for slicing QuerySet.
     :return: products list in html format
     """
@@ -217,7 +217,7 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None):
     # 11 // 12 = 0, 0 // 12 = 0 but it should be the first page
     # 12 // 12 = 1, 23 // 12 = 1, but it should be the second page
     page_number = (offset // products_on_page) + 1
-    category = get_object_or_404(models.CategoryPage, slug=category_slug).model
+    category = get_object_or_404(models.CategoryPage, slug=slug).model
     sorting_option = context.SortingOption(index=int(sorting))
 
     all_products = (


Test patch:
diff --git a/shopelectro/tests/tests_views.py b/shopelectro/tests/tests_views.py
--- a/shopelectro/tests/tests_views.py
+++ b/shopelectro/tests/tests_views.py
@@ -10,7 +10,7 @@
 from itertools import chain
 from operator import attrgetter
 from xml.etree import ElementTree as ET
-from urllib.parse import urlencode, urlparse, quote
+from urllib.parse import urlparse, quote
 
 from bs4 import BeautifulSoup
 from django.conf import settings
@@ -20,6 +20,8 @@
 from django.urls import reverse
 from django.utils.translation import ugettext as _
 
+from catalog.helpers import reverse_catalog_url
+
 from shopelectro import models
 from shopelectro.views.service import generate_md5_for_ya_kassa, YANDEX_REQUEST_PARAM
 from shopelectro.tests.helpers import create_doubled_tag
@@ -28,25 +30,6 @@
 CANONICAL_HTML_TAG = '<link rel="canonical" href="{path}">'
 
 
-def reverse_catalog_url(
-    route: str,
-    route_kwargs: dict,
-    tags: models.TagQuerySet=None,
-    sorting: int=None,
-    query_string: dict=None,
-) -> str:
-    query_string = f'?{urlencode(query_string)}' if query_string else ''
-    if tags:
-        # PyCharm's option:
-        # noinspection PyTypeChecker
-        tags_slug = tags.as_url()
-        route_kwargs['tags'] = tags_slug
-    if sorting is not None:
-        route_kwargs['sorting'] = sorting
-
-    return f'{reverse(route, kwargs=route_kwargs)}{query_string}'
-
-
 def get_page_number(response):
     return response.context['paginated_page'].number
 
@@ -70,10 +53,17 @@ def get_category_page(
         sorting: int=None,
         query_string: dict=None,
         route='category',
+        route_kwargs: dict=None,
     ):
+        route_kwargs = route_kwargs or {}
         category = category or self.category
+        route_kwargs = {
+            'slug': category.page.slug,
+            **route_kwargs
+        }
+
         return self.client.get(reverse_catalog_url(
-            route, {'slug': category.page.slug}, tags, sorting, query_string,
+            route, route_kwargs, tags, sorting, query_string,
         ))
 
 
@@ -307,17 +297,12 @@ def test_pagination_canonical(self):
 
 
 @tag('fast')
-class LoadMore(TestCase):
+class LoadMore(BaseCatalogTestCase):
 
     fixtures = ['dump.json']
     DEFAULT_LIMIT = 48
     PRODUCT_ID_WITH_IMAGE = 114
 
-    def setUp(self):
-        self.category = models.Category.objects.root_nodes().select_related('page').first()
-
-    # @todo #645:15m Reuse get_category_page method in LoadMore.load_more
-    #  BaseCatalogTestCase.get_category_page.
     def load_more(
         self,
         category: models.Category=None,
@@ -330,14 +315,18 @@ def load_more(
     ) -> HttpResponse:
         category = category or self.category
         route_kwargs = {
-            'category_slug': category.page.slug,
             'offset': offset,
             # uncomment after implementation urls for load_more with pagination
             # 'limit': limit,
         }
-        return self.client.get(reverse_catalog_url(
-            'load_more', route_kwargs, tags, sorting, query_string,
-        ))
+        return self.get_category_page(
+            category=category,
+            tags=tags,
+            sorting=sorting,
+            query_string=query_string,
+            route='load_more',
+            route_kwargs=route_kwargs
+        )
 
     def get_load_more_soup(self, *args, **kwargs) -> BeautifulSoup:
         """Use interface of `self.load_more` method."""