Created
November 11, 2017 09:23
-
-
Save sobolevn/19032ff9ed03a167a3cc3337ebaebd99 to your computer and use it in GitHub Desktop.
mimesis-factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import random | |
class Account(object): | |
def __init__(self, username, email): | |
self.username = username | |
self.email = email | |
self.password = ''.join(str(random.randint(0, 10)) for _ in range(10)) | |
self.date_joined = datetime.datetime.today() | |
def __str__(self): | |
return '{} ({})'.format(self.username, self.email) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.python.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[packages] | |
mimesis = "*" | |
factory-boy = "==2.8.1" | |
pytest = "*" | |
pytest-factoryboy = "*" | |
[dev-packages] | |
[requires] | |
python_version = "3.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import factory | |
from pytest_factoryboy import register | |
from account import Account | |
@register | |
class AccountFactory(factory.Factory): | |
class Meta: | |
model = Account | |
username = factory.Sequence(lambda n: 'john%s' % n) | |
email = factory.LazyAttribute(lambda o: '%[email protected]' % o.username) | |
def test_account_exists(account): | |
assert account.username == 'john0' | |
assert account.email == '[email protected]' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ======= | |
# Now with mimesis | |
import factory | |
from factory import declarations | |
from mimesis import Generic | |
from pytest_factoryboy import register | |
from account import Account | |
class Mimesis(declarations.OrderedDeclaration): | |
def __init__(self, provider, field, locale=None, **kwargs): | |
self.locale = locale | |
self.mimesis = Generic(self.locale) | |
self.provider = provider | |
self.provider_kwargs = kwargs | |
self.field = field | |
def generate(self, extra_kwargs): | |
print(extra_kwargs) | |
provider = getattr(self.mimesis, self.provider) | |
field = getattr(provider, self.field)() | |
return field | |
def evaluate(self, sequence, obj, create, extra=None, containers=()): | |
print(sequence, obj, create, extra, containers) | |
return self.generate(extra or {}) | |
@register | |
class AccountFactory(factory.Factory): | |
class Meta: | |
model = Account | |
username = Mimesis('personal', 'username') | |
email = factory.LazyAttribute( | |
lambda o: '%[email protected]' % o.username | |
) | |
def test_account_exists(account): | |
assert account.username != 'john0' | |
assert account.email != '[email protected]' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment