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. 79
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/test_models.py

@ -17,7 +17,7 @@ else:
app = create_app(DevConfig)
manager = Manager(app)
TEST_CMD = "nosetests"
TEST_CMD = "py.test"
def _make_context():
'''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.alchemy import SQLAlchemyModelFactory
from myflaskapp.user.models import User
from myflaskapp.database import db
from {{cookiecutter.app_name}}.user.models import User
from {{cookiecutter.app_name}}.database import db
class BaseFactory(SQLAlchemyModelFactory):
FACTORY_SESSION = db.session
@classmethod
def _create(cls, target_class, *args, **kwargs):
@ -18,10 +20,11 @@ class BaseFactory(SQLAlchemyModelFactory):
class UserFactory(BaseFactory):
FACTORY_SESSION = db.session
FACTORY_FOR = User
username = Sequence(lambda n: "user{0}".format(n))
email = Sequence(lambda n: "user{0}@example.com".format(n))
password = 'example'
active = True
ALL_FACTORIES = [UserFactory]

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

Loading…
Cancel
Save