2011-05-02 07:20:28 +00:00
import pytest
2014-07-07 03:25:04 +00:00
parametrize = pytest . mark . parametrize
2011-05-02 07:20:28 +00:00
2018-09-29 18:35:13 +00:00
import re
2013-10-29 09:54:36 +00:00
from sqlalchemy . orm import aliased , joinedload , lazyload
2011-04-19 09:24:40 +00:00
from sqlalchemy . orm . exc import NoResultFound
2013-10-29 09:54:36 +00:00
from sqlalchemy . sql import func
2011-01-27 05:33:03 +00:00
2016-11-24 21:29:58 +00:00
from pokedex . db import tables , util
2011-01-27 05:33:03 +00:00
2014-07-07 03:25:04 +00:00
def test_encounter_slots ( session ) :
""" Encounters have a version, which has a version group; encounters also
have an encounter_slot , which has a version group . The two version
groups should match , universally .
"""
2011-01-27 05:33:03 +00:00
version_group_a = aliased ( tables . VersionGroup )
version_group_b = aliased ( tables . VersionGroup )
sanity_q = session . query ( tables . Encounter ) \
. join ( ( tables . EncounterSlot , tables . Encounter . slot ) ) \
. join ( ( version_group_a , tables . EncounterSlot . version_group ) ) \
. join ( ( tables . Version , tables . Encounter . version ) ) \
. join ( ( version_group_b , tables . Version . version_group ) ) \
. filter ( version_group_a . id != version_group_b . id )
2011-05-02 07:20:28 +00:00
# Encounter slots all match the encounters they belong to
assert sanity_q . count ( ) == 0
2011-04-19 09:24:40 +00:00
2019-07-13 01:48:07 +00:00
def test_encounter_regions ( session ) :
""" Check that encounter locations match the region of the game they ' re from.
"""
sanity_q = session . query ( tables . Encounter ) \
. join ( ( tables . Version , tables . Encounter . version ) ) \
. join ( ( tables . VersionGroup , tables . Version . version_group ) ) \
. join ( ( tables . LocationArea , tables . Encounter . location_area ) ) \
. join ( ( tables . Location , tables . LocationArea . location ) ) \
. join ( ( tables . Region , tables . Location . region ) ) \
. filter ( ~ tables . VersionGroup . version_group_regions . any ( tables . VersionGroupRegion . region_id == tables . Region . id ) )
for e in sanity_q . limit ( 20 ) :
acceptable_regions = " or " . join ( r . identifier for r in e . version . version_group . regions )
if e . location_area . location . region is not None :
print ( " {e} ( {e.pokemon.identifier} , {e.slot.method.identifier} , {e.version.identifier} ) is in {e.location_area.location.region.identifier} ( {e.location_area.location.identifier} ) but should be in {acceptable_regions} ( {e.version.identifier} ) " . format ( e = e , acceptable_regions = acceptable_regions ) )
else :
print ( " {e} ( {e.pokemon.identifier} , {e.slot.method.identifier} , {e.version.identifier} ) is in a pseudo-location ( {e.location_area.location.identifier} ) that is not part of any region, but should be in {acceptable_regions} ( {e.version.identifier} ) " . format ( e = e , acceptable_regions = acceptable_regions ) )
# Encounter regions match the games they belong to
assert sanity_q . count ( ) == 0
2014-07-07 03:25:04 +00:00
@parametrize ( ' cls ' , tables . mapped_classes )
def test_nonzero_autoincrement_ids ( session , cls ) :
2011-04-19 09:24:40 +00:00
""" Check that autoincrementing ids don ' t contain zeroes
MySQL doesn ' t like these, see e.g. bug #580
"""
2014-07-07 03:25:04 +00:00
if ' id ' not in cls . __table__ . c :
return
if not cls . __table__ . c . id . autoincrement :
return
2011-04-19 09:24:40 +00:00
2014-07-07 03:25:04 +00:00
try :
util . get ( session , cls , id = 0 )
except NoResultFound :
pass
else :
pytest . fail ( " No zero id in %s " % cls . __name__ )
2011-09-05 08:20:16 +00:00
2014-07-07 03:25:04 +00:00
def test_unique_form_order ( session ) :
""" Check that tone PokemonForm.order value isn ' t used for more species """
2011-09-05 08:20:16 +00:00
species_by_form_order = { }
query = session . query ( tables . PokemonForm )
query = query . options ( joinedload ( ' pokemon.species ' ) )
for form in query :
try :
previous_species = species_by_form_order [ form . order ]
except KeyError :
species_by_form_order [ form . order ] = form . species
else :
assert previous_species == form . species , (
" PokemonForm.order == %s is used for %s and %s " % (
form . order ,
species_by_form_order [ form . order ] . name ,
form . species . name ) )
2013-10-29 09:54:36 +00:00
2014-07-07 03:25:04 +00:00
def test_default_forms ( session ) :
2013-10-29 09:54:36 +00:00
""" Check that each pokemon has one default form and each species has one
default pokemon . """
q = session . query ( tables . Pokemon )
q = q . join ( tables . PokemonForm )
q = q . filter ( tables . PokemonForm . is_default == True )
q = q . options ( lazyload ( ' * ' ) )
q = q . group_by ( tables . Pokemon )
q = q . add_columns ( func . count ( tables . PokemonForm . id ) )
for pokemon , num_default_forms in q :
if num_default_forms == 0 :
2014-07-07 03:25:04 +00:00
pytest . fail ( " pokemon %s has no default forms " % pokemon . name )
2013-10-29 09:54:36 +00:00
elif num_default_forms > 1 :
2014-07-07 03:25:04 +00:00
pytest . fail ( " pokemon %s has %d default forms " % ( pokemon . name , num_default_forms ) )
2013-10-29 09:54:36 +00:00
q = session . query ( tables . PokemonSpecies )
q = q . join ( tables . Pokemon )
q = q . filter ( tables . Pokemon . is_default == True )
q = q . options ( lazyload ( ' * ' ) )
q = q . group_by ( tables . PokemonSpecies )
q = q . add_columns ( func . count ( tables . Pokemon . id ) )
for species , num_default_pokemon in q :
if num_default_pokemon == 0 :
2014-07-07 03:25:04 +00:00
pytest . fail ( " species %s has no default pokemon " % species . name )
2013-10-29 09:54:36 +00:00
elif num_default_pokemon > 1 :
2014-07-07 03:25:04 +00:00
pytest . fail ( " species %s has %d default pokemon " % ( species . name , num_default_pokemon ) )
2018-09-29 18:35:13 +00:00
2018-09-29 18:56:40 +00:00
ROUTE_RE = re . compile ( u ' route- \\ d+ ' )
2018-09-29 18:35:13 +00:00
def test_location_identifiers ( session ) :
""" Check that location identifiers for some common locations are prefixed
with the region name , ala kalos - route - 2 """
q = session . query ( tables . Location )
q = q . join ( tables . Region )
q = q . options ( lazyload ( ' * ' ) )
for loc in q :
if ( loc . identifier in [ u ' victory-road ' , u ' pokemon-league ' , u ' safari-zone ' ]
or ROUTE_RE . match ( loc . identifier ) ) :
if loc . region :
region = loc . region . identifier . lower ( )
suggested_identifier = region + " - " + loc . identifier
pytest . fail ( " location %d : identifier %s should be prefixed with its region (e.g. %s ) " % ( loc . id , loc . identifier , suggested_identifier ) )
else :
pytest . fail ( " location %d : identifier %s should be prefixed with its region " % ( loc . id , loc . identifier ) )