ahriman/tests/ahriman/core/test_context_init.py
Evgenii Alekseev 2b9880bd3c feat: allow to use simplified keys for context
Initial implementation requires explicit context key name to be set.
Though it is still useful sometimes (e.g. if there should be two
variables with the same type), in the most used scenarios internally
only type is required. This commit extends set and get methods to allow
to construct ContextKey from type directly

Also it breaks old keys, since - in order to reduce amount of possible
mistakes - internal classes uses this generation method
2024-08-14 17:07:10 +03:00

105 lines
2.2 KiB
Python

import pytest
from ahriman.core import _Context
from ahriman.models.context_key import ContextKey
def test_get_set() -> None:
"""
must set and get variable
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
assert ctx.get(key) == value
def test_get_set_type() -> None:
"""
must set and get variable by type
"""
key, value = int, 42
ctx = _Context()
ctx.set(key, value)
assert ctx.get(key) == value
assert ctx.get(ContextKey.from_type(int)) == value
def test_get_key_exception() -> None:
"""
must raise KeyError in case if key was not found
"""
ctx = _Context()
with pytest.raises(KeyError):
ctx.get(ContextKey("key", int))
def test_get_value_exception() -> None:
"""
must raise ValueError in case if key type differs from existing value
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
with pytest.raises(ValueError):
ctx.get(ContextKey("key", str))
def test_set_key_exception() -> None:
"""
must raise KeyError in case if key already exists
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
with pytest.raises(KeyError):
ctx.set(key, value)
def test_set_value_exception() -> None:
"""
must raise ValueError in case if key type differs from new value
"""
ctx = _Context()
with pytest.raises(ValueError):
ctx.set(ContextKey("key", str), 42)
def test_contains() -> None:
"""
must correctly check if element is in list
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
assert key not in ctx
assert key.key in ctx
assert "random-key" not in ctx
def test_iter() -> None:
"""
must return keys iterator
"""
key, value = ContextKey("key", int), 42
ctx = _Context()
ctx.set(key, value)
assert set(iter(ctx)) == set(iter({key.key: value}))
def test_len() -> None:
"""
must correctly define collection length
"""
ctx = _Context()
ctx.set(ContextKey("str", str), "str")
ctx.set(ContextKey("int", int), 42)
assert len(ctx) == 2