-
Notifications
You must be signed in to change notification settings - Fork 678
fix: resolve get_normalized_dict() returning empty for V3 endpoints #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brandonhawi
wants to merge
2
commits into
swar:master
Choose a base branch
from
brandonhawi:fix/issue-602-normalized-methods
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
tests/unit/stats/endpoints/test_boxscoretraditionalv3_normalized.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import json | ||
| import pytest | ||
| from nba_api.stats.endpoints.boxscoretraditionalv3 import BoxScoreTraditionalV3 | ||
| from nba_api.stats.library.http import NBAStatsResponse | ||
| from .data.boxscoretraditionalv3 import BOXSCORETRADITIONALV3_SAMPLE | ||
|
|
||
|
|
||
| class MockResponse(NBAStatsResponse): | ||
|
|
||
| def __init__(self, data): | ||
| super().__init__(json.dumps(data), 200, "http://mock.url") | ||
| self._mock_data = data | ||
|
|
||
| def get_dict(self): | ||
| return self._mock_data | ||
|
|
||
|
|
||
| class TestBoxScoreTraditionalV3Normalized: | ||
|
|
||
| def test_get_normalized_dict_returns_data(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| result = endpoint.get_normalized_dict() | ||
|
|
||
| assert isinstance(result, dict) | ||
| assert len(result) > 0, "get_normalized_dict() should not return empty dict" | ||
|
|
||
| assert "PlayerStats" in result | ||
| assert "TeamStats" in result | ||
| assert "TeamStarterBenchStats" in result | ||
|
|
||
| def test_get_normalized_dict_structure(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| result = endpoint.get_normalized_dict() | ||
|
|
||
| assert isinstance(result["PlayerStats"], list) | ||
| assert isinstance(result["TeamStats"], list) | ||
| assert isinstance(result["TeamStarterBenchStats"], list) | ||
|
|
||
| if result["PlayerStats"]: | ||
| first_player = result["PlayerStats"][0] | ||
| assert isinstance(first_player, dict) | ||
| assert "gameId" in first_player | ||
| assert "personId" in first_player | ||
| assert "firstName" in first_player | ||
| assert "points" in first_player | ||
|
|
||
| if result["TeamStats"]: | ||
| first_team = result["TeamStats"][0] | ||
| assert isinstance(first_team, dict) | ||
| assert "gameId" in first_team | ||
| assert "teamId" in first_team | ||
| assert "teamName" in first_team | ||
| assert "points" in first_team | ||
|
|
||
| def test_get_normalized_json_returns_data(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| result = endpoint.get_normalized_json() | ||
|
|
||
| assert isinstance(result, str) | ||
| assert len(result) > 2, "get_normalized_json() should not return empty JSON" | ||
| assert result != "{}", "get_normalized_json() should not return empty object" | ||
|
|
||
| parsed = json.loads(result) | ||
| assert isinstance(parsed, dict) | ||
| assert len(parsed) > 0 | ||
|
|
||
| def test_get_normalized_json_is_valid_json(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| json_str = endpoint.get_normalized_json() | ||
| parsed = json.loads(json_str) | ||
|
|
||
| dict_result = endpoint.get_normalized_dict() | ||
| assert parsed == dict_result | ||
|
|
||
| def test_get_normalized_dict_player_data_correctness(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| result = endpoint.get_normalized_dict() | ||
| player_stats = result["PlayerStats"] | ||
|
|
||
| assert len(player_stats) == 2 | ||
|
|
||
| tatum = player_stats[0] | ||
| assert tatum["gameId"] == "0022500165" | ||
| assert tatum["teamId"] == 1610612738 | ||
| assert tatum["personId"] == 1630162 | ||
| assert tatum["firstName"] == "Jayson" | ||
| assert tatum["familyName"] == "Tatum" | ||
| assert tatum["points"] == 32 | ||
| assert tatum["plusMinusPoints"] == 12 | ||
|
|
||
| def test_get_normalized_dict_team_data_correctness(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| result = endpoint.get_normalized_dict() | ||
| team_stats = result["TeamStats"] | ||
|
|
||
| assert len(team_stats) == 2 | ||
|
|
||
| celtics = team_stats[0] | ||
| assert celtics["gameId"] == "0022500165" | ||
| assert celtics["teamId"] == 1610612738 | ||
| assert celtics["teamCity"] == "Boston" | ||
| assert celtics["teamName"] == "Celtics" | ||
| assert celtics["points"] == 122 | ||
| assert celtics["plusMinusPoints"] == 14 | ||
|
|
||
| warriors = team_stats[1] | ||
| assert warriors["teamId"] == 1610612744 | ||
| assert warriors["teamCity"] == "Golden State" | ||
| assert warriors["points"] == 108 | ||
| assert warriors["plusMinusPoints"] == -14 | ||
|
|
||
| def test_regression_issue_602(self): | ||
| endpoint = BoxScoreTraditionalV3(game_id="0022500165", get_request=False) | ||
| endpoint.nba_response = MockResponse(BOXSCORETRADITIONALV3_SAMPLE) | ||
| endpoint.load_response() | ||
|
|
||
| normalized_dict = endpoint.get_normalized_dict() | ||
| assert normalized_dict != {}, "Issue #602: get_normalized_dict() returns empty dict" | ||
| assert len(normalized_dict) > 0, "Issue #602: get_normalized_dict() has no data" | ||
|
|
||
| normalized_json = endpoint.get_normalized_json() | ||
| assert normalized_json != "{}", "Issue #602: get_normalized_json() returns empty JSON" | ||
| assert len(normalized_json) > 2, "Issue #602: get_normalized_json() has no data" | ||
|
|
||
| parsed = json.loads(normalized_json) | ||
| assert len(parsed) > 0, "Issue #602: Parsed JSON is empty" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the intention behind
self._endpoint?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._endpointstores the endpoint name fromget_data_sets(endpoint="...")so thatget_normalized_dict()knows which V3 parser to use.Without it,
get_normalized_dict()only has access to the raw response data (viaself.get_dict()), which doesn't contain the endpoint name needed to select the correct parser.Set to
Noneby default, so legacy endpoints continue working unchanged.