Add Factory Boy support

master
Steven Loria 11 years ago
parent 7328186086
commit d1d832ba97
  1. 3
      README.rst
  2. 1
      {{cookiecutter.repo_name}}/requirements/dev.txt
  3. 1
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py
  4. 15
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/factories.py
  5. 10
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/test_models.py
  6. 10
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/user/models.py

@ -13,7 +13,7 @@ Features
- Flask-WTForms with login and registration forms
- Flask-Login for authentication
- Procfile for deploying to a PaaS (e.g. Heroku)
- Flask-Testing and nose for testing
- Flask-Testing, nose, and Factory-Boy for testing
- A simple ``manage.py`` script.
- CSS and JS minification using Flask-Assets
- Utilizes best practices: `Blueprints <http://flask.pocoo.org/docs/blueprints/>`_ and `Application Factory <http://flask.pocoo.org/docs/patterns/appfactories/>`_ patterns
@ -69,6 +69,7 @@ Changelog
- Use Flask-Bcrypt for password hashing.
- Flask-Testing support.
- Flask-Login for authentication.
- Use Factory-Boy for test factories.
0.2.0 (09/21/2013)
******************

@ -4,6 +4,7 @@
# Testing
nose
Flask-Testing
factory-boy>=2.2.1
# Management script
Flask-Script

@ -5,6 +5,7 @@ class Config(object):
SECRET_KEY = 'shhhh'
APP_DIR = os.path.abspath(os.path.dirname(__file__)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
BCRYPT_LEVEL = 13
class ProdConfig(Config):
ENV = 'prod'

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
from factory import Sequence, PostGenerationMethodCall
from factory.alchemy import SQLAlchemyModelFactory
from {{cookiecutter.repo_name}}.user.models import User
from {{cookiecutter.repo_name}}.database import db
class UserFactory(SQLAlchemyModelFactory):
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 = PostGenerationMethodCall("set_password", 'example')

@ -6,6 +6,7 @@ from flask.ext.testing import TestCase
from {{ cookiecutter.repo_name }}.app import create_app
from {{ cookiecutter.repo_name }}.database import db
from {{ cookiecutter.repo_name }}.user.models import User
from .factories import UserFactory
class TestUser(TestCase):
@ -23,6 +24,15 @@ class TestUser(TestCase):
db.session.remove()
db.drop_all()
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_false(user.active)
assert_true(user.check_password("myprecious"))
def test_check_password(self):
user = User(username="foo", email="foo@bar.com",
password="foobarbaz123")

@ -12,24 +12,26 @@ class User(UserMixin, CRUDMixin, db.Model):
__tablename__ = 'users'
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(80), unique=True, nullable=False)
password_hash = db.Column(db.String, nullable=False)
password = db.Column(db.String, nullable=False) # The hashed password
created_at = db.Column(db.DateTime(), nullable=False)
active = db.Column(db.Boolean())
is_admin = db.Column(db.Boolean())
def __init__(self, username, email, password, active=False, is_admin=False):
def __init__(self, username=None, email=None, password=None,
active=False, is_admin=False):
self.username = username
self.email = email
if password:
self.set_password(password)
self.active = active
self.is_admin = is_admin
self.created_at = dt.datetime.utcnow()
def set_password(self, password):
self.password_hash = bcrypt.generate_password_hash(password)
self.password = bcrypt.generate_password_hash(password)
def check_password(self, password):
return bcrypt.check_password_hash(self.password_hash, password)
return bcrypt.check_password_hash(self.password, password)
def __repr__(self):
return '<User "{username}">'.format(username=self.username)

Loading…
Cancel
Save