12
12
import random
13
13
14
14
# Libs
15
+ import discord .ext .commands
15
16
import discord .ext .test as dpytest
16
17
import mock
17
18
import pytest
18
19
19
20
# Own modules
20
21
import KoalaBot
21
22
from cogs import Insights , BaseCog
22
- from tests .utils_testing import LastCtxCog
23
+ from tests .utils_testing import LastCtxCog , TestUtils
23
24
24
25
25
26
# Constants
26
27
27
28
# Variables
28
29
29
- @pytest .fixture (scope = "function" , autouse = True )
30
- def utils_cog (bot ):
30
+ @pytest .fixture (scope = "function" , autouse = False )
31
+ def setup (bot ):
31
32
utils_cog = LastCtxCog .LastCtxCog (bot )
33
+ insights_cog = Insights .Insights (bot )
32
34
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
46
38
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 )
49
43
insights_cog = Insights .Insights (bot )
44
+ bot .add_cog (utils_cog )
50
45
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
54
48
55
49
56
50
@pytest .mark .asyncio
57
51
@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 )
60
56
test_config = dpytest .get_config ()
61
57
62
58
for i in range (num_guilds ):
@@ -77,3 +73,59 @@ async def test_insights(num_guilds, num_members):
77
73
await dpytest .message (KoalaBot .COMMAND_PREFIX + "insights" )
78
74
assert dpytest .verify ().message ().content (
79
75
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