Skip to content

Commit

Permalink
Merge pull request #681 from TotallyNotRobots/hookup-tests
Browse files Browse the repository at this point in the history
Add tests for the hookup plugin
  • Loading branch information
linuxdaemon authored Mar 17, 2024
2 parents c1a2465 + 7216c37 commit 0105098
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugins/hookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ 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:
return "something went wrong"

# Make sure the list of people is unique
people = list({row[0] for row in results})
people = sorted({row[0] for row in results})
random.shuffle(people)
person1, person2 = people[:2]
variables = {
"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
7 changes: 7 additions & 0 deletions tests/core_tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ async def test_migrate_db(
Column("b", String, default="bar"),
)

_ = Table(
"foobar2",
database.metadata,
Column("a", String, primary_key=True),
Column("b", String, default="bar"),
)

table.create(old_db.engine)
other_table.create(old_db.engine)
mock_bot = mock_bot_factory(
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 == "testnick2 : testnick"


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 0105098

Please sign in to comment.