-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_goal_bot.py
184 lines (166 loc) · 6.92 KB
/
test_goal_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import unittest
from datetime import datetime, timezone
import sys
import os
import re
# Add the parent directory to the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from goal_bot import normalize_score_pattern, is_duplicate_score, posted_scores
# List of Premier League teams and their aliases
premier_league_teams = {
"Arsenal": ["The Gunners"],
"Aston Villa": [],
"Bournemouth": [],
"Brentford": [],
"Brighton & Hove Albion": [],
"Chelsea": ["The Blues"],
"Crystal Palace": ["The Eagles"],
"Everton": ["The Toffees"],
"Fulham": ["The Cottagers"],
"Leeds United": ["The Whites"],
"Leicester City": ["The Foxes"],
"Liverpool": ["The Reds"],
"Manchester City": ["The Citizens"],
"Manchester United": ["The Red Devils"],
"Newcastle United": ["The Magpies"],
"Nottingham Forest": ["The Reds"],
"Southampton": ["The Saints"],
"Tottenham Hotspur": ["Spurs"],
"West Ham United": ["The Hammers"],
"Wolverhampton Wanderers": ["Wolves"],
}
def contains_premier_league_team(title):
"""Check if a title contains a Premier League team name or alias"""
title = title.lower()
for team, aliases in premier_league_teams.items():
team = team.lower()
if re.search(r"\b" + re.escape(team) + r"\b", title):
return True
for alias in aliases:
alias = alias.lower()
if re.search(r"\b" + re.escape(alias) + r"\b", title):
return True
return False
class TestGoalBot(unittest.TestCase):
def setUp(self):
# Clear posted_scores before each test
posted_scores.clear()
def test_normalize_score_pattern(self):
test_cases = [
# Test case 1: Abbreviated vs Full name (Haaland)
(
"Crystal Palace 1 - [1] Manchester City - E. Haaland 30'",
"crystal palace 1 - [1] manchester city - haaland 30'"
),
# Test case 2: Different name format (Lacroix)
(
"Crystal Palace [2] - 1 Manchester City - M. Lacroix 56'",
"crystal palace [2] - 1 manchester city - lacroix 56'"
),
]
for input_title, expected in test_cases:
with self.subTest(input_title=input_title):
result = normalize_score_pattern(input_title)
self.assertEqual(result, expected)
def test_duplicate_detection(self):
test_cases = [
# Test case 1: Haaland goal variations
(
"Crystal Palace 1 - [1] Manchester City - E. Haaland 30'",
"Crystal Palace 1 - [1] Manchester City - Erling Haaland 30'",
True # Should be considered duplicate
),
# Test case 2: Lacroix goal variations
(
"Crystal Palace [2] - 1 Manchester City - M. Lacroix 56'",
"Crystal Palace [2] - 1 Manchester City - Maxence Lacroix 56'",
True # Should be considered duplicate
),
# Test case 3: Different goals (should not be duplicate)
(
"Crystal Palace 1 - [1] Manchester City - E. Haaland 30'",
"Crystal Palace [2] - 1 Manchester City - M. Lacroix 56'",
False
),
# Test case 4: Same minute, different score (should not be duplicate)
(
"Crystal Palace 1 - [1] Manchester City - E. Haaland 30'",
"Crystal Palace 1 - [2] Manchester City - E. Haaland 30'",
False
),
]
for title1, title2, should_be_duplicate in test_cases:
with self.subTest(title1=title1, title2=title2):
# Clear posted_scores before each subtest
posted_scores.clear()
# Use a fixed timestamp for testing
timestamp = datetime.now(timezone.utc)
# Post the first title
is_duplicate_score(title1, timestamp)
# Check if second title is considered duplicate
result = is_duplicate_score(title2, timestamp)
self.assertEqual(
result,
should_be_duplicate,
f"Expected duplicate={should_be_duplicate} for:\n{title1}\n{title2}"
)
class TestPremierLeagueTeams(unittest.TestCase):
def test_exact_matches(self):
"""Test exact team name matches"""
test_cases = [
("Arsenal score a goal!", True),
("Manchester United win 2-0", True),
("Chelsea vs Liverpool", True),
("Random text without team", False),
]
for title, expected in test_cases:
with self.subTest(title=title):
self.assertEqual(contains_premier_league_team(title), expected)
def test_partial_matches(self):
"""Test that partial matches don't trigger false positives"""
test_cases = [
("Arsenalistas win the game", False),
("Liverpoolian culture", False),
("Chelseafc.com", False),
("Manchesterford", False),
]
for title, expected in test_cases:
with self.subTest(title=title):
self.assertEqual(contains_premier_league_team(title), expected)
def test_team_aliases(self):
"""Test that team aliases are properly recognized"""
test_cases = [
("The Gunners take the lead", True), # Arsenal alias
("The Red Devils score", True), # Man United alias
("The Blues equalize", True), # Chelsea alias
("Random Devils in the title", False) # Shouldn't match partial alias
]
for title, expected in test_cases:
with self.subTest(title=title):
self.assertEqual(contains_premier_league_team(title), expected)
def test_case_insensitivity(self):
"""Test that matching is case insensitive"""
test_cases = [
("ARSENAL GOAL!", True),
("arsenal score", True),
("ArSeNaL win", True),
("The GUNNERS celebrate", True),
]
for title, expected in test_cases:
with self.subTest(title=title):
self.assertEqual(contains_premier_league_team(title), expected)
def test_word_boundaries(self):
"""Test that word boundaries are respected"""
test_cases = [
("Arsenal vs Chelsea", True),
("Arsenal's goal", True),
("Arsenal-Chelsea", True),
("Arsenal/Chelsea matchday", True),
("ArsenalChelsea", False), # No word boundary
("xArsenalx", False), # No word boundary
]
for title, expected in test_cases:
with self.subTest(title=title):
self.assertEqual(contains_premier_league_team(title), expected)
if __name__ == '__main__':
unittest.main()