Skip to content

Commit

Permalink
Replace uses of old .count() method in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspen Barnes committed Aug 24, 2021
1 parent 7a55dc9 commit c03f18a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ omit =
tests/data/*
tests/util/*
.*
venv/*
18 changes: 6 additions & 12 deletions plugins/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PrimaryKeyConstraint,
String,
Table,
func,
not_,
select,
)
Expand Down Expand Up @@ -143,11 +144,9 @@ def get_quote_by_nick(db, nick, num=False):
"""Returns a formatted quote from a nick, random or selected by number"""

count_query = (
select([qtable])
select([func.count(qtable.c.msg)])
.where(not_(qtable.c.deleted))
.where(qtable.c.nick == nick.lower())
.alias("count")
.count()
)
count = db.execute(count_query).fetchall()[0][0]

Expand All @@ -171,12 +170,10 @@ def get_quote_by_nick(db, nick, num=False):
def get_quote_by_nick_chan(db, chan, nick, num=False):
"""Returns a formatted quote from a nick in a channel, random or selected by number"""
count_query = (
select([qtable])
select([func.count(qtable.c.msg)])
.where(not_(qtable.c.deleted))
.where(qtable.c.chan == chan)
.where(qtable.c.nick == nick.lower())
.alias("count")
.count()
)
count = db.execute(count_query).fetchall()[0][0]

Expand All @@ -201,11 +198,9 @@ def get_quote_by_nick_chan(db, chan, nick, num=False):
def get_quote_by_chan(db, chan, num=False):
"""Returns a formatted quote from a channel, random or selected by number"""
count_query = (
select([qtable])
select([func.count(qtable.c.msg)])
.where(not_(qtable.c.deleted))
.where(qtable.c.chan == chan)
.alias("count")
.count()
)
count = db.execute(count_query).fetchall()[0][0]

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


@hook.command("q", "quote")
def quote(text, nick, chan, db, notice, event):
def quote(text, nick, chan, db, event):
"""[#chan] [nick] [#n] OR add <nick> <message> - gets the [#n]th quote by <nick> (defaulting to random)
OR adds <message> as a quote for <nick> in the caller's channel"""

add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", text, re.I)
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", text)
retrieve_chan = re.match(r"(#\S+)\s+(\S+)(?:\s+#?(-?\d+))?$", text)

if add:
quoted_nick, msg = add.groups()
notice(add_quote(db, chan, quoted_nick, nick, msg))
event.notice(add_quote(db, chan, quoted_nick, nick, msg))
return None

if retrieve:
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ addopts =
--ignore=.*
--cov .
--cov-report=xml
--cov-report=html
--doctest-modules
--random-order
testpaths = .
Expand Down
80 changes: 80 additions & 0 deletions tests/plugin_tests/quote_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import time
from unittest.mock import MagicMock, call

from plugins import quote


def test_add_quote(mock_db, freeze_time):
db = mock_db.session()
quote.qtable.create(bind=mock_db.engine)
chan = "#foo"
target = "bar"
sender = "baz"
msg = "Some test quote"
quote.add_quote(db, chan, target, sender, msg)
assert mock_db.get_data(quote.qtable) == [
(chan, target, sender, msg, time.time(), False)
]


def test_quote_cmd_add(mock_db, freeze_time):
db = mock_db.session()
quote.qtable.create(bind=mock_db.engine)
chan = "#foo"
target = "bar"
sender = "baz"
msg = "Some test quote"
text = "add " + target + " " + msg
event = MagicMock()
res = quote.quote(text, sender, chan, db, event)
assert res is None
assert mock_db.get_data(quote.qtable) == [
(chan, target, sender, msg, time.time(), False)
]
assert event.mock_calls == [call.notice("Quote added.")]


def test_quote_cmd_get_nick_random(mock_db, freeze_time):
db = mock_db.session()
quote.qtable.create(bind=mock_db.engine)
chan = "#foo"
target = "bar"
sender = "baz"
msg = "Some test quote"
quote.add_quote(db, chan, target, sender, msg)
text = target
event = MagicMock()
res = quote.quote(text, sender, chan, db, event)
assert res == "[1/1] <b\u200bar> Some test quote"
# assert mock_db.get_data(quote.qtable) == [(chan, target, sender, msg, time.time(), False)]
assert event.mock_calls == []


def test_quote_cmd_get_chan_random(mock_db, freeze_time):
db = mock_db.session()
quote.qtable.create(bind=mock_db.engine)
chan = "#foo"
target = "bar"
sender = "baz"
msg = "Some test quote"
quote.add_quote(db, chan, target, sender, msg)
text = chan
event = MagicMock()
res = quote.quote(text, sender, chan, db, event)
assert res == "[1/1] <b\u200bar> Some test quote"
assert event.mock_calls == []


def test_quote_cmd_get_nick_chan_random(mock_db, freeze_time):
db = mock_db.session()
quote.qtable.create(bind=mock_db.engine)
chan = "#foo"
target = "bar"
sender = "baz"
msg = "Some test quote"
quote.add_quote(db, chan, target, sender, msg)
text = chan + " " + target
event = MagicMock()
res = quote.quote(text, sender, chan, db, event)
assert res == "[1/1] <b\u200bar> Some test quote"
assert event.mock_calls == []

0 comments on commit c03f18a

Please sign in to comment.