@@ -5797,3 +5797,196 @@ def test_case_insensitive_api(self):
57975797 self .assertEqual (uppercase_response .status_code , status .HTTP_200_OK )
57985798
57995799 self .assertEqual (lowercase_response .data , uppercase_response .data )
5800+
5801+ def test_location_area_api_with_non_sequential_version_ids (self ):
5802+ """
5803+ Test that location area API works correctly with non-sequential version IDs.
5804+ This test reproduces the IndexError scenario that was fixed in issue #1313.
5805+ """
5806+ # Create versions with non-sequential IDs to trigger the original bug
5807+ # We'll create versions with IDs 1, 5, 10 to simulate non-sequential ordering
5808+ version_group = self .setup_version_group_data (name = "test version group" )
5809+
5810+ # Create versions with specific IDs by deleting and recreating with explicit IDs
5811+ Version .objects .all ().delete () # Clear existing versions
5812+ version1 = Version .objects .create (
5813+ id = 1 , name = "version1" , version_group = version_group
5814+ )
5815+ version5 = Version .objects .create (
5816+ id = 5 , name = "version5" , version_group = version_group
5817+ )
5818+ version10 = Version .objects .create (
5819+ id = 10 , name = "version10" , version_group = version_group
5820+ )
5821+
5822+ # Create location area and encounter data
5823+ location = self .setup_location_data (name = "test location" )
5824+ location_area = self .setup_location_area_data (
5825+ location , name = "test location area"
5826+ )
5827+
5828+ encounter_method = self .setup_encounter_method_data (
5829+ name = "test encounter method"
5830+ )
5831+ location_area_encounter_rate = self .setup_location_area_encounter_rate_data (
5832+ location_area , encounter_method , rate = 20
5833+ )
5834+
5835+ pokemon_species = self .setup_pokemon_species_data (name = "test pokemon species" )
5836+ pokemon = self .setup_pokemon_data (
5837+ name = "test pokemon" , pokemon_species = pokemon_species
5838+ )
5839+ encounter_slot = self .setup_encounter_slot_data (
5840+ encounter_method , slot = 1 , rarity = 30
5841+ )
5842+
5843+ # Create encounters with different versions (including the non-sequential IDs)
5844+ encounter1 = self .setup_encounter_data (
5845+ pokemon = pokemon ,
5846+ location_area = location_area ,
5847+ encounter_slot = encounter_slot ,
5848+ version = version1 , # ID 1
5849+ min_level = 30 ,
5850+ max_level = 35 ,
5851+ )
5852+
5853+ encounter2 = self .setup_encounter_data (
5854+ pokemon = pokemon ,
5855+ location_area = location_area ,
5856+ encounter_slot = encounter_slot ,
5857+ version = version5 , # ID 5
5858+ min_level = 32 ,
5859+ max_level = 38 ,
5860+ )
5861+
5862+ encounter3 = self .setup_encounter_data (
5863+ pokemon = pokemon ,
5864+ location_area = location_area ,
5865+ encounter_slot = encounter_slot ,
5866+ version = version10 , # ID 10
5867+ min_level = 35 ,
5868+ max_level = 40 ,
5869+ )
5870+
5871+ # Test the API endpoint - this should not raise IndexError
5872+ response = self .client .get (f"{ API_V2 } /location-area/{ location_area .pk } /" )
5873+
5874+ self .assertEqual (response .status_code , status .HTTP_200_OK )
5875+
5876+ # Verify the response contains the expected structure
5877+ self .assertIn ("pokemon_encounters" , response .data )
5878+ self .assertIsInstance (response .data ["pokemon_encounters" ], list )
5879+
5880+ # Verify that version data is correctly mapped (not using array indexing)
5881+ pokemon_encounters = response .data ["pokemon_encounters" ]
5882+ self .assertGreater (len (pokemon_encounters ), 0 )
5883+
5884+ # Check that version details are properly structured
5885+ for encounter in pokemon_encounters :
5886+ self .assertIn ("version_details" , encounter )
5887+ for version_detail in encounter ["version_details" ]:
5888+ self .assertIn ("version" , version_detail )
5889+ # The version should be a proper object, not None or malformed
5890+ self .assertIsInstance (version_detail ["version" ], dict )
5891+ self .assertIn ("name" , version_detail ["version" ])
5892+
5893+ def test_pokemon_api_with_non_sequential_ids (self ):
5894+ """
5895+ Test that pokemon API works correctly with non-sequential version group and method IDs.
5896+ This test reproduces the IndexError scenario that was fixed in issue #1313.
5897+ """
5898+ # Create version groups with non-sequential IDs
5899+ VersionGroup .objects .all ().delete () # Clear existing version groups
5900+ MoveLearnMethod .objects .all ().delete () # Clear existing methods
5901+
5902+ generation = self .setup_generation_data (name = "test generation" )
5903+
5904+ # Create version groups with specific IDs
5905+ version_group1 = VersionGroup .objects .create (
5906+ id = 1 , name = "version group 1" , generation = generation , order = 1
5907+ )
5908+ version_group5 = VersionGroup .objects .create (
5909+ id = 5 , name = "version group 5" , generation = generation , order = 2
5910+ )
5911+ version_group10 = VersionGroup .objects .create (
5912+ id = 10 , name = "version group 10" , generation = generation , order = 3
5913+ )
5914+
5915+ # Create move learn methods with specific IDs
5916+ method1 = MoveLearnMethod .objects .create (id = 1 , name = "method 1" )
5917+ method5 = MoveLearnMethod .objects .create (id = 5 , name = "method 5" )
5918+ method10 = MoveLearnMethod .objects .create (id = 10 , name = "method 10" )
5919+
5920+ # Create pokemon and move data with all required setup
5921+ pokemon_species = self .setup_pokemon_species_data (name = "test pokemon species" )
5922+ pokemon = self .setup_pokemon_data (
5923+ name = "test pokemon" , pokemon_species = pokemon_species
5924+ )
5925+ pokemon_form = self .setup_pokemon_form_data (
5926+ pokemon = pokemon , name = "test pokemon form"
5927+ )
5928+ pokemon_ability = self .setup_pokemon_ability_data (pokemon = pokemon )
5929+ pokemon_stat = self .setup_pokemon_stat_data (pokemon = pokemon )
5930+ pokemon_type = self .setup_pokemon_type_data (pokemon = pokemon )
5931+ pokemon_item = self .setup_pokemon_item_data (pokemon = pokemon )
5932+ pokemon_sprites = self .setup_pokemon_sprites_data (pokemon = pokemon )
5933+ pokemon_cries = self .setup_pokemon_cries_data (pokemon , latest = True , legacy = True )
5934+ pokemon_game_index = self .setup_pokemon_game_index_data (
5935+ pokemon = pokemon , game_index = 10
5936+ )
5937+ move = self .setup_move_data (name = "test move" )
5938+
5939+ # Create pokemon moves with different version groups
5940+ # Note: setup_pokemon_move_data creates its own move_learn_method, so we'll create the moves manually
5941+ pokemon_move1 = PokemonMove .objects .create (
5942+ pokemon = pokemon ,
5943+ move = move ,
5944+ version_group = version_group1 , # ID 1
5945+ move_learn_method = method1 , # ID 1
5946+ level = 10 ,
5947+ order = 1 ,
5948+ )
5949+
5950+ pokemon_move2 = PokemonMove .objects .create (
5951+ pokemon = pokemon ,
5952+ move = move ,
5953+ version_group = version_group5 , # ID 5
5954+ move_learn_method = method5 , # ID 5
5955+ level = 15 ,
5956+ order = 2 ,
5957+ )
5958+
5959+ pokemon_move3 = PokemonMove .objects .create (
5960+ pokemon = pokemon ,
5961+ move = move ,
5962+ version_group = version_group10 , # ID 10
5963+ move_learn_method = method10 , # ID 10
5964+ level = 20 ,
5965+ order = 3 ,
5966+ )
5967+
5968+ # Test the API endpoint - this should not raise IndexError
5969+ response = self .client .get (f"{ API_V2 } /pokemon/{ pokemon .pk } /" )
5970+
5971+ self .assertEqual (response .status_code , status .HTTP_200_OK )
5972+
5973+ # Verify the response contains the expected structure
5974+ self .assertIn ("moves" , response .data )
5975+ self .assertIsInstance (response .data ["moves" ], list )
5976+
5977+ # Verify that move data is correctly structured
5978+ moves = response .data ["moves" ]
5979+ self .assertGreater (len (moves ), 0 )
5980+
5981+ # Check that version group details are properly structured
5982+ for move_data in moves :
5983+ self .assertIn ("version_group_details" , move_data )
5984+ for version_detail in move_data ["version_group_details" ]:
5985+ self .assertIn ("version_group" , version_detail )
5986+ self .assertIn ("move_learn_method" , version_detail )
5987+
5988+ # The version group and method should be proper objects, not None or malformed
5989+ self .assertIsInstance (version_detail ["version_group" ], dict )
5990+ self .assertIsInstance (version_detail ["move_learn_method" ], dict )
5991+ self .assertIn ("name" , version_detail ["version_group" ])
5992+ self .assertIn ("name" , version_detail ["move_learn_method" ])
0 commit comments