Exemplo n.º 1
0
def test_global_configurations():
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_global_configurations',
                }
            }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(None, "key1", {"data": "test1"})
        configuration.set(shop, "key2", {"data": "test2"})

        # key1 from shop should come from global configuration
        assert configuration.get(shop, "key1").get("data") == "test1"
        # key2 shouldn't be in global configurations
        assert configuration.get(None, "key2") is None

        # Update global configuration
        configuration.set(None, "key1", {"data": "test_bump"})
        assert configuration.get(shop, "key1").get("data") == "test_bump"

        # Override shop data for global key1
        configuration.set(shop, "key1", "test_data")
        assert configuration.get(shop, "key1") == "test_data"

        # Update shop configuration for global key1
        configuration.set(shop, "key1", "test_data1")
        assert configuration.get(shop, "key1") == "test_data1"
Exemplo n.º 2
0
def test_simple_set_and_get_cascading():
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_simple_set_and_get_cascading',
                }
            }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(None, "answer", 42)
        assert configuration.get(None, "answer") == 42
        assert configuration.get(shop, "answer", 42)

        assert configuration.get(None, "non-existing") is None
        assert configuration.get(shop, "non-existing") is None
        configuration.set(shop, "non-existing", "hello")
        assert configuration.get(None, "non-existing") is None
        assert configuration.get(shop, "non-existing") == "hello"

        assert configuration.get(None, "foo") is None
        assert configuration.get(shop, "foo") is None
        configuration.set(None, "foo", "bar")
        configuration.set(shop, "foo", "baz")
        assert configuration.get(None, "foo") == "bar"
        assert configuration.get(shop, "foo") == "baz"
Exemplo n.º 3
0
def test_global_configurations():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_global_configurations',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(None, "key1", {"data": "test1"})
        configuration.set(shop, "key2", {"data": "test2"})

        # key1 from shop should come from global configuration
        assert configuration.get(shop, "key1").get("data") == "test1"
        # key2 shouldn't be in global configurations
        assert configuration.get(None, "key2") is None

        # Update global configuration
        configuration.set(None, "key1", {"data": "test_bump"})
        assert configuration.get(shop, "key1").get("data") == "test_bump"

        # Override shop data for global key1
        configuration.set(shop, "key1", "test_data")
        assert configuration.get(shop, "key1") == "test_data"

        # Update shop configuration for global key1
        configuration.set(shop, "key1", "test_data1")
        assert configuration.get(shop, "key1") == "test_data1"
Exemplo n.º 4
0
def test_simple_set_and_get_cascading():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_simple_set_and_get_cascading',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(None, "answer", 42)
        assert configuration.get(None, "answer") == 42
        assert configuration.get(shop, "answer", 42)

        assert configuration.get(None, "non-existing") is None
        assert configuration.get(shop, "non-existing") is None
        configuration.set(shop, "non-existing", "hello")
        assert configuration.get(None, "non-existing") is None
        assert configuration.get(shop, "non-existing") == "hello"

        assert configuration.get(None, "foo") is None
        assert configuration.get(shop, "foo") is None
        configuration.set(None, "foo", "bar")
        configuration.set(shop, "foo", "baz")
        assert configuration.get(None, "foo") == "bar"
        assert configuration.get(shop, "foo") == "baz"
Exemplo n.º 5
0
def test_alert_limit_notification(rf, admin_user):
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_configuration_cache',
                }
            }):
        cache.init_cache()

        supplier = get_simple_supplier()
        shop = get_default_shop()
        product = create_product("simple-test-product", shop, supplier)
        product.stock_behavior = StockBehavior.STOCKED
        product.save()

        sc = StockCount.objects.get(supplier=supplier, product=product)
        sc.alert_limit = 10
        sc.save()

        # nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        # put 11 units in stock
        supplier.adjust_stock(product.pk, +11)

        # still nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False

        # stock should be 6, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)
        last_run = cache.get(cache_key)
        assert last_run is not None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is True

        # stock should be 1, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)

        # last run should be updated
        assert cache.get(cache_key) != last_run

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is True

        # fake we have a cache with more than 24hrs
        cache.set(cache_key, time() - (24 * 60 * 60 * 2))

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False
Exemplo n.º 6
0
def test_xtheme_extra_view_exceptions(rf):
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()
        with override_current_theme_class(H2G2Theme, get_default_shop()):
            request = rf.get("/")
            request.shop = get_default_shop()
            assert extra_view_dispatch(request, "vogons").status_code == 404
            with pytest.raises(ImproperlyConfigured):
                assert extra_view_dispatch(request, "true")
Exemplo n.º 7
0
def test_theme_with_default_template_dir():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()
        get_default_shop()
        with override_current_theme_class(ShuupTestingThemeWithCustomBase, get_default_shop()):
            c = SmartClient()
            soup = c.soup(reverse("shuup:index"))
            assert "Simple base for themes to use" in soup.find("h1").text
            assert "Welcome to test Shuup!" in soup.find("h1").text
Exemplo n.º 8
0
def test_theme_activation():
    cache.init_cache()
    shop = get_default_shop()

    with override_provides("xtheme", [
        "shuup_tests.xtheme.utils:FauxTheme",
        "shuup_tests.xtheme.utils:FauxTheme2"
    ]):
        set_current_theme(FauxTheme.identifier, shop)
        assert isinstance(get_current_theme(shop), FauxTheme)
        set_current_theme(FauxTheme2.identifier, shop)
        assert isinstance(get_current_theme(shop), FauxTheme2)
        with pytest.raises(ValueError):
            set_current_theme(printable_gibberish(), shop)
Exemplo n.º 9
0
def test_simple_set_and_get_without_shop():
    with override_settings(
            CACHES={
                "default": {
                    "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
                    "LOCATION": "test_simple_set_and_get_without_shop",
                }
            }):
        cache.init_cache()
        configuration.set(None, "answer", 42)
        assert configuration.get(None, "answer") == 42

        assert configuration.get(None, "non-existing") is None
        configuration.set(None, "non-existing", "hello")
        assert configuration.get(None, "non-existing") == "hello"
Exemplo n.º 10
0
def test_xtheme_extra_view_exceptions(rf):
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_configuration_cache',
                }
            }):
        cache.init_cache()
        with override_current_theme_class(H2G2Theme, get_default_shop()):
            request = rf.get("/")
            request.shop = get_default_shop()
            assert extra_view_dispatch(request, "vogons").status_code == 404
            with pytest.raises(ImproperlyConfigured):
                assert extra_view_dispatch(request, "true")
Exemplo n.º 11
0
def test_configuration_set_and_get():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_set_and_get',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        test_conf_data = {"data": "test"}
        configuration.set(shop, "key", test_conf_data)

        # Get the configuration via configuration API
        assert configuration.get(shop, "key") == test_conf_data
        # Check that configuration is saved to database
        assert ConfigurationItem.objects.get(shop=shop, key="key").value == test_conf_data
Exemplo n.º 12
0
def test_configuration_set_and_get():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_set_and_get',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        test_conf_data = {"data": "test"}
        configuration.set(shop, "key", test_conf_data)

        # Get the configuration via configuration API
        assert configuration.get(shop, "key") == test_conf_data
        # Check that configuration is saved to database
        assert ConfigurationItem.objects.get(shop=shop, key="key").value == test_conf_data
Exemplo n.º 13
0
def test_configuration_gets_saved():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_gets_saved',
        }
    }):
        cache.init_cache()
        configuration.set(None, "x", 1)
        assert configuration.get(None, "x") == 1
        configuration.set(None, "x", 2)
        assert configuration.get(None, "x") == 2
        configuration.set(None, "x", 3)
        assert configuration.get(None, "x") == 3
        conf_item = ConfigurationItem.objects.get(shop=None, key="x")
        assert conf_item.value == 3
Exemplo n.º 14
0
def test_configuration_gets_saved():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_gets_saved',
        }
    }):
        cache.init_cache()
        configuration.set(None, "x", 1)
        assert configuration.get(None, "x") == 1
        configuration.set(None, "x", 2)
        assert configuration.get(None, "x") == 2
        configuration.set(None, "x", 3)
        assert configuration.get(None, "x") == 3
        conf_item = ConfigurationItem.objects.get(shop=None, key="x")
        assert conf_item.value == 3
Exemplo n.º 15
0
def test_cache_api():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()

        key = "test_prefix:123"
        value = "456"
        cache.set(key, value)
        assert cache.get(key) == value
        cache.bump_version(key)
        assert cache.get(key, default="derp") == "derp"  # version was bumped, so no way this is there
        cache.set(key, value)
        assert cache.get(key) == value
Exemplo n.º 16
0
def test_theme_settings_api():
    cache.init_cache()
    shop = get_default_shop()
    with override_provides("xtheme", [
        "shuup_tests.xtheme.utils:FauxTheme",
        "shuup_tests.xtheme.utils:FauxTheme2"
    ]):
        ThemeSettings.objects.all().delete()
        theme = get_theme_by_identifier(FauxTheme2.identifier, shop)
        theme.set_setting("foo", "bar")
        theme.set_settings(quux=[4, 8, 15, 16, 23, 42])
        theme = get_theme_by_identifier(FauxTheme2.identifier, shop)
        assert theme.get_setting("foo") == "bar"
        assert theme.get_settings() == {
            "foo": "bar",
            "quux": [4, 8, 15, 16, 23, 42]
        }
Exemplo n.º 17
0
def test_cache_api():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()

        key = "test_prefix:123"
        value = "456"
        cache.set(key, value)
        assert cache.get(key) == value
        cache.bump_version(key)
        assert cache.get(key, default="derp") == "derp"  # version was bumped, so no way this is there
        cache.set(key, value)
        assert cache.get(key) == value
Exemplo n.º 18
0
def test_configuration_cache():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()

        shop = get_default_shop()
        configuration.set(None, "key1", "test1")
        configuration.set(shop, "key2", "test2")

        # Shop configurations cache should be bumped
        assert cache.get(configuration._get_cache_key(shop)) is None
        configuration.get(shop, "key1")
        # Now shop configurations and key2 should found from cache
        assert cache.get(configuration._get_cache_key(shop)).get("key2") == "test2"
Exemplo n.º 19
0
def test_configuration_update():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_update',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(shop, "key1", {"data": "test1"})
        configuration.set(shop, "key2", {"data": "test2"})
        configuration.set(shop, "key3", {"data": "test3"})
        assert configuration.get(shop, "key1").get("data") == "test1"
        assert configuration.get(shop, "key3").get("data") == "test3"

        # Update configuration
        configuration.set(shop, "key3", {"data": "test_bump"})
        assert configuration.get(shop, "key3").get("data") == "test_bump"
Exemplo n.º 20
0
def test_configuration_cache():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()

        shop = get_default_shop()
        configuration.set(None, "key1", "test1")
        configuration.set(shop, "key2", "test2")

        # Shop configurations cache should be bumped
        assert cache.get(configuration._get_cache_key(shop)) is None
        configuration.get(shop, "key1")
        # Now shop configurations and key2 should found from cache
        assert cache.get(configuration._get_cache_key(shop)).get("key2") == "test2"
Exemplo n.º 21
0
def test_configuration_update():
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_update',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()
        configuration.set(shop, "key1", {"data": "test1"})
        configuration.set(shop, "key2", {"data": "test2"})
        configuration.set(shop, "key3", {"data": "test3"})
        assert configuration.get(shop, "key1").get("data") == "test1"
        assert configuration.get(shop, "key3").get("data") == "test3"

        # Update configuration
        configuration.set(shop, "key3", {"data": "test_bump"})
        assert configuration.get(shop, "key3").get("data") == "test_bump"
Exemplo n.º 22
0
def test_theme_without_default_template_dir():
    with override_settings(
            CACHES={
                "default": {
                    "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
                    "LOCATION": "test_configuration_cache",
                }
            }):
        cache.init_cache()

        with override_current_theme_class(ShuupTestingTheme,
                                          get_default_shop()):
            c = SmartClient()
            soup = c.soup(reverse("shuup:index"))
            assert "Simple base for themes to use" not in soup
            assert "Welcome to test Shuup!" in soup.find(
                "div", {
                    "class": "page-content"
                }).text
Exemplo n.º 23
0
def test_xtheme_extra_views(rf):
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()
        with override_current_theme_class(H2G2Theme, get_default_shop()):
            request = rf.get("/", {"name": "Arthur Dent"})
            request.shop = get_default_shop()
            # Simulate /xtheme/greeting
            response = extra_view_dispatch(request, "greeting")
            assert force_text(response.content) == "So long, and thanks for all the fish, Arthur Dent"
            # Try that again (to exercise the _VIEW_CACHE code path):
            response = extra_view_dispatch(request, "greeting")
            assert force_text(response.content) == "So long, and thanks for all the fish, Arthur Dent"
            # Now test that CBVs work
            assert not extra_view_dispatch(request, "faux").content
def test_classic_gray_theme_settings(admin_user):
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_configuration_cache',
                }
            }):
        cache.init_cache()
        shop = get_default_shop()

        with override_provides(
                "xtheme",
            ["shuup.themes.classic_gray.theme:ClassicGrayTheme"]):
            set_current_theme(ClassicGrayTheme.identifier, shop)
            theme = _get_current_theme(shop)
            assert isinstance(theme, ClassicGrayTheme)
            ThemeSettings.objects.all().delete()

            client = Client()
            admin_user.set_password("admin")
            admin_user.save()
            client.login(username=admin_user.username, password="******")

            theme_config_url = reverse(
                "shuup_admin:xtheme.config_detail",
                kwargs=dict(theme_identifier=ClassicGrayTheme.identifier))
            response = client.get(theme_config_url)
            assert response.status_code == 200

            assert theme.get_setting("shop_logo_width") is None
            assert theme.get_setting("shop_logo_height") is None
            assert theme.get_setting("shop_logo_alignment") is None
            assert theme.get_setting("shop_logo_aspect_ratio") is None

            settings = {"stylesheet": "shuup/classic_gray/blue/style.css"}
            response = client.post(theme_config_url, data=settings)
            assert response.status_code == 302

            theme = _get_current_theme(shop)
            for key, value in settings.items():
                assert theme.get_setting(key) == value
def test_classic_gray_theme_settings(admin_user):
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()
        shop = get_default_shop()

        with override_provides("xtheme", ["shuup.themes.classic_gray.theme:ClassicGrayTheme"]):
            set_current_theme(ClassicGrayTheme.identifier, shop)
            theme = _get_current_theme(shop)
            assert isinstance(theme, ClassicGrayTheme)
            ThemeSettings.objects.all().delete()

            client = Client()
            admin_user.set_password("admin")
            admin_user.save()
            client.login(username=admin_user.username, password="******")

            theme_config_url = reverse("shuup_admin:xtheme.config_detail", kwargs=dict(theme_identifier=ClassicGrayTheme.identifier))
            response = client.get(theme_config_url)
            assert response.status_code == 200

            assert theme.get_setting("shop_logo_width") is None
            assert theme.get_setting("shop_logo_height") is None
            assert theme.get_setting("shop_logo_alignment") is None
            assert theme.get_setting("shop_logo_aspect_ratio") is None

            settings = {
                "stylesheet": "shuup/classic_gray/blue/style.css"
            }
            response = client.post(theme_config_url, data=settings)
            assert response.status_code == 302

            theme = _get_current_theme(shop)
            for key, value in settings.items():
                assert theme.get_setting(key) == value
Exemplo n.º 26
0
def test_xtheme_wizard_pane(rf, admin_user):
    with override_settings(
        SHUUP_SETUP_WIZARD_PANE_SPEC=["shuup.xtheme.admin_module.views.ThemeWizardPane"],
        CACHES={
            "default": {
                "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
                "LOCATION": "test_simple_set_and_get_with_shop",
            }
        },
    ):
        cache.init_cache()
        shop = get_default_shop()
        with override_provides("xtheme", ["shuup_tests.xtheme.utils:FauxTheme"]):
            from shuup_tests.xtheme.utils import FauxTheme

            assert get_current_theme(shop) is None
            fields = _extract_fields(rf, admin_user)
            fields["theme-activate"] = FauxTheme.identifier
            request = apply_request_middleware(rf.post("/", data=fields), user=admin_user)
            response = WizardView.as_view()(request)
            assert isinstance(get_current_theme(shop), FauxTheme)
            assert_redirect_to_dashboard(rf, admin_user, shop)
Exemplo n.º 27
0
def test_xtheme_wizard_pane(rf, admin_user):
    with override_settings(
        SHUUP_SETUP_WIZARD_PANE_SPEC = [
            "shuup.xtheme.admin_module.views.ThemeWizardPane"
        ],
        CACHES={
            'default': {
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                'LOCATION': 'test_simple_set_and_get_with_shop',
            }
        }
    ):
        cache.init_cache()
        shop = get_default_shop()
        with override_provides("xtheme", ["shuup_tests.xtheme.utils:FauxTheme"]):
            from shuup_tests.xtheme.utils import FauxTheme
            assert get_current_theme(shop) is None
            fields = _extract_fields(rf, admin_user)
            fields["theme-activate"] = FauxTheme.identifier
            request = apply_request_middleware(rf.post("/", data=fields), user=admin_user)
            response = WizardView.as_view()(request)
            assert isinstance(get_current_theme(shop), FauxTheme)
            assert_redirect_to_dashboard(rf, admin_user, shop)
Exemplo n.º 28
0
def test_xtheme_extra_views(rf):
    with override_settings(
            CACHES={
                'default': {
                    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                    'LOCATION': 'test_configuration_cache',
                }
            }):
        cache.init_cache()
        with override_current_theme_class(H2G2Theme, get_default_shop()):
            request = rf.get("/", {"name": "Arthur Dent"})
            request.shop = get_default_shop()
            # Simulate /xtheme/greeting
            response = extra_view_dispatch(request, "greeting")
            assert force_text(
                response.content
            ) == "So long, and thanks for all the fish, Arthur Dent"
            # Try that again (to exercise the _VIEW_CACHE code path):
            response = extra_view_dispatch(request, "greeting")
            assert force_text(
                response.content
            ) == "So long, and thanks for all the fish, Arthur Dent"
            # Now test that CBVs work
            assert not extra_view_dispatch(request, "faux").content
Exemplo n.º 29
0
def test_alert_limit_notification(rf, admin_user):
    with override_settings(
            CACHES={
                "default": {
                    "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
                    "LOCATION": "test_configuration_cache",
                }
            }):
        cache.init_cache()

        supplier = get_simple_supplier()
        shop = get_default_shop()
        product = create_product("simple-test-product", shop, supplier)
        supplier.update_stock(product.pk)
        sc = StockCount.objects.get(supplier=supplier, product=product)
        sc.alert_limit = 10
        sc.save()

        # nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        # put 11 units in stock
        supplier.adjust_stock(product.pk, +11)

        # still nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False

        # stock should be 6, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)
        last_run = cache.get(cache_key)
        assert last_run is not None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is True

        # stock should be 1, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)

        # test whether that runs inside a minute
        event = AlertLimitReached(product=product, supplier=supplier)
        event.run(shop)
        # not updated, not ran
        assert cache.get(cache_key) == last_run

        last_run -= 1000
        cache.set(cache_key, last_run)
        event = AlertLimitReached(product=product, supplier=supplier)
        event.run(shop)

        # last run should be updated
        assert cache.get(cache_key) != last_run

        event = AlertLimitReached(
            product=product,
            supplier=supplier,
            supplier_email="*****@*****.**",
            shop_email="*****@*****.**",
        )
        assert event.variable_values["dispatched_last_24hs"] is True

        # fake we have a cache with more than 24hrs
        cache.set(cache_key, time() - (24 * 60 * 60 * 2))

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False
Exemplo n.º 30
0
def test_alert_limit_notification(rf, admin_user):
    with override_settings(CACHES={
        'default': {
            'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
            'LOCATION': 'test_configuration_cache',
        }
    }):
        cache.init_cache()

        supplier = get_simple_supplier()
        shop = get_default_shop()
        product = create_product("simple-test-product", shop, supplier)

        sc = StockCount.objects.get(supplier=supplier, product=product)
        sc.alert_limit = 10
        sc.save()

        # nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        # put 11 units in stock
        supplier.adjust_stock(product.pk, +11)

        # still nothing in cache
        cache_key = AlertLimitReached.cache_key_fmt % (supplier.pk, product.pk)
        assert cache.get(cache_key) is None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False

        # stock should be 6, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)
        last_run = cache.get(cache_key)
        assert last_run is not None

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is True

        # stock should be 1, lower then the alert limit
        supplier.adjust_stock(product.pk, -5)

        # test whether that runs inside a minute
        event = AlertLimitReached(product=product, supplier=supplier)
        event.run(shop)
        # not updated, not ran
        assert cache.get(cache_key) == last_run

        last_run -= 1000
        cache.set(cache_key, last_run)
        event = AlertLimitReached(product=product, supplier=supplier)
        event.run(shop)

        # last run should be updated
        assert cache.get(cache_key) != last_run

        event = AlertLimitReached(
            product=product,
            supplier=supplier,
            supplier_email="*****@*****.**",
            shop_email="*****@*****.**"
        )
        assert event.variable_values["dispatched_last_24hs"] is True

        # fake we have a cache with more than 24hrs
        cache.set(cache_key, time() - (24 * 60 * 60 * 2))

        event = AlertLimitReached(product=product, supplier=supplier)
        assert event.variable_values["dispatched_last_24hs"] is False