Add BaseFactory which flushes the db on create

master
Steven Loria 11 years ago
parent 388cf106c2
commit 5bfd073802
  1. 19
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/tests/factories.py
  2. 6
      {{cookiecutter.app_name}}/{{cookiecutter.app_name}}/user/models.py

@ -1,12 +1,23 @@
# -*- coding: utf-8 -*-
from factory import Sequence, PostGenerationMethodCall
from factory import Sequence
from factory.alchemy import SQLAlchemyModelFactory
from {{cookiecutter.app_name}}.user.models import User
from {{cookiecutter.app_name}}.database import db
from myflaskapp.user.models import User
from myflaskapp.database import db
class BaseFactory(SQLAlchemyModelFactory):
class UserFactory(SQLAlchemyModelFactory):
@classmethod
def _create(cls, target_class, *args, **kwargs):
"""Create an instance of the model, and save it to the database."""
session = cls.FACTORY_SESSION
obj = target_class(*args, **kwargs)
session.add(obj)
session.commit()
return obj
class UserFactory(BaseFactory):
FACTORY_SESSION = db.session
FACTORY_FOR = User

@ -26,7 +26,7 @@ class User(UserMixin, CRUDMixin, db.Model):
username = Column(db.String(80), unique=True, nullable=False)
email = Column(db.String(80), unique=True, nullable=False)
password = Column(BcryptType, nullable=True)
created_at = Column(db.DateTime, nullable=False)
created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
first_name = Column(db.String(30), nullable=True)
last_name = Column(db.String(30), nullable=True)
active = Column(db.Boolean(), default=False)
@ -34,12 +34,10 @@ class User(UserMixin, CRUDMixin, db.Model):
def __init__(self, username, email, **kwargs):
db.Model.__init__(self, username=username, email=email, **kwargs)
if not self.created_at:
self.created_at = dt.datetime.utcnow()
@property
def full_name(self):
return "{0} {1}".format(self.first_name, self.last_name)
def __repr__(self):
return '<User "{username}">'.format(username=self.username)
return '<User {username!r}>'.format(username=self.username)

Loading…
Cancel
Save