From ba212bb19f62f1751064b057ff95d695c33397e7 Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Sun, 17 Mar 2024 18:29:47 +0000 Subject: [PATCH] Add more tests for karma plugin --- plugins/karma.py | 10 +- tests/plugin_tests/karma_test.py | 256 +++++++++++++++++++++++++++++++ 2 files changed, 263 insertions(+), 3 deletions(-) diff --git a/plugins/karma.py b/plugins/karma.py index eb3cef8be..929477acf 100644 --- a/plugins/karma.py +++ b/plugins/karma.py @@ -81,7 +81,9 @@ def re_addpt(match, nick, chan, db, notice): if thing: addpoint(thing, nick, chan, db) else: - notice(pluspts(nick, chan, db)) + out = pluspts(nick, chan, db) + if out: + notice(out) @hook.command("mm", "rmpoint") @@ -141,10 +143,12 @@ def re_rmpt(match, nick, chan, db, notice): if thing: rmpoint(thing, nick, chan, db) else: - notice(minuspts(nick, chan, db)) + out = minuspts(nick, chan, db) + if out: + notice(out) -@hook.command("points", autohelp=False) +@hook.command("points") def points_cmd(text, chan, db): """ - will print the total points for in the channel.""" score = 0 diff --git a/tests/plugin_tests/karma_test.py b/tests/plugin_tests/karma_test.py index 31ba1134c..973882039 100644 --- a/tests/plugin_tests/karma_test.py +++ b/tests/plugin_tests/karma_test.py @@ -1,4 +1,7 @@ +from unittest.mock import MagicMock, call + from plugins import karma +from tests.util.mock_db import MockDB def test_db_clean(mock_db): @@ -25,6 +28,139 @@ def test_update_score(mock_db): assert mock_db.get_data(karma.karma_table) == [("foo", "#bar", "thing", 1)] +def test_update_score_in_pm(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + karma.update_score("foo", "foo", "thing", 1, db) + assert mock_db.get_data(karma.karma_table) == [] + + +def test_addpoint(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + karma.addpoint("foo", "foo", "#bar", db) + assert mock_db.get_data(karma.karma_table) == [ + ("foo", "#bar", "foo", 1), + ] + + +def test_re_addpt(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + match = karma.karmaplus_re.search("foo++") + notice = MagicMock() + karma.re_addpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [] + assert mock_db.get_data(karma.karma_table) == [ + ("testnick", "#chan", "foo", 1), + ] + + +def test_re_addpt_empty_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + match = karma.karmaplus_re.search("++") + notice = MagicMock() + karma.re_addpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [] + assert mock_db.get_data(karma.karma_table) == [] + + +def test_re_addpt_single_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": 4}, + ], + ) + db = mock_db.session() + match = karma.karmaplus_re.search("++") + notice = MagicMock() + karma.re_addpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [call("foo has 4 points ")] + assert mock_db.get_data(karma.karma_table) == [ + ("testnick", "#chan", "foo", 4) + ] + + +def test_re_addpt_multi_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick2", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick", "thing": "foo2", "chan": "#chan", "score": 4}, + {"name": "testnick", "thing": "foo3", "chan": "#chan", "score": -4}, + ], + ) + + db = mock_db.session() + match = karma.karmaplus_re.search("++") + notice = MagicMock() + karma.re_addpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [call("foo has 4 points foo2 has 4 points ")] + + +def test_re_rmpt(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + match = karma.karmaminus_re.search("foo--") + notice = MagicMock() + karma.re_rmpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [] + assert mock_db.get_data(karma.karma_table) == [ + ("testnick", "#chan", "foo", -1), + ] + + +def test_re_rmpt_empty_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + match = karma.karmaminus_re.search("--") + notice = MagicMock() + karma.re_rmpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [] + assert mock_db.get_data(karma.karma_table) == [] + + +def test_re_rmpt_single_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4}, + ], + ) + + db = mock_db.session() + match = karma.karmaminus_re.search("--") + notice = MagicMock() + karma.re_rmpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [call("foo has -4 points ")] + + +def test_re_rmpt_multi_get(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4}, + {"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4}, + ], + ) + db = mock_db.session() + match = karma.karmaminus_re.search("--") + notice = MagicMock() + karma.re_rmpt(match, "testnick", "#chan", db, notice) + assert notice.mock_calls == [call("foo has -4 points foo2 has -4 points ")] + + def test_pointstop(mock_db): karma.karma_table.create(mock_db.engine, checkfirst=True) db = mock_db.session() @@ -86,3 +222,123 @@ def test_pointstop_global_empty(mock_db): chan = "#bar" res = karma.pointstop("global", chan, db) assert res is None + + +def test_pointsbottom(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + karma.update_score("foo", chan, "thing", 1, db) + res = karma.pointsbottom("", chan, db) + assert res == "The 1 most hated things in #bar are: thing with 1 points" + + +def test_pointsbottom_empty(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + res = karma.pointsbottom("", chan, db) + assert res is None + + +def test_pointsbottom_global_multi(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + karma.update_score("foo", chan, "thing", 1, db) + karma.update_score("foo", chan + "1", "thing", 1, db) + karma.update_score("foo", chan + "1", "thing1", 1, db) + res = karma.pointsbottom("global", chan, db) + assert res == ( + "The 2 most hated things in all channels are: thing1 with 1 points • thing with 2 points" + ) + + +def test_pointsbottom_global(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + karma.update_score("foo", chan, "thing", 1, db) + res = karma.pointsbottom("global", chan, db) + assert ( + res + == "The 1 most hated things in all channels are: thing with 1 points" + ) + + +def test_pointsbottom_global_other_chan(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + karma.update_score("foo", chan, "thing", 1, db) + res = karma.pointsbottom("global", chan + "1", db) + assert ( + res + == "The 1 most hated things in all channels are: thing with 1 points" + ) + + +def test_pointsbottom_global_empty(mock_db): + karma.karma_table.create(mock_db.engine, checkfirst=True) + db = mock_db.session() + chan = "#bar" + res = karma.pointsbottom("global", chan, db) + assert res is None + + +def test_points_cmd_unknown(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4}, + {"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4}, + ], + ) + + db = mock_db.session() + res = karma.points_cmd("unknown", "#chan", db) + assert res == "I couldn't find unknown in the database." + + +def test_points_cmd_basic(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4}, + {"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4}, + ], + ) + + db = mock_db.session() + res = karma.points_cmd("foo", "#chan", db) + assert res == "foo has a total score of -4 (+4/-8) in #chan." + + +def test_points_cmd_global(mock_db: MockDB): + karma.karma_table.create(mock_db.engine, checkfirst=True) + mock_db.load_data( + karma.karma_table, + [ + {"name": "testnick", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick2", "thing": "foo", "chan": "#chan", "score": -4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan", "score": 4}, + {"name": "testnick", "thing": "foo2", "chan": "#chan", "score": -4}, + {"name": "testnick3", "thing": "foo", "chan": "#chan2", "score": 7}, + {"name": "testnick", "thing": "foo3", "chan": "#chan", "score": 4}, + ], + ) + + db = mock_db.session() + res = karma.points_cmd("foo global", "#chan", db) + assert ( + res + == "foo has a total score of 3 (+11/-8) across all channels I know about." + )