File size: 6,892 Bytes
36b3cb8 | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/core/__init__.py b/tests/core/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/core/test_config.py b/tests/core/test_config.py
new file mode 100644
index 00000000..57bcea73
--- /dev/null
+++ b/tests/core/test_config.py
@@ -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()
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -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
|