2011-03-12 12:36:08 +00:00
# Encoding: UTF-8
2011-05-02 07:20:28 +00:00
import pytest
2014-07-07 03:25:04 +00:00
parametrize = pytest . mark . parametrize
2011-03-12 12:36:08 +00:00
2014-07-07 03:25:04 +00:00
from sqlalchemy . orm . exc import NoResultFound
2011-03-13 15:47:31 +00:00
2011-05-02 07:20:28 +00:00
from pokedex . db import tables , connect , util , markdown
2011-03-12 12:36:08 +00:00
2014-07-07 03:25:04 +00:00
@pytest.fixture ( scope = " module " )
def session ( request ) :
uri = request . config . getvalue ( " engine " )
return connect ( uri )
2011-05-02 07:20:28 +00:00
2014-07-07 03:25:04 +00:00
def test_filter ( session ) :
q = session . query ( tables . PokemonSpecies ) . filter (
2011-05-02 07:20:28 +00:00
tables . PokemonSpecies . name == u " Marowak " )
assert q . one ( ) . identifier == ' marowak '
2014-07-07 03:25:04 +00:00
def test_languages ( session ) :
q = session . query ( tables . PokemonSpecies ) . filter (
2011-05-02 07:20:28 +00:00
tables . PokemonSpecies . name == u " Mightyena " )
pkmn = q . one ( )
for lang , name in (
( ' en ' , u ' Mightyena ' ) ,
( ' ja ' , u ' グラエナ ' ) ,
( ' roomaji ' , u ' Guraena ' ) ,
( ' fr ' , u ' Grahyèna ' ) ,
) :
2014-07-07 03:25:04 +00:00
language = session . query ( tables . Language ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = lang ) . one ( )
assert pkmn . name_map [ language ] == name
2014-07-07 03:25:04 +00:00
def test_bad_lang ( session ) :
2011-05-02 07:20:28 +00:00
with pytest . raises ( KeyError ) :
2014-07-07 03:25:04 +00:00
q = session . query ( tables . PokemonSpecies ) . filter (
2011-04-29 23:14:34 +00:00
tables . PokemonSpecies . name == u " Mightyena " )
2011-03-12 12:36:08 +00:00
pkmn = q . one ( )
pkmn . names [ " identifier of a language that doesn ' t exist " ]
2011-03-13 15:47:31 +00:00
2014-07-07 03:25:04 +00:00
def test_mutating ( session ) :
item = session . query ( tables . Item ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " jade-orb " ) . one ( )
2014-07-07 03:25:04 +00:00
language = session . query ( tables . Language ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " de " ) . one ( )
item . name_map [ language ] = u " foo "
assert item . name_map [ language ] == " foo "
item . name_map [ language ] = u " xyzzy "
assert item . name_map [ language ] == " xyzzy "
2014-07-07 03:25:04 +00:00
def test_mutating_default ( session ) :
item = session . query ( tables . Item ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " jade-orb " ) . one ( )
item . name = u " foo "
assert item . name == " foo "
2014-07-07 03:25:04 +00:00
def test_string_mapping ( session ) :
item = session . query ( tables . Item ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " jade-orb " ) . one ( )
assert len ( item . name_map ) == len ( item . names )
for lang in item . names :
assert item . name_map [ lang ] == item . names [ lang ] . name
assert lang in item . name_map
assert " language that doesn ' t exist " not in item . name_map
assert tables . Language ( ) not in item . name_map
2014-07-07 03:25:04 +00:00
def test_new_language ( session ) :
item = session . query ( tables . Item ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " jade-orb " ) . one ( )
language = tables . Language ( )
language . id = - 1
language . identifier = u ' test '
language . iso639 = language . iso3166 = u ' -- '
language . official = False
2014-07-07 03:25:04 +00:00
session . add ( language )
2011-05-02 07:20:28 +00:00
item . name_map [ language ] = u " foo "
assert item . name_map [ language ] == " foo "
assert language in item . name_map
item . name_map [ language ] = u " xyzzy "
assert item . name_map [ language ] == " xyzzy "
2014-07-07 03:25:04 +00:00
def test_markdown ( session ) :
move = session . query ( tables . Move ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " thunderbolt " ) . one ( )
2014-07-07 03:25:04 +00:00
language = session . query ( tables . Language ) . filter_by (
2011-05-02 07:20:28 +00:00
identifier = u " en " ) . one ( )
assert ' 10 % ' in move . effect . as_text ( )
assert ' 10 % ' in move . effect_map [ language ] . as_text ( )
assert ' 10 % ' in move . effect . as_html ( )
assert ' 10 % ' in move . effect_map [ language ] . as_html ( )
assert ' 10 % ' in unicode ( move . effect )
assert ' 10 % ' in unicode ( move . effect_map [ language ] )
assert ' 10 % ' in move . effect . __html__ ( )
assert ' 10 % ' in move . effect_map [ language ] . __html__ ( )
2014-07-07 03:25:04 +00:00
def test_markdown_string ( session ) :
en = util . get ( session , tables . Language , ' en ' )
md = markdown . MarkdownString ( ' [] { move:thunderbolt} [paralyzes] { mechanic:paralysis} [] { form:sky shaymin}. [] { pokemon:mewthree} does not exist. ' , session , en )
2011-09-12 15:56:23 +00:00
assert unicode ( md ) == ' Thunderbolt paralyzes Sky Shaymin. mewthree does not exist. '
assert md . as_html ( ) == ' <p><span>Thunderbolt</span> <span>paralyzes</span> <span>Sky Shaymin</span>. <span>mewthree</span> does not exist.</p> '
2011-09-07 04:50:00 +00:00
class ObjectTestExtension ( markdown . PokedexLinkExtension ) :
def object_url ( self , category , obj ) :
2011-09-12 14:50:31 +00:00
if isinstance ( obj , tables . PokemonForm ) :
return " %s / %s %s " % ( category , obj . form_identifier ,
obj . species . identifier )
else :
return " %s / %s " % ( category , obj . identifier )
2011-09-07 04:50:00 +00:00
class IdentifierTestExtension ( markdown . PokedexLinkExtension ) :
def identifier_url ( self , category , ident ) :
2011-09-12 15:56:23 +00:00
return " %s / %s " % ( category , ident )
2011-09-07 04:50:00 +00:00
2014-07-07 03:25:04 +00:00
assert md . as_html ( extension = ObjectTestExtension ( session ) ) == (
2011-09-12 15:56:23 +00:00
' <p><a href= " move/thunderbolt " >Thunderbolt</a> <span>paralyzes</span> <a href= " form/sky shaymin " >Sky Shaymin</a>. <span>mewthree</span> does not exist.</p> ' )
2014-07-07 03:25:04 +00:00
assert md . as_html ( extension = IdentifierTestExtension ( session ) ) == (
2011-09-12 15:56:23 +00:00
' <p><a href= " move/thunderbolt " >Thunderbolt</a> <a href= " mechanic/paralysis " >paralyzes</a> <a href= " form/sky shaymin " >Sky Shaymin</a>. <a href= " pokemon/mewthree " >mewthree</a> does not exist.</p> ' )
2011-05-02 07:20:28 +00:00
def markdown_column_params ( ) :
""" Check all markdown values
Scans the database schema for Markdown columns , runs through every value
in each , and ensures that it ' s valid Markdown.
"""
# Move effects have their own special wrappers. Explicitly test them separately
yield tables . Move , None , ' effect '
yield tables . Move , None , ' short_effect '
for cls in tables . mapped_classes :
for translation_cls in cls . translation_classes :
for column in translation_cls . __table__ . c :
if column . info . get ( ' string_getter ' ) == markdown . MarkdownString :
yield cls , translation_cls , column . name
2014-07-07 03:25:04 +00:00
@pytest.mark.slow
@parametrize (
( ' parent_class ' , ' translation_class ' , ' column_name ' ) ,
list ( markdown_column_params ( ) )
)
def test_markdown_values ( session , parent_class , translation_class , column_name ) :
2011-05-02 07:20:28 +00:00
""" Implementation for the above """
2014-07-07 03:25:04 +00:00
query = session . query ( parent_class )
2011-05-02 07:20:28 +00:00
if translation_class :
query = query . join ( translation_class )
2011-09-12 15:56:23 +00:00
2011-09-12 16:57:06 +00:00
class TestExtension ( markdown . PokedexLinkExtension ) :
def object_url ( self , category , obj ) :
" Swallow good links "
return ' ok '
def identifier_url ( self , category , ident ) :
" Only allow mechanic links here (good links handled in object_url) "
# Note: 'key' is a closed variable that gets set in the loop below
assert category == ' mechanic ' , (
' %s : unknown link target: { %s : %s } ' %
( key , category , ident ) )
2014-07-07 03:25:04 +00:00
test_extension = TestExtension ( session )
2011-09-12 16:57:06 +00:00
2011-05-02 07:20:28 +00:00
for item in query :
2011-09-12 15:56:23 +00:00
for language , md_text in getattr ( item , column_name + ' _map ' ) . items ( ) :
2011-05-02 07:20:28 +00:00
2011-09-12 15:56:23 +00:00
if md_text is None :
2011-05-02 07:20:28 +00:00
continue
key = u " Markdown in {0} # {1} ' s {2} (lang= {3} ) " . format (
parent_class . __name__ , item . id , column_name , language . identifier )
2011-09-12 16:57:06 +00:00
text = md_text . as_html ( extension = test_extension )
2011-05-02 07:20:28 +00:00
error_message = u " {0} leaves syntax cruft: \n {1} "
error_message = error_message . format ( key , text )
assert not any ( char in text for char in ' [] {} ' ) , error_message