Python ORM的测试工具
项目描述
distillery 是一个类似 factory_girl 的库,用于Python ORM。
安装
pip安装distillery
蒸馏厂
每个蒸馏厂都有一个 __model__ 以及一组属性和方法。其中 __model__ 是生产实例的ORM模型类
class UserDistillery(MyOrmDistillery):
__model__ = User
属性
蒸馏厂类属性定义了特定模型字段的默认值
class UserDistillery(MyOrmDistillery):
__model__ = User
username = "defaultusername"
蒸馏厂的属性值作为 默认值。如果使用此蒸馏厂创建 User 对象,则其 username 属性将默认为 "defaultusername"。
懒属性
使用 lazy 装饰器,您可以提供动态属性。
from distillery import lazy
class UserDistillery(MyOrmDistillery):
__model__ = User
username = "defaultusername"
@lazy
def email_address(cls, instance, sequence):
return "%s@%s" % (instance.username, instance.company.domain)
所有从 UserDistillery 创建的新 User 都将有一个从其用户名和公司域名计算出的 email_address。
注意:所有懒属性都接收一个 instance 和一个 sequence,分别代表对象实例和一个自动递增的序列。
钩子
蒸馏厂可以提供一个 _after_create 类方法来挂钩到工厂机制。
class UserDistillery(MyOrmDistillery):
__model__ = User
username = "defaultusername"
@classmethod
def _after_create(cls, instance):
# Do stuff after instance creation
# ...
Distillery.init()
init() 方法创建并填充了一个新的实例。
user = UserDistillery.init()
assert user.username == "defaultusername"
assert user.id is None
user = UserDistillery.create(username="overriddenusername")
assert user.username == "overriddenusername"
assert user.id is None
Distillery.create()
方法 create() 使用 init() 初始化对象,并随后进行 保存。
user = UserDistillery.create()
assert user.username == "defaultusername"
assert user.id is not None
Distillery.bulk()
批量创建实例。
users = UserDistillery.bulk(12, username="user_%(i)%")
assert users[7].username = 'user_7'
集合
distillery.Set 类充当固定容器。
需要定义一个 __distillery__ 属性,该属性用于创建对象。
from distillery import Set
class UserSet(Set):
__distillery__ = UserDistillery
class jeanphix:
username = 'jeanphix'
要创建固定值,只需实例化集合。
users = UserSet()
assert users.jeanphix.username == 'jeanphix'
重要的是,只要保持对 实例化集合 的引用(例如,本例中的 users 变量),就可以再次调用集合,并返回相同的实例
assert UserSet() is UserSet()
您还可以引用其他集合。请注意,您必须使用类来引用,或使用延迟属性(稍后描述)
from distillery import Set
class CompanySet(Set):
__distillery__ = CompanyDistillery
class my_company:
name = 'My company'
class UserSet(Set):
__distillery__ = UserDistillery
class jeanphix:
username = 'jeanphix'
company = CompanySet.company
users = UserSet()
assert users.jeanphix.company == 'My company'
除了类之外,还可以定义方法;每个方法都将导致一个对象被添加到集合中。
class ProfileSet(Set)
class __distillery__:
__model__ = Profile
admin = lambda s: UserDistillery.create(username='admin').profile
此功能扩展到基于类的引用。请注意,引用必须在创建点可解析;目前不支持循环关系。
class UserSet(Set):
class peter:
friend = None
class paul:
friend = classmethod(lambda c: UserSet.peter)
如果设置了 on_demand 标志,则对象仅在首次访问时创建。
users = UserSet(on_demand=True)
users.jeanphix # jeanphix will be created here.
最后,集合可以嵌套。
class fixtures(Set):
users = UserSet
assert fixtures().users.jeanphix.username == 'jeanphix'
钩子
集合中的每个固定值都可以提供一个 _after_create 监听器
class ProfileSet(Set):
class __distillery__:
__model__ = Profile
class admin:
@classmethod
def _after_create(cls, profile):
profile.name = 'Full name'
assert ProfileSet().admin.name == 'Full name'
ORM
支持 Django 和 SQLAlchemy。
Django
可以使用 DjangoDistillery 对 Django 模型进行精炼,该精炼只需要一个 __model__ 类成员
from distillery import DjangoDistillery
from django.auth.models import User
class UserDistillery(DjangoDistillery):
__model__ = User
# ...
SQLAlchemy
SQLAlchemy 精炼需要 __model__ 和 __session__ 属性。
from distillery import SQLAlchemyDistillery
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite://', echo=False)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
Base = declarative_base()
class User(Base):
# ...
class UserDistillery(SQLAlchemyDistillery):
__model__ = User
__session__ = session
# ...
项目详细信息
distillery-0.5.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e861f0f333c936771ae568ade129488d670d7b0c67055d703e69a6b50965371f |
|
MD5 | f57925d62d22a5bb1cb713208730325a |
|
BLAKE2b-256 | 64c65b6e53eee9fd4af84c6a1dc9bc438976e69999163c7e3a44f48b896a119e |