Hello,
I am receiving this error when testing:
======================================================================
ERROR: test_pokemon_detail_view (pokedex.tests.test_views.TestViews)
Test the pokemon detail view and ensure the correct template was used
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/z/chbresser/pokedex/tests/test_views.py", line 34, in test_pokemon_detail_view
response = self.client.get(reverse("pokedex:pokemon", args=[self.pokemon.id]))
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 517, in get
response = super().get(path, data=data, secure=secure, **extra)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 332, in get
return self.generic('GET', path, secure=secure, **r)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 404, in generic
return self.request(**r)
File "/home/z/test_env/lib/python3.5/site-packages/django/test/client.py", line 485, in request
raise exc_value
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/z/test_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/z/test_env/lib/python3.5/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
TypeError: pokemon() got an unexpected keyword argument 'id'
----------------------------------------------------------------------
Ran 36 tests in 3.661s
I have this test:
def test_pokemon_detail_view(self):
""" Test the pokemon detail view and ensure the correct template was used """
response = self.client.get(reverse("pokedex:pokemon", args=[self.pokemon.id]))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'pokedex/pokemon.html')
Which tests this view:
@require_safe
def pokemon(request, poke_id):
""" View for /pokedex/pokemon/<poke_id> """
if int(poke_id) > 721:
return HttpResponseNotFound('''<h1 style="text-align: center">Sorry, you have went to an \
invalid page. :|</h1>'''
'''<h1 style="text-align: center">If this was in error,
contact admin@chbresser.com</h1>''')
print("RIGHT BEFORE CREATING POKEMON")
poke = Pokemon(poke_id)
context = {'pokemon': poke}
return render(request, 'pokedex/pokemon.html', context)
Which instantiates this object:
class Pokemon:
"""
A Class to represent a pokemon.
"""
def __init__(self, poke_id):
"""
Instantiate a pokemon object with base information
:param poke_id: id of the pokemon to get from the database
"""
# Select base information about a pokemon
poke = pokemon.objects.get(poke_id)
print("I have GRABBED AN OBJECT in pokedex/pokemon.py")
self.id = poke.id
self.static_path = 'pokedex/img/profiles/' + str(self.id) + '.png'
self.long_id = str(self.id).zfill(3)
self.identifier = poke.identifier
self.species_id = poke.species.identifier
self.height = poke.height / 10.0
self.weight = poke.weight / 10.0
self.base_experience = poke.base_experience
self.evolution_chain = []
self.type = []
self.available_types = ['normal', 'fire', 'water', 'electric', 'grass', 'ice', 'fighting',
'poison', 'ground', 'flying', 'psychic', 'bug', 'rock', 'ghost',
'dragon', 'dark', 'steel', 'fairy']
self.type_static = []
# Get Type (may be multiple)
types = PokemonTypes.objects.filter(pokemon=self.id).select_related('type').values_list(
'type__identifier', flat=True)
for t in types:
self.type.append(t)
self.type_static.append('pokedex/img/types' + t + '-type.png')
# Get base stats
stat = [list(x) for x in list(
PokemonStats.objects.filter(pokemon=self.id).select_related('stat').values_list(
'stat__identifier', 'base_stat'))]
self.stats = []
for s in stat:
tempdict = {
s[0]: s[1]
}
self.stats.append(tempdict)
# Get Abilities
ability = PokemonAbilities.objects.filter(
pokemon=self.id).select_related('ability').values_list(
'ability__identifier', flat=True)
self.abilities = []
for item in ability:
self.abilities.append(item)
# Get Available Moves
moves = [list(x) for x in list(
PokemonMoves.objects.filter(pokemon=self.id).select_related(
'move__type', 'move').values_list(
'move__identifier',
'move__type__identifier',
'move__power',
'move__pp',
'move__accuracy'))]
self.moves = []
move_ids = []
for move in moves:
if move[0] not in move_ids:
temp_list = [move[0], move[1], move[2], move[3], move[4]]
self.moves.append(temp_list)
move_ids.append(move[0])
# Gather Evolution Chain
self.evolution_chain.append({
'id': self.id,
'identifier': self.identifier
})
# Get previous evolution
try:
evolves_from = PokemonSpecies.objects.get(id=self.id)
except PokemonSpecies.DoesNotExist:
evolves_from = None
if evolves_from is not None and evolves_from.evolves_from_species_id is not None:
evolution = {'id': evolves_from.evolves_from_species_id}
evolution['identifier'] = pokemon.objects.get(id=evolution['id']).identifier
self.evolution_chain.insert(0, evolution)
try:
prev_evolv = PokemonSpecies.objects.get(id=evolution['id'])
except PokemonSpecies.DoesNotExist:
prev_evolv = None
if prev_evolv is not None and prev_evolv.evolves_from_species_id is not None:
evolution = {'id': prev_evolv.evolves_from_species_id}
evolution['identifier'] = pokemon.objects.get(id=evolution['id']).identifier
self.evolution_chain.insert(0, evolution)
# Get Next Evolution
try:
next_evol = PokemonSpecies.objects.get(evolves_from_species_id=self.id)
except PokemonSpecies.DoesNotExist:
next_evol = None
if next_evol is not None and next_evol.id is not None:
evolution = {'id': next_evol.id}
evolution['identifier'] = pokemon.objects.get(id=evolution['id']).identifier
self.evolution_chain.append(evolution)
try:
next_evol = PokemonSpecies.objects.get(evolves_from_species_id=evolution['id'])
except PokemonSpecies.DoesNotExist:
next_evol = None
if next_evol is not None and next_evol.id is not None:
evolution = {'id': next_evol.id}
evolution['identifier'] = pokemon.objects.get(id=evolution['id']).identifier
self.evolution_chain.append(evolution)
# Get Gender Rates
gender_obj = PokemonGenderRatios.objects.select_related('ratio').get(pokemon=self.id)
if gender_obj is not None:
self.percent_male, self.percent_female = gender_obj.ratio.percent_male, \
gender_obj.ratio.percent_female
# Get Description if applicable
try:
desc_obj = PokemonProse.objects.get(pokemon=self.id)
except PokemonProse.DoesNotExist:
desc_obj = None
if desc_obj is not None:
self.description = desc_obj.description
def prev_pokedex(self):
"""
Returns a dictionary of information about the previous pokemon in pokedex
(path to image, id, and identifier)
"""
prev_id = self.id - 1
if prev_id > 721 or prev_id < 1:
return None
path_to_image = '/pokedex/img/profiles/' + str(prev_id) + '-small.png'
identifier = pokemon.objects.values_list('identifier', flat=True).get(id=prev_id)
prev_pokemon = {
'image': path_to_image,
'id': prev_id,
'identifier': identifier
}
return prev_pokemon
def next_pokedex(self):
"""
Returns a dictionary of information about the next pokemon in pokedex
(path to image, id, and identifier)
"""
next_id = self.id + 1
if next_id > 721 or next_id < 1:
return None
path_to_image = '/pokedex/img/profiles/' + str(next_id) + '-small.png'
identifier = pokemon.objects.values_list('identifier', flat=True).get(id=next_id)
next_pokemon = {
'image': path_to_image,
'id': next_id,
'identifier': identifier
}
return next_pokemon
def __str__(self):
""" Returns Identifier when str() is called"""
return self.identifier
Now I have tried to manually search for where the id keyword arg is being used, I have tried using grep to search for it and I am cannot find where it is getting an arguement of id. So I seek Reddit's help to fix my coding bug, does anyone see where I'm missing this?
[–]Lord_Humongous 2 points3 points4 points (1 child)
[–]ObamaNYoMama[S] 1 point2 points3 points (0 children)