def test_product_categories(settings):
    with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=True):
        shop_product = get_default_shop_product()
        shop_product.categories.clear()
        shop_product.primary_category = None
        shop_product.save()

        assert not shop_product.primary_category
        assert not shop_product.categories.count()

        category_one = CategoryFactory()
        category_two = CategoryFactory()

        shop_product.categories = Category.objects.all()

        assert shop_product.primary_category  # this was automatically populated
        assert shop_product.primary_category.pk == category_one.pk  # it's the first one also

        shop_product.categories.clear()

        shop_product.primary_category = category_one
        shop_product.save()

        assert shop_product.primary_category == category_one
        assert category_one in shop_product.categories.all()

        # test removing
        shop_product.categories.remove(category_one)
        shop_product.refresh_from_db()
        assert not shop_product.categories.exists()

        shop_product.categories.add(category_one)
        category_one.soft_delete()
        assert not shop_product.categories.exists()

    with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=False):
        shop_product.categories.clear()
        shop_product.primary_category = None
        shop_product.save()

        assert not shop_product.primary_category
        assert not shop_product.categories.count()

        category_one = CategoryFactory()
        category_two = CategoryFactory()

        shop_product.categories = Category.objects.all()

        assert not shop_product.primary_category  # this was NOT automatically populated

        shop_product.categories.clear()

        shop_product.primary_category = category_one
        shop_product.save()

        assert shop_product.primary_category == category_one
        assert category_one not in shop_product.categories.all()
Example #2
0
def test_product_categories(settings):
    with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=True):
        shop_product = get_default_shop_product()
        shop_product.categories.clear()
        shop_product.primary_category = None
        shop_product.save()

        assert not shop_product.primary_category
        assert not shop_product.categories.count()

        category_one = CategoryFactory()
        category_two = CategoryFactory()

        shop_product.categories = Category.objects.all()

        assert shop_product.primary_category  # this was automatically populated
        assert shop_product.primary_category.pk == category_one.pk  # it's the first one also

        shop_product.categories.clear()

        shop_product.primary_category = category_one
        shop_product.save()

        assert shop_product.primary_category == category_one
        assert category_one in shop_product.categories.all()

        # test removing
        shop_product.categories.remove(category_one)
        shop_product.refresh_from_db()
        assert not shop_product.categories.exists()

        shop_product.categories.add(category_one)
        category_one.soft_delete()
        assert not shop_product.categories.exists()

    with override_settings(SHUUP_AUTO_SHOP_PRODUCT_CATEGORIES=False):
        shop_product.categories.clear()
        shop_product.primary_category = None
        shop_product.save()

        assert not shop_product.primary_category
        assert not shop_product.categories.count()

        category_one = CategoryFactory()
        category_two = CategoryFactory()

        shop_product.categories = Category.objects.all()

        assert not shop_product.primary_category  # this was NOT automatically populated

        shop_product.categories.clear()

        shop_product.primary_category = category_one
        shop_product.save()

        assert shop_product.primary_category == category_one
        assert category_one not in shop_product.categories.all()