Port model tests to pytest

master
Steven Loria 11 years ago
parent 7c8dcf2b19
commit ba68f616c8
  1. 2
      {{cookiecutter.app_name}}/manage.py
  2. 51
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/conftest.py
  3. 9
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/factories.py
  4. 45
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/test_models.py

@ -17,7 +17,7 @@ else:
app = create_app(DevConfig) app = create_app(DevConfig)
manager = Manager(app) manager = Manager(app)
TEST_CMD = "nosetests" TEST_CMD = "py.test"
def _make_context(): def _make_context():
'''Return context dict for a shell session so you can access '''Return context dict for a shell session so you can access

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
import os
import pytest
from {{ cookiecutter.app_name }}.settings import TestConfig
from {{cookiecutter.app_name}}.app import create_app
from {{cookiecutter.app_name}}.database import db as _db
from .factories import ALL_FACTORIES
@pytest.yield_fixture(scope='session')
def app():
_app = create_app(TestConfig)
ctx = _app.app_context()
ctx.push()
yield _app
ctx.pop()
@pytest.yield_fixture(scope='session')
def db(app):
_db.app = app
with app.app_context():
_db.create_all()
yield _db
_db.drop_all()
@pytest.yield_fixture(scope='function')
def session(db):
conn = db.engine.connect()
transaction = conn.begin()
opts = {'bind': conn, 'binds': {}}
_session = db.create_scoped_session(options=opts)
# Set session for each factory class
for FactoryClass in ALL_FACTORIES:
FactoryClass.FACTORY_SESSION = _session
db.session = _session
yield _session
transaction.rollback()
conn.close()
_session.remove()

@ -2,10 +2,12 @@
from factory import Sequence from factory import Sequence
from factory.alchemy import SQLAlchemyModelFactory from factory.alchemy import SQLAlchemyModelFactory
from myflaskapp.user.models import User from {{cookiecutter.app_name}}.user.models import User
from myflaskapp.database import db from {{cookiecutter.app_name}}.database import db
class BaseFactory(SQLAlchemyModelFactory): class BaseFactory(SQLAlchemyModelFactory):
FACTORY_SESSION = db.session
@classmethod @classmethod
def _create(cls, target_class, *args, **kwargs): def _create(cls, target_class, *args, **kwargs):
@ -18,10 +20,11 @@ class BaseFactory(SQLAlchemyModelFactory):
class UserFactory(BaseFactory): class UserFactory(BaseFactory):
FACTORY_SESSION = db.session
FACTORY_FOR = User FACTORY_FOR = User
username = Sequence(lambda n: "user{0}".format(n)) username = Sequence(lambda n: "user{0}".format(n))
email = Sequence(lambda n: "user{0}@example.com".format(n)) email = Sequence(lambda n: "user{0}@example.com".format(n))
password = 'example' password = 'example'
active = True active = True
ALL_FACTORIES = [UserFactory]

@ -1,7 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import unittest import unittest
import datetime as dt import datetime as dt
from nose.tools import * # PEP8 asserts
import pytest
from {{ cookiecutter.app_name }}.database import db from {{ cookiecutter.app_name }}.database import db
from {{ cookiecutter.app_name }}.user.models import User, Role from {{ cookiecutter.app_name }}.user.models import User, Role
@ -9,45 +10,43 @@ from .base import DbTestCase
from .factories import UserFactory from .factories import UserFactory
class TestUser(DbTestCase): def test_created_at_defaults_to_datetime(session):
def test_created_at_defaults_to_utcnow(self):
user = User(username='foo', email='foo@bar.com') user = User(username='foo', email='foo@bar.com')
user.save() user.save()
assert_true(user.created_at) assert bool(user.created_at)
assert_true(isinstance(user.created_at, dt.datetime)) assert isinstance(user.created_at, dt.datetime) is True
def test_password_is_nullable(self): def test_password_is_nullable(session):
user = User(username='foo', email='foo@bar.com') user = User(username='foo', email='foo@bar.com')
user.save() user.save()
assert_is(user.password, None) assert user.password is None
def test_factory(self): def test_factory(session):
user = UserFactory(password="myprecious") user = UserFactory(password="myprecious")
assert_true(user.username) assert bool(user.username)
assert_true(user.email) assert bool(user.email)
assert_true(user.created_at) assert bool(user.created_at)
assert_false(user.is_admin) assert user.is_admin is False
assert_true(user.active) assert user.active is True
assert_true(user.password == "myprecious") assert user.password == "myprecious"
def test_check_password(self): def test_check_password_with_equality_operators(session):
user = User.create(username="foo", email="foo@bar.com", user = User.create(username="foo", email="foo@bar.com",
password="foobarbaz123") password="foobarbaz123")
assert_true(user.password == 'foobarbaz123') assert user.password == 'foobarbaz123'
assert_false(user.password != "barfoobaz") assert user.password != "barfoobaz"
def test_full_name(self): def test_full_name(session):
user = UserFactory(first_name="Foo", last_name="Bar") user = UserFactory(first_name="Foo", last_name="Bar")
assert_equal(user.full_name, "Foo Bar") assert user.full_name == "Foo Bar"
def test_roles(self): def test_roles(session):
role = Role(name='admin') role = Role(name='admin')
role.save() role.save()
u = UserFactory() u = UserFactory()
u.roles.append(role) u.roles.append(role)
u.save() u.save()
assert_in(role, u.roles) assert role in u.roles
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save