|
1 | | -import datetime |
2 | | - |
3 | | -import mock |
4 | | -import pytest |
5 | | -from aiohttp import ClientSession |
6 | | -from pytest_mock import MockerFixture |
7 | | - |
8 | | -from topgg import DBLClient |
9 | | -from topgg.autopost import AutoPoster |
10 | | -from topgg.errors import HTTPException, TopGGException |
11 | | - |
12 | | - |
13 | | -MOCK_TOKEN = ".eyJfdCI6IiIsImlkIjoiMzY0ODA2MDI5ODc2NTU1Nzc2In0=." |
14 | | - |
15 | | - |
16 | | -@pytest.fixture |
17 | | -def session() -> ClientSession: |
18 | | - return mock.Mock(ClientSession) |
19 | | - |
20 | | - |
21 | | -@pytest.fixture |
22 | | -def autopost(session: ClientSession) -> AutoPoster: |
23 | | - return AutoPoster(DBLClient(MOCK_TOKEN, session=session)) |
24 | | - |
25 | | - |
26 | | -@pytest.mark.asyncio |
27 | | -async def test_AutoPoster_breaks_autopost_loop_on_401( |
28 | | - mocker: MockerFixture, session: ClientSession |
29 | | -) -> None: |
30 | | - response = mock.Mock("reason, status") |
31 | | - response.reason = "Unauthorized" |
32 | | - response.status = 401 |
33 | | - |
34 | | - mocker.patch( |
35 | | - "topgg.DBLClient.post_guild_count", side_effect=HTTPException(response, {}) |
36 | | - ) |
37 | | - |
38 | | - callback = mock.Mock() |
39 | | - autopost = DBLClient(MOCK_TOKEN, session=session).autopost().stats(callback) |
40 | | - assert isinstance(autopost, AutoPoster) |
41 | | - assert not isinstance(autopost.stats()(callback), AutoPoster) |
42 | | - |
43 | | - with pytest.raises(HTTPException): |
44 | | - await autopost.start() |
45 | | - |
46 | | - callback.assert_called_once() |
47 | | - assert not autopost.is_running |
48 | | - |
49 | | - |
50 | | -@pytest.mark.asyncio |
51 | | -async def test_AutoPoster_raises_missing_stats(autopost: AutoPoster) -> None: |
52 | | - with pytest.raises( |
53 | | - TopGGException, match="you must provide a callback that returns the stats." |
54 | | - ): |
55 | | - await autopost.start() |
56 | | - |
57 | | - |
58 | | -@pytest.mark.asyncio |
59 | | -async def test_AutoPoster_raises_already_running(autopost: AutoPoster) -> None: |
60 | | - autopost.stats(mock.Mock()).start() |
61 | | - with pytest.raises(TopGGException, match="the autopost is already running."): |
62 | | - await autopost.start() |
63 | | - |
64 | | - |
65 | | -@pytest.mark.asyncio |
66 | | -async def test_AutoPoster_interval_too_short(autopost: AutoPoster) -> None: |
67 | | - with pytest.raises(ValueError, match="interval must be greated than 900 seconds."): |
68 | | - autopost.set_interval(50) |
69 | | - |
70 | | - |
71 | | -@pytest.mark.asyncio |
72 | | -async def test_AutoPoster_error_callback( |
73 | | - mocker: MockerFixture, autopost: AutoPoster |
74 | | -) -> None: |
75 | | - error_callback = mock.Mock() |
76 | | - response = mock.Mock("reason, status") |
77 | | - response.reason = "Internal Server Error" |
78 | | - response.status = 500 |
79 | | - side_effect = HTTPException(response, {}) |
80 | | - |
81 | | - mocker.patch("topgg.DBLClient.post_guild_count", side_effect=side_effect) |
82 | | - task = autopost.on_error(error_callback).stats(mock.Mock()).start() |
83 | | - autopost.stop() |
84 | | - await task |
85 | | - error_callback.assert_called_once_with(side_effect) |
86 | | - |
87 | | - |
88 | | -def test_AutoPoster_interval(autopost: AutoPoster): |
89 | | - assert autopost.interval == 900 |
90 | | - autopost.set_interval(datetime.timedelta(hours=1)) |
91 | | - assert autopost.interval == 3600 |
92 | | - autopost.interval = datetime.timedelta(hours=2) |
93 | | - assert autopost.interval == 7200 |
94 | | - autopost.interval = 3600 |
95 | | - assert autopost.interval == 3600 |
| 1 | +import datetime |
| 2 | + |
| 3 | +import mock |
| 4 | +import pytest |
| 5 | +from aiohttp import ClientSession |
| 6 | +from pytest_mock import MockerFixture |
| 7 | + |
| 8 | +from topgg import DBLClient |
| 9 | +from topgg.autopost import AutoPoster |
| 10 | +from topgg.errors import HTTPException, TopGGException |
| 11 | + |
| 12 | + |
| 13 | +MOCK_TOKEN = ".eyJfdCI6IiIsImlkIjoiMzY0ODA2MDI5ODc2NTU1Nzc2In0=." |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def session() -> ClientSession: |
| 18 | + return mock.Mock(ClientSession) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def autopost(session: ClientSession) -> AutoPoster: |
| 23 | + return AutoPoster(DBLClient(MOCK_TOKEN, session=session)) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_AutoPoster_breaks_autopost_loop_on_401( |
| 28 | + mocker: MockerFixture, session: ClientSession |
| 29 | +) -> None: |
| 30 | + mocker.patch( |
| 31 | + "topgg.DBLClient.post_guild_count", |
| 32 | + side_effect=HTTPException("Unauthorized", 401), |
| 33 | + ) |
| 34 | + |
| 35 | + callback = mock.Mock() |
| 36 | + autopost = DBLClient(MOCK_TOKEN, session=session).autopost().stats(callback) |
| 37 | + |
| 38 | + assert isinstance(autopost, AutoPoster) |
| 39 | + assert not isinstance(autopost.stats()(callback), AutoPoster) |
| 40 | + |
| 41 | + autopost._interval = 1 |
| 42 | + |
| 43 | + with pytest.raises(HTTPException): |
| 44 | + await autopost.start() |
| 45 | + |
| 46 | + callback.assert_called_once() |
| 47 | + assert not autopost.is_running |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_AutoPoster_raises_missing_stats(autopost: AutoPoster) -> None: |
| 52 | + with pytest.raises( |
| 53 | + TopGGException, match="You must provide a callback that returns the stats." |
| 54 | + ): |
| 55 | + await autopost.start() |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_AutoPoster_raises_already_running(autopost: AutoPoster) -> None: |
| 60 | + autopost.stats(mock.Mock()).start() |
| 61 | + with pytest.raises(TopGGException, match="The autoposter is already running."): |
| 62 | + await autopost.start() |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_AutoPoster_interval_too_short(autopost: AutoPoster) -> None: |
| 67 | + with pytest.raises(ValueError, match="interval must be greater than 900 seconds."): |
| 68 | + autopost.set_interval(50) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_AutoPoster_error_callback( |
| 73 | + mocker: MockerFixture, autopost: AutoPoster |
| 74 | +) -> None: |
| 75 | + error_callback = mock.Mock() |
| 76 | + side_effect = HTTPException("Internal Server Error", 500) |
| 77 | + |
| 78 | + mocker.patch("topgg.DBLClient.post_guild_count", side_effect=side_effect) |
| 79 | + task = autopost.on_error(error_callback).stats(mock.Mock()).start() |
| 80 | + autopost.stop() |
| 81 | + await task |
| 82 | + error_callback.assert_called_once_with(side_effect) |
| 83 | + |
| 84 | + |
| 85 | +def test_AutoPoster_interval(autopost: AutoPoster): |
| 86 | + assert autopost.interval == 900 |
| 87 | + autopost.set_interval(datetime.timedelta(hours=1)) |
| 88 | + assert autopost.interval == 3600 |
| 89 | + autopost.interval = datetime.timedelta(hours=2) |
| 90 | + assert autopost.interval == 7200 |
| 91 | + autopost.interval = 3600 |
| 92 | + assert autopost.interval == 3600 |
0 commit comments