1- import asyncio
2- from typing import Optional
3-
41import mock
52import pytest
63from aiohttp import ClientSession
7- from discord import Client
8- from discord .ext .commands import Bot
9- from pytest import CaptureFixture
10- from pytest_mock import MockerFixture
114
12- from topgg import DBLClient
13- from topgg .errors import ClientException , Unauthorized , UnauthorizedDetected
5+ import topgg
146
157
16- @pytest .fixture
17- def bot () -> Client :
18- bot = mock .Mock (Client )
19- bot .loop = asyncio .get_event_loop ()
20- bot .guilds = []
21- bot .is_closed .return_value = False
22- return bot
8+ MOCK_TOKEN = ".eyJfdCI6IiIsImlkIjoiMzY0ODA2MDI5ODc2NTU1Nzc2In0=."
239
2410
2511@pytest .fixture
@@ -28,162 +14,52 @@ def session() -> ClientSession:
2814
2915
3016@pytest .fixture
31- def exc () -> Exception :
32- return Exception ("Test Exception" )
33-
34-
35- @pytest .mark .parametrize (
36- "autopost, post_shard_count, autopost_interval" ,
37- [
38- (True , True , 900 ),
39- (True , False , 900 ),
40- (True , True , None ),
41- (True , False , None ),
42- (False , False , None ),
43- (False , False , 0 ),
44- ],
45- )
46- @pytest .mark .asyncio
47- async def test_DBLClient_validates_constructor_and_passes_for_valid_values (
48- bot : Client ,
49- mocker : MockerFixture ,
50- autopost : bool ,
51- post_shard_count : bool ,
52- autopost_interval : Optional [int ],
53- session : ClientSession ,
54- ) -> None :
55- mocker .patch ("topgg.DBLClient._auto_post" , new_callable = mock .AsyncMock ) # type: ignore
56- DBLClient (
57- bot ,
58- "" ,
59- session = session ,
60- autopost = autopost ,
61- post_shard_count = post_shard_count ,
62- autopost_interval = autopost_interval ,
63- )
64-
65-
66- @pytest .mark .parametrize (
67- "autopost, post_shard_count, autopost_interval" ,
68- [
69- (True , True , 0 ),
70- (True , False , 500 ),
71- (False , True , 0 ),
72- (False , True , 900 ),
73- (False , True , None ),
74- (False , False , 1800 ),
75- ],
76- )
77- def test_DBLClient_validates_constructor_and_fails_for_invalid_values (
78- bot : Client ,
79- mocker : MockerFixture ,
80- autopost : bool ,
81- post_shard_count : bool ,
82- autopost_interval : Optional [int ],
83- session : ClientSession ,
84- ) -> None :
85- with pytest .raises (ClientException ):
86- DBLClient (
87- bot ,
88- "" ,
89- session = session ,
90- autopost = autopost ,
91- post_shard_count = post_shard_count ,
92- autopost_interval = autopost_interval ,
93- )
17+ def client (session : ClientSession ) -> topgg .DBLClient :
18+ return topgg .DBLClient (MOCK_TOKEN , session = session )
9419
9520
9621@pytest .mark .asyncio
97- async def test_DBLClient_breaks_autopost_loop_on_401 (
98- bot : Client , mocker : MockerFixture , session : ClientSession
99- ) -> None :
100- response = mock .Mock ("reason, status" )
101- response .reason = "Unauthorized"
102- response .status = 401
103-
104- mocker .patch (
105- "topgg.DBLClient.post_guild_count" , side_effect = Unauthorized (response , {})
106- )
107- mocker .patch (
108- "topgg.DBLClient._ensure_bot_user" ,
109- new_callable = mock .AsyncMock , # type: ignore
110- )
111-
112- obj = DBLClient (bot , "" , False , session = session )
113-
114- with pytest .raises (Unauthorized ):
115- await obj ._auto_post ()
22+ async def test_DBLClient_post_guild_count_with_no_args (client : topgg .DBLClient ):
23+ with pytest .raises (ValueError , match = "Got an invalid server count. Got None." ):
24+ await client .post_guild_count ()
11625
11726
11827@pytest .mark .asyncio
119- @pytest .mark .parametrize (
120- "token" ,
121- [None , "" ],
122- # treat None as str to suppress mypy
123- )
124- async def test_HTTPClient_fails_for_no_token (
125- bot : Client , token : str , session : ClientSession
126- ) -> None :
127- with pytest .raises (UnauthorizedDetected ):
128- await DBLClient (bot = bot , token = token , session = session ).post_guild_count ()
28+ async def test_DBLClient_get_weekend_status (monkeypatch , client : topgg .DBLClient ):
29+ monkeypatch .setattr ("topgg.DBLClient._DBLClient__request" , mock .AsyncMock ())
30+ await client .get_weekend_status ()
31+ client ._DBLClient__request .assert_called_once ()
12932
13033
13134@pytest .mark .asyncio
132- async def test_Client_with_default_autopost_error_handler (
133- mocker : MockerFixture ,
134- capsys : CaptureFixture [str ],
135- session : ClientSession ,
136- exc : Exception ,
137- ) -> None :
138- client = Client ()
139- mocker .patch ("topgg.DBLClient._auto_post" , new_callable = mock .AsyncMock ) # type: ignore
140- dbl = DBLClient (client , "" , True , session = session )
141- assert client .on_autopost_error == dbl .on_autopost_error
142- await client .on_autopost_error (exc )
143- assert "Ignoring exception in auto post loop" in capsys .readouterr ().err
35+ async def test_DBLClient_post_guild_count (monkeypatch , client : topgg .DBLClient ):
36+ monkeypatch .setattr ("topgg.DBLClient._DBLClient__request" , mock .AsyncMock ())
37+ await client .post_guild_count (guild_count = 123 )
38+ client ._DBLClient__request .assert_called_once ()
14439
14540
14641@pytest .mark .asyncio
147- async def test_Client_with_custom_autopost_error_handler (
148- mocker : MockerFixture , session : ClientSession , exc : Exception
149- ) -> None :
150- client = Client ()
151- state = False
42+ async def test_DBLClient_get_guild_count (monkeypatch , client : topgg .DBLClient ):
43+ monkeypatch .setattr (
44+ "topgg.DBLClient._DBLClient__request" , mock .AsyncMock (return_value = {})
45+ )
46+ await client .get_guild_count ()
47+ client ._DBLClient__request .assert_called_once ()
15248
153- @client .event
154- async def on_autopost_error (exc : Exception ) -> None :
155- nonlocal state
156- state = True
15749
158- mocker .patch ("topgg.DBLClient._auto_post" , new_callable = mock .AsyncMock ) # type: ignore
159- DBLClient (client , "" , True , session = session )
160- await client .on_autopost_error (exc )
161- assert state
50+ @pytest .mark .asyncio
51+ async def test_DBLClient_get_bot_votes (monkeypatch , client : topgg .DBLClient ):
52+ monkeypatch .setattr (
53+ "topgg.DBLClient._DBLClient__request" , mock .AsyncMock (return_value = [])
54+ )
55+ await client .get_bot_votes ()
56+ client ._DBLClient__request .assert_called_once ()
16257
16358
16459@pytest .mark .asyncio
165- async def test_Bot_with_autopost_error_listener (
166- mocker : MockerFixture ,
167- capsys : CaptureFixture [str ],
168- session : ClientSession ,
169- exc : Exception ,
170- ) -> None :
171- bot = Bot ("" )
172- state = False
173-
174- @bot .listen ()
175- async def on_autopost_error (exc : Exception ) -> None :
176- nonlocal state
177- state = True
178-
179- mocker .patch ("topgg.DBLClient._auto_post" , new_callable = mock .AsyncMock ) # type: ignore
180- DBLClient (bot , "" , True , session = session )
181-
182- # await to make sure all the listeners were run before asserting
183- # as <Bot>.dispatch schedules the events and will continue
184- # to the assert line without finishing the event callbacks
185- await bot .on_autopost_error (exc )
186- await on_autopost_error (exc )
187-
188- assert "Ignoring exception in auto post loop" not in capsys .readouterr ().err
189- assert state
60+ async def test_DBLClient_get_user_vote (monkeypatch , client : topgg .DBLClient ):
61+ monkeypatch .setattr (
62+ "topgg.DBLClient._DBLClient__request" , mock .AsyncMock (return_value = {"voted" : 1 })
63+ )
64+ await client .get_user_vote (1234 )
65+ client ._DBLClient__request .assert_called_once ()
0 commit comments