Skip to content

Commit

Permalink
Add tests for the hookup plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Mar 17, 2024
1 parent c1a2465 commit 71ef1be
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plugins/hookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def hookup(db, chan):
select(
[seen_table.c.name],
and_(seen_table.c.chan == chan, seen_table.c.time > times),
)
).order_by(seen_table.c.time)
).fetchall()

if not results or len(results) < 2:
Expand All @@ -53,7 +53,9 @@ def hookup(db, chan):
"user1": person1,
"user2": person2,
}

generator = TextGenerator(
hookups["templates"], hookups["parts"], variables=variables
)

return generator.generate_string()
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ addopts =
--cov-report=html
--doctest-modules
--random-order
--pythonhashseed=123
testpaths = .
filterwarnings =
error
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pytest == 8.1.1
pytest-asyncio == 0.20.3
pytest-cov == 4.1.0
pytest-random-order == 1.1.1
pytest-pythonhashseed == 1.0.1
responses == 0.25.0
typing_extensions == 4.10.0
types-requests ~= 2.31.0.20240311
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
from tests.util.mock_db import MockDB


@pytest.fixture(autouse=True)
def clear_metadata():
database.metadata.clear()
yield
database.metadata.clear()


@pytest.fixture()
def tmp_logs(tmp_path):
cloudbot._setup(tmp_path)
Expand Down
125 changes: 125 additions & 0 deletions tests/plugin_tests/hookup_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import random
from datetime import timedelta
from pathlib import Path

from cloudbot.util import database
from plugins import hookup, seen
from tests.util.mock_db import MockDB


def test_load_data(mock_bot_factory):
bot = mock_bot_factory(
base_dir=Path(__file__).parent.parent.parent.resolve()
)
hookup.load_data(bot)
assert hookup.hookups


def test_hookup_no_seen(mock_db: MockDB):
db = mock_db.session()
res = hookup.hookup(db, "#chan")
assert res is None


def test_hookup_no_data(mock_db: MockDB):
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
seen.table.create(mock_db.engine)
db = mock_db.session()
res = hookup.hookup(db, "#chan")
assert res == "something went wrong"


def test_hookup_one_user(mock_db: MockDB, freeze_time):
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
seen.table.create(mock_db.engine)

mock_db.load_data(
seen.table,
[
{
"name": "testnick",
"time": (
freeze_time.time_to_freeze - timedelta(hours=1)
).timestamp(),
"quote": "foo bar baz",
"chan": "#chan",
"host": "user@host",
},
],
)

db = mock_db.session()
res = hookup.hookup(db, "#chan")
assert res == "something went wrong"


def test_hookup_basic(mock_db: MockDB, freeze_time):
hookup.hookups = {
"templates": [
"{user1} : {user2}",
],
"parts": {},
}

database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
seen.table.create(mock_db.engine)
mock_db.load_data(
seen.table,
[
{
"name": "testnick",
"time": (
freeze_time.time_to_freeze - timedelta(hours=2)
).timestamp(),
"quote": "foo bar baz",
"chan": "#chan",
"host": "user@host",
},
{
"name": "testnick2",
"time": (
freeze_time.time_to_freeze - timedelta(hours=1)
).timestamp(),
"quote": "foo bar baz",
"chan": "#chan",
"host": "user@host",
},
],
)

db = mock_db.session()
random.seed(1)
res = hookup.hookup(db, "#chan")
assert res == "testnick : testnick2"


def test_hookup_active_time(mock_db: MockDB, freeze_time):
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
seen.table.create(mock_db.engine)
mock_db.load_data(
seen.table,
[
{
"name": "testnick",
"time": (
freeze_time.time_to_freeze - timedelta(weeks=1)
).timestamp(),
"quote": "foo bar baz",
"chan": "#chan",
"host": "user@host",
},
{
"name": "testnick2",
"time": (
freeze_time.time_to_freeze - timedelta(hours=1)
).timestamp(),
"quote": "foo bar baz",
"chan": "#chan",
"host": "user@host",
},
],
)

db = mock_db.session()
res = hookup.hookup(db, "#chan")
assert res == "something went wrong"

0 comments on commit 71ef1be

Please sign in to comment.