File size: 4,756 Bytes
15f5485 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | # -*- coding: utf-8 -*-
# This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
import pytest
from shoop import configuration
from shoop.core import cache
from shoop.core.models import ConfigurationItem
from shoop.testing.factories import get_default_shop
@pytest.mark.django_db
def test_simple_set_and_get_with_shop():
shop = get_default_shop()
configuration.set(shop, "answer", 42)
assert configuration.get(shop, "answer") == 42
assert configuration.get(shop, "non-existing") is None
configuration.set(shop, "non-existing", "hello")
assert configuration.get(shop, "non-existing") == "hello"
@pytest.mark.django_db
def test_simple_set_and_get_without_shop():
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"
@pytest.mark.django_db
def test_simple_set_and_get_cascading():
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"
@pytest.mark.django_db
def test_configuration_gets_saved():
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
@pytest.mark.django_db
def test_configuration_set_and_get():
cache.clear()
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
@pytest.mark.django_db
def test_configuration_update():
cache.clear()
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"
@pytest.mark.django_db
def test_global_configurations():
cache.clear()
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"
@pytest.mark.django_db
def test_configuration_cache():
cache.clear()
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"
|