Skip to content

Commit 9572621

Browse files
authored
Merge pull request #234 from davidhozic/feature/testing
Better testing structure
2 parents 2ba17ae + 442f4ea commit 9572621

File tree

7 files changed

+110
-120
lines changed

7 files changed

+110
-120
lines changed

src/daf/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def remove_object(snowflake: Union[int, dc.Object, dc.Guild, dc.User, dc.Object,
309309
GLOBALS.server_list.remove(snowflake)
310310
snowflake._delete()
311311

312+
@typechecked
312313
@misc.doc_category("Getters")
313314
def get_guild_user(snowflake: Union[int, dc.Object, dc.Guild, dc.User, dc.Object]) -> Union[guild.GUILD, guild.USER, None]:
314315
"""

testing/fixtures/main.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import suppress
12
import asyncio
23
import pytest
34
import pytest_asyncio
@@ -31,31 +32,42 @@ def start_daf(event_loop: asyncio.AbstractEventLoop):
3132

3233

3334
@pytest_asyncio.fixture(scope="session")
34-
async def category():
35+
async def guilds():
36+
"""
37+
Create tests guilds.
38+
"""
3539
client = daf.get_client()
36-
guild: daf.discord.Guild = client.get_guild(TEST_GUILD_ID)
37-
cat = await guild.create_category(TEST_CATEGORY_NAME)
38-
yield cat
39-
await cat.delete()
40+
guild_include = None
41+
guild_exclude = None
4042

43+
for guild in client.guilds:
44+
if guild.name == "magic-123-magic":
45+
guild_include = guild
46+
break
47+
else:
48+
guild_include = await client.create_guild(name="magic-123-magic")
4149

42-
@pytest_asyncio.fixture(scope="session")
43-
async def text_channels(category: daf.discord.CategoryChannel):
44-
ret = []
45-
for i in range(TEST_TEXT_CHANNEL_NUM):
46-
ret.append(await category.create_text_channel(TEST_TEXT_CHANNEL_NAME_FORM))
50+
for guild in client.guilds:
51+
if guild.name == "magic-321-magic":
52+
guild_exclude = guild
53+
break
54+
else:
55+
guild_exclude = await client.create_guild(name="magic-321-magic")
4756

48-
yield ret
49-
for c in ret:
50-
await c.delete()
57+
return guild_include, guild_exclude
5158

5259

5360
@pytest_asyncio.fixture(scope="session")
54-
async def voice_channels(category: daf.discord.CategoryChannel):
55-
ret = []
56-
for i in range(TEST_VOICE_CHANNEL_NUM):
57-
ret.append(await category.create_voice_channel(TEST_VOICE_CHANNEL_NAME_FORM))
58-
59-
yield ret
60-
for c in ret:
61-
await c.delete()
61+
async def channels(guilds):
62+
guild: daf.discord.Guild
63+
guild, _ = guilds
64+
t_channels = []
65+
v_channels = []
66+
for i in range(10):
67+
t_channels.append(await guild.create_text_channel(f"testpy-{i}"))
68+
v_channels.append(await guild.create_voice_channel(f"testpy-{i}"))
69+
70+
yield t_channels, v_channels
71+
for channel in t_channels + v_channels:
72+
with suppress(daf.discord.HTTPException):
73+
await channel.delete()

testing/test_autogen.py

Lines changed: 39 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,6 @@
1010
import daf
1111
import asyncio
1212

13-
@pytest_asyncio.fixture(scope="module")
14-
async def guilds():
15-
"""
16-
Create tests guilds.
17-
"""
18-
client = daf.get_client()
19-
guild_include = None
20-
guild_exclude = None
21-
22-
for guild in client.guilds:
23-
if guild.name == "magic-123-magic":
24-
guild_include = guild
25-
break
26-
else:
27-
guild_include = await client.create_guild(name="magic-123-magic")
28-
29-
for guild in client.guilds:
30-
if guild.name == "magic-321-magic":
31-
guild_exclude = guild
32-
break
33-
else:
34-
guild_exclude = await client.create_guild(name="magic-321-magic")
35-
36-
return guild_include, guild_exclude
37-
38-
39-
@pytest_asyncio.fixture(scope="module")
40-
async def channels(guilds):
41-
guild: daf.discord.Guild
42-
guild, _ = guilds
43-
t_channels = []
44-
v_channels = []
45-
for i in range(10):
46-
t_channels.append(await guild.create_text_channel(f"testpy-{i}"))
47-
v_channels.append(await guild.create_voice_channel(f"testpy-{i}"))
48-
49-
yield t_channels, v_channels
50-
for channel in t_channels + v_channels:
51-
await channel.delete()
52-
5313

5414
@pytest.mark.asyncio
5515
async def test_autoguild(guilds):
@@ -70,41 +30,46 @@ async def test_autochannel(guilds, channels):
7030
"""
7131
Tests if AutoCHANNEL functions properly.
7232
"""
73-
guild: daf.discord.Guild
74-
guild, _ = guilds
75-
text_channels, voice_channels = channels
76-
auto_channel = daf.message.AutoCHANNEL(
33+
daf_guild = None
34+
try:
35+
guild: daf.discord.Guild
36+
guild, _ = guilds
37+
text_channels, voice_channels = channels
38+
auto_channel = daf.message.AutoCHANNEL(
39+
"testpy-[0-9]", "testpy-[5-9]")
40+
auto_channel2 = daf.message.AutoCHANNEL(
7741
"testpy-[0-9]", "testpy-[5-9]")
78-
auto_channel2 = daf.message.AutoCHANNEL(
79-
"testpy-[0-9]", "testpy-[5-9]")
80-
81-
daf_guild = daf.GUILD(
82-
guild,
83-
messages=[
84-
tm := daf.TextMESSAGE(None, timedelta(seconds=1), "Hello World", auto_channel),
85-
vc := daf.VoiceMESSAGE(None, timedelta(seconds=1), daf.AUDIO("https://www.youtube.com/watch?v=IGQBtbKSVhY"), auto_channel2)
86-
]
87-
)
88-
await daf.add_object(daf_guild)
89-
sort_key = lambda x: x.name
90-
auto_channel._process()
91-
auto_channel2._process()
92-
assert sorted(text_channels[:5], key=sort_key) == sorted(auto_channel.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to only find first 5 text channels"
93-
assert sorted(voice_channels[:5], key=sort_key) == sorted(auto_channel2.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to only find first 5 voice channels"
94-
95-
await auto_channel.update(exclude_pattern=None)
96-
await auto_channel2.update(exclude_pattern=None)
97-
auto_channel._process()
98-
auto_channel2._process()
99-
assert sorted(text_channels, key=sort_key) == sorted(auto_channel.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to find all text channels"
100-
assert sorted(voice_channels, key=sort_key) == sorted(auto_channel2.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to find all voice channels"
101-
102-
# Test update
103-
await daf_guild.update()
104-
await tm.update()
105-
await vc.update()
106-
await auto_channel.update()
107-
await auto_channel2.update()
42+
43+
daf_guild = daf.GUILD(
44+
guild,
45+
messages=[
46+
tm := daf.TextMESSAGE(None, timedelta(seconds=1), "Hello World", auto_channel),
47+
vc := daf.VoiceMESSAGE(None, timedelta(seconds=1), daf.AUDIO("https://www.youtube.com/watch?v=IGQBtbKSVhY"), auto_channel2)
48+
]
49+
)
50+
await daf.add_object(daf_guild)
51+
sort_key = lambda x: x.name
52+
auto_channel._process()
53+
auto_channel2._process()
54+
assert sorted(text_channels[:5], key=sort_key) == sorted(auto_channel.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to only find first 5 text channels"
55+
assert sorted(voice_channels[:5], key=sort_key) == sorted(auto_channel2.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to only find first 5 voice channels"
56+
57+
await auto_channel.update(exclude_pattern=None)
58+
await auto_channel2.update(exclude_pattern=None)
59+
auto_channel._process()
60+
auto_channel2._process()
61+
assert sorted(text_channels, key=sort_key) == sorted(auto_channel.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to find all text channels"
62+
assert sorted(voice_channels, key=sort_key) == sorted(auto_channel2.channels, key=sort_key), "Correct behavior would be for AutoCHANNEL to find all voice channels"
63+
64+
# Test update
65+
await daf_guild.update()
66+
await tm.update()
67+
await vc.update()
68+
await auto_channel.update()
69+
await auto_channel2.update()
70+
finally:
71+
if daf_guild is not None:
72+
daf.remove_object(daf_guild)
10873

10974

11075

testing/test_logging.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
import pathlib
88
import json
99

10-
TEST_GUILD_ID = 863071397207212052
1110
TEST_USER_ID = 145196308985020416
1211
C_FILE_NAME_FORBIDDEN_CHAR = ('<','>','"','/','\\','|','?','*',":")
1312

1413

1514
@pytest.mark.asyncio
16-
async def test_logging_json(text_channels):
15+
async def test_logging_json(channels, guilds):
1716
"Test if json logging works"
17+
dc_guild, _ = guilds
18+
text_channels, _ = channels
1819
try:
1920
json_logger = daf.LoggerJSON("./History")
2021
await json_logger.initialize()
2122
daf.logging._set_logger(json_logger)
2223

23-
guild = daf.GUILD(TEST_GUILD_ID, logging=True)
24+
guild = daf.GUILD(dc_guild, logging=True)
2425
await guild.initialize()
2526
guild_context = guild.generate_log_context()
2627
await guild.add_message(tm := daf.TextMESSAGE(None, timedelta(seconds=5), data="Hello World", channels=text_channels))
@@ -68,17 +69,19 @@ def check_json_results(message_context):
6869

6970

7071
@pytest.mark.asyncio
71-
async def test_logging_sql(text_channels):
72+
async def test_logging_sql(channels, guilds):
7273
"""
7374
Tests if SQL logging works(only sqlite).
7475
It does not test any of the results as it assumes the database
7576
will raise an exception if anything is wrong.
7677
"""
78+
dc_guild, _ = guilds
79+
text_channels, _ = channels
7780
try:
7881
sql_logger = daf.LoggerSQL(database="testdb")
7982
await sql_logger.initialize()
8083
daf.logging._set_logger(sql_logger)
81-
guild = daf.GUILD(TEST_GUILD_ID, logging=True)
84+
guild = daf.GUILD(dc_guild, logging=True)
8285
await guild.initialize()
8386
guild_context = guild.generate_log_context()
8487
await guild.add_message(tm := daf.TextMESSAGE(None, timedelta(seconds=5), data="Hello World", channels=text_channels))

testing/test_period_dynamic.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# SIMULATION
1010

1111
# CONFIGURATION
12-
TEST_GUILD_ID = 863071397207212052
1312
TEST_USER_ID = 145196308985020416
1413
TEST_SEND_PERIOD_TEXT = timedelta(seconds=6)
1514
TEST_SEND_PERIOD_VOICE = timedelta(seconds=10)
@@ -19,14 +18,17 @@
1918

2019

2120
@pytest.mark.asyncio
22-
async def test_text_period(text_channels):
21+
async def test_text_period(channels, guilds):
2322
"Tests if the period and dynamic data works"
24-
guild = daf.GUILD(TEST_GUILD_ID)
23+
text_channels, _ = channels
24+
dc_guild, _ = guilds
25+
guild = daf.GUILD(dc_guild)
2526
user = daf.USER(TEST_USER_ID)
2627
try:
28+
await asyncio.sleep(5) # Clears rate limit
2729
await daf.add_object(guild)
2830
await daf.add_object(user)
29-
31+
3032
@daf.data_function
3133
def dynamic_getter(items: list):
3234
item = items.pop(0)
@@ -46,7 +48,6 @@ async def dynamic_getter_async(items: list):
4648
TEXT_MESSAGE_TEST_MESSAGE = dynamic_getter(data_.copy())
4749
DIRECT_MESSAGE_TEST_MESSAGE = dynamic_getter_async(data_.copy())
4850

49-
await asyncio.sleep(10) # Clears rate limit
5051
client = daf.get_client()
5152

5253
# Preparation
@@ -76,7 +77,7 @@ async def dynamic_getter_async(items: list):
7677
assert item.to_dict() in embeds, "TextMESSAGE embed not in message embeds"
7778

7879
guild.remove_message(text_message)
79-
await asyncio.sleep(10) # Clears rate limit
80+
await asyncio.sleep(5)
8081
# Test DirectMESSAGE
8182
direct_message = daf.message.DirectMESSAGE(None, TEST_SEND_PERIOD_TEXT, DIRECT_MESSAGE_TEST_MESSAGE, "send",
8283
start_in=TEST_SEND_PERIOD_TEXT, remove_after=None)
@@ -107,12 +108,14 @@ async def dynamic_getter_async(items: list):
107108

108109

109110
@pytest.mark.asyncio
110-
async def test_voice_period(voice_channels):
111+
async def test_voice_period(channels, guilds):
111112
"Tests if the period and dynamic data works"
112-
guild = daf.GUILD(TEST_GUILD_ID)
113+
_, voice_channels = channels
114+
dc_guild, _ = guilds
115+
guild = daf.GUILD(dc_guild)
113116
user = daf.USER(TEST_USER_ID)
114117
try:
115-
await asyncio.sleep(10)
118+
await asyncio.sleep(5)
116119

117120
@daf.data_function
118121
def dynamic_getter(items: list):
@@ -127,7 +130,7 @@ def dynamic_getter(items: list):
127130
(3, daf.AUDIO("https://www.youtube.com/watch?v=1O0yazhqaxs"))
128131
]
129132
VOICE_MESSAGE_TEST_MESSAGE = dynamic_getter(data_.copy())
130-
guild = daf.get_guild_user(TEST_GUILD_ID)
133+
guild = daf.get_guild_user(dc_guild)
131134
client = daf.get_client()
132135

133136
test_period_secs = TEST_SEND_PERIOD_VOICE.total_seconds()

testing/test_send_normal.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import asyncio
77

88
# CONFIGURATION
9-
TEST_GUILD_ID = 863071397207212052
109
TEST_USER_ID = 145196308985020416
1110
VOICE_MESSAGE_TEST_LENGTH = 3 # Test if entire message is played
1211

1312

1413
@pytest.mark.asyncio
15-
async def test_text_message_send(text_channels):
14+
async def test_text_message_send(channels, guilds):
1615
"This tests if all the text messages succeed in their sends"
16+
dc_guild, _ = guilds
17+
text_channels, _ = channels
1718
TEXT_MESSAGE_TEST_MESSAGE = "Hello world", daf.discord.Embed(title="Hello world")
1819

19-
guild = daf.GUILD(TEST_GUILD_ID)
20+
guild = daf.GUILD(dc_guild)
2021
user = daf.USER(TEST_USER_ID)
2122
text_message = daf.message.TextMESSAGE(None, timedelta(seconds=5), TEXT_MESSAGE_TEST_MESSAGE, text_channels,
2223
"send", start_in=timedelta(), remove_after=None)
@@ -57,12 +58,14 @@ async def test_text_message_send(text_channels):
5758

5859

5960
@pytest.mark.asyncio
60-
async def test_voice_message_send(voice_channels):
61+
async def test_voice_message_send(channels, guilds):
6162
"This tests if all the voice messages succeed in their sends"
63+
dc_guild, _ = guilds
64+
_, voice_channels = channels
6265
await asyncio.sleep(5) # Wait for any messages still playing
6366
VOICE_MESSAGE_TEST_MESSAGE = daf.AUDIO("https://www.youtube.com/watch?v=1O0yazhqaxs") # 3 second countdown
6467

65-
guild = daf.GUILD(TEST_GUILD_ID)
68+
guild = daf.GUILD(dc_guild)
6669
voice_message = daf.message.VoiceMESSAGE(None, timedelta(seconds=20), VOICE_MESSAGE_TEST_MESSAGE, voice_channels,
6770
volume=50, start_in=timedelta(), remove_after=None)
6871

0 commit comments

Comments
 (0)