Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def factory(locale):
if locale not in cached_instances:
cached_instances[locale] = Field(locale)
return cached_instances[locale]
@pytest.fixture
def field():
return Field('en')
def test_field(locale):
filed = Field(locale)
result = filed('full_name')
assert result
assert isinstance(result, str)
with pytest.raises(UnsupportedField):
filed('unsupported_field')
with pytest.raises(UndefinedField):
filed()
def test_field_with_custom_providers():
field = Field(providers=[USASpecProvider])
assert field('ssn')
assert field('usa_provider.ssn')
def _get_cached_instance(
cls,
locale: Optional[str] = None,
providers: Optional[_Providers] = None,
) -> Field:
if locale is None:
locale = cls._default_locale
key = (locale, providers)
if key not in cls._cached_instances:
cls._cached_instances[key] = Field(locale, providers=providers)
return cls._cached_instances[key]
from mimesis.providers.base import BaseProvider
from mimesis.schema import Field, Generic
_CacheKey = Tuple[str, Any]
_Providers = Iterable[Type[BaseProvider]]
class MimesisField(declarations.BaseDeclaration):
"""
Mimesis integration with FactoryBoy starts here.
This class provides common interface for FactoryBoy,
but inside it has Mimesis generators.
"""
_cached_instances: ClassVar[Dict[_CacheKey, Field]] = {}
_default_locale: ClassVar[str] = locales.DEFAULT_LOCALE
def __init__(
self, field: str, locale: Optional[str] = None, **kwargs,
) -> None:
"""
Creates a field instance.
The created field is lazy. It also receives build time parameters.
These parameters are not applied yet.
Args:
field: name to be passed to ``Field`` from ``Mimesis``.
locale: locale to use. This parameter has the highest priority.
kwargs: optional parameters that would be passed to ``Field``.