Fix pokemon moves lookup assuming contiguous ids#1603
Conversation
VersionGroup and MoveLearnMethod summaries were looked up by list position (id - 1), assuming ids form a contiguous sequence starting at 1. Any gap in either table (e.g. a deleted row) causes the wrong version group or learn method to be returned, or an IndexError. Fixes PokeAPI#1567
Naramsim
left a comment
There was a problem hiding this comment.
Hi! Thanks for the submission! The passing tests tell me that everything is working as expected. I honestly don't understand very well the additional python test you added, but that's my lack of knowledge. I'll merge this, thanks for the contribution!
Can you spot in the code base other issues like this one?
|
A PokeAPI/api-data refresh has started. In ~45 minutes the staging branch of PokeAPI/api-data will be pushed with the new generated data. |
|
The updater script has finished its job and has now opened a Pull Request towards PokeAPI/api-data with the updated data. |
|
Thanks for merging! Yes, I found at least one more spot with the same pattern: This one was actually touched before in #1314 (closed without merging), so there's some history there. Happy to open a follow-up PR fixing it the same way (dict keyed by id) with a regression test, if that's useful — let me know. |
Problem
PokemonDetailSerializer.get_pokemon_moveslooks upVersionGroupandMoveLearnMethodsummaries by list position (id - 1), assuming both tables form a contiguous 1-indexed sequence with no gaps.Any gap in either table (e.g. a deleted row, or a new entry inserted with an id that doesn't match its position) causes the wrong version group / move learn method to be returned for a move, or raises an
IndexError.Fix
Build a dict keyed by the actual
idinstead of relying on list position, so lookups are correct regardless of gaps in the id sequence.Test plan
test_pokemon_moves_with_non_contiguous_version_group_and_method_ids, which creates and deletes aVersionGroupand aMoveLearnMethodto force a gap, then asserts thepokemonendpoint still returns the correct version group / move learn method.IndexErroragainst the old code and passes with the fix.manage.py test pokemon_v2passes (55 tests).black --checkpasses.Fixes #1567