diff --git a/pokemon_v2/serializers.py b/pokemon_v2/serializers.py index d662d913..dc7fd8ac 100644 --- a/pokemon_v2/serializers.py +++ b/pokemon_v2/serializers.py @@ -2272,8 +2272,10 @@ class PokemonDetailSerializer(serializers.ModelSerializer): method_objects = MoveLearnMethod.objects.all() method_data = MoveLearnMethodSummarySerializer(method_objects, many=True, context=self.context).data - # Get moves related to this pokemon and pull out unique Move IDs - pokemon_moves = PokemonMove.objects.filter(pokemon_id=obj).order_by('level') + # Get moves related to this pokemon and pull out unique Move IDs. Note that it's important to order + # by the same column we're using to determine if the entries are unique. Otherwise distinct() will + # return apparent duplicates. + pokemon_moves = PokemonMove.objects.filter(pokemon_id=obj).order_by('move_id') move_ids = pokemon_moves.values('move_id').distinct() move_list = [] diff --git a/pokemon_v2/tests.py b/pokemon_v2/tests.py index 023fc3e0..87ea7b17 100644 --- a/pokemon_v2/tests.py +++ b/pokemon_v2/tests.py @@ -1806,17 +1806,11 @@ class APIData(): return pokemon_item @classmethod - def setup_pokemon_move_data(self, pokemon, level=0, order=1): - - move = self.setup_move_data( - name='mv for pkmn') + def setup_pokemon_move_data(self, pokemon, move, version_group, level=0, order=1): move_learn_method = self.setup_move_learn_method_data( name='mv lrn mthd for pkmn') - version_group = self.setup_version_group_data( - name='ver grp for pkmn') - pokemon_move = PokemonMove.objects.create ( pokemon = pokemon, version_group = version_group, @@ -3535,8 +3529,21 @@ class APITests(APIData, APITestCase): pokemon_stat = self.setup_pokemon_stat_data(pokemon=pokemon) pokemon_type = self.setup_pokemon_type_data(pokemon=pokemon) pokemon_item = self.setup_pokemon_item_data(pokemon=pokemon) - pokemon_move = self.setup_pokemon_move_data(pokemon=pokemon) pokemon_game_index = self.setup_pokemon_game_index_data(pokemon=pokemon, game_index=10) + # To test issue #85, we will create one move that has multiple + # learn levels in different version groups. Later, we'll + # assert that we only got one move record back. + pokemon_move = self.setup_move_data(name='mv for pkmn') + pokemon_moves = [] + for move in range(0,4): + version_group = self.setup_version_group_data( + name='ver grp '+str(move)+' for pkmn') + new_move = self.setup_pokemon_move_data( + pokemon=pokemon, + move=pokemon_move, + version_group=version_group, + level=move) + pokemon_moves.append(new_move) encounter_method = self.setup_encounter_method_data(name='encntr mthd for lctn area') location_area1 = self.setup_location_area_data(name='lctn1 area for base pkmn') @@ -3580,14 +3587,56 @@ class APITests(APIData, APITestCase): self.assertEqual(response.data['held_items'][0]['version_details'][0]['rarity'], pokemon_item.rarity) self.assertEqual(response.data['held_items'][0]['version_details'][0]['version']['name'], pokemon_item.version.name) self.assertEqual(response.data['held_items'][0]['version_details'][0]['version']['url'], '{}{}/version/{}/'.format(test_host, api_v2, pokemon_item.version.pk)) - # move params - self.assertEqual(response.data['moves'][0]['move']['name'], pokemon_move.move.name) - self.assertEqual(response.data['moves'][0]['move']['url'], '{}{}/move/{}/'.format(test_host, api_v2, pokemon_move.move.pk)) - self.assertEqual(response.data['moves'][0]['version_group_details'][0]['level_learned_at'], pokemon_move.level) - self.assertEqual(response.data['moves'][0]['version_group_details'][0]['version_group']['name'], pokemon_move.version_group.name) - self.assertEqual(response.data['moves'][0]['version_group_details'][0]['version_group']['url'], '{}{}/version-group/{}/'.format(test_host, api_v2, pokemon_move.version_group.pk)) - self.assertEqual(response.data['moves'][0]['version_group_details'][0]['move_learn_method']['name'], pokemon_move.move_learn_method.name) - self.assertEqual(response.data['moves'][0]['version_group_details'][0]['move_learn_method']['url'], '{}{}/move-learn-method/{}/'.format(test_host, api_v2, pokemon_move.move_learn_method.pk)) + # move params -- Make sure that we only got one move back, + # but that we got all of the distinct version group and learn + # level values. (See issue #85) + # Number of Moves + expected = 1 + actual = len(response.data['moves']) + self.assertEqual(expected, actual) + # Move name + expected = pokemon_moves[0].move.name + actual = response.data['moves'][0]['move']['name'] + self.assertEqual(expected, actual) + # Move URL + expected = '{}{}/move/{}/'.format( + test_host, + api_v2, + pokemon_moves[0].move.pk) + actual = response.data['moves'][0]['move']['url'] + self.assertEqual(expected, actual) + # Numbver of version groups + expected = len(pokemon_moves) + actual = len(response.data['moves'][0]['version_group_details']) + self.assertEqual(expected, actual) + for i in range(0,len(pokemon_moves)): + version = response.data['moves'][0]['version_group_details'][i] + # Learn Level + expected = pokemon_moves[i].level + actual = version['level_learned_at'] + self.assertEqual(expected ,actual) + # Version Group Name + expected = pokemon_moves[i].version_group.name + actual = version['version_group']['name'] + self.assertEqual(expected, actual) + # Version Group URL + expected = '{}{}/version-group/{}/'.format( + test_host, + api_v2, + pokemon_moves[i].version_group.pk) + actual = version['version_group']['url'] + self.assertEqual(expected, actual) + # Learn Method Name + expected = pokemon_moves[i].move_learn_method.name + actual = version['move_learn_method']['name'] + self.assertEqual(expected, actual) + # Learn Method URL + expected = '{}{}/move-learn-method/{}/'.format( + test_host, + api_v2, + pokemon_moves[i].move_learn_method.pk) + actual = version['move_learn_method']['url'] + self.assertEqual(expected, actual) # game indices params self.assertEqual(response.data['game_indices'][0]['game_index'], pokemon_game_index.game_index) self.assertEqual(response.data['game_indices'][0]['version']['name'], pokemon_game_index.version.name)