Skip to content

Commit

Permalink
Switch away from ParameteredDeclaration
Browse files Browse the repository at this point in the history
Now that BaseDeclaration handles arbitrary kwargs as a set of default
parameters for the declaration (and exposes them through its
`unroll_context` default), the ParameteredDeclaration is no longer
required.
  • Loading branch information
rbarrois committed Dec 23, 2020
1 parent 372f3c2 commit 824c6e0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions factory/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def __repr__(self):
return f'<_FactoryImport: {self.factory.__class__}>'


class SubFactory(ParameteredAttribute):
class SubFactory(BaseDeclaration):
"""Base class for attributes based upon a sub-factory.
Attributes:
Expand All @@ -399,7 +399,7 @@ def get_factory(self):
"""Retrieve the wrapped factory.Factory subclass."""
return self.factory_wrapper.get()

def generate(self, step, params):
def evaluate(self, instance, step, extra):
"""Evaluate the current definition and fill its attributes.
Args:
Expand All @@ -411,11 +411,11 @@ def generate(self, step, params):
logger.debug(
"SubFactory: Instantiating %s.%s(%s), create=%r",
subfactory.__module__, subfactory.__name__,
utils.log_pprint(kwargs=params),
utils.log_pprint(kwargs=extra),
step,
)
force_sequence = step.sequence if self.FORCE_SEQUENCE else None
return step.recurse(subfactory, params, force_sequence=force_sequence)
return step.recurse(subfactory, extra, force_sequence=force_sequence)


class Dict(SubFactory):
Expand Down
6 changes: 3 additions & 3 deletions factory/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _after_postgeneration(cls, instance, create, results=None):
instance.save()


class FileField(declarations.ParameteredDeclaration):
class FileField(declarations.BaseDeclaration):
"""Helper to fill in django.db.models.FileField from a Factory."""

DEFAULT_FILENAME = 'example.dat'
Expand Down Expand Up @@ -219,9 +219,9 @@ def _make_content(self, params):
filename = params.get('filename', default_filename)
return filename, content

def generate(self, params):
def evaluate(self, instance, step, extra):
"""Fill in the field."""
filename, content = self._make_content(params)
filename, content = self._make_content(extra)
return django_files.File(content.file, filename)


Expand Down
8 changes: 4 additions & 4 deletions factory/faker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Meta:
from . import declarations


class Faker(declarations.ParameteredDeclaration):
class Faker(declarations.BaseDeclaration):
"""Wrapper for 'faker' values.
Args:
Expand All @@ -42,10 +42,10 @@ def __init__(self, provider, **kwargs):
locale=locale,
**kwargs)

def generate(self, params):
locale = params.pop('locale')
def evaluate(self, instance, step, extra):
locale = extra.pop('locale')
subfaker = self._get_faker(locale)
return subfaker.format(self.provider, **params)
return subfaker.format(self.provider, **extra)

_FAKER_REGISTRY = {}
_DEFAULT_LOCALE = faker.config.DEFAULT_LOCALE
Expand Down
2 changes: 1 addition & 1 deletion tests/test_faker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _setup_advanced_mock_faker(self, locale=None, **handlers):
def test_simple_biased(self):
self._setup_mock_faker(name="John Doe")
faker_field = factory.Faker('name')
self.assertEqual("John Doe", faker_field.generate({'locale': None}))
self.assertEqual("John Doe", faker_field.evaluate(None, None, {'locale': None}))

def test_full_factory(self):
class Profile:
Expand Down

6 comments on commit 824c6e0

@Jonamaita
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, good day:

I was use generate method to get value random in my tests. Do you have any tip for generate random values with Faker class in the new release?.

Thank you!!!.

Jonathan Maita.

@tlrh314
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, good day:

I was use generate method to get value random in my tests. Do you have any tip for generate random values with Faker class in the new release?.

Thank you!!!.

Jonathan Maita.

It seems generate was renamed to evaluate and requires two additional parameters, e.g.

-            Faker().generate(extra_kwargs={})
+            Faker().evaluate(None, None, extra={"locale": None})

@Jonamaita
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, good day:
I was use generate method to get value random in my tests. Do you have any tip for generate random values with Faker class in the new release?.
Thank you!!!.
Jonathan Maita.

It seems generate was renamed to evaluate and requires two additional parameters, e.g.

-            Faker().generate(extra_kwargs={})
+            Faker().evaluate(None, None, extra={"locale": None})

Thank you for the tip!

@rbarrois
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jonamaita @tlrh314 Note that both .generate() and .evaluate() are private methods.

The supported way to use faker in a factory is simply:

class UserFactory(factory.Factory):
    class Meta:
        model = User

    name = factory.Faker("name")

@Jonamaita
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jonamaita @tlrh314 Note that both .generate() and .evaluate() are private methods.

The supported way to use faker in a factory is simply:

class UserFactory(factory.Factory):
    class Meta:
        model = User

    name = factory.Faker("name")

@rbarrois Thank you.! Now I will use the Faker class to generate false values ​​where it is not necessary to use the model, e,g:

from faker import Faker

def password_generator():
    fake = Faker()
    return fake.password()

@Aiky30
Copy link

@Aiky30 Aiky30 commented on 824c6e0 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also found the same issue with generate, the changelog also doesn't mention this change.

FYI a private method should be _generate, _evaluate or else it is assumed public.

Please sign in to comment.