You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
837 B
31 lines
837 B
11 years ago
|
# -*- coding: utf-8 -*-
|
||
11 years ago
|
from factory import Sequence
|
||
11 years ago
|
from factory.alchemy import SQLAlchemyModelFactory
|
||
|
|
||
11 years ago
|
from {{cookiecutter.app_name}}.user.models import User
|
||
|
from {{cookiecutter.app_name}}.database import db
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
class BaseFactory(SQLAlchemyModelFactory):
|
||
11 years ago
|
FACTORY_SESSION = db.session
|
||
11 years ago
|
|
||
11 years ago
|
@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):
|
||
11 years ago
|
FACTORY_FOR = User
|
||
|
|
||
|
username = Sequence(lambda n: "user{0}".format(n))
|
||
|
email = Sequence(lambda n: "user{0}@example.com".format(n))
|
||
11 years ago
|
password = 'example'
|
||
11 years ago
|
active = True
|
||
11 years ago
|
|
||
|
ALL_FACTORIES = [UserFactory]
|