Skip to content

Commit c1a2465

Browse files
authored
Merge pull request #680 from TotallyNotRobots/karma-tests
Add more tests for karma plugin
2 parents 3f987db + ba212bb commit c1a2465

File tree

2 files changed

+263
-3
lines changed

2 files changed

+263
-3
lines changed

plugins/karma.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def re_addpt(match, nick, chan, db, notice):
8181
if thing:
8282
addpoint(thing, nick, chan, db)
8383
else:
84-
notice(pluspts(nick, chan, db))
84+
out = pluspts(nick, chan, db)
85+
if out:
86+
notice(out)
8587

8688

8789
@hook.command("mm", "rmpoint")
@@ -141,10 +143,12 @@ def re_rmpt(match, nick, chan, db, notice):
141143
if thing:
142144
rmpoint(thing, nick, chan, db)
143145
else:
144-
notice(minuspts(nick, chan, db))
146+
out = minuspts(nick, chan, db)
147+
if out:
148+
notice(out)
145149

146150

147-
@hook.command("points", autohelp=False)
151+
@hook.command("points")
148152
def points_cmd(text, chan, db):
149153
"""<thing> - will print the total points for <thing> in the channel."""
150154
score = 0

tests/plugin_tests/karma_test.py

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from unittest.mock import MagicMock, call
2+
13
from plugins import karma
4+
from tests.util.mock_db import MockDB
25

36

47
def test_db_clean(mock_db):
@@ -25,6 +28,139 @@ def test_update_score(mock_db):
2528
assert mock_db.get_data(karma.karma_table) == [("foo", "#bar", "thing", 1)]
2629

2730

31+
def test_update_score_in_pm(mock_db):
32+
karma.karma_table.create(mock_db.engine, checkfirst=True)
33+
db = mock_db.session()
34+
karma.update_score("foo", "foo", "thing", 1, db)
35+
assert mock_db.get_data(karma.karma_table) == []
36+
37+
38+
def test_addpoint(mock_db):
39+
karma.karma_table.create(mock_db.engine, checkfirst=True)
40+
db = mock_db.session()
41+
karma.addpoint("foo", "foo", "#bar", db)
42+
assert mock_db.get_data(karma.karma_table) == [
43+
("foo", "#bar", "foo", 1),
44+
]
45+
46+
47+
def test_re_addpt(mock_db: MockDB):
48+
karma.karma_table.create(mock_db.engine, checkfirst=True)
49+
db = mock_db.session()
50+
match = karma.karmaplus_re.search("foo++")
51+
notice = MagicMock()
52+
karma.re_addpt(match, "testnick", "#chan", db, notice)
53+
assert notice.mock_calls == []
54+
assert mock_db.get_data(karma.karma_table) == [
55+
("testnick", "#chan", "foo", 1),
56+
]
57+
58+
59+
def test_re_addpt_empty_get(mock_db: MockDB):
60+
karma.karma_table.create(mock_db.engine, checkfirst=True)
61+
db = mock_db.session()
62+
match = karma.karmaplus_re.search("++")
63+
notice = MagicMock()
64+
karma.re_addpt(match, "testnick", "#chan", db, notice)
65+
assert notice.mock_calls == []
66+
assert mock_db.get_data(karma.karma_table) == []
67+
68+
69+
def test_re_addpt_single_get(mock_db: MockDB):
70+
karma.karma_table.create(mock_db.engine, checkfirst=True)
71+
mock_db.load_data(
72+
karma.karma_table,
73+
[
74+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": 4},
75+
],
76+
)
77+
db = mock_db.session()
78+
match = karma.karmaplus_re.search("++")
79+
notice = MagicMock()
80+
karma.re_addpt(match, "testnick", "#chan", db, notice)
81+
assert notice.mock_calls == [call("foo has 4 points ")]
82+
assert mock_db.get_data(karma.karma_table) == [
83+
("testnick", "#chan", "foo", 4)
84+
]
85+
86+
87+
def test_re_addpt_multi_get(mock_db: MockDB):
88+
karma.karma_table.create(mock_db.engine, checkfirst=True)
89+
mock_db.load_data(
90+
karma.karma_table,
91+
[
92+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": 4},
93+
{"name": "testnick2", "thing": "foo", "chan": "#chan", "score": 4},
94+
{"name": "testnick3", "thing": "foo", "chan": "#chan", "score": -4},
95+
{"name": "testnick", "thing": "foo2", "chan": "#chan", "score": 4},
96+
{"name": "testnick", "thing": "foo3", "chan": "#chan", "score": -4},
97+
],
98+
)
99+
100+
db = mock_db.session()
101+
match = karma.karmaplus_re.search("++")
102+
notice = MagicMock()
103+
karma.re_addpt(match, "testnick", "#chan", db, notice)
104+
assert notice.mock_calls == [call("foo has 4 points foo2 has 4 points ")]
105+
106+
107+
def test_re_rmpt(mock_db: MockDB):
108+
karma.karma_table.create(mock_db.engine, checkfirst=True)
109+
db = mock_db.session()
110+
match = karma.karmaminus_re.search("foo--")
111+
notice = MagicMock()
112+
karma.re_rmpt(match, "testnick", "#chan", db, notice)
113+
assert notice.mock_calls == []
114+
assert mock_db.get_data(karma.karma_table) == [
115+
("testnick", "#chan", "foo", -1),
116+
]
117+
118+
119+
def test_re_rmpt_empty_get(mock_db: MockDB):
120+
karma.karma_table.create(mock_db.engine, checkfirst=True)
121+
db = mock_db.session()
122+
match = karma.karmaminus_re.search("--")
123+
notice = MagicMock()
124+
karma.re_rmpt(match, "testnick", "#chan", db, notice)
125+
assert notice.mock_calls == []
126+
assert mock_db.get_data(karma.karma_table) == []
127+
128+
129+
def test_re_rmpt_single_get(mock_db: MockDB):
130+
karma.karma_table.create(mock_db.engine, checkfirst=True)
131+
mock_db.load_data(
132+
karma.karma_table,
133+
[
134+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4},
135+
],
136+
)
137+
138+
db = mock_db.session()
139+
match = karma.karmaminus_re.search("--")
140+
notice = MagicMock()
141+
karma.re_rmpt(match, "testnick", "#chan", db, notice)
142+
assert notice.mock_calls == [call("foo has -4 points ")]
143+
144+
145+
def test_re_rmpt_multi_get(mock_db: MockDB):
146+
karma.karma_table.create(mock_db.engine, checkfirst=True)
147+
mock_db.load_data(
148+
karma.karma_table,
149+
[
150+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4},
151+
{"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4},
152+
{"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4},
153+
{"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4},
154+
{"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4},
155+
],
156+
)
157+
db = mock_db.session()
158+
match = karma.karmaminus_re.search("--")
159+
notice = MagicMock()
160+
karma.re_rmpt(match, "testnick", "#chan", db, notice)
161+
assert notice.mock_calls == [call("foo has -4 points foo2 has -4 points ")]
162+
163+
28164
def test_pointstop(mock_db):
29165
karma.karma_table.create(mock_db.engine, checkfirst=True)
30166
db = mock_db.session()
@@ -86,3 +222,123 @@ def test_pointstop_global_empty(mock_db):
86222
chan = "#bar"
87223
res = karma.pointstop("global", chan, db)
88224
assert res is None
225+
226+
227+
def test_pointsbottom(mock_db):
228+
karma.karma_table.create(mock_db.engine, checkfirst=True)
229+
db = mock_db.session()
230+
chan = "#bar"
231+
karma.update_score("foo", chan, "thing", 1, db)
232+
res = karma.pointsbottom("", chan, db)
233+
assert res == "The 1 most hated things in #bar are: thing with 1 points"
234+
235+
236+
def test_pointsbottom_empty(mock_db):
237+
karma.karma_table.create(mock_db.engine, checkfirst=True)
238+
db = mock_db.session()
239+
chan = "#bar"
240+
res = karma.pointsbottom("", chan, db)
241+
assert res is None
242+
243+
244+
def test_pointsbottom_global_multi(mock_db):
245+
karma.karma_table.create(mock_db.engine, checkfirst=True)
246+
db = mock_db.session()
247+
chan = "#bar"
248+
karma.update_score("foo", chan, "thing", 1, db)
249+
karma.update_score("foo", chan + "1", "thing", 1, db)
250+
karma.update_score("foo", chan + "1", "thing1", 1, db)
251+
res = karma.pointsbottom("global", chan, db)
252+
assert res == (
253+
"The 2 most hated things in all channels are: thing1 with 1 points • thing with 2 points"
254+
)
255+
256+
257+
def test_pointsbottom_global(mock_db):
258+
karma.karma_table.create(mock_db.engine, checkfirst=True)
259+
db = mock_db.session()
260+
chan = "#bar"
261+
karma.update_score("foo", chan, "thing", 1, db)
262+
res = karma.pointsbottom("global", chan, db)
263+
assert (
264+
res
265+
== "The 1 most hated things in all channels are: thing with 1 points"
266+
)
267+
268+
269+
def test_pointsbottom_global_other_chan(mock_db):
270+
karma.karma_table.create(mock_db.engine, checkfirst=True)
271+
db = mock_db.session()
272+
chan = "#bar"
273+
karma.update_score("foo", chan, "thing", 1, db)
274+
res = karma.pointsbottom("global", chan + "1", db)
275+
assert (
276+
res
277+
== "The 1 most hated things in all channels are: thing with 1 points"
278+
)
279+
280+
281+
def test_pointsbottom_global_empty(mock_db):
282+
karma.karma_table.create(mock_db.engine, checkfirst=True)
283+
db = mock_db.session()
284+
chan = "#bar"
285+
res = karma.pointsbottom("global", chan, db)
286+
assert res is None
287+
288+
289+
def test_points_cmd_unknown(mock_db: MockDB):
290+
karma.karma_table.create(mock_db.engine, checkfirst=True)
291+
mock_db.load_data(
292+
karma.karma_table,
293+
[
294+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4},
295+
{"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4},
296+
{"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4},
297+
{"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4},
298+
{"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4},
299+
],
300+
)
301+
302+
db = mock_db.session()
303+
res = karma.points_cmd("unknown", "#chan", db)
304+
assert res == "I couldn't find unknown in the database."
305+
306+
307+
def test_points_cmd_basic(mock_db: MockDB):
308+
karma.karma_table.create(mock_db.engine, checkfirst=True)
309+
mock_db.load_data(
310+
karma.karma_table,
311+
[
312+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4},
313+
{"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4},
314+
{"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4},
315+
{"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4},
316+
{"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4},
317+
],
318+
)
319+
320+
db = mock_db.session()
321+
res = karma.points_cmd("foo", "#chan", db)
322+
assert res == "foo has a total score of -4 (+4/-8) in #chan."
323+
324+
325+
def test_points_cmd_global(mock_db: MockDB):
326+
karma.karma_table.create(mock_db.engine, checkfirst=True)
327+
mock_db.load_data(
328+
karma.karma_table,
329+
[
330+
{"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4},
331+
{"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4},
332+
{"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4},
333+
{"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4},
334+
{"name": "testnick3", "thing": "foo", "chan": "#chan2", "score": 7},
335+
{"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4},
336+
],
337+
)
338+
339+
db = mock_db.session()
340+
res = karma.points_cmd("foo global", "#chan", db)
341+
assert (
342+
res
343+
== "foo has a total score of 3 (+11/-8) across all channels I know about."
344+
)

0 commit comments

Comments
 (0)