This commit is contained in:
2025-04-18 08:14:37 -05:00
parent 4105cc2373
commit 2503141c34
25 changed files with 1127 additions and 278 deletions

View File

@@ -1,7 +1,9 @@
import unittest
from pathlib import Path
# from convert_to_sportspress
from convert_to_sportspress.utils import validate_csv_header, normalize_header_key, read_csv, parse_score, is_visitor_home_order_reversed, process_data, aggregate_teams
from src.utils.common import validate_csv_header, normalize_header_key, read_and_normalize_csv, parse_score, is_visitor_home_order_reversed, import_gamebygame, aggregate_teams
from src.utils.normalize import normalize_value, normalize_header_key, load_config
import toml
class TestConvertToSportsPress(unittest.TestCase):
def setUp(self):
@@ -23,11 +25,11 @@ class TestConvertToSportsPress(unittest.TestCase):
def test_read_csv(self):
# Assuming that the CSV file has a valid header
with self.subTest("Read CSV data"):
data = read_csv(self.test_csv_path_2009)
data = read_and_normalize_csv(self.test_csv_path_2009)
self.assertIsInstance(data, list)
self.assertTrue(all(isinstance(row, dict) for row in data))
with self.subTest("Normalized keys"):
normalized_data = read_csv(self.test_csv_path_2009)
normalized_data = read_and_normalize_csv(self.test_csv_path_2009)
self.assertTrue(all("visitor" in row.keys() and "results" in row.keys() for row in normalized_data))
def test_parse_score_visitor_first(self):
@@ -115,8 +117,8 @@ class TestConvertToSportsPress(unittest.TestCase):
def test_process_data(self):
# Assuming that the CSV file has a valid header and read_csv is good
data = read_csv(self.test_csv_path_2009)
processed_data = process_data(data)
data = read_and_normalize_csv(self.test_csv_path_2009)
processed_data = import_gamebygame(data)
aggregate_team_data = aggregate_teams(processed_data)
expected_result = [
{"team": "Marlins", "gp": 28, "win": 23, "loss": 5, "tie": 0, "pts": 46, "runs_for": 249, "runs_against": 117},
@@ -139,7 +141,85 @@ class TestConvertToSportsPress(unittest.TestCase):
with self.subTest(f'Results of "{team}"'):
self.assertDictContainsSubset(aggregate_team_data_dict, expected_dict)
class TestNormalization(unittest.TestCase):
def test_normalize_key(self):
header_key_normalization = {
"date": {"potential_keys": ["Date", "EventDate"]},
"time": {"potential_keys": ["Time", "EventTime"]},
"visitor": {"potential_keys": ["Away"]},
"field":
{
"potential_keys": ["Field", "Location", "Venue"],
"values": [{"original": ["Winnemac"], "normalized": "Winnemac Park"}],
}
}
# Test cases for normalize_key function
self.assertEqual(normalize_header_key("Date", header_key_normalization), "date")
self.assertEqual(normalize_header_key("Time", header_key_normalization), "time")
self.assertEqual(normalize_header_key("Venue", header_key_normalization), "field")
self.assertEqual(normalize_header_key("Away", header_key_normalization), "visitor")
def test_load_config_file(self):
expected = {
"win": {"potential_keys": ["w", "wins"]},
"loss": {"potential_keys": ["l", "losses"]},
"tie": {"potential_keys": ["t", "ties"]},
"points": {"potential_keys": ["pts.", "pts", "pt"]},
"runs_for": {"potential_keys": ["rf", "rs"]},
"runs_against": {"potential_keys": ["ra"]},
"division": {"potential_keys": ["div"]},
"date": {"potential_keys": ["Date", "EventDate"]},
"time": {"potential_keys": ["Time", "EventTime"]},
"visitor": {"potential_keys": ["Away"]},
"field": {
"potential_keys": ["Field", "Location", "Venue"],
"values": [{"original": ["Winnemac"], "normalized": "Winnemac Park"}],
},
"results": {"potential_keys": ["Final Score", "Score", "Result", "Outcome"]},
"team": {
"values": [
{
"original": ["Hounds", "Chicago Hounds", "Winnemac Hounds", "Hound"],
"normalized": "Hounds",
},
{"original": ["Chicago Red Sox"], "normalized": "Red Sox"},
]
},
}
config_path = "normalization.toml"
config = load_config(config_path)
self.assertEqual(config, expected)
def test_normalize_value(self):
# Test cases for normalize_value function
team_normalization = {
"team": {
"values": [
{
"original": ["Hounds", "Chicago Hounds", "Winnemac Hounds", "Hound"],
"normalized": "Hounds",
},
{"original": ["Chicago Red Sox"], "normalized": "Red Sox"},
]
}
}
# Test case with normalization
self.assertEqual(normalize_value("Chicago Hounds", 'team', team_normalization), "Hounds")
# Test case without title case normalization
# self.assertEqual(normalize_value("red sox", team_normalization, 'team'), "Red Sox")
# Add more test cases for other values
if __name__ == '__main__':
unittest.main()
if __name__ == "__main__":
unittest.main()