mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-20 09:56:05 -05:00
[Config] Asynchronous getters (#907)
* Make config get async * Asyncify alias * Asyncify bank * Asyncify cog manager * IT BOOTS * Asyncify core commands * Asyncify repo manager * Asyncify downloader * Asyncify economy * Asyncify alias TESTS * Asyncify economy TESTS * Asyncify downloader TESTS * Asyncify config TESTS * A bank thing * Asyncify Bank cog * Warning message in docs * Update docs with await syntax * Update docs with await syntax
This commit is contained in:
@@ -16,12 +16,14 @@ def test_is_valid_alias_name(alias):
|
||||
assert alias.is_valid_alias_name("not valid name") is False
|
||||
|
||||
|
||||
def test_empty_guild_aliases(alias, empty_guild):
|
||||
assert list(alias.unloaded_aliases(empty_guild)) == []
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_guild_aliases(alias, empty_guild):
|
||||
assert list(await alias.unloaded_aliases(empty_guild)) == []
|
||||
|
||||
|
||||
def test_empty_global_aliases(alias):
|
||||
assert list(alias.unloaded_global_aliases()) == []
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_global_aliases(alias):
|
||||
assert list(await alias.unloaded_global_aliases()) == []
|
||||
|
||||
|
||||
async def create_test_guild_alias(alias, ctx):
|
||||
@@ -36,7 +38,7 @@ async def create_test_global_alias(alias, ctx):
|
||||
async def test_add_guild_alias(alias, ctx):
|
||||
await create_test_guild_alias(alias, ctx)
|
||||
|
||||
is_alias, alias_obj = alias.is_alias(ctx.guild, "test")
|
||||
is_alias, alias_obj = await alias.is_alias(ctx.guild, "test")
|
||||
assert is_alias is True
|
||||
assert alias_obj.global_ is False
|
||||
|
||||
@@ -44,19 +46,19 @@ async def test_add_guild_alias(alias, ctx):
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_guild_alias(alias, ctx):
|
||||
await create_test_guild_alias(alias, ctx)
|
||||
is_alias, _ = alias.is_alias(ctx.guild, "test")
|
||||
is_alias, _ = await alias.is_alias(ctx.guild, "test")
|
||||
assert is_alias is True
|
||||
|
||||
await alias.delete_alias(ctx, "test")
|
||||
|
||||
is_alias, _ = alias.is_alias(ctx.guild, "test")
|
||||
is_alias, _ = await alias.is_alias(ctx.guild, "test")
|
||||
assert is_alias is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_global_alias(alias, ctx):
|
||||
await create_test_global_alias(alias, ctx)
|
||||
is_alias, alias_obj = alias.is_alias(ctx.guild, "test")
|
||||
is_alias, alias_obj = await alias.is_alias(ctx.guild, "test")
|
||||
|
||||
assert is_alias is True
|
||||
assert alias_obj.global_ is True
|
||||
@@ -65,7 +67,7 @@ async def test_add_global_alias(alias, ctx):
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_global_alias(alias, ctx):
|
||||
await create_test_global_alias(alias, ctx)
|
||||
is_alias, alias_obj = alias.is_alias(ctx.guild, "test")
|
||||
is_alias, alias_obj = await alias.is_alias(ctx.guild, "test")
|
||||
assert is_alias is True
|
||||
assert alias_obj.global_ is True
|
||||
|
||||
|
||||
@@ -11,13 +11,14 @@ def bank(config):
|
||||
return bank
|
||||
|
||||
|
||||
def test_bank_register(bank, ctx):
|
||||
default_bal = bank.get_default_balance(ctx.guild)
|
||||
assert default_bal == bank.get_account(ctx.author).balance
|
||||
@pytest.mark.asyncio
|
||||
async def test_bank_register(bank, ctx):
|
||||
default_bal = await bank.get_default_balance(ctx.guild)
|
||||
assert default_bal == (await bank.get_account(ctx.author)).balance
|
||||
|
||||
|
||||
async def has_account(member, bank):
|
||||
balance = bank.get_balance(member)
|
||||
balance = await bank.get_balance(member)
|
||||
if balance == 0:
|
||||
balance = 1
|
||||
await bank.set_balance(member, balance)
|
||||
@@ -27,11 +28,11 @@ async def has_account(member, bank):
|
||||
async def test_bank_transfer(bank, member_factory):
|
||||
mbr1 = member_factory.get()
|
||||
mbr2 = member_factory.get()
|
||||
bal1 = bank.get_account(mbr1).balance
|
||||
bal2 = bank.get_account(mbr2).balance
|
||||
bal1 = (await bank.get_account(mbr1)).balance
|
||||
bal2 = (await bank.get_account(mbr2)).balance
|
||||
await bank.transfer_credits(mbr1, mbr2, 50)
|
||||
newbal1 = bank.get_account(mbr1).balance
|
||||
newbal2 = bank.get_account(mbr2).balance
|
||||
newbal1 = (await bank.get_account(mbr1)).balance
|
||||
newbal2 = (await bank.get_account(mbr2)).balance
|
||||
assert bal1 - 50 == newbal1
|
||||
assert bal2 + 50 == newbal2
|
||||
|
||||
@@ -40,16 +41,16 @@ async def test_bank_transfer(bank, member_factory):
|
||||
async def test_bank_set(bank, member_factory):
|
||||
mbr = member_factory.get()
|
||||
await bank.set_balance(mbr, 250)
|
||||
acc = bank.get_account(mbr)
|
||||
acc = await bank.get_account(mbr)
|
||||
assert acc.balance == 250
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bank_can_spend(bank, member_factory):
|
||||
mbr = member_factory.get()
|
||||
canspend = bank.can_spend(mbr, 50)
|
||||
assert canspend == (50 < bank.get_default_balance(mbr.guild))
|
||||
canspend = await bank.can_spend(mbr, 50)
|
||||
assert canspend == (50 < await bank.get_default_balance(mbr.guild))
|
||||
await bank.set_balance(mbr, 200)
|
||||
acc = bank.get_account(mbr)
|
||||
canspendnow = bank.can_spend(mbr, 100)
|
||||
acc = await bank.get_account(mbr)
|
||||
canspendnow = await bank.can_spend(mbr, 100)
|
||||
assert canspendnow
|
||||
|
||||
@@ -14,16 +14,17 @@ def default_dir(red):
|
||||
return red.main_dir
|
||||
|
||||
|
||||
def test_ensure_cogs_in_paths(cog_mgr, default_dir):
|
||||
@pytest.mark.asyncio
|
||||
async def test_ensure_cogs_in_paths(cog_mgr, default_dir):
|
||||
cogs_dir = default_dir / 'cogs'
|
||||
assert cogs_dir in cog_mgr.paths
|
||||
assert cogs_dir in await cog_mgr.paths()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_path_set(cog_mgr: cog_manager.CogManager, tmpdir):
|
||||
path = Path(str(tmpdir))
|
||||
await cog_mgr.set_install_path(path)
|
||||
assert cog_mgr.install_path == path
|
||||
assert await cog_mgr.install_path() == path
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -38,7 +39,7 @@ async def test_install_path_set_bad(cog_mgr):
|
||||
async def test_add_path(cog_mgr, tmpdir):
|
||||
path = Path(str(tmpdir))
|
||||
await cog_mgr.add_path(path)
|
||||
assert path in cog_mgr.paths
|
||||
assert path in await cog_mgr.paths()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -54,4 +55,4 @@ async def test_remove_path(cog_mgr, tmpdir):
|
||||
path = Path(str(tmpdir))
|
||||
await cog_mgr.add_path(path)
|
||||
await cog_mgr.remove_path(path)
|
||||
assert path not in cog_mgr.paths
|
||||
assert path not in await cog_mgr.paths()
|
||||
|
||||
@@ -2,10 +2,11 @@ import pytest
|
||||
|
||||
|
||||
#region Register Tests
|
||||
def test_config_register_global(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_global(config):
|
||||
config.register_global(enabled=False)
|
||||
assert config.defaults["GLOBAL"]["enabled"] is False
|
||||
assert config.enabled() is False
|
||||
assert await config.enabled() is False
|
||||
|
||||
|
||||
def test_config_register_global_badvalues(config):
|
||||
@@ -13,61 +14,69 @@ def test_config_register_global_badvalues(config):
|
||||
config.register_global(**{"invalid var name": True})
|
||||
|
||||
|
||||
def test_config_register_guild(config, empty_guild):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_guild(config, empty_guild):
|
||||
config.register_guild(enabled=False, some_list=[], some_dict={})
|
||||
assert config.defaults[config.GUILD]["enabled"] is False
|
||||
assert config.defaults[config.GUILD]["some_list"] == []
|
||||
assert config.defaults[config.GUILD]["some_dict"] == {}
|
||||
|
||||
assert config.guild(empty_guild).enabled() is False
|
||||
assert config.guild(empty_guild).some_list() == []
|
||||
assert config.guild(empty_guild).some_dict() == {}
|
||||
assert await config.guild(empty_guild).enabled() is False
|
||||
assert await config.guild(empty_guild).some_list() == []
|
||||
assert await config.guild(empty_guild).some_dict() == {}
|
||||
|
||||
|
||||
def test_config_register_channel(config, empty_channel):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_channel(config, empty_channel):
|
||||
config.register_channel(enabled=False)
|
||||
assert config.defaults[config.CHANNEL]["enabled"] is False
|
||||
assert config.channel(empty_channel).enabled() is False
|
||||
assert await config.channel(empty_channel).enabled() is False
|
||||
|
||||
|
||||
def test_config_register_role(config, empty_role):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_role(config, empty_role):
|
||||
config.register_role(enabled=False)
|
||||
assert config.defaults[config.ROLE]["enabled"] is False
|
||||
assert config.role(empty_role).enabled() is False
|
||||
assert await config.role(empty_role).enabled() is False
|
||||
|
||||
|
||||
def test_config_register_member(config, empty_member):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_member(config, empty_member):
|
||||
config.register_member(some_number=-1)
|
||||
assert config.defaults[config.MEMBER]["some_number"] == -1
|
||||
assert config.member(empty_member).some_number() == -1
|
||||
assert await config.member(empty_member).some_number() == -1
|
||||
|
||||
|
||||
def test_config_register_user(config, empty_user):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_register_user(config, empty_user):
|
||||
config.register_user(some_value=None)
|
||||
assert config.defaults[config.USER]["some_value"] is None
|
||||
assert config.user(empty_user).some_value() is None
|
||||
assert await config.user(empty_user).some_value() is None
|
||||
|
||||
|
||||
def test_config_force_register_global(config_fr):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_force_register_global(config_fr):
|
||||
with pytest.raises(AttributeError):
|
||||
config_fr.enabled()
|
||||
await config_fr.enabled()
|
||||
|
||||
config_fr.register_global(enabled=True)
|
||||
assert config_fr.enabled() is True
|
||||
assert await config_fr.enabled() is True
|
||||
#endregion
|
||||
|
||||
|
||||
# Test nested registration
|
||||
def test_nested_registration(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested_registration(config):
|
||||
config.register_global(foo__bar__baz=False)
|
||||
assert config.foo.bar.baz() is False
|
||||
assert await config.foo.bar.baz() is False
|
||||
|
||||
|
||||
def test_nested_registration_asdict(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested_registration_asdict(config):
|
||||
defaults = {'bar': {'baz': False}}
|
||||
config.register_global(foo=defaults)
|
||||
|
||||
assert config.foo.bar.baz() is False
|
||||
assert await config.foo.bar.baz() is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -75,20 +84,22 @@ async def test_nested_registration_and_changing(config):
|
||||
defaults = {'bar': {'baz': False}}
|
||||
config.register_global(foo=defaults)
|
||||
|
||||
assert config.foo.bar.baz() is False
|
||||
assert await config.foo.bar.baz() is False
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
await config.foo.set(True)
|
||||
|
||||
|
||||
def test_doubleset_default(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_doubleset_default(config):
|
||||
config.register_global(foo=True)
|
||||
config.register_global(foo=False)
|
||||
|
||||
assert config.foo() is False
|
||||
assert await config.foo() is False
|
||||
|
||||
|
||||
def test_nested_registration_multidict(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested_registration_multidict(config):
|
||||
defaults = {
|
||||
"foo": {
|
||||
"bar": {
|
||||
@@ -99,8 +110,8 @@ def test_nested_registration_multidict(config):
|
||||
}
|
||||
config.register_global(**defaults)
|
||||
|
||||
assert config.foo.bar.baz() is True
|
||||
assert config.blah() is True
|
||||
assert await config.foo.bar.baz() is True
|
||||
assert await config.blah() is True
|
||||
|
||||
|
||||
def test_nested_group_value_badreg(config):
|
||||
@@ -109,56 +120,66 @@ def test_nested_group_value_badreg(config):
|
||||
config.register_global(foo__bar=False)
|
||||
|
||||
|
||||
def test_nested_toplevel_reg(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested_toplevel_reg(config):
|
||||
defaults = {'bar': True, 'baz': False}
|
||||
config.register_global(foo=defaults)
|
||||
|
||||
assert config.foo.bar() is True
|
||||
assert config.foo.baz() is False
|
||||
assert await config.foo.bar() is True
|
||||
assert await config.foo.baz() is False
|
||||
|
||||
|
||||
def test_nested_overlapping(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nested_overlapping(config):
|
||||
config.register_global(foo__bar=True)
|
||||
config.register_global(foo__baz=False)
|
||||
|
||||
assert config.foo.bar() is True
|
||||
assert config.foo.baz() is False
|
||||
assert await config.foo.bar() is True
|
||||
assert await config.foo.baz() is False
|
||||
|
||||
|
||||
def test_nesting_nofr(config):
|
||||
@pytest.mark.asyncio
|
||||
async def test_nesting_nofr(config):
|
||||
config.register_global(foo={})
|
||||
assert config.foo.bar() is None
|
||||
assert config.foo() == {}
|
||||
assert await config.foo.bar() is None
|
||||
assert await config.foo() == {}
|
||||
|
||||
|
||||
#region Default Value Overrides
|
||||
def test_global_default_override(config):
|
||||
assert config.enabled(True) is True
|
||||
# region Default Value Overrides
|
||||
@pytest.mark.asyncio
|
||||
async def test_global_default_override(config):
|
||||
assert await config.enabled(True) is True
|
||||
|
||||
|
||||
def test_global_default_nofr(config):
|
||||
assert config.nofr() is None
|
||||
assert config.nofr(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_global_default_nofr(config):
|
||||
assert await config.nofr() is None
|
||||
assert await config.nofr(True) is True
|
||||
|
||||
|
||||
def test_guild_default_override(config, empty_guild):
|
||||
assert config.guild(empty_guild).enabled(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_guild_default_override(config, empty_guild):
|
||||
assert await config.guild(empty_guild).enabled(True) is True
|
||||
|
||||
|
||||
def test_channel_default_override(config, empty_channel):
|
||||
assert config.channel(empty_channel).enabled(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_channel_default_override(config, empty_channel):
|
||||
assert await config.channel(empty_channel).enabled(True) is True
|
||||
|
||||
|
||||
def test_role_default_override(config, empty_role):
|
||||
assert config.role(empty_role).enabled(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_role_default_override(config, empty_role):
|
||||
assert await config.role(empty_role).enabled(True) is True
|
||||
|
||||
|
||||
def test_member_default_override(config, empty_member):
|
||||
assert config.member(empty_member).enabled(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_member_default_override(config, empty_member):
|
||||
assert await config.member(empty_member).enabled(True) is True
|
||||
|
||||
|
||||
def test_user_default_override(config, empty_user):
|
||||
assert config.user(empty_user).some_value(True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_user_default_override(config, empty_user):
|
||||
assert await config.user(empty_user).some_value(True) is True
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -166,32 +187,32 @@ def test_user_default_override(config, empty_user):
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_global(config):
|
||||
await config.enabled.set(True)
|
||||
assert config.enabled() is True
|
||||
assert await config.enabled() is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_guild(config, empty_guild):
|
||||
await config.guild(empty_guild).enabled.set(True)
|
||||
assert config.guild(empty_guild).enabled() is True
|
||||
assert await config.guild(empty_guild).enabled() is True
|
||||
|
||||
curr_list = config.guild(empty_guild).some_list([1, 2, 3])
|
||||
curr_list = await config.guild(empty_guild).some_list([1, 2, 3])
|
||||
assert curr_list == [1, 2, 3]
|
||||
curr_list.append(4)
|
||||
|
||||
await config.guild(empty_guild).some_list.set(curr_list)
|
||||
assert config.guild(empty_guild).some_list() == curr_list
|
||||
assert await config.guild(empty_guild).some_list() == curr_list
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_channel(config, empty_channel):
|
||||
await config.channel(empty_channel).enabled.set(True)
|
||||
assert config.channel(empty_channel).enabled() is True
|
||||
assert await config.channel(empty_channel).enabled() is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_channel_no_register(config, empty_channel):
|
||||
await config.channel(empty_channel).no_register.set(True)
|
||||
assert config.channel(empty_channel).no_register() is True
|
||||
assert await config.channel(empty_channel).no_register() is True
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -200,11 +221,12 @@ async def test_set_channel_no_register(config, empty_channel):
|
||||
async def test_set_dynamic_attr(config):
|
||||
await config.set_attr("foobar", True)
|
||||
|
||||
assert config.foobar() is True
|
||||
assert await config.foobar() is True
|
||||
|
||||
|
||||
def test_get_dynamic_attr(config):
|
||||
assert config.get_attr("foobaz", True) is True
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_dynamic_attr(config):
|
||||
assert await config.get_attr("foobaz", True) is True
|
||||
|
||||
|
||||
# Member Group testing
|
||||
@@ -212,7 +234,7 @@ def test_get_dynamic_attr(config):
|
||||
async def test_membergroup_allguilds(config, empty_member):
|
||||
await config.member(empty_member).foo.set(False)
|
||||
|
||||
all_servers = config.member(empty_member).all_guilds()
|
||||
all_servers = await config.member(empty_member).all_guilds()
|
||||
assert str(empty_member.guild.id) in all_servers
|
||||
|
||||
|
||||
@@ -220,7 +242,7 @@ async def test_membergroup_allguilds(config, empty_member):
|
||||
async def test_membergroup_allmembers(config, empty_member):
|
||||
await config.member(empty_member).foo.set(False)
|
||||
|
||||
all_members = config.member(empty_member).all()
|
||||
all_members = await config.member(empty_member).all()
|
||||
assert str(empty_member.id) in all_members
|
||||
|
||||
|
||||
@@ -232,13 +254,13 @@ async def test_global_clear(config):
|
||||
await config.foo.set(False)
|
||||
await config.bar.set(True)
|
||||
|
||||
assert config.foo() is False
|
||||
assert config.bar() is True
|
||||
assert await config.foo() is False
|
||||
assert await config.bar() is True
|
||||
|
||||
await config.clear()
|
||||
|
||||
assert config.foo() is True
|
||||
assert config.bar() is False
|
||||
assert await config.foo() is True
|
||||
assert await config.bar() is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -247,17 +269,17 @@ async def test_member_clear(config, member_factory):
|
||||
|
||||
m1 = member_factory.get()
|
||||
await config.member(m1).foo.set(False)
|
||||
assert config.member(m1).foo() is False
|
||||
assert await config.member(m1).foo() is False
|
||||
|
||||
m2 = member_factory.get()
|
||||
await config.member(m2).foo.set(False)
|
||||
assert config.member(m2).foo() is False
|
||||
assert await config.member(m2).foo() is False
|
||||
|
||||
assert m1.guild.id != m2.guild.id
|
||||
|
||||
await config.member(m1).clear()
|
||||
assert config.member(m1).foo() is True
|
||||
assert config.member(m2).foo() is False
|
||||
assert await config.member(m1).foo() is True
|
||||
assert await config.member(m2).foo() is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -269,11 +291,11 @@ async def test_member_clear_all(config, member_factory):
|
||||
server_ids.append(member.guild.id)
|
||||
|
||||
member = member_factory.get()
|
||||
assert len(config.member(member).all_guilds()) == len(server_ids)
|
||||
assert len(await config.member(member).all_guilds()) == len(server_ids)
|
||||
|
||||
await config.member(member).clear_all()
|
||||
|
||||
assert len(config.member(member).all_guilds()) == 0
|
||||
assert len(await config.member(member).all_guilds()) == 0
|
||||
|
||||
|
||||
# Get All testing
|
||||
@@ -284,7 +306,7 @@ async def test_user_get_all_from_kind(config, user_factory):
|
||||
await config.user(user).foo.set(True)
|
||||
|
||||
user = user_factory.get()
|
||||
all_data = config.user(user).all_from_kind()
|
||||
all_data = await config.user(user).all_from_kind()
|
||||
|
||||
assert len(all_data) == 5
|
||||
|
||||
@@ -294,4 +316,4 @@ async def test_user_getalldata(config, user_factory):
|
||||
user = user_factory.get()
|
||||
await config.user(user).foo.set(False)
|
||||
|
||||
assert "foo" in config.user(user).all()
|
||||
assert "foo" in await config.user(user).all()
|
||||
|
||||
Reference in New Issue
Block a user