| |
| new file mode 100644 |
| |
| |
| new file mode 100644 |
| |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,226 @@ |
| + |
| +import asyncio |
| +import itertools |
| + |
| +import pytest |
| + |
| +from core.config import Config |
| + |
| + |
| + |
| +_counter = itertools.count() |
| + |
| + |
| +def make_conf(force_registration=False): |
| + name = "TestCog{}".format(next(_counter)) |
| + return Config.get_conf(name, unique_identifier=next(_counter), |
| + force_registration=force_registration) |
| + |
| + |
| +def run(coro): |
| + |
| + loop = asyncio.new_event_loop() |
| + try: |
| + return loop.run_until_complete(coro) |
| + finally: |
| + loop.close() |
| + |
| + |
| +class FakeIdObject: |
| + def __init__(self, id): |
| + self.id = id |
| + |
| + |
| + |
| + |
| +def test_global_default_returned_when_unset(): |
| + conf = make_conf() |
| + conf.register_global(foo=42, bar="baz") |
| + assert conf.foo() == 42 |
| + assert conf.bar() == "baz" |
| + |
| + |
| +def test_global_set_then_read(): |
| + conf = make_conf() |
| + conf.register_global(foo=42) |
| + run(conf.set("foo", 100)) |
| + assert conf.foo() == 100 |
| + |
| + |
| +def test_unregistered_read_returns_none_without_strict(): |
| + conf = make_conf() |
| + assert conf.nonexistent() is None |
| + |
| + |
| +def test_set_unregistered_allowed_without_strict(): |
| + conf = make_conf() |
| + run(conf.set("dynamic", 7)) |
| + assert conf.dynamic() == 7 |
| + |
| + |
| + |
| + |
| +def test_guild_default_and_set(): |
| + conf = make_conf() |
| + conf.register_guild(prefix="!") |
| + assert conf.guild(1).prefix() == "!" |
| + run(conf.guild(1).set("prefix", "?")) |
| + assert conf.guild(1).prefix() == "?" |
| + |
| + |
| +def test_guild_isolation_between_ids(): |
| + conf = make_conf() |
| + conf.register_guild(prefix="!") |
| + run(conf.guild(1).set("prefix", "?")) |
| + assert conf.guild(1).prefix() == "?" |
| + assert conf.guild(2).prefix() == "!" |
| + |
| + |
| +def test_guild_does_not_leak_into_global(): |
| + conf = make_conf() |
| + conf.register_global(value=0) |
| + conf.register_guild(value=0) |
| + run(conf.guild(1).set("value", 99)) |
| + assert conf.value() == 0 |
| + assert conf.guild(2).value() == 0 |
| + |
| + |
| +def test_scopes_have_independent_keysets(): |
| + conf = make_conf() |
| + conf.register_global(only_global=1) |
| + conf.register_guild(only_guild=2) |
| + assert conf.only_global() == 1 |
| + assert conf.guild(1).only_global() is None |
| + assert conf.guild(1).only_guild() == 2 |
| + assert conf.only_guild() is None |
| + |
| + |
| + |
| + |
| +def test_channel_role_user_scopes(): |
| + conf = make_conf() |
| + conf.register_channel(a=1) |
| + conf.register_role(b=2) |
| + conf.register_user(c=3) |
| + |
| + assert conf.channel(10).a() == 1 |
| + assert conf.role(10).b() == 2 |
| + assert conf.user(10).c() == 3 |
| + |
| + run(conf.channel(10).set("a", 11)) |
| + run(conf.role(10).set("b", 22)) |
| + run(conf.user(10).set("c", 33)) |
| + |
| + assert conf.channel(10).a() == 11 |
| + assert conf.channel(99).a() == 1 |
| + assert conf.role(10).b() == 22 |
| + assert conf.role(99).b() == 2 |
| + assert conf.user(10).c() == 33 |
| + assert conf.user(99).c() == 3 |
| + |
| + |
| + |
| + |
| +def test_member_default_and_set(): |
| + conf = make_conf() |
| + conf.register_member(points=0) |
| + assert conf.member(1, 5).points() == 0 |
| + run(conf.member(1, 5).set("points", 50)) |
| + assert conf.member(1, 5).points() == 50 |
| + |
| + |
| +def test_member_composite_isolation(): |
| + conf = make_conf() |
| + conf.register_member(points=0) |
| + run(conf.member(1, 5).set("points", 50)) |
| + |
| + assert conf.member(2, 5).points() == 0 |
| + |
| + assert conf.member(1, 6).points() == 0 |
| + assert conf.member(1, 5).points() == 50 |
| + |
| + |
| + |
| + |
| +def test_id_object_equivalent_to_int(): |
| + conf = make_conf() |
| + conf.register_guild(prefix="!") |
| + run(conf.guild(123).set("prefix", "$")) |
| + assert conf.guild(FakeIdObject(123)).prefix() == "$" |
| + run(conf.guild(FakeIdObject(123)).set("prefix", "%")) |
| + assert conf.guild(123).prefix() == "%" |
| + |
| + |
| +def test_member_id_objects(): |
| + conf = make_conf() |
| + conf.register_member(points=0) |
| + run(conf.member(FakeIdObject(1), FakeIdObject(5)).set("points", 7)) |
| + assert conf.member(1, 5).points() == 7 |
| + |
| + |
| + |
| + |
| +def test_clear_resets_scope_to_defaults(): |
| + conf = make_conf() |
| + conf.register_guild(prefix="!") |
| + run(conf.guild(1).set("prefix", "?")) |
| + assert conf.guild(1).prefix() == "?" |
| + run(conf.guild(1).clear()) |
| + assert conf.guild(1).prefix() == "!" |
| + |
| + |
| +def test_clear_only_affects_target_scope(): |
| + conf = make_conf() |
| + conf.register_guild(prefix="!") |
| + run(conf.guild(1).set("prefix", "?")) |
| + run(conf.guild(2).set("prefix", ".")) |
| + run(conf.guild(1).clear()) |
| + assert conf.guild(1).prefix() == "!" |
| + assert conf.guild(2).prefix() == "." |
| + |
| + |
| + |
| + |
| +def test_register_merges_and_overrides(): |
| + conf = make_conf() |
| + conf.register_global(a=1, b=2) |
| + conf.register_global(b=20, c=3) |
| + assert conf.a() == 1 |
| + assert conf.b() == 20 |
| + assert conf.c() == 3 |
| + |
| + |
| + |
| + |
| +def test_strict_read_unregistered_raises(): |
| + conf = make_conf(force_registration=True) |
| + conf.register_global(known=1) |
| + with pytest.raises(AttributeError): |
| + conf.unknown() |
| + |
| + |
| +def test_strict_set_unregistered_raises(): |
| + conf = make_conf(force_registration=True) |
| + conf.register_global(known=1) |
| + with pytest.raises(AttributeError): |
| + run(conf.set("unknown", 5)) |
| + |
| + |
| +def test_strict_registered_key_works(): |
| + conf = make_conf(force_registration=True) |
| + conf.register_global(known=1) |
| + conf.register_guild(gkey=2) |
| + assert conf.known() == 1 |
| + run(conf.set("known", 9)) |
| + assert conf.known() == 9 |
| + assert conf.guild(1).gkey() == 2 |
| + run(conf.guild(1).set("gkey", 8)) |
| + assert conf.guild(1).gkey() == 8 |
| + |
| + |
| +def test_strict_is_per_scope(): |
| + conf = make_conf(force_registration=True) |
| + conf.register_global(known=1) |
| + |
| + with pytest.raises(AttributeError): |
| + conf.guild(1).known() |
| |
| new file mode 100755 |
| |
| |
| @@ -0,0 +1,36 @@ |
| +#!/usr/bin/env bash |
| + |
| + |
| + |
| +set -euo pipefail |
| +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| +cd /testbed |
| + |
| + |
| + |
| + |
| + |
| + |
| + |
| + |
| + |
| +SP="$PWD/.venv/lib/python3.12/site-packages" |
| +if [ ! -f "$SP/pytest/__init__.pyc" ]; then |
| + find "$SP" -type d -name __pycache__ | while read -r d; do |
| + parent="$(dirname "$d")" |
| + for f in "$d"/*.cpython-312.pyc; do |
| + [ -e "$f" ] || continue |
| + base="$(basename "$f" .cpython-312.pyc)" |
| + [ -e "$parent/$base.pyc" ] || cp "$f" "$parent/$base.pyc" |
| + done |
| + done |
| +fi |
| +export PYTHONPATH="$SP" |
| + |
| + |
| + |
| + |
| +python3 -c "import core, core.json_io, core.utils.chat_formatting" |
| + |
| + |
| +python3 -m pytest tests/core/test_config.py -q |
|
|