def test_product_leaf_category(authorized_client, product_in_stock, default_category: Category): root_category = Category.objects.create(name='root', slug='root') default_category.parent = root_category default_category.save() def _get_products(_category): _url = reverse('product:category', kwargs={ 'path': _category.get_full_path(), 'category_id': _category.pk }) _resp = authorized_client.get(_url) assert _resp.status_code == 200 return _resp # non-leaf category (no products must be returned) response = _get_products(root_category) assert 'filter_set' not in response.context # leaf category (products must be returned) products = models.Product.objects.all().filter( category__name=default_category).order_by('-price') response = _get_products(default_category) assert list(products) == list(response.context['filter_set'].qs)
def test_category_formatter(db): main_category = Category(name="Main", slug="main") main_category.save() main_category_item = Mock(product=Mock(category=main_category)) sub_category = Category(name="Sub", slug="sub", parent=main_category) sub_category.save() sub_category_item = Mock(product=Mock(category=sub_category)) assert item_google_product_category(main_category_item, {}) == "Main" assert item_google_product_category(sub_category_item, {}) == "Main > Sub"
def test_category_formatter(db): main_category = Category(name='Main', slug='main') main_category.save() main_category_item = Mock(product=Mock(category=main_category)) sub_category = Category(name='Sub', slug='sub', parent=main_category) sub_category.save() sub_category_item = Mock(product=Mock(category=sub_category)) assert item_google_product_category(main_category_item, {}) == 'Main' assert item_google_product_category(sub_category_item, {}) == 'Main > Sub'
def get_linked_object_url(menu_item): if menu_item.category: return Category( **get_linked_object_kwargs(menu_item.category)).get_absolute_url() elif menu_item.collection: return Collection(**get_linked_object_kwargs( menu_item.collection)).get_absolute_url() elif menu_item.page: return Page( **get_linked_object_kwargs(menu_item.page)).get_absolute_url() return None
def test_validate_slug_and_generate_if_needed_generate_slug(cleaned_input): category = Category(name="test") validate_slug_and_generate_if_needed(category, "name", cleaned_input)