Repository: fidals/shopelectro

Issue:
Wrong category in gm.yml price
This category should be excluded from gm.yml
https://www.shopelectro.ru/catalog/categories/usiliteli-zvuka-dlia-slaboslyshashchikh/

Bug from production


Discussion:

Resolving patch:
diff --git a/shopelectro/management/commands/price.py b/shopelectro/management/commands/price.py
--- a/shopelectro/management/commands/price.py
+++ b/shopelectro/management/commands/price.py
@@ -3,6 +3,7 @@
 
 from django.conf import settings
 from django.core.management.base import BaseCommand
+from django.db.models import Q
 from django.template.loader import render_to_string
 from django.urls import reverse
 
@@ -75,8 +76,10 @@ def put_crumbs(product):  # Ignore PyDocStyleBear
         def filter_categories(utm):
             categories_to_exclude = (
                 Category.objects
-                .filter(name__in=cls.IGNORED_CATEGORIES)
-                .filter(name__in=cls.IGNORED_CATEGORIES_BY_TARGET[utm])
+                .filter(
+                    Q(name__in=cls.IGNORED_CATEGORIES)
+                    | Q(name__in=cls.IGNORED_CATEGORIES_BY_TARGET[utm])
+                )
                 .get_descendants(include_self=True)
             )
 


Test patch:
diff --git a/shopelectro/tests/tests_commands.py b/shopelectro/tests/tests_commands.py
--- a/shopelectro/tests/tests_commands.py
+++ b/shopelectro/tests/tests_commands.py
@@ -134,6 +134,7 @@ def test_update_or_create_tags(self):
 class GeneratePrices(TestCase):
 
     fixtures = ['dump.json']
+    CATEGORY_TO_EXCLUDE = 'Category #1 of #Category #0 of #Category #1'
 
     @classmethod
     def setUpTestData(cls):
@@ -147,13 +148,13 @@ def tearDownClass(cls):
         super(GeneratePrices, cls).tearDownClass()
 
     @classmethod
-    def call_command_patched(self, name):
+    def call_command_patched(cls, name):
         with mock.patch(
             'shopelectro.management.commands.price.Command.IGNORED_CATEGORIES_BY_TARGET',
             new_callable=mock.PropertyMock
         ) as target:
             target.return_value = defaultdict(list, {
-                'GM': ['Category #1 of #Category #0 of #Category #1']
+                'GM': [cls.CATEGORY_TO_EXCLUDE]
             })
             call_command(name)
 
@@ -199,18 +200,27 @@ def test_categories_in_yandex_price(self):
         )
 
     def test_categories_excluded_by_utm(self):
+        """Price file should not contain it's excluded category."""
         def find_category(categories, name):
             for category in categories:
-                if category.text == name:
+                if category.text.strip() == name:
                     return category
             return None
-        excluded_name = 'Category #1 of #Category #0 of #Category #1'
+        included_name = 'Category #0 of #Category #0 of #Category #1'
         categories_node = self.get_price_categories_node('gm.yml')
 
-        self.assertFalse(
+        # check if find_category inner function is correct
+        self.assertIsNotNone(
             find_category(
                 categories=categories_node.findall('category'),
-                name=excluded_name
+                name=included_name
+            )
+        )
+        # check if category excluded
+        self.assertIsNone(
+            find_category(
+                categories=categories_node.findall('category'),
+                name=self.CATEGORY_TO_EXCLUDE
             )
         )