Skip to content

Commit 374a819

Browse files
committed
Add tests pre-dpytest patch for GuildIterator
- These are broken without a working patch
1 parent 5fb2bed commit 374a819

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

tests/test_Insights.py

+75-23
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,47 @@
1212
import random
1313

1414
# Libs
15+
import discord.ext.commands
1516
import discord.ext.test as dpytest
1617
import mock
1718
import pytest
1819

1920
# Own modules
2021
import KoalaBot
2122
from cogs import Insights, BaseCog
22-
from tests.utils_testing import LastCtxCog
23+
from tests.utils_testing import LastCtxCog, TestUtils
2324

2425

2526
# Constants
2627

2728
# Variables
2829

29-
@pytest.fixture(scope="function", autouse=True)
30-
def utils_cog(bot):
30+
@pytest.fixture(scope="function", autouse=False)
31+
def setup(bot):
3132
utils_cog = LastCtxCog.LastCtxCog(bot)
33+
insights_cog = Insights.Insights(bot)
3234
bot.add_cog(utils_cog)
33-
dpytest.configure(bot)
34-
print("Tests starting")
35-
return utils_cog
36-
37-
38-
@pytest.fixture(scope="function", autouse=True)
39-
def base_cog(bot):
40-
base_cog = BaseCog.BaseCog(bot)
41-
bot.add_cog(base_cog)
42-
dpytest.configure(bot)
43-
print("Tests starting")
44-
return base_cog
45-
35+
bot.add_cog(insights_cog)
36+
print("Tests starting (setup)")
37+
return bot
4638

47-
@pytest.fixture(scope="function", autouse=True)
48-
async def insights_cog(bot):
39+
@pytest.fixture(scope="function", autouse=False)
40+
def setup_no_conf(bot_no_configure):
41+
bot: discord.ext.commands.Bot = bot_no_configure
42+
utils_cog = LastCtxCog.LastCtxCog(bot)
4943
insights_cog = Insights.Insights(bot)
44+
bot.add_cog(utils_cog)
5045
bot.add_cog(insights_cog)
51-
dpytest.configure(bot)
52-
print("Tests starting")
53-
return insights_cog
46+
print("Tests starting (setup)")
47+
return bot
5448

5549

5650
@pytest.mark.asyncio
5751
@pytest.mark.parametrize("num_guilds, num_members",
58-
[(1, 1), (1, 2), (1, 10), (2, 2), (2, 5), (2, 20), (5, 100), (100, 10000), (20, 20000)])
59-
async def test_insights(num_guilds, num_members):
52+
[(1, 1), (1, 2), (1, 10), (2, 2), (2, 5), (2, 20), (5, 100), (100, 500), (20, 2000)])
53+
async def test_insights(setup_no_conf, num_guilds, num_members):
54+
bot = setup_no_conf
55+
dpytest.configure(bot,num_guilds,1,num_members)
6056
test_config = dpytest.get_config()
6157

6258
for i in range(num_guilds):
@@ -77,3 +73,59 @@ async def test_insights(num_guilds, num_members):
7773
await dpytest.message(KoalaBot.COMMAND_PREFIX + "insights")
7874
assert dpytest.verify().message().content(
7975
f"KoalaBot is in {len(dpytest.get_config().guilds)} servers with a member total of {expected_users}.")
76+
77+
78+
@pytest.mark.asyncio
79+
@pytest.mark.parametrize("num_guilds", [1,2,3,4,5,6,7,8,9,10,20,50,100])
80+
async def test_servers_no_args(setup_no_conf, num_guilds):
81+
bot = setup_no_conf
82+
dpytest.configure(bot, num_guilds, 1, 1)
83+
test_config = dpytest.get_config()
84+
85+
expected_str = [""]
86+
expected_index = 0
87+
88+
for g in test_config.guilds:
89+
if len(expected_str[expected_index]) + 2 + len(g.name) > 2000:
90+
expected_index += 1
91+
expected_str.append("")
92+
expected_str[expected_index] += g.name + ", "
93+
94+
expected_str[-1] = expected_str[-1][:-2]
95+
await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers")
96+
for m in expected_str:
97+
assert dpytest.verify().message().content(m), print(dpytest.sent_queue)
98+
99+
100+
@pytest.mark.asyncio
101+
@pytest.mark.parametrize("num_guilds, num_valid", [(1,0), (1,1), (1,10), (2,0), (2, 1), (2,10), (5,0), (5,1), (5,10), (10, 0), (10, 1), (10, 50)])
102+
async def test_servers_args(setup_no_conf, num_guilds, num_valid):
103+
bot = setup_no_conf
104+
dpytest.configure(bot, num_guilds, 1, 1)
105+
test_config = dpytest.get_config()
106+
107+
arg_name = TestUtils.fake_custom_emoji_name_str()
108+
109+
for i in range(num_valid):
110+
g = dpytest.back.make_guild(arg_name + " " + str(i), test_config.members)
111+
print(g)
112+
113+
if num_valid != 0:
114+
expected_str = [""]
115+
expected_index = 0
116+
117+
for g in test_config.guilds:
118+
if arg_name in g.name:
119+
if len(expected_str[expected_index]) + 2 + len(g.name) > 2000:
120+
expected_index += 1
121+
expected_str.append("")
122+
expected_str[expected_index] += g.name + ", "
123+
124+
expected_str[-1] = expected_str[-1][:-2]
125+
await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers")
126+
for m in expected_str:
127+
assert dpytest.verify().message().content(m), print(dpytest.sent_queue)
128+
else:
129+
await dpytest.message(KoalaBot.COMMAND_PREFIX + "servers " + arg_name)
130+
assert dpytest.verify().message().content(f"No servers with {arg_name} in their name!")
131+

0 commit comments

Comments
 (0)