2013-02-22 13:58:41 +00:00
|
|
|
from django.test import TestCase
|
2026-03-04 22:37:37 +00:00
|
|
|
from django.test import TransactionTestCase
|
2013-02-22 13:58:41 +00:00
|
|
|
|
|
|
|
|
from constance import settings
|
2026-03-04 22:37:37 +00:00
|
|
|
from constance.base import Config
|
2013-04-12 15:34:48 +00:00
|
|
|
from tests.storage import StorageTestsMixin
|
2013-02-22 13:58:41 +00:00
|
|
|
|
|
|
|
|
|
2020-06-10 17:49:42 +00:00
|
|
|
class TestMemory(StorageTestsMixin, TestCase):
|
2013-02-22 13:58:41 +00:00
|
|
|
def setUp(self):
|
|
|
|
|
self.old_backend = settings.BACKEND
|
2025-10-07 09:25:07 +00:00
|
|
|
settings.BACKEND = "constance.backends.memory.MemoryBackend"
|
2019-12-23 21:20:41 +00:00
|
|
|
super().setUp()
|
2020-06-10 17:49:42 +00:00
|
|
|
self.config._backend._storage = {}
|
2013-02-22 13:58:41 +00:00
|
|
|
|
|
|
|
|
def tearDown(self):
|
2020-06-10 17:49:42 +00:00
|
|
|
self.config._backend._storage = {}
|
2013-02-22 13:58:41 +00:00
|
|
|
settings.BACKEND = self.old_backend
|
2026-03-04 22:37:37 +00:00
|
|
|
|
|
|
|
|
def test_mget_empty_keys(self):
|
|
|
|
|
result = self.config._backend.mget([])
|
|
|
|
|
self.assertIsNone(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMemoryAsync(TransactionTestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.old_backend = settings.BACKEND
|
|
|
|
|
settings.BACKEND = "constance.backends.memory.MemoryBackend"
|
|
|
|
|
self.config = Config()
|
|
|
|
|
self.config._backend._storage = {}
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
self.config._backend._storage = {}
|
|
|
|
|
settings.BACKEND = self.old_backend
|
|
|
|
|
|
|
|
|
|
async def test_aget_returns_none_for_missing_key(self):
|
|
|
|
|
result = await self.config._backend.aget("INT_VALUE")
|
|
|
|
|
self.assertIsNone(result)
|
|
|
|
|
|
|
|
|
|
async def test_aget_returns_value(self):
|
|
|
|
|
self.config._backend.set("INT_VALUE", 42)
|
|
|
|
|
result = await self.config._backend.aget("INT_VALUE")
|
|
|
|
|
self.assertEqual(result, 42)
|
|
|
|
|
|
|
|
|
|
async def test_aset_stores_value(self):
|
|
|
|
|
await self.config._backend.aset("INT_VALUE", 99)
|
|
|
|
|
result = self.config._backend.get("INT_VALUE")
|
|
|
|
|
self.assertEqual(result, 99)
|
|
|
|
|
|
|
|
|
|
async def test_amget_returns_empty_for_no_keys(self):
|
|
|
|
|
result = await self.config._backend.amget([])
|
|
|
|
|
self.assertEqual(result, {})
|
|
|
|
|
|
|
|
|
|
async def test_amget_returns_values(self):
|
|
|
|
|
self.config._backend.set("INT_VALUE", 10)
|
|
|
|
|
self.config._backend.set("BOOL_VALUE", value=True)
|
|
|
|
|
result = await self.config._backend.amget(["INT_VALUE", "BOOL_VALUE"])
|
|
|
|
|
self.assertEqual(result, {"INT_VALUE": 10, "BOOL_VALUE": True})
|