Skip to content

Commit ee7d7a5

Browse files
authored
Merge pull request #407 from TotallyNotRobots/fix-sql-count
Replace uses of old .count() method in queries
2 parents 7a55dc9 + c03f18a commit ee7d7a5

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ omit =
1717
tests/data/*
1818
tests/util/*
1919
.*
20+
venv/*

plugins/quote.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
PrimaryKeyConstraint,
99
String,
1010
Table,
11+
func,
1112
not_,
1213
select,
1314
)
@@ -143,11 +144,9 @@ def get_quote_by_nick(db, nick, num=False):
143144
"""Returns a formatted quote from a nick, random or selected by number"""
144145

145146
count_query = (
146-
select([qtable])
147+
select([func.count(qtable.c.msg)])
147148
.where(not_(qtable.c.deleted))
148149
.where(qtable.c.nick == nick.lower())
149-
.alias("count")
150-
.count()
151150
)
152151
count = db.execute(count_query).fetchall()[0][0]
153152

@@ -171,12 +170,10 @@ def get_quote_by_nick(db, nick, num=False):
171170
def get_quote_by_nick_chan(db, chan, nick, num=False):
172171
"""Returns a formatted quote from a nick in a channel, random or selected by number"""
173172
count_query = (
174-
select([qtable])
173+
select([func.count(qtable.c.msg)])
175174
.where(not_(qtable.c.deleted))
176175
.where(qtable.c.chan == chan)
177176
.where(qtable.c.nick == nick.lower())
178-
.alias("count")
179-
.count()
180177
)
181178
count = db.execute(count_query).fetchall()[0][0]
182179

@@ -201,11 +198,9 @@ def get_quote_by_nick_chan(db, chan, nick, num=False):
201198
def get_quote_by_chan(db, chan, num=False):
202199
"""Returns a formatted quote from a channel, random or selected by number"""
203200
count_query = (
204-
select([qtable])
201+
select([func.count(qtable.c.msg)])
205202
.where(not_(qtable.c.deleted))
206203
.where(qtable.c.chan == chan)
207-
.alias("count")
208-
.count()
209204
)
210205
count = db.execute(count_query).fetchall()[0][0]
211206

@@ -227,17 +222,16 @@ def get_quote_by_chan(db, chan, num=False):
227222

228223

229224
@hook.command("q", "quote")
230-
def quote(text, nick, chan, db, notice, event):
225+
def quote(text, nick, chan, db, event):
231226
"""[#chan] [nick] [#n] OR add <nick> <message> - gets the [#n]th quote by <nick> (defaulting to random)
232227
OR adds <message> as a quote for <nick> in the caller's channel"""
233-
234228
add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", text, re.I)
235229
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", text)
236230
retrieve_chan = re.match(r"(#\S+)\s+(\S+)(?:\s+#?(-?\d+))?$", text)
237231

238232
if add:
239233
quoted_nick, msg = add.groups()
240-
notice(add_quote(db, chan, quoted_nick, nick, msg))
234+
event.notice(add_quote(db, chan, quoted_nick, nick, msg))
241235
return None
242236

243237
if retrieve:

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ addopts =
44
--ignore=.*
55
--cov .
66
--cov-report=xml
7+
--cov-report=html
78
--doctest-modules
89
--random-order
910
testpaths = .

tests/plugin_tests/quote_test.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import time
2+
from unittest.mock import MagicMock, call
3+
4+
from plugins import quote
5+
6+
7+
def test_add_quote(mock_db, freeze_time):
8+
db = mock_db.session()
9+
quote.qtable.create(bind=mock_db.engine)
10+
chan = "#foo"
11+
target = "bar"
12+
sender = "baz"
13+
msg = "Some test quote"
14+
quote.add_quote(db, chan, target, sender, msg)
15+
assert mock_db.get_data(quote.qtable) == [
16+
(chan, target, sender, msg, time.time(), False)
17+
]
18+
19+
20+
def test_quote_cmd_add(mock_db, freeze_time):
21+
db = mock_db.session()
22+
quote.qtable.create(bind=mock_db.engine)
23+
chan = "#foo"
24+
target = "bar"
25+
sender = "baz"
26+
msg = "Some test quote"
27+
text = "add " + target + " " + msg
28+
event = MagicMock()
29+
res = quote.quote(text, sender, chan, db, event)
30+
assert res is None
31+
assert mock_db.get_data(quote.qtable) == [
32+
(chan, target, sender, msg, time.time(), False)
33+
]
34+
assert event.mock_calls == [call.notice("Quote added.")]
35+
36+
37+
def test_quote_cmd_get_nick_random(mock_db, freeze_time):
38+
db = mock_db.session()
39+
quote.qtable.create(bind=mock_db.engine)
40+
chan = "#foo"
41+
target = "bar"
42+
sender = "baz"
43+
msg = "Some test quote"
44+
quote.add_quote(db, chan, target, sender, msg)
45+
text = target
46+
event = MagicMock()
47+
res = quote.quote(text, sender, chan, db, event)
48+
assert res == "[1/1] <b\u200bar> Some test quote"
49+
# assert mock_db.get_data(quote.qtable) == [(chan, target, sender, msg, time.time(), False)]
50+
assert event.mock_calls == []
51+
52+
53+
def test_quote_cmd_get_chan_random(mock_db, freeze_time):
54+
db = mock_db.session()
55+
quote.qtable.create(bind=mock_db.engine)
56+
chan = "#foo"
57+
target = "bar"
58+
sender = "baz"
59+
msg = "Some test quote"
60+
quote.add_quote(db, chan, target, sender, msg)
61+
text = chan
62+
event = MagicMock()
63+
res = quote.quote(text, sender, chan, db, event)
64+
assert res == "[1/1] <b\u200bar> Some test quote"
65+
assert event.mock_calls == []
66+
67+
68+
def test_quote_cmd_get_nick_chan_random(mock_db, freeze_time):
69+
db = mock_db.session()
70+
quote.qtable.create(bind=mock_db.engine)
71+
chan = "#foo"
72+
target = "bar"
73+
sender = "baz"
74+
msg = "Some test quote"
75+
quote.add_quote(db, chan, target, sender, msg)
76+
text = chan + " " + target
77+
event = MagicMock()
78+
res = quote.quote(text, sender, chan, db, event)
79+
assert res == "[1/1] <b\u200bar> Some test quote"
80+
assert event.mock_calls == []

0 commit comments

Comments
 (0)