Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default faker locale in factory_boy

How can I set the default locale in Python's factory_boy for all of my Factories?

In docs says that one should set it with factory.Faker.override_default_locale but that does nothing to my fakers...

import factory
from app.models import Example
from custom_fakers import CustomFakers

# I use custom fakers, this indeed are added
factory.Faker.add_provider(CustomFakers)
# But not default locales
factory.Faker.override_default_locale('es_ES')

class ExampleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Example

    name = factory.Faker('first_name')


>>> from example import ExampleFactory
>>> e1 = ExampleFactory()
>>> e1.name
>>> u'Chad'
like image 911
Felix Avatar asked Aug 19 '17 16:08

Felix


3 Answers

The Faker.override_default_locale() is a context manager, although it's not very clear from the docs.

As such, to change the default locale for a part of a test:

with factory.Faker.override_default_locale('es_ES'):
    ExampleFactory()

For the whole test:

@factory.Faker.override_default_locale('es_ES')
def test_foo(self):
    user = ExampleFactory()

For all the tests (Django):

# settings.py
TEST_RUNNER = 'myproject.testing.MyTestRunner'

# myproject/testing.py
import factory
from django.conf import settings
from django.util import translation
import django.test.runner

class MyTestRunner(django.test.runner.DiscoverRunner):
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        with factory.Faker.override_default_locale(translation.to_locale(settings.LANGUAGE_CODE)):
            return super().run_tests(test_labels, extra_tests=extra_tests, **kwargs)

More on it here.

like image 81
Xelnor Avatar answered Oct 16 '22 17:10

Xelnor


UPD As I said, this solution is suboptimal:

  • factory.Faker._DEFAULT_LOCALE is a private field
  • fake() and faker() use the private interface
  • fake() doesn't work since factory-boy==3.1.0
  • if I were to use faker, I'd use it directly, not via factory-boy

You should generally prefer the other answer. Leaving this one for posterity.


Not a good solution, but for now it's as good as it gets. You can change the variable that holds the value:

import factory
factory.Faker._DEFAULT_LOCALE = 'xx_XX'

Moreover, you can create a file like this (app/faker.py):

import factory
from faker.providers import BaseProvider

factory.Faker._DEFAULT_LOCALE = 'xx_XX'

def fake(name):
    return factory.Faker(name).generate({})

def faker():
    return factory.Faker._get_faker()

class MyProvider(BaseProvider):
    def category_name(self):
        return self.random_element(category_names)
    ...
factory.Faker.add_provider(MyProvider)

category_names = [...]

Then, once you import the file, the locale changes. Also, you get your providers and an easy way to use factory_boy's faker outside of the factories:

from app.faker import fake
print(fake('random_int'))
print(faker().random_int())
like image 7
x-yuri Avatar answered Oct 16 '22 17:10

x-yuri


I'm having same issue as yours. For a temporary solution try passing locale in factory.Faker.

For example:

name = factory.Faker('first_name', locale='es_ES')
like image 3
Alireza Amouzadeh Avatar answered Oct 16 '22 16:10

Alireza Amouzadeh