Skip to content

Commit

Permalink
Update tests for new version of responses
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Mar 13, 2024
1 parent 9431cfb commit 551acd0
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 150 deletions.
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"github.vscode-github-actions",
"github.vscode-pull-request-github"
]
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configurations": [
{
"name": "Run CloudBot",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "cloudbot",
"justMyCode": true
Expand Down
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"coverage-gutters.showLineCoverage": true,
"coverage-gutters.showRulerCoverage": true
}
4 changes: 2 additions & 2 deletions cloudbot/util/async_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from asyncio import AbstractEventLoop
from asyncio.tasks import Task
from functools import partial
from typing import List, cast
from typing import List, Optional, cast

from cloudbot.util.func_utils import call_with_args

Expand Down Expand Up @@ -62,7 +62,7 @@ def create_future(loop):
return loop.create_future()


def get_all_tasks(loop: AbstractEventLoop = None) -> List[Task]:
def get_all_tasks(loop: Optional[AbstractEventLoop] = None) -> List[Task]:
"""
Get a list of all tasks for the current loop
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pytest == 6.2.5
pytest-asyncio == 0.20.3
pytest-cov == 4.1.0
pytest-random-order == 1.0.4
responses == 0.16.0
responses == 0.25.0
types-requests == 2.27.7
types-setuptools == 57.4.7
types-six == 1.16.2
Expand Down
36 changes: 27 additions & 9 deletions tests/plugin_tests/test_brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from unittest.mock import MagicMock

import pytest

import responses
from responses.matchers import query_param_matcher
from cloudbot.bot import bot
from plugins import brew

Expand All @@ -25,10 +26,15 @@ def test_empty_body(mock_bot_factory, mock_requests, unset_bot, event_loop):
)
mock_requests.add(
"GET",
"http://api.brewerydb.com/v2/search"
"?format=json&key=APIKEY&type=beer&withBreweries=Y&q=some+text",
match_querystring=True,
"http://api.brewerydb.com/v2/search",
json={},
match=[query_param_matcher({
'format': 'json',
'key': 'APIKEY',
'type': 'beer',
'withBreweries': 'Y',
'q': 'some text',
})]
)

reply = MagicMock()
Expand All @@ -48,9 +54,15 @@ def test_no_results(mock_bot_factory, mock_requests, unset_bot, event_loop):
mock_requests.add(
"GET",
"http://api.brewerydb.com/v2/search"
"?format=json&key=APIKEY&type=beer&withBreweries=Y&q=some+text",
match_querystring=True,
json={"totalResults": 0},
,
match=[query_param_matcher({
'format': 'json',
'key': 'APIKEY',
'type': 'beer',
'withBreweries': 'Y',
'q': 'some text',
})],
json={"totalResults": 0},
)

reply = MagicMock()
Expand Down Expand Up @@ -124,8 +136,14 @@ def test_results(
mock_requests.add(
"GET",
"http://api.brewerydb.com/v2/search"
"?format=json&key=APIKEY&type=beer&withBreweries=Y&q=some+text",
match_querystring=True,
,
match=[query_param_matcher({
'format': 'json',
'key': 'APIKEY',
'type': 'beer',
'withBreweries': 'Y',
'q': 'some text',
})],
json={
"totalResults": 1,
"data": [beer],
Expand Down
35 changes: 22 additions & 13 deletions tests/plugin_tests/test_horoscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import pytest
import requests

from responses import RequestsMock
from responses.matchers import query_param_matcher
from plugins import horoscope
from tests.util.mock_db import MockDB

BASE_URL = "http://www.horoscope.com/us/horoscopes/general/"\
"horoscope-general-daily-today.aspx"

URL = (
"http://www.horoscope.com/us/horoscopes/general/"
Expand All @@ -19,18 +24,20 @@ def setup_db(mock_db):
return sess


def test_horoscope(mock_requests, mock_db):
def test_horoscope(mock_requests: RequestsMock, mock_db: MockDB):
sess = setup_db(mock_db)
mock_requests.add(
"GET",
URL.format(sign=1),
BASE_URL,
headers={"User-Agent": "Some user agent"},
body="""
<div class="main-horoscope">
<p>Some horoscope text</p>
</div>
""",
match_querystring=True,
match=[
query_param_matcher({'sign': 1})
],
)

event = MagicMock()
Expand All @@ -46,7 +53,7 @@ def test_horoscope(mock_requests, mock_db):
assert mock_db.get_data(horoscope.table) == [("some_user", "aries")]


def test_invalid_syntax(mock_requests, mock_db):
def test_invalid_syntax(mock_requests: RequestsMock, mock_db: MockDB):
sess = setup_db(mock_db)

event = MagicMock()
Expand All @@ -60,19 +67,21 @@ def test_invalid_syntax(mock_requests, mock_db):
assert event.notice_doc.call_count == 1


def test_database_read(mock_requests, mock_db):
def test_database_read(mock_requests: RequestsMock, mock_db: MockDB):
sess = setup_db(mock_db)

mock_requests.add(
"GET",
URL.format(sign=4),
BASE_URL,
headers={"User-Agent": "Some user agent"},
body="""
<div class="main-horoscope">
<p>Some horoscope text</p>
</div>
""",
match_querystring=True,
match=[
query_param_matcher({'sign':4})
],
)

mock_db.add_row(horoscope.table, nick="some_user", sign="cancer")
Expand All @@ -88,18 +97,18 @@ def test_database_read(mock_requests, mock_db):
event.message.assert_called_once_with("\x02cancer\x02 Some horoscope text")


def test_parse_fail(mock_requests, mock_db):
def test_parse_fail(mock_requests: RequestsMock, mock_db: MockDB):
sess = setup_db(mock_db)

mock_requests.add(
"GET",
URL.format(sign=4),
BASE_URL,
headers={"User-Agent": "Some user agent"},
body="""
<div class="main-horoscope">
</div>
""",
match_querystring=True,
match=[query_param_matcher({'sign':4})],
)

event = MagicMock()
Expand All @@ -112,7 +121,7 @@ def test_parse_fail(mock_requests, mock_db):
event.reply.assert_called_once_with("Unable to parse horoscope posting")


def test_page_error(mock_requests, mock_db):
def test_page_error(mock_requests: RequestsMock, mock_db: MockDB):
sess = setup_db(mock_db)

event = MagicMock()
Expand All @@ -131,7 +140,7 @@ def test_page_error(mock_requests, mock_db):
)


def test_bad_sign(mock_requests):
def test_bad_sign(mock_requests: RequestsMock):
db = MagicMock()
event = MagicMock()
sign = "some_sign"
Expand Down
31 changes: 17 additions & 14 deletions tests/plugin_tests/test_lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from cloudbot.bot import bot
from plugins import lastfm

from responses.matchers import query_param_matcher

def test_get_account(mock_db, mock_requests):
lastfm.table.create(mock_db.engine)
Expand Down Expand Up @@ -77,21 +77,24 @@ def test_api_error_message(mock_requests, mock_api_keys):


def test_getartisttags(mock_requests, mock_api_keys):
url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&method=artist.getTopTags&api_key=APIKEY"
url = "http://ws.audioscrobbler.com/2.0/"
mock_requests.add(
"GET",
url,
json={
"toptags": {},
},
match_querystring=True,
match=[query_param_matcher({'format':'json','artist':'foobar','autocorrect':1,'method':'artist.getTopTags','api_key':'APIKEY'})],
)
res = lastfm.getartisttags("foobar")
assert res == "no tags"


class TestGetArtistTags:
url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&method=artist.getTopTags&api_key=APIKEY"
url = "http://ws.audioscrobbler.com/2.0/"

def get_params(self):
return {"format":"json","artist":"foobar","autocorrect":"1","method":"artist.getTopTags","api_key":"APIKEY"}

def get_tags(self):
return lastfm.getartisttags("foobar")
Expand All @@ -103,7 +106,7 @@ def test_missing_tags(self, mock_requests, mock_api_keys):
json={
"toptags": {},
},
match_querystring=True,
match=[query_param_matcher(self.get_params())],
)
res = self.get_tags()
assert res == "no tags"
Expand All @@ -115,7 +118,7 @@ def test_no_tags(self, mock_requests, mock_api_keys):
json={
"toptags": {"tags": []},
},
match_querystring=True,
match=[query_param_matcher(self.get_params())],
)
res = self.get_tags()
assert res == "no tags"
Expand All @@ -125,7 +128,7 @@ def test_non_existent_artist(self, mock_requests, mock_api_keys):
"GET",
self.url,
json={"error": 6, "message": "Missing artist."},
match_querystring=True,
match=[query_param_matcher(self.get_params())],
)
res = self.get_tags()
assert res == "no tags"
Expand All @@ -150,19 +153,19 @@ def test_tags(self, mock_requests, mock_api_keys):
]
},
},
match_querystring=True,
match=[query_param_matcher(self.get_params())],
)
res = self.get_tags()
assert res == "tag2, tag4, tag5, tag6"


def test_gettracktags(mock_requests, mock_api_keys):
url = "http://ws.audioscrobbler.com/2.0/?format=json&artist=foobar&autocorrect=1&track=foobaz&method=track.getTopTags&api_key=APIKEY"
url = "http://ws.audioscrobbler.com/2.0/"
mock_requests.add(
"GET",
url,
json={"toptags": {}},
match_querystring=True,
match=[query_param_matcher({"format":"json","artist":"foobar", "autocorrect":1,"track":"foobaz","method":"track.getTopTags","api_key":"APIKEY"})],
)
res = lastfm.gettracktags("foobar", "foobaz")
assert res == "no tags"
Expand Down Expand Up @@ -212,8 +215,8 @@ def test_topweek_self(self, mock_api_keys, mock_requests, mock_db):

mock_requests.add(
"GET",
"http://ws.audioscrobbler.com/2.0/?format=json&user=bar&limit=10&period=7day&method=user.gettopartists&api_key=APIKEY",
match_querystring=True,
"http://ws.audioscrobbler.com/2.0/",
match=[query_param_matcher({"format":"json","user":"bar","limit":"10", "period": "7day","method":"user.gettopartists","api_key":"APIKEY"})],
json={
"topartists": {
"artist": [
Expand All @@ -237,8 +240,8 @@ def test_toptrack_self(self, mock_api_keys, mock_requests, mock_db):

mock_requests.add(
"GET",
"http://ws.audioscrobbler.com/2.0/?format=json&user=bar&limit=5&method=user.gettoptracks&api_key=APIKEY",
match_querystring=True,
"http://ws.audioscrobbler.com/2.0/",
match=[query_param_matcher({"format":"json","user":"bar","limit":"5","method":"user.gettoptracks","api_key":"APIKEY"})],
json={
"toptracks": {
"track": [
Expand Down
3 changes: 3 additions & 0 deletions tests/plugin_tests/test_optout.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ def test_match():
with patch.dict(optout.optout_cache, clear=True, test=[opt]):
res = optout.optout_sieve(bot, event, _hook)
assert res is None

def test_get_channel_optouts():
pass
Loading

0 comments on commit 551acd0

Please sign in to comment.