1+ import contextlib
2+ from datetime import timedelta
13from unittest import mock
24from unittest .mock import AsyncMock , patch
3- import contextlib
4- import discord
5-
6- from django .db import connections
75
6+ import discord
87import pytest
98from asgiref .sync import sync_to_async
10- from core .bot .main import ping , poll_database , qlen , source , version , wiki , close
9+ from core .bot .main import close , ping , poll_database , qlen , source , version , wiki
1110from core .models import DiscordMessage
11+ from django .db import connections
1212from django .utils import timezone
1313
1414# NOTE(artcz)
@@ -101,6 +101,7 @@ async def test_wiki_command():
101101 suppress_embeds = True ,
102102 )
103103
104+
104105@pytest .mark .asyncio
105106async def test_close_command_working ():
106107 # Mock context
@@ -118,6 +119,7 @@ async def test_close_command_working():
118119 suppress_embeds = True ,
119120 )
120121
122+
121123@pytest .mark .asyncio
122124async def test_close_command_notworking ():
123125 # Mock context
@@ -131,7 +133,7 @@ async def test_close_command_notworking():
131133 ctx .channel .send .assert_called_once_with (
132134 "The !close command is intended to be used inside a thread/post" ,
133135 suppress_embeds = True ,
134- delete_after = 5
136+ delete_after = 5 ,
135137 )
136138
137139
@@ -235,12 +237,40 @@ async def test_polling_messages_sends_nothing_if_all_messages_are_sent():
235237
236238@pytest .mark .asyncio
237239@pytest .mark .django_db
238- async def test_polling_messages_sends_message_if_not_sent_and_sets_sent_at ():
240+ async def test_polling_messages_sends_nothing_if_all_messages_in_the_future ():
241+ mock_channel = AsyncMock ()
242+ mock_channel .send = AsyncMock ()
243+ await DiscordMessage .objects .acreate (
244+ send_after = timezone .now () + timedelta (hours = 3 ),
245+ sent_at = None ,
246+ )
247+
248+ with patch ("core.bot.main.bot.get_channel" , return_value = mock_channel ):
249+ await poll_database ()
250+
251+ mock_channel .send .assert_not_called ()
252+
253+
254+ @pytest .mark .asyncio
255+ @pytest .mark .django_db
256+ @pytest .mark .parametrize (
257+ "send_after" ,
258+ [
259+ None ,
260+ timezone .now (),
261+ ],
262+ ids = [
263+ "send_after_isnull" ,
264+ "send_after_is_in_the_past" ,
265+ ],
266+ )
267+ async def test_polling_messages_sends_message_if_not_sent_and_sets_sent_at (send_after ):
239268 start = timezone .now ()
240269 dm = await DiscordMessage .objects .acreate (
241270 channel_id = "1234" ,
242271 content = "asdf" ,
243272 sent_at = None ,
273+ send_after = send_after ,
244274 )
245275 mock_channel = AsyncMock ()
246276 mock_channel .send = AsyncMock ()
0 commit comments