Skip to content

Commit 0105098

Browse files
authored
Merge pull request #681 from TotallyNotRobots/hookup-tests
Add tests for the hookup plugin
2 parents c1a2465 + 7216c37 commit 0105098

File tree

6 files changed

+145
-2
lines changed

6 files changed

+145
-2
lines changed

plugins/hookup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ def hookup(db, chan):
3939
select(
4040
[seen_table.c.name],
4141
and_(seen_table.c.chan == chan, seen_table.c.time > times),
42-
)
42+
).order_by(seen_table.c.time)
4343
).fetchall()
4444

4545
if not results or len(results) < 2:
4646
return "something went wrong"
4747

4848
# Make sure the list of people is unique
49-
people = list({row[0] for row in results})
49+
people = sorted({row[0] for row in results})
5050
random.shuffle(people)
5151
person1, person2 = people[:2]
5252
variables = {
5353
"user1": person1,
5454
"user2": person2,
5555
}
56+
5657
generator = TextGenerator(
5758
hookups["templates"], hookups["parts"], variables=variables
5859
)
60+
5961
return generator.generate_string()

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ addopts =
77
--cov-report=html
88
--doctest-modules
99
--random-order
10+
--pythonhashseed=123
1011
testpaths = .
1112
filterwarnings =
1213
error

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pytest == 8.1.1
66
pytest-asyncio == 0.20.3
77
pytest-cov == 4.1.0
88
pytest-random-order == 1.1.1
9+
pytest-pythonhashseed == 1.0.1
910
responses == 0.25.0
1011
typing_extensions == 4.10.0
1112
types-requests ~= 2.31.0.20240311

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
from tests.util.mock_db import MockDB
1818

1919

20+
@pytest.fixture(autouse=True)
21+
def clear_metadata():
22+
database.metadata.clear()
23+
yield
24+
database.metadata.clear()
25+
26+
2027
@pytest.fixture()
2128
def tmp_logs(tmp_path):
2229
cloudbot._setup(tmp_path)

tests/core_tests/test_bot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ async def test_migrate_db(
3535
Column("b", String, default="bar"),
3636
)
3737

38+
_ = Table(
39+
"foobar2",
40+
database.metadata,
41+
Column("a", String, primary_key=True),
42+
Column("b", String, default="bar"),
43+
)
44+
3845
table.create(old_db.engine)
3946
other_table.create(old_db.engine)
4047
mock_bot = mock_bot_factory(

tests/plugin_tests/hookup_test.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import random
2+
from datetime import timedelta
3+
from pathlib import Path
4+
5+
from cloudbot.util import database
6+
from plugins import hookup, seen
7+
from tests.util.mock_db import MockDB
8+
9+
10+
def test_load_data(mock_bot_factory):
11+
bot = mock_bot_factory(
12+
base_dir=Path(__file__).parent.parent.parent.resolve()
13+
)
14+
hookup.load_data(bot)
15+
assert hookup.hookups
16+
17+
18+
def test_hookup_no_seen(mock_db: MockDB):
19+
db = mock_db.session()
20+
res = hookup.hookup(db, "#chan")
21+
assert res is None
22+
23+
24+
def test_hookup_no_data(mock_db: MockDB):
25+
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
26+
seen.table.create(mock_db.engine)
27+
db = mock_db.session()
28+
res = hookup.hookup(db, "#chan")
29+
assert res == "something went wrong"
30+
31+
32+
def test_hookup_one_user(mock_db: MockDB, freeze_time):
33+
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
34+
seen.table.create(mock_db.engine)
35+
36+
mock_db.load_data(
37+
seen.table,
38+
[
39+
{
40+
"name": "testnick",
41+
"time": (
42+
freeze_time.time_to_freeze - timedelta(hours=1)
43+
).timestamp(),
44+
"quote": "foo bar baz",
45+
"chan": "#chan",
46+
"host": "user@host",
47+
},
48+
],
49+
)
50+
51+
db = mock_db.session()
52+
res = hookup.hookup(db, "#chan")
53+
assert res == "something went wrong"
54+
55+
56+
def test_hookup_basic(mock_db: MockDB, freeze_time):
57+
hookup.hookups = {
58+
"templates": [
59+
"{user1} : {user2}",
60+
],
61+
"parts": {},
62+
}
63+
64+
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
65+
seen.table.create(mock_db.engine)
66+
mock_db.load_data(
67+
seen.table,
68+
[
69+
{
70+
"name": "testnick",
71+
"time": (
72+
freeze_time.time_to_freeze - timedelta(hours=2)
73+
).timestamp(),
74+
"quote": "foo bar baz",
75+
"chan": "#chan",
76+
"host": "user@host",
77+
},
78+
{
79+
"name": "testnick2",
80+
"time": (
81+
freeze_time.time_to_freeze - timedelta(hours=1)
82+
).timestamp(),
83+
"quote": "foo bar baz",
84+
"chan": "#chan",
85+
"host": "user@host",
86+
},
87+
],
88+
)
89+
90+
db = mock_db.session()
91+
random.seed(1)
92+
res = hookup.hookup(db, "#chan")
93+
assert res == "testnick2 : testnick"
94+
95+
96+
def test_hookup_active_time(mock_db: MockDB, freeze_time):
97+
database.metadata._add_table(seen.table.name, seen.table.schema, seen.table)
98+
seen.table.create(mock_db.engine)
99+
mock_db.load_data(
100+
seen.table,
101+
[
102+
{
103+
"name": "testnick",
104+
"time": (
105+
freeze_time.time_to_freeze - timedelta(weeks=1)
106+
).timestamp(),
107+
"quote": "foo bar baz",
108+
"chan": "#chan",
109+
"host": "user@host",
110+
},
111+
{
112+
"name": "testnick2",
113+
"time": (
114+
freeze_time.time_to_freeze - timedelta(hours=1)
115+
).timestamp(),
116+
"quote": "foo bar baz",
117+
"chan": "#chan",
118+
"host": "user@host",
119+
},
120+
],
121+
)
122+
123+
db = mock_db.session()
124+
res = hookup.hookup(db, "#chan")
125+
assert res == "something went wrong"

0 commit comments

Comments
 (0)