Skip to content

Commit

Permalink
Added support for setting the locale against the Faker maker.
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyjb committed Aug 4, 2016
1 parent 164820f commit 984de8d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
27 changes: 17 additions & 10 deletions mongoframes/factory/makers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/factory/makers/test_makers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from mongoframes.factory import blueprints
from mongoframes.factory import makers
from mongoframes.factory import presets
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 984de8d

Please sign in to comment.