Skip to content

Commit df6e434

Browse files
authored
Merge pull request #230 from TotallyNotRobots/fix-test-branches
Fix branch coverage in tests
2 parents b8a7719 + cdfed9b commit df6e434

File tree

5 files changed

+87
-112
lines changed

5 files changed

+87
-112
lines changed

cloudbot/clients/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Client implementations
3+
"""

tests/core_tests/irc_client_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ def map_calls(calls):
1616
return [tuple(c) for c in calls]
1717

1818

19-
def make_mock_conn(name="testconn", loop=None):
20-
if loop is None:
21-
loop = asyncio.get_event_loop()
19+
def make_mock_conn(name="testconn"):
20+
loop = asyncio.get_event_loop()
2221

2322
conn = MagicMock()
2423
conn.name = name

tests/plugin_tests/test_chan_track.py

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
import asyncio
22
from unittest.mock import MagicMock
33

4-
from irclib.parser import Message, Prefix, TagList
4+
from irclib.parser import Prefix, TagList
55

66
from cloudbot.util.func_utils import call_with_args
7-
from plugins.core import chan_track
7+
from plugins.core import chan_track, server_info
88

99

1010
class MockConn:
1111
def __init__(self, bot=None):
1212
self.name = "foo"
1313
self.memory = {
14-
"server_info": {"statuses": {},},
15-
"server_caps": {"userhost-in-names": True, "multi-prefix": True,},
14+
"server_info": {
15+
"statuses": {},
16+
},
17+
"server_caps": {
18+
"userhost-in-names": True,
19+
"multi-prefix": True,
20+
},
1621
}
1722
self.nick = "BotFoo"
1823
self.bot = bot
24+
if self.bot:
25+
self.loop = self.bot.loop
26+
else:
27+
self.loop = asyncio.get_event_loop()
1928

2029
def get_statuses(self, chars):
2130
return [self.memory["server_info"]["statuses"][c] for c in chars]
2231

2332

2433
def test_replace_user_data():
25-
from plugins.core.chan_track import UsersDict, replace_user_data, Channel
26-
from plugins.core.server_info import handle_prefixes
27-
2834
conn = MockConn()
2935
serv_info = conn.memory["server_info"]
30-
handle_prefixes("(YohvV)!@%+-", serv_info)
31-
users = UsersDict(conn)
36+
server_info.handle_prefixes("(YohvV)!@%+-", serv_info)
37+
users = chan_track.UsersDict(conn)
3238
conn.memory["users"] = users
3339

34-
chan = Channel("#test", conn)
40+
chan = chan_track.Channel("#test", conn)
3541
chan.data["new_users"] = [
3642
"@+foo!bar@baz",
3743
"@ExampleUser!bar@baz",
3844
"ExampleUser2!bar@baz",
3945
"!@%+-foo1!bar@baz",
4046
]
41-
replace_user_data(conn, chan)
47+
chan_track.replace_user_data(conn, chan)
4248

4349
assert chan.users["foo"].user.mask == Prefix("foo", "bar", "baz")
4450
assert chan.users["foo1"].user.mask == Prefix("foo1", "bar", "baz")
@@ -52,25 +58,14 @@ def test_replace_user_data():
5258

5359

5460
def test_channel_members():
55-
from plugins.core.server_info import handle_prefixes, handle_chan_modes
56-
from plugins.core.chan_track import (
57-
get_users,
58-
get_chans,
59-
replace_user_data,
60-
on_nick,
61-
on_join,
62-
on_mode,
63-
on_part,
64-
on_kick,
65-
on_quit,
66-
)
67-
6861
conn = MockConn()
6962
serv_info = conn.memory["server_info"]
70-
handle_prefixes("(YohvV)!@%+-", serv_info)
71-
handle_chan_modes("IXZbegw,k,FHJLWdfjlx,ABCDKMNOPQRSTcimnprstuz", serv_info)
72-
users = get_users(conn)
73-
chans = get_chans(conn)
63+
server_info.handle_prefixes("(YohvV)!@%+-", serv_info)
64+
server_info.handle_chan_modes(
65+
"IXZbegw,k,FHJLWdfjlx,ABCDKMNOPQRSTcimnprstuz", serv_info
66+
)
67+
users = chan_track.get_users(conn)
68+
chans = chan_track.get_chans(conn)
7469

7570
chan = chans.getchan("#foo")
7671
assert chan.name == "#foo"
@@ -81,12 +76,12 @@ def test_channel_members():
8176
"-ExampleUser2!bar@baz",
8277
"!@%+-foo1!bar@baz",
8378
]
84-
replace_user_data(conn, chan)
79+
chan_track.replace_user_data(conn, chan)
8580

8681
assert users["exampleuser"].host == "baz"
8782

8883
test_user = users["exampleuser2"]
89-
on_nick("exampleuser2", ["ExampleUserFoo"], conn)
84+
chan_track.on_nick("exampleuser2", ["ExampleUserFoo"], conn)
9085

9186
assert test_user.nick == "ExampleUserFoo"
9287
assert "exampleuserfoo" in chan.users
@@ -95,26 +90,26 @@ def test_channel_members():
9590

9691
assert chan.get_member(user).status == conn.get_statuses("-")
9792

98-
on_join("nick1", "user", "host", conn, ["#bar"])
93+
chan_track.on_join("nick1", "user", "host", conn, ["#bar"])
9994

10095
assert users["Nick1"].host == "host"
10196

10297
assert chans["#Bar"].users["Nick1"].status == conn.get_statuses("")
10398

104-
on_mode(chan.name, [chan.name, "+sop", test_user.nick], conn)
99+
chan_track.on_mode(chan.name, [chan.name, "+sop", test_user.nick], conn)
105100

106101
assert chan.get_member(test_user).status == conn.get_statuses("@-")
107102

108-
on_part(chan.name, test_user.nick, conn)
103+
chan_track.on_part(chan.name, test_user.nick, conn)
109104

110105
assert test_user.nick not in chan.users
111106

112107
assert "foo" in chan.users
113-
on_kick(chan.name, "foo", conn)
108+
chan_track.on_kick(chan.name, "foo", conn)
114109
assert "foo" not in chan.users
115110

116111
assert "foo1" in chan.users
117-
on_quit("foo1", conn)
112+
chan_track.on_quit("foo1", conn)
118113
assert "foo1" not in chan.users
119114

120115

@@ -132,54 +127,30 @@ def test_channel_members():
132127

133128

134129
def test_names_handling():
135-
from plugins.core.server_info import handle_prefixes, handle_chan_modes
136-
from plugins.core.chan_track import on_join, on_part, on_kick, on_quit, on_names
137-
138130
handlers = {
139-
"JOIN": on_join,
140-
"PART": on_part,
141-
"QUIT": on_quit,
142-
"KICK": on_kick,
143-
"353": on_names,
144-
"366": on_names,
145-
}
146-
147-
chan_pos = {
148-
"JOIN": 0,
149-
"PART": 0,
150-
"KICK": 0,
151-
"353": 2,
152-
"366": 1,
131+
"JOIN": chan_track.on_join,
132+
"PART": chan_track.on_part,
133+
"QUIT": chan_track.on_quit,
134+
"KICK": chan_track.on_kick,
135+
"353": chan_track.on_names,
136+
"366": chan_track.on_names,
153137
}
154138

155139
bot = MagicMock()
156140
bot.loop = asyncio.get_event_loop()
157141

158142
conn = MockConn(bot)
159143
serv_info = conn.memory["server_info"]
160-
handle_prefixes("(YohvV)!@%+-", serv_info)
161-
handle_chan_modes("IXZbegw,k,FHJLWdfjlx,ABCDKMNOPQRSTcimnprstuz", serv_info)
144+
server_info.handle_prefixes("(YohvV)!@%+-", serv_info)
145+
server_info.handle_chan_modes(
146+
"IXZbegw,k,FHJLWdfjlx,ABCDKMNOPQRSTcimnprstuz", serv_info
147+
)
162148

163149
for line in NAMES_MOCK_TRAFFIC:
164-
line = Message.parse(line)
165-
data = {
166-
"nick": line.prefix.nick,
167-
"user": line.prefix.ident,
168-
"host": line.prefix.host,
169-
"conn": conn,
170-
"irc_paramlist": line.parameters,
171-
"irc_command": line.command,
172-
"chan": None,
173-
"target": None,
174-
}
175-
176-
if line.command in chan_pos:
177-
data["chan"] = line.parameters[chan_pos[line.command]]
178-
179-
if line.command == "KICK":
180-
data["target"] = line.parameters[1]
150+
from cloudbot.clients.irc import _IrcProtocol
181151

182-
call_with_args(handlers[line.command], data)
152+
event = _IrcProtocol(conn=conn).parse_line(line)
153+
call_with_args(handlers[event.irc_command], event)
183154

184155

185156
def test_account_tag():

tests/plugin_tests/test_cryptocurrency.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
def test_parse():
1616
assert cryptocurrency.ResponseStatus._fields != cryptocurrency.Quote._fields
1717
cryptocurrency.Platform( # nosec
18-
id=1, name="name", symbol="symbol", slug="slug", token_address="foobar",
18+
id=1,
19+
name="name",
20+
symbol="symbol",
21+
slug="slug",
22+
token_address="foobar",
1923
)
2024
assert len(cryptocurrency.Platform._fields) == 5
2125
data = {
@@ -48,7 +52,6 @@ def matches(self, request):
4852

4953
def init_response(
5054
mock_requests,
51-
crypto_map=True,
5255
fiat_map=True,
5356
quote=True,
5457
error_msg=None,
@@ -65,31 +68,30 @@ def init_response(
6568

6669
iso_fmt = "%Y-%m-%dT%H:%M:%S.%f%z"
6770

68-
if crypto_map:
69-
mock_requests.add(
70-
MatchAPIKey(
71-
"GET",
72-
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/map",
73-
api_key="APIKEY" if check_api_key else None,
74-
json={
75-
"status": {
76-
"timestamp": now.strftime(iso_fmt),
77-
"error_code": 200,
78-
"elapsed": 1,
79-
"credit_count": 1,
80-
},
81-
"data": [
82-
{
83-
"id": 1,
84-
"name": "bitcoin",
85-
"symbol": "BTC",
86-
"slug": "bitcoin",
87-
"is_active": 1,
88-
},
89-
],
71+
mock_requests.add(
72+
MatchAPIKey(
73+
"GET",
74+
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/map",
75+
api_key="APIKEY" if check_api_key else None,
76+
json={
77+
"status": {
78+
"timestamp": now.strftime(iso_fmt),
79+
"error_code": 200,
80+
"elapsed": 1,
81+
"credit_count": 1,
9082
},
91-
)
83+
"data": [
84+
{
85+
"id": 1,
86+
"name": "bitcoin",
87+
"symbol": "BTC",
88+
"slug": "bitcoin",
89+
"is_active": 1,
90+
},
91+
],
92+
},
9293
)
94+
)
9395

9496
if fiat_map:
9597
mock_requests.add(
@@ -429,6 +431,6 @@ def test_cmd_api_error(mock_requests):
429431

430432

431433
def test_list_currencies(patch_paste, mock_requests):
432-
init_response(mock_requests, quote=False, fiat_map=False)
434+
init_response(mock_requests, fiat_map=False, quote=False)
433435
cryptocurrency.currency_list()
434436
patch_paste.assert_called_with("Symbol Name\nBTC bitcoin")

tests/test_json.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
from collections import OrderedDict
88
from pathlib import Path
99

10-
11-
def pytest_generate_tests(metafunc):
12-
if 'json_file' in metafunc.fixturenames:
13-
paths = [
14-
file for file in Path().rglob("*.json")
15-
if len(file.parts) == 1 or file.parts[0] in (
16-
'cloudbot', 'data', 'tests', 'travis', 'docs'
17-
)
18-
]
19-
metafunc.parametrize('json_file', paths, ids=list(map(str, paths)))
10+
import pytest
2011

2112

13+
@pytest.mark.parametrize(
14+
"json_file",
15+
[
16+
file
17+
for file in Path().rglob("*.json")
18+
if len(file.parts) == 1
19+
or file.parts[0] in ("cloudbot", "data", "tests", "travis", "docs")
20+
],
21+
)
2222
def test_json(json_file):
2323
with json_file.open(encoding="utf-8") as f:
2424
text = f.read()
2525

2626
data = json.loads(text, object_pairs_hook=OrderedDict)
27-
formatted_text = json.dumps(data, indent=4) + '\n'
27+
formatted_text = json.dumps(data, indent=4) + "\n"
2828
assert formatted_text == text, "Improperly formatted JSON file"

0 commit comments

Comments
 (0)