From e2c30c2badabc596bfe648b134866c7ea9873ab2 Mon Sep 17 00:00:00 2001 From: joeyagreco Date: Wed, 6 Dec 2023 12:14:28 -0600 Subject: [PATCH] Fixed ESPN League Loader --- CHANGELOG.md | 7 +- leeger/league_loader/ESPNLeagueLoader.py | 23 +++- requirements.txt | 2 +- .../test_ESPNLeagueLoader.py | 101 +++++++++--------- 4 files changed, 79 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 389ebe74..d2fef2a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file. - N/A +## [2.6.1] + +- Updated logic in ESPN League Loader to work with new ESPN owner flow + ## [2.6.0] - Fixed bug where older Sleeper leagues would sometimes fail to load @@ -243,7 +247,8 @@ All notable changes to this project will be documented in this file. ### Initial Release -[Unreleased]: https://github.com/joeyagreco/leeger/compare/v2.6.0...HEAD +[Unreleased]: https://github.com/joeyagreco/leeger/compare/v2.6.1...HEAD +[2.6.1]: https://github.com/joeyagreco/leeger/releases/tag/v2.6.1 [2.6.0]: https://github.com/joeyagreco/leeger/releases/tag/v2.6.0 [2.5.5]: https://github.com/joeyagreco/leeger/releases/tag/v2.5.5 [2.5.4]: https://github.com/joeyagreco/leeger/releases/tag/v2.5.4 diff --git a/leeger/league_loader/ESPNLeagueLoader.py b/leeger/league_loader/ESPNLeagueLoader.py index 9238594c..badb400e 100644 --- a/leeger/league_loader/ESPNLeagueLoader.py +++ b/leeger/league_loader/ESPNLeagueLoader.py @@ -108,8 +108,9 @@ def __loadOwners(self, espnTeams: list[ESPNTeam]) -> None: owners = list() for espnTeam in espnTeams: # get general owner name if there is one - generalOwnerName = self._getGeneralOwnerNameFromGivenOwnerName(espnTeam.owner) - ownerName = generalOwnerName if generalOwnerName is not None else espnTeam.owner + ownerName = self.__getESPNOwnerName(espnTeam.owners) + generalOwnerName = self._getGeneralOwnerNameFromGivenOwnerName(ownerName) + ownerName = generalOwnerName if generalOwnerName is not None else ownerName owners.append(Owner(name=ownerName)) self._owners = owners @@ -227,8 +228,24 @@ def __buildTeams(self, espnTeams: list[ESPNTeam]) -> list[Team]: for espnTeam in espnTeams: # TODO: see if there are cases where ESPN leagues do NOT have divisions divisionId = self.__espnDivisionIdToDivisionMap[espnTeam.division_id].id - owner = self._getOwnerByName(espnTeam.owner) + ownerName = self.__getESPNOwnerName(espnTeam.owners) + owner = self._getOwnerByName(ownerName) team = Team(ownerId=owner.id, name=espnTeam.team_name, divisionId=divisionId) teams.append(team) self.__espnTeamIdToTeamMap[espnTeam.team_id] = team return teams + + def __getESPNOwnerName(self, espnOwners: list[dict]) -> str: + """ + Owners look like this: + [ + { + "displayName": "FooBarBazQuz", + "firstName": "Foo", + "id": "{1B3463F3-B3C6-4053-9893-EdB9C7805888}", + "lastName": "Bar", + ... + } + ] + """ + return f"{espnOwners[0]['firstName']} {espnOwners[0]['lastName']}" diff --git a/requirements.txt b/requirements.txt index d7fd056b..d312ef1a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -espn_api~=0.33.0 +espn_api~=0.34.0 fleaflicker~=1.0.0 numpy~=1.24.4 openpyxl~=3.1.2 diff --git a/test/test_league_loader/test_ESPNLeagueLoader.py b/test/test_league_loader/test_ESPNLeagueLoader.py index 435c6ba4..44bbd841 100644 --- a/test/test_league_loader/test_ESPNLeagueLoader.py +++ b/test/test_league_loader/test_ESPNLeagueLoader.py @@ -1,5 +1,5 @@ import unittest -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch from leeger.enum.MatchupType import MatchupType from leeger.league_loader.ESPNLeagueLoader import ESPNLeagueLoader @@ -13,6 +13,9 @@ class TestESPNLeagueLoader(unittest.TestCase): + def __getMockOwnersList(self, firstName: str, lastName: str) -> list[dict]: + return [{"firstName": firstName, "lastName": lastName}] + def test_loadLeague_nonIntPassingStringForLeagueId(self): with self.assertRaises(ValueError) as context: ESPNLeagueLoader("a", [2000]) @@ -30,7 +33,7 @@ def test_load_league_happyPath(self, mockLeague): mockEspnLeague2022.settings.division_map = {0: "d1_2022", 1: "d2_2022"} mockTeam1_2022 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W", "W", "U", "W"], scores=[100, 110, 0, 100], @@ -39,7 +42,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam2_2022 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L", "L", "L", "L"], scores=[100, 70, 1, 1], @@ -48,7 +51,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam3_2022 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W", "L", "L", "W"], scores=[90.5, 100, 80, 2], @@ -57,7 +60,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam4_2022 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L", "W", "U", "L"], scores=[70.5, 80, 0, 1], @@ -66,7 +69,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam5_2022 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W", "L", "W", "W"], scores=[110, 80, 2, 2], @@ -75,7 +78,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam6_2022 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L", "W", "W", "W"], scores=[60, 90, 2, 2], @@ -84,7 +87,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam7_2022 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W", "W", "W", "L"], scores=[120, 130, 90, 90], @@ -93,7 +96,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam8_2022 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L", "L", "L", "L"], scores=[50, 40, 1, 1], @@ -132,7 +135,7 @@ def test_load_league_happyPath(self, mockLeague): mockEspnLeague2023.settings.division_map = {0: "d1_2023", 1: "d2_2023"} mockTeam1_2023 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W"], scores=[100], @@ -140,7 +143,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam2_2023 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L"], scores=[100], @@ -148,7 +151,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam3_2023 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W"], scores=[90.5], @@ -156,7 +159,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam4_2023 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L"], scores=[70.5], @@ -164,7 +167,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam5_2023 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W"], scores=[110], @@ -172,7 +175,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam6_2023 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L"], scores=[60], @@ -180,7 +183,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam7_2023 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W"], scores=[120], @@ -188,7 +191,7 @@ def test_load_league_happyPath(self, mockLeague): ) mockTeam8_2023 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L"], scores=[50], @@ -508,7 +511,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): mockEspnLeague2022.settings.division_map = {0: "d1_2022", 1: "d2_2022"} mockTeam1_2022 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W", "W", "U", "W"], scores=[100, 110, 0, 100], @@ -517,7 +520,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam2_2022 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L", "L", "L", "L"], scores=[100, 70, 1, 1], @@ -526,7 +529,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam3_2022 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W", "L", "L", "W"], scores=[90.5, 100, 80, 2], @@ -535,7 +538,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam4_2022 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L", "W", "U", "L"], scores=[70.5, 80, 0, 1], @@ -544,7 +547,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam5_2022 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W", "L", "W", "W"], scores=[110, 80, 2, 2], @@ -553,7 +556,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam6_2022 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L", "W", "W", "W"], scores=[60, 90, 2, 2], @@ -562,7 +565,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam7_2022 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W", "W", "W", "L"], scores=[120, 130, 90, 90], @@ -571,7 +574,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam8_2022 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L", "L", "L", "L"], scores=[50, 40, 1, 1], @@ -610,7 +613,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): mockEspnLeague2023.settings.division_map = {0: "d1_2023", 1: "d2_2023"} mockTeam1_2023 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W"], scores=[100], @@ -618,7 +621,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam2_2023 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L"], scores=[100], @@ -626,7 +629,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam3_2023 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W"], scores=[90.5], @@ -634,7 +637,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam4_2023 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L"], scores=[70.5], @@ -642,7 +645,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam5_2023 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W"], scores=[110], @@ -650,7 +653,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam6_2023 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L"], scores=[60], @@ -658,7 +661,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam7_2023 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W"], scores=[120], @@ -666,7 +669,7 @@ def test_load_league_happyPath_withOwnerNamesAndAliases(self, mockLeague): ) mockTeam8_2023 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L"], scores=[50], @@ -999,7 +1002,7 @@ def test_load_league_withLeagueName(self, mockLeague): mockEspnLeague2022.settings.division_map = {0: "d1_2022", 1: "d2_2022"} mockTeam1_2022 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W", "W", "U", "W"], scores=[100, 110, 0, 100], @@ -1008,7 +1011,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam2_2022 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L", "L", "L", "L"], scores=[100, 70, 1, 1], @@ -1017,7 +1020,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam3_2022 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W", "L", "L", "W"], scores=[90.5, 100, 80, 2], @@ -1026,7 +1029,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam4_2022 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L", "W", "U", "L"], scores=[70.5, 80, 0, 1], @@ -1035,7 +1038,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam5_2022 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W", "L", "W", "W"], scores=[110, 80, 2, 2], @@ -1044,7 +1047,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam6_2022 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L", "W", "W", "W"], scores=[60, 90, 2, 2], @@ -1053,7 +1056,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam7_2022 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W", "W", "W", "L"], scores=[120, 130, 90, 90], @@ -1062,7 +1065,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam8_2022 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L", "L", "L", "L"], scores=[50, 40, 1, 1], @@ -1101,7 +1104,7 @@ def test_load_league_withLeagueName(self, mockLeague): mockEspnLeague2023.settings.division_map = {0: "d1_2023", 1: "d2_2023"} mockTeam1_2023 = Mock( team_id=1, - owner="Owner 1", + owners=self.__getMockOwnersList("Owner", "1"), team_name="Team 1", outcomes=["W"], scores=[100], @@ -1109,7 +1112,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam2_2023 = Mock( team_id=2, - owner="Owner 2", + owners=self.__getMockOwnersList("Owner", "2"), team_name="Team 2", outcomes=["L"], scores=[100], @@ -1117,7 +1120,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam3_2023 = Mock( team_id=3, - owner="Owner 3", + owners=self.__getMockOwnersList("Owner", "3"), team_name="Team 3", outcomes=["W"], scores=[90.5], @@ -1125,7 +1128,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam4_2023 = Mock( team_id=4, - owner="Owner 4", + owners=self.__getMockOwnersList("Owner", "4"), team_name="Team 4", outcomes=["L"], scores=[70.5], @@ -1133,7 +1136,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam5_2023 = Mock( team_id=5, - owner="Owner 5", + owners=self.__getMockOwnersList("Owner", "5"), team_name="Team 5", outcomes=["W"], scores=[110], @@ -1141,7 +1144,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam6_2023 = Mock( team_id=6, - owner="Owner 6", + owners=self.__getMockOwnersList("Owner", "6"), team_name="Team 6", outcomes=["L"], scores=[60], @@ -1149,7 +1152,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam7_2023 = Mock( team_id=7, - owner="Owner 7", + owners=self.__getMockOwnersList("Owner", "7"), team_name="Team 7", outcomes=["W"], scores=[120], @@ -1157,7 +1160,7 @@ def test_load_league_withLeagueName(self, mockLeague): ) mockTeam8_2023 = Mock( team_id=8, - owner="Owner 8", + owners=self.__getMockOwnersList("Owner", "8"), team_name="Team 8", outcomes=["L"], scores=[50],