1
+ from unittest .mock import MagicMock , call
2
+
1
3
from plugins import karma
4
+ from tests .util .mock_db import MockDB
2
5
3
6
4
7
def test_db_clean (mock_db ):
@@ -25,6 +28,139 @@ def test_update_score(mock_db):
25
28
assert mock_db .get_data (karma .karma_table ) == [("foo" , "#bar" , "thing" , 1 )]
26
29
27
30
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
+
28
164
def test_pointstop (mock_db ):
29
165
karma .karma_table .create (mock_db .engine , checkfirst = True )
30
166
db = mock_db .session ()
@@ -86,3 +222,123 @@ def test_pointstop_global_empty(mock_db):
86
222
chan = "#bar"
87
223
res = karma .pointstop ("global" , chan , db )
88
224
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