From 984de8db1f68388d5ff38467c1ca0b542aacf134 Mon Sep 17 00:00:00 2001 From: Anthony Blackshaw Date: Thu, 4 Aug 2016 12:51:39 +0100 Subject: [PATCH] Added support for setting the locale against the Faker maker. --- mongoframes/factory/makers/__init__.py | 27 ++++++++++++++++---------- setup.py | 2 +- tests/factory/makers/test_makers.py | 10 ++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/mongoframes/factory/makers/__init__.py b/mongoframes/factory/makers/__init__.py index 7b4aa65..4b1c8fc 100644 --- a/mongoframes/factory/makers/__init__.py +++ b/mongoframes/factory/makers/__init__.py @@ -36,13 +36,6 @@ def _assemble(self): def _finish(self, value): return value - @staticmethod - def get_fake(): - """Return a shared faker factory used to generate fake data""" - if not hasattr(Maker, '_fake'): - Maker._fake = faker.Factory.create() - return Maker._fake - class DictOf(Maker): """ @@ -82,7 +75,9 @@ class Faker(Maker): http://fake-factory.readthedocs.io/) """ - def __init__(self, provider, assembler=True, **kwargs): + default_locale = 'en_US' + + def __init__(self, provider, assembler=True, locale=None, **kwargs): # The provider that will be used to generate the value self._provider = provider @@ -91,18 +86,30 @@ def __init__(self, provider, assembler=True, **kwargs): # or _finish (False). self._assembler = assembler + # The locale that will be used by the faker factory + self._locale = locale or self.default_locale + # The keyword arguments for the provider self._kwargs = kwargs def _assemble(self): if not self._assembler: return None - return getattr(self.get_fake(), self._provider)(**self._kwargs) + provider = getattr(self.get_fake(self._locale), self._provider) + return provider(**self._kwargs) def _finish(self, value): if self._assembler: return value - return getattr(self.get_fake(), self._provider)(**self._kwargs) + provider = getattr(self.get_fake(self._locale), self._provider) + return provider(**self._kwargs) + + @staticmethod + def get_fake(locale): + """Return a shared faker factory used to generate fake data""" + if not hasattr(Maker, '_fake_' + locale): + Faker._fake = faker.Factory.create(locale) + return Faker._fake class Lambda(Maker): diff --git a/setup.py b/setup.py index 5df0451..9b5f8f0 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='1.1.0', + version='1.1.1', description='A fast unobtrusive MongoDB ODM for Python', long_description=long_description, diff --git a/tests/factory/makers/test_makers.py b/tests/factory/makers/test_makers.py index a63c33b..6f0c56c 100644 --- a/tests/factory/makers/test_makers.py +++ b/tests/factory/makers/test_makers.py @@ -1,3 +1,5 @@ +import re + from mongoframes.factory import blueprints from mongoframes.factory import makers from mongoframes.factory import presets @@ -62,6 +64,14 @@ def test_faker(): finished = maker._finish(assembled) assert finished in am_pm + # Configured with a different locale + maker = makers.Faker('postcode', locale='en_GB') + + # Check the assembled result resembles a UK postcode + assembled = maker._assemble() + + assert re.match('(\w+?\d{1,2}).*', assembled) and len(assembled) <= 8 + def test_lambda(): """ `Lambda` makers should return the output of the function you initialize them