|
1 | | -import typing as t |
2 | | - |
3 | 1 | import mock |
4 | 2 | import pytest |
5 | | -from aiohttp import ClientSession |
6 | 3 |
|
7 | 4 | import topgg |
8 | | -from topgg import errors |
9 | | - |
10 | | - |
11 | | -@pytest.fixture |
12 | | -def session() -> ClientSession: |
13 | | - return mock.Mock(ClientSession) |
14 | | - |
15 | | - |
16 | | -@pytest.fixture |
17 | | -def client() -> topgg.DBLClient: |
18 | | - client = topgg.DBLClient(token="TOKEN", default_bot_id=1234) |
19 | | - client.http = mock.Mock(topgg.http.HTTPClient) |
20 | | - return client |
21 | | - |
22 | | - |
23 | | -@pytest.mark.asyncio |
24 | | -async def test_HTTPClient_with_external_session(session: ClientSession): |
25 | | - http = topgg.http.HTTPClient("TOKEN", session=session) |
26 | | - assert not http._own_session |
27 | | - await http.close() |
28 | | - session.close.assert_not_called() |
29 | | - |
30 | | - |
31 | | -@pytest.mark.asyncio |
32 | | -async def test_HTTPClient_with_no_external_session(session: ClientSession): |
33 | | - http = topgg.http.HTTPClient("TOKEN") |
34 | | - http.session = session |
35 | | - assert http._own_session |
36 | | - await http.close() |
37 | | - session.close.assert_called_once() |
38 | 5 |
|
39 | 6 |
|
40 | | -@pytest.mark.asyncio |
41 | | -async def test_DBLClient_get_bot_votes_with_no_default_bot_id(): |
42 | | - client = topgg.DBLClient("TOKEN") |
43 | | - with pytest.raises( |
44 | | - errors.ClientException, |
45 | | - match="you must set default_bot_id when constructing the client.", |
46 | | - ): |
47 | | - await client.get_bot_votes() |
| 7 | +MOCK_TOKEN = ".eyJfdCI6IiIsImlkIjoiMzY0ODA2MDI5ODc2NTU1Nzc2In0=." |
48 | 8 |
|
49 | 9 |
|
50 | 10 | @pytest.mark.asyncio |
51 | 11 | async def test_DBLClient_post_guild_count_with_no_args(): |
52 | | - client = topgg.DBLClient("TOKEN", default_bot_id=1234) |
| 12 | + client = topgg.DBLClient(MOCK_TOKEN) |
53 | 13 | with pytest.raises(TypeError, match="stats or guild_count must be provided."): |
54 | 14 | await client.post_guild_count() |
55 | 15 |
|
56 | 16 |
|
57 | | -@pytest.mark.parametrize( |
58 | | - "method, kwargs", |
59 | | - [ |
60 | | - (topgg.DBLClient.get_guild_count, {}), |
61 | | - (topgg.DBLClient.get_bot_info, {}), |
62 | | - ( |
63 | | - topgg.DBLClient.generate_widget, |
64 | | - { |
65 | | - "options": topgg.types.WidgetOptions(), |
66 | | - }, |
67 | | - ), |
68 | | - ], |
69 | | -) |
70 | 17 | @pytest.mark.asyncio |
71 | | -async def test_DBLClient_get_guild_count_with_no_id( |
72 | | - method: t.Callable, kwargs: t.Dict[str, t.Any] |
73 | | -): |
74 | | - client = topgg.DBLClient("TOKEN") |
75 | | - with pytest.raises( |
76 | | - errors.ClientException, match="bot_id or default_bot_id is unset." |
77 | | - ): |
78 | | - await method(client, **kwargs) |
79 | | - |
80 | | - |
81 | | -@pytest.mark.asyncio |
82 | | -async def test_closed_DBLClient_raises_exception(): |
83 | | - client = topgg.DBLClient("TOKEN") |
84 | | - assert not client.is_closed |
85 | | - await client.close() |
86 | | - assert client.is_closed |
87 | | - with pytest.raises(errors.ClientException, match="client has been closed."): |
88 | | - await client.get_weekend_status() |
89 | | - |
90 | | - |
91 | | -@pytest.mark.asyncio |
92 | | -async def test_DBLClient_get_weekend_status(client: topgg.DBLClient): |
93 | | - client.http.get_weekend_status = mock.AsyncMock() |
| 18 | +async def test_DBLClient_get_weekend_status(monkeypatch): |
| 19 | + client = topgg.DBLClient(MOCK_TOKEN) |
| 20 | + monkeypatch.setattr("topgg.DBLClient._DBLClient__request", mock.AsyncMock()) |
94 | 21 | await client.get_weekend_status() |
95 | | - client.http.get_weekend_status.assert_called_once() |
| 22 | + client._DBLClient__request.assert_called_once() |
96 | 23 |
|
97 | 24 |
|
98 | 25 | @pytest.mark.asyncio |
99 | | -async def test_DBLClient_post_guild_count(client: topgg.DBLClient): |
100 | | - client.http.post_guild_count = mock.AsyncMock() |
| 26 | +async def test_DBLClient_post_guild_count(monkeypatch): |
| 27 | + client = topgg.DBLClient(MOCK_TOKEN) |
| 28 | + monkeypatch.setattr("topgg.DBLClient._DBLClient__request", mock.AsyncMock()) |
101 | 29 | await client.post_guild_count(guild_count=123) |
102 | | - client.http.post_guild_count.assert_called_once() |
| 30 | + client._DBLClient__request.assert_called_once() |
103 | 31 |
|
104 | 32 |
|
105 | 33 | @pytest.mark.asyncio |
106 | | -async def test_DBLClient_get_guild_count(client: topgg.DBLClient): |
107 | | - client.http.get_guild_count = mock.AsyncMock(return_value={}) |
| 34 | +async def test_DBLClient_get_guild_count(monkeypatch): |
| 35 | + client = topgg.DBLClient(MOCK_TOKEN) |
| 36 | + monkeypatch.setattr("topgg.DBLClient._DBLClient__request", mock.AsyncMock(return_value={})) |
108 | 37 | await client.get_guild_count() |
109 | | - client.http.get_guild_count.assert_called_once() |
| 38 | + client._DBLClient__request.assert_called_once() |
110 | 39 |
|
111 | 40 |
|
112 | 41 | @pytest.mark.asyncio |
113 | | -async def test_DBLClient_get_bot_votes(client: topgg.DBLClient): |
114 | | - client.http.get_bot_votes = mock.AsyncMock(return_value=[]) |
| 42 | +async def test_DBLClient_get_bot_votes(monkeypatch): |
| 43 | + client = topgg.DBLClient(MOCK_TOKEN) |
| 44 | + monkeypatch.setattr("topgg.DBLClient._DBLClient__request", mock.AsyncMock(return_value=[])) |
115 | 45 | await client.get_bot_votes() |
116 | | - client.http.get_bot_votes.assert_called_once() |
117 | | - |
118 | | - |
119 | | -@pytest.mark.asyncio |
120 | | -async def test_DBLClient_get_bots(client: topgg.DBLClient): |
121 | | - client.http.get_bots = mock.AsyncMock(return_value={"results": []}) |
122 | | - await client.get_bots() |
123 | | - client.http.get_bots.assert_called_once() |
124 | | - |
125 | | - |
126 | | -@pytest.mark.asyncio |
127 | | -async def test_DBLClient_get_user_info(client: topgg.DBLClient): |
128 | | - client.http.get_user_info = mock.AsyncMock(return_value={}) |
129 | | - await client.get_user_info(1234) |
130 | | - client.http.get_user_info.assert_called_once() |
| 46 | + client._DBLClient__request.assert_called_once() |
131 | 47 |
|
132 | 48 |
|
133 | 49 | @pytest.mark.asyncio |
134 | | -async def test_DBLClient_get_user_vote(client: topgg.DBLClient): |
135 | | - client.http.get_user_vote = mock.AsyncMock(return_value={"voted": 1}) |
| 50 | +async def test_DBLClient_get_user_vote(monkeypatch): |
| 51 | + client = topgg.DBLClient(MOCK_TOKEN) |
| 52 | + monkeypatch.setattr("topgg.DBLClient._DBLClient__request", mock.AsyncMock(return_value={"voted": 1})) |
136 | 53 | await client.get_user_vote(1234) |
137 | | - client.http.get_user_vote.assert_called_once() |
| 54 | + client._DBLClient__request.assert_called_once() |
0 commit comments