Esempio n. 1
0
    def seed(cls, seed=None):
        """
        Seeds the shared `random.Random` object across all factories

        :param seed: seed value
        """
        Generator.seed(seed)
Esempio n. 2
0
    def test_ipv4_distribution_selection(self):
        from faker.generator import Generator, random
        from faker.utils.distribution import choices_distribution
        provider = InternetProvider(Generator())

        subnets = [ip_network('10.0.0.0/8'), ip_network('11.0.0.0/8')]
        valid_weights = [1, 1]
        list_of_invalid_weights = [
            [1, 2, 3],  # List size does not match subnet list size
            ['a', 'b'],  # List size matches, but elements are invalid
            11,  # Not a list or valid iterable
        ]

        with patch('faker.providers.internet.choices_distribution',
                   wraps=choices_distribution) as mock_choices_fn:
            with patch('faker.generator.random.choice',
                       wraps=random.choice) as mock_random_choice:
                # If weights argument is valid, only `choices_distribution` should be called
                provider._random_ipv4_address_from_subnets(
                    subnets, valid_weights)
                assert mock_choices_fn.call_count == 1
                assert mock_random_choice.call_count == 0

                # If weights argument is invalid, calls to `choices_distribution` will fail
                # and calls to `random.choice` will be made as failover behavior
                for invalid_weights in list_of_invalid_weights:
                    # Reset mock objects for each iteration
                    mock_random_choice.reset_mock()
                    mock_choices_fn.reset_mock()

                    provider._random_ipv4_address_from_subnets(
                        subnets, invalid_weights)
                    assert mock_choices_fn.call_count == 1
                    assert mock_random_choice.call_count == 1
Esempio n. 3
0
    def create(
            cls,
            locale=None,
            providers=None,
            generator=None,
            includes=None,
            **config):
        if includes is None:
            includes = []

        # fix locale to package name
        locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
        locale = pylocale.normalize(locale).split('.')[0]
        if locale not in AVAILABLE_LOCALES:
            msg = 'Invalid configuration for faker locale `{}`'.format(locale)
            raise AttributeError(msg)

        config['locale'] = locale
        providers = providers or PROVIDERS

        providers += includes

        faker = generator or Generator(**config)

        for prov_name in providers:
            if prov_name == 'faker.providers':
                continue

            prov_cls, lang_found = cls._get_provider_class(prov_name, locale)
            provider = prov_cls(faker)
            provider.__provider__ = prov_name
            provider.__lang__ = lang_found
            faker.add_provider(provider)

        return faker
    def create(
            cls,
            locale=None,
            providers=None,
            generator=None,
            includes=None,
            # Should we use weightings (more realistic) or weight every element equally (faster)?
            # By default, use weightings for backwards compatibility & realism
            use_weighting=True,
            **config):
        if includes is None:
            includes = []

        # fix locale to package name
        locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
        locale = pylocale.normalize(locale).split('.')[0]
        if locale not in AVAILABLE_LOCALES:
            msg = f'Invalid configuration for faker locale `{locale}`'
            raise AttributeError(msg)

        config['locale'] = locale
        config['use_weighting'] = use_weighting
        providers = providers or PROVIDERS

        providers += includes

        faker = generator or Generator(**config)

        for prov_name in providers:
            if prov_name == 'faker.providers':
                continue

            prov_cls, lang_found = cls._get_provider_class(prov_name, locale)
            provider = prov_cls(faker)
            provider.__use_weighting__ = use_weighting
            provider.__provider__ = prov_name
            provider.__lang__ = lang_found
            faker.add_provider(provider)

        return faker
 def test_generate_fake_data(self):
     """
     Tests if a value generated is the correct type or the override value
     """
     assert self.field.generate_fake_data(self.faker) in MilMoveProvider(Generator()).safe_data["addresses"]
     assert self.field.generate_fake_data(self.faker, overrides={"streetAddress": "test address"}) == "test address"
Esempio n. 6
0
from faker.providers.lorem.zh_CN import Provider as zh_CN_lorem
from faker.providers.person.zh_CN import Provider as zh_CN_person
from faker.generator import Generator
from faker.config import *

faker = Generator()
faker.add_provider(zh_CN_person)
faker.add_provider(zh_CN_lorem)