veekun_pokedex/pokedex/tests/test_struct.py
Petr Viktorin ba5fa4355a Add tests
2012-10-14 16:43:52 +02:00

162 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Encoding: utf8
import base64
import pytest
from pokedex import struct
from pokedex.db import connect, tables, util
session = connect()
def check_with_roundtrip(gen, pkmn, expected):
blob = pkmn.blob
del pkmn.blob
assert blob == pkmn.blob
assert pkmn.export_dict() == expected
from_dict = struct.save_file_pokemon_classes[5](session=session,
dict_=expected)
assert from_dict.blob == blob
assert from_dict.export_dict() == expected
from_blob = struct.save_file_pokemon_classes[5](session=session,
blob=pkmn.blob)
assert from_blob.blob == blob
assert from_blob.export_dict() == expected
voltorb_species = util.get(session, tables.PokemonSpecies, 'voltorb')
def voltorb_and_dict():
pkmn = struct.save_file_pokemon_classes[5](session=session)
voltorb_species = util.get(session, tables.PokemonSpecies, 'voltorb')
pkmn.species = voltorb_species
expected = {
'gender': 'male',
'species': dict(id=100, name=u'Voltorb'),
'nickname': u'\0' * 11,
'nickname trash': 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==',
'moves': [],
}
return pkmn, expected
def test_species():
pkmn, expected = voltorb_and_dict()
assert pkmn.species == voltorb_species
assert pkmn.pokemon == voltorb_species.default_pokemon
assert pkmn.form == voltorb_species.default_form
assert pkmn.export_dict() == expected
def test_nickname():
pkmn, expected = voltorb_and_dict()
pkmn.nickname = pkmn.nickname
expected['nicknamed'] = True
check_with_roundtrip(5, pkmn, expected)
pkmn.is_nicknamed = False
del expected['nicknamed']
check_with_roundtrip(5, pkmn, expected)
def test_moves():
pkmn, expected = voltorb_and_dict()
new_moves = (util.get(session, tables.Move, 'sonicboom'), )
expected['moves'] = [dict(id=49, name=u'SonicBoom', pp=0)]
pkmn.moves = new_moves
assert pkmn.moves == new_moves
check_with_roundtrip(5, pkmn, expected)
new_moves += (util.get(session, tables.Move, 'explosion'),)
expected['moves'].append(dict(id=153, name=u'Explosion', pp=0))
pkmn.moves = new_moves
assert pkmn.moves == new_moves
check_with_roundtrip(5, pkmn, expected)
new_pp = (20,)
expected['moves'][0]['pp'] = 20
pkmn.move_pp = new_pp
assert pkmn.move_pp == (20, 0, 0, 0)
check_with_roundtrip(5, pkmn, expected)
def test_personality():
pkmn, expected = voltorb_and_dict()
assert pkmn.is_shiny == True
pkmn.personality = 12345
assert pkmn.is_shiny == False
expected['personality'] = 12345
check_with_roundtrip(5, pkmn, expected)
def test_pokeball():
pkmn, expected = voltorb_and_dict()
masterball = util.get(session, tables.Item, 'master-ball')
pkmn.pokeball = masterball
assert pkmn.pokeball == masterball
expected['pokeball'] = dict(id_dppt=1, name='Master Ball')
check_with_roundtrip(5, pkmn, expected)
def test_experience():
pkmn, expected = voltorb_and_dict()
exp = 2340
pkmn.exp = exp
assert pkmn.exp == exp
assert pkmn.experience_rung.experience < pkmn.exp
assert pkmn.next_experience_rung.experience > pkmn.exp
assert pkmn.experience_rung.level + 1 == pkmn.next_experience_rung.level
assert (pkmn.experience_rung.growth_rate ==
pkmn.next_experience_rung.growth_rate ==
pkmn.species.growth_rate)
assert pkmn.level == pkmn.experience_rung.level
assert pkmn.exp_to_next == pkmn.next_experience_rung.experience - pkmn.exp
rung_difference = (pkmn.next_experience_rung.experience -
pkmn.experience_rung.experience)
assert pkmn.progress_to_next == (
pkmn.exp - pkmn.experience_rung.experience) / float(rung_difference)
expected['exp'] = exp
check_with_roundtrip(5, pkmn, expected)
def test_ability():
pkmn, expected = voltorb_and_dict()
ability = util.get(session, tables.Ability, 'drizzle')
pkmn.ability = ability
assert pkmn.ability == ability
expected['ability'] = dict(id=2, name='Drizzle')
check_with_roundtrip(5, pkmn, expected)
def test_squitle_blob():
# Japanese Dream World Squirtle, coutresy of
# http://projectpokemon.org/events/
blob = base64.b64decode('J2ZqBgAAICQHAAAAkOaKyTACAABGLAABAAAAAAAAAAAAAAAAA'
'AAAACEAJwCRAG4AIx4eKAAAAAD171MHAAAAAAAAAQAAAAAAvDDLMKww4TD//wAAAAAAAA'
'AAAAD//wAVAAAAAAAAAAAw/zD/T/9S/0f///8AAAAAAAAACgoOAABLAAAZCgAAAA==')
expected = {
'ability': {'id': 44, 'name': u'Rain Dish'},
'date met': '2010-10-14',
'exp': 560,
'gender': 'male',
'genes': {u'attack': 31,
u'defense': 27,
u'hp': 21,
u'special attack': 21,
u'special defense': 3,
u'speed': 7},
'happiness': 70,
'met at level': 10,
'met location': {'id_dp': 75, 'name': u'Spring Path'},
'moves': [{'id': 33, 'name': u'Tackle', 'pp': 35},
{'id': 39, 'name': u'Tail Whip', 'pp': 30},
{'id': 145, 'name': u'Bubble', 'pp': 30},
{'id': 110, 'name': u'Withdraw', 'pp': 40}],
'nickname': u'ゼニガメ',
'nickname trash': 'vDDLMKww4TD//wAAAAAAAAAAAAD//w==',
'oiginal trainer': {'gender': 'male',
'id': 59024,
'name': u'',
'secret': 51594},
'original country': 'jp',
'original version': 21,
'personality': 107636263,
'pokeball': {'id_dppt': 25, 'name': u'Hyper Potion'},
'species': {'id': 7, 'name': u'Squirtle'}}
pkmn = struct.save_file_pokemon_classes[5](session=session, blob=blob)
check_with_roundtrip(5, pkmn, expected)