Skip to content

Conversation

@joereg4
Copy link
Contributor

@joereg4 joereg4 commented Oct 9, 2025

Issue 1: IndexError in test_location_area_api

#1313

Problem

The test_location_area_api test was failing with an IndexError: list index out of range error in the get_encounters method of LocationAreaDetailSerializer.

Root Cause

The serializer was trying to access version data using array indexing (version_data[ver["version"] - 1]), but the version_data array was not ordered by ID. The code assumed that version IDs were sequential and that array index i corresponded to version ID i+1, but this wasn't guaranteed.

Error Details

File "/code/pokemon_v2/serializers.py", line 1221, in get_encounters
    version_detail["version"] = version_data[ver["version"] - 1]
IndexError: list index out of range

Solution

  • Modified get_encounters method to order Version objects by ID
  • Created a mapping dictionary from version ID to serialized data
  • Changed array access to use the mapping: version_data_map[ver["version"]]

Code Changes

# Before
version_objects = Version.objects.all()
version_data = VersionSummarySerializer(version_objects, many=True, context=self.context).data
version_detail["version"] = version_data[ver["version"] - 1]

# After  
version_objects = Version.objects.all().order_by('id')
version_data = VersionSummarySerializer(version_objects, many=True, context=self.context).data
version_data_map = {version_objects[i].id: version_data[i] for i in range(len(version_objects))}
version_detail["version"] = version_data_map[ver["version"]]

Issue 2: IndexError in test_pokemon_api

Problem

The test_pokemon_api test was failing with an IndexError: list index out of range error in the get_pokemon_moves method of PokemonDetailSerializer.

Root Cause

Similar to Issue 1, the serializer was trying to access version group and method data using array indexing (version_data[move["version_group"] - 1] and method_data[move["move_learn_method"] - 1]), but the arrays were not ordered by ID.

Error Details

File "/code/pokemon_v2/serializers.py", line 4745, in get_pokemon_moves
    version_detail["version_group"] = version_data[move["version_group"] - 1]
IndexError: list index out of range

Solution

  • Modified get_pokemon_moves method to order VersionGroup and MoveLearnMethod objects by ID
  • Created mapping dictionaries from ID to serialized data for both version groups and methods
  • Changed array access to use the mappings

Code Changes

# Before
version_objects = VersionGroup.objects.all()
version_data = VersionGroupSummarySerializer(version_objects, many=True, context=self.context).data
method_objects = MoveLearnMethod.objects.all()
method_data = MoveLearnMethodSummarySerializer(method_objects, many=True, context=self.context).data
version_detail["version_group"] = version_data[move["version_group"] - 1]
version_detail["move_learn_method"] = method_data[move["move_learn_method"] - 1]

# After
version_objects = VersionGroup.objects.all().order_by('id')
version_data = VersionGroupSummarySerializer(version_objects, many=True, context=self.context).data
version_data_map = {version_objects[i].id: version_data[i] for i in range(len(version_objects))}
method_objects = MoveLearnMethod.objects.all().order_by('id')
method_data = MoveLearnMethodSummarySerializer(method_objects, many=True, context=self.context).data
method_data_map = {method_objects[i].id: method_data[i] for i in range(len(method_objects))}
version_detail["version_group"] = version_data_map[move["version_group"]]
version_detail["move_learn_method"] = method_data_map[move["move_learn_method"]]

Impact

These fixes resolve two critical test failures that were preventing the test suite from passing. The changes ensure that:

  1. Version data is properly mapped by ID rather than relying on array position
  2. Version group and method data are properly mapped by ID
  3. The serializers work correctly regardless of database query ordering
  4. All 51 tests now pass successfully

Files Modified

  • pokemon_v2/serializers.py - Fixed array indexing issues in get_encounters and get_pokemon_moves methods

joereg4 and others added 4 commits October 2, 2025 19:11
Fix: Removed regional restrictions so Rattata can evolve into Raticate in any region
- Updated `LocationAreaDetailSerializer` and `PokemonDetailSerializer` to order version and method objects by ID.
- Created mappings from version and method IDs to their serialized data for improved clarity and efficiency in data access.
@joereg4
Copy link
Contributor Author

joereg4 commented Oct 10, 2025

@Naramsim This is simply to fix the issue with two tests and think it should go before the PR for the evolution chain metadata addition of locked regions.

@joereg4
Copy link
Contributor Author

joereg4 commented Oct 14, 2025

@Naramsim I'm sure you are busy, are there other maintainers?

@joereg4
Copy link
Contributor Author

joereg4 commented Oct 18, 2025

@Naramsim, this PR is now ready for merge. I have fast-forwarded the branch with the latest master changes, and all tests are passing.

The two broken tests that were originally failing have been resolved. The changes are minimal and focused specifically on the serializer index error fixes.

Could you please review and merge when convenient?

@Naramsim
Copy link
Member

Hi! I don't have the time to dig into the Python code at the moment. @phalt, be sure to thoroughly review to these AI-generated PR, in the past @joereg4 opened some other AI-PRs and although they were very convincing we found out they had basic errors in them.

@joereg4
Copy link
Contributor Author

joereg4 commented Oct 21, 2025

Yes, @Naramsim there were issues with the regional pokemon evolutions, but that was my lack of understanding the build process and the limitations where regional variants aren't included in the evolution chain because they are not their own species. Those errors are on me.

joereg4 and others added 3 commits October 21, 2025 07:16
…ion IDs

- Implemented `test_location_area_api_with_non_sequential_version_ids` to ensure the location area API handles non-sequential version IDs correctly, addressing the IndexError from issue PokeAPI#1313.
- Added `test_pokemon_api_with_non_sequential_ids` to verify the pokemon API functions properly with non-sequential version group and method IDs, also related to issue PokeAPI#1313.
@joereg4
Copy link
Contributor Author

joereg4 commented Oct 26, 2025

Closing due to a lack of interest

@joereg4 joereg4 closed this Oct 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants