-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from linuxdaemon/gonzobot+refactor-duck-score
Refactor duckhunt .killers/.friends commands
- Loading branch information
Showing
2 changed files
with
233 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import importlib | ||
|
||
import pytest | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import scoped_session, sessionmaker | ||
|
||
from plugins.duckhunt import top_list | ||
|
||
|
||
class MockDB: | ||
def __init__(self): | ||
self.engine = create_engine('sqlite:///:memory:') | ||
self.session = scoped_session(sessionmaker(self.engine)) | ||
|
||
|
||
@pytest.fixture() | ||
def mock_db(): | ||
return MockDB() | ||
|
||
|
||
@pytest.mark.parametrize('prefix,items,result', [ | ||
[ | ||
'Duck friend scores in #TestChannel: ', | ||
{ | ||
'testuser': 5, | ||
'testuser1': 1, | ||
}, | ||
'Duck friend scores in #TestChannel: ' | ||
'\x02t\u200bestuser\x02: 5 • \x02t\u200bestuser1\x02: 1' | ||
], | ||
]) | ||
def test_top_list(prefix, items, result): | ||
assert top_list(prefix, items.items()) == result | ||
|
||
|
||
def test_display_scores(mock_db): | ||
from cloudbot.util.database import metadata | ||
metadata.bind = mock_db.engine | ||
from plugins import duckhunt | ||
|
||
importlib.reload(duckhunt) | ||
|
||
metadata.create_all(checkfirst=True) | ||
|
||
session = mock_db.session() | ||
|
||
class Conn: | ||
name = 'TestConn' | ||
|
||
conn = Conn() | ||
|
||
chan = '#TestChannel' | ||
chan1 = '#TestChannel1' | ||
|
||
duckhunt.update_score('TestUser', chan, session, conn, 5, 4) | ||
duckhunt.update_score('TestUser1', chan, session, conn, 1, 7) | ||
duckhunt.update_score('OtherUser', chan1, session, conn, 9, 2) | ||
|
||
expected_testchan_friend_scores = {'testuser': 4, 'testuser1': 7} | ||
|
||
actual_testchan_friend_scores = duckhunt.get_channel_scores( | ||
session, duckhunt.SCORE_TYPES['friend'], | ||
conn, chan | ||
) | ||
|
||
assert actual_testchan_friend_scores == expected_testchan_friend_scores | ||
|
||
chan_friends = ( | ||
'Duck friend scores in #TestChannel: ' | ||
'\x02t\u200bestuser1\x02: 7 • \x02t\u200bestuser\x02: 4' | ||
) | ||
|
||
chan_kills = ( | ||
'Duck killer scores in #TestChannel: ' | ||
'\x02t\u200bestuser\x02: 5 • \x02t\u200bestuser1\x02: 1' | ||
) | ||
|
||
global_friends = ( | ||
'Duck friend scores across the network: ' | ||
'\x02t\u200bestuser1\x02: 7' | ||
' • \x02t\u200bestuser\x02: 4' | ||
' • \x02o\u200btheruser\x02: 2' | ||
) | ||
|
||
global_kills = ( | ||
'Duck killer scores across the network: ' | ||
'\x02o\u200btheruser\x02: 9' | ||
' • \x02t\u200bestuser\x02: 5' | ||
' • \x02t\u200bestuser1\x02: 1' | ||
) | ||
|
||
average_friends = ( | ||
'Duck friend scores across the network: ' | ||
'\x02t\u200bestuser1\x02: 7' | ||
' • \x02t\u200bestuser\x02: 4' | ||
' • \x02o\u200btheruser\x02: 2' | ||
) | ||
|
||
average_kills = ( | ||
'Duck killer scores across the network: ' | ||
'\x02o\u200btheruser\x02: 9' | ||
' • \x02t\u200bestuser\x02: 5' | ||
' • \x02t\u200bestuser1\x02: 1' | ||
) | ||
|
||
assert duckhunt.friends('', chan, conn, session) == chan_friends | ||
|
||
assert duckhunt.killers('', chan, conn, session) == chan_kills | ||
|
||
assert duckhunt.friends('global', chan, conn, session) == global_friends | ||
|
||
assert duckhunt.killers('global', chan, conn, session) == global_kills | ||
|
||
assert duckhunt.friends('average', chan, conn, session) == average_friends | ||
|
||
assert duckhunt.killers('average', chan, conn, session) == average_kills |