mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Modernize our use of py.test
This commit updates the tests to take advantage of some of py.test's newer features. Requires py.test 2.3 or newer. Tested with 2.3.0 and 2.5.2. Tests which were parametrized now use py.test's built-in parametrization[1]. The session and lookup objects are now implemented as fixtures[2]. The media root is a fixture as well. Fixtures are automatically passed to any function that expects them. Since the session is now created in one place, it is now possible to provide an engine URI on the command line when running py.test. Ditto for the index directory. (But the environment variables still work of course.) Slow tests are now marked as such and not run unless the --all option is given. A couple media tests are marked as xfail (expected to fail) because they are broken. [1]: http://pytest.org/latest/parametrize.html [2]: http://pytest.org/latest/fixture.html
This commit is contained in:
parent
29824c73f4
commit
0094e9584c
10 changed files with 285 additions and 299 deletions
pokedex/tests
|
@ -1,5 +1,5 @@
|
|||
|
||||
import pytest
|
||||
parametrize = pytest.mark.parametrize
|
||||
|
||||
from sqlalchemy.orm import aliased, joinedload, lazyload
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
@ -7,11 +7,11 @@ from sqlalchemy.sql import func
|
|||
|
||||
from pokedex.db import connect, tables, util
|
||||
|
||||
def test_encounter_slots():
|
||||
# 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.
|
||||
session = connect()
|
||||
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.
|
||||
"""
|
||||
|
||||
version_group_a = aliased(tables.VersionGroup)
|
||||
version_group_b = aliased(tables.VersionGroup)
|
||||
|
@ -26,27 +26,26 @@ def test_encounter_slots():
|
|||
# Encounter slots all match the encounters they belong to
|
||||
assert sanity_q.count() == 0
|
||||
|
||||
def test_nonzero_autoincrement_ids():
|
||||
@parametrize('cls', tables.mapped_classes)
|
||||
def test_nonzero_autoincrement_ids(session, cls):
|
||||
"""Check that autoincrementing ids don't contain zeroes
|
||||
|
||||
MySQL doesn't like these, see e.g. bug #580
|
||||
"""
|
||||
if 'id' not in cls.__table__.c:
|
||||
return
|
||||
if not cls.__table__.c.id.autoincrement:
|
||||
return
|
||||
|
||||
session = connect()
|
||||
for cls in tables.mapped_classes:
|
||||
if 'id' in cls.__table__.c:
|
||||
if cls.__table__.c.id.autoincrement:
|
||||
def nonzero_id(cls):
|
||||
with pytest.raises(NoResultFound):
|
||||
util.get(session, cls, id=0)
|
||||
nonzero_id.description = "No zero id in %s" % cls.__name__
|
||||
yield nonzero_id, cls
|
||||
try:
|
||||
util.get(session, cls, id=0)
|
||||
except NoResultFound:
|
||||
pass
|
||||
else:
|
||||
pytest.fail("No zero id in %s" % cls.__name__)
|
||||
|
||||
def test_unique_form_order():
|
||||
"""Check that tone PokemonForm.order value isn't used for more species
|
||||
"""
|
||||
|
||||
session = connect()
|
||||
def test_unique_form_order(session):
|
||||
"""Check that tone PokemonForm.order value isn't used for more species"""
|
||||
|
||||
species_by_form_order = {}
|
||||
|
||||
|
@ -66,12 +65,10 @@ def test_unique_form_order():
|
|||
species_by_form_order[form.order].name,
|
||||
form.species.name))
|
||||
|
||||
def test_default_forms():
|
||||
def test_default_forms(session):
|
||||
"""Check that each pokemon has one default form and each species has one
|
||||
default pokemon."""
|
||||
|
||||
session = connect()
|
||||
|
||||
q = session.query(tables.Pokemon)
|
||||
q = q.join(tables.PokemonForm)
|
||||
q = q.filter(tables.PokemonForm.is_default==True)
|
||||
|
@ -81,9 +78,9 @@ def test_default_forms():
|
|||
|
||||
for pokemon, num_default_forms in q:
|
||||
if num_default_forms == 0:
|
||||
raise AssertionError("pokemon %s has no default forms" % pokemon.name)
|
||||
pytest.fail("pokemon %s has no default forms" % pokemon.name)
|
||||
elif num_default_forms > 1:
|
||||
raise AssertionError("pokemon %s has %d default forms" % (pokemon.name, num_default_forms))
|
||||
pytest.fail("pokemon %s has %d default forms" % (pokemon.name, num_default_forms))
|
||||
|
||||
q = session.query(tables.PokemonSpecies)
|
||||
q = q.join(tables.Pokemon)
|
||||
|
@ -94,6 +91,6 @@ def test_default_forms():
|
|||
|
||||
for species, num_default_pokemon in q:
|
||||
if num_default_pokemon == 0:
|
||||
raise AssertionError("species %s has no default pokemon" % species.name)
|
||||
pytest.fail("species %s has no default pokemon" % species.name)
|
||||
elif num_default_pokemon > 1:
|
||||
raise AssertionError("species %s has %d default pokemon" % (species.name, num_default_pokemon))
|
||||
pytest.fail("species %s has %d default pokemon" % (species.name, num_default_pokemon))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue