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
This commit is contained in:
2024-05-10 17:31:45 +03:00
parent 50a045434d
commit 02b13de7f4
12 changed files with 64 additions and 30 deletions

View File

@ -3,7 +3,6 @@ from pytest_mock import MockerFixture
from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.gitremote import RemotePushTrigger
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package
from ahriman.models.result import Result
@ -30,5 +29,5 @@ def test_on_result(configuration: Configuration, result: Result, package_ahriman
trigger = RemotePushTrigger(repository_id, configuration)
trigger.on_result(result, [package_ahriman])
database_mock.assert_called_once_with(ContextKey("database", SQLite))
database_mock.assert_called_once_with(SQLite)
run_mock.assert_called_once_with(result)

View File

@ -6,7 +6,6 @@ from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.repository import Repository
from ahriman.core.sign.gpg import GPG
from ahriman.models.context_key import ContextKey
def test_load(configuration: Configuration, database: SQLite, mocker: MockerFixture) -> None:
@ -29,9 +28,9 @@ def test_set_context(configuration: Configuration, database: SQLite, mocker: Moc
instance = Repository.load(repository_id, configuration, database, report=False)
set_mock.assert_has_calls([
MockCall(ContextKey("database", SQLite), instance.database),
MockCall(ContextKey("configuration", Configuration), instance.configuration),
MockCall(ContextKey("pacman", Pacman), instance.pacman),
MockCall(ContextKey("sign", GPG), instance.sign),
MockCall(ContextKey("repository", Repository), instance),
MockCall(SQLite, instance.database),
MockCall(Configuration, instance.configuration),
MockCall(Pacman, instance.pacman),
MockCall(GPG, instance.sign),
MockCall(Repository, instance),
])

View File

@ -5,7 +5,6 @@ from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.core.sign.gpg import GPG
from ahriman.core.support import KeyringTrigger
from ahriman.models.context_key import ContextKey
def test_configuration_sections(configuration: Configuration) -> None:
@ -29,5 +28,5 @@ def test_on_start(configuration: Configuration, mocker: MockerFixture) -> None:
trigger = KeyringTrigger(repository_id, configuration)
trigger.on_start()
context_mock.assert_has_calls([MockCall(ContextKey("sign", GPG)), MockCall(ContextKey("database", SQLite))])
context_mock.assert_has_calls([MockCall(GPG), MockCall(SQLite)])
run_mock.assert_called_once_with()

View File

@ -4,7 +4,6 @@ from pytest_mock import MockerFixture
from ahriman.core.database import SQLite
from ahriman.core.support.package_creator import PackageCreator
from ahriman.models.context_key import ContextKey
from ahriman.models.package import Package
from ahriman.models.package_description import PackageDescription
from ahriman.models.package_source import PackageSource
@ -38,5 +37,5 @@ def test_run(package_creator: PackageCreator, database: SQLite, mocker: MockerFi
init_mock.assert_called_once_with(local_path)
package_mock.assert_called_once_with(local_path, "x86_64", None)
database_mock.assert_called_once_with(ContextKey("database", SQLite))
database_mock.assert_called_once_with(SQLite)
insert_mock.assert_called_once_with(package, pytest.helpers.anyvar(int))

View File

@ -15,6 +15,18 @@ def test_get_set() -> None:
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

View File

@ -0,0 +1,9 @@
from ahriman.models.context_key import ContextKey
def test_from_type() -> None:
"""
must construct key from type
"""
assert ContextKey.from_type(int) == ContextKey("int", int)
assert ContextKey.from_type(ContextKey) == ContextKey("ContextKey", ContextKey)