Example #1
0
    def test_random_sample_unique(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        # Too many items requested
        with self.assertRaises(ValueError):
            provider.random_sample('abcde', 6)

        # Same length
        sample = provider.random_sample('abcd', 4)
        assert sorted(sample) == list('abcd')

        sample = provider.random_sample('abcde', 5)
        assert sorted(sample) == list('abcde')

        # Length = 3
        sample = provider.random_sample('abcde', 3)
        assert len(sample) == 3
        assert set(sample).issubset(set('abcde'))

        # Length = 1
        sample = provider.random_sample('abcde', 1)
        assert len(sample) == 1
        assert set(sample).issubset(set('abcde'))

        # Length = 0
        sample = provider.random_sample('abcde', 0)
        assert sample == []
Example #2
0
    def test_random_sample_unique(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        # Too many items requested
        with self.assertRaises(ValueError):
            provider.random_sample('abcde', 6)

        # Same length
        sample = provider.random_sample('abcd', 4)
        self.assertEqual(sorted(sample), list('abcd'))

        sample = provider.random_sample('abcde', 5)
        self.assertEqual(sorted(sample), list('abcde'))

        # Length = 3
        sample = provider.random_sample('abcde', 3)
        self.assertEqual(len(sample), 3)
        self.assertTrue(set(sample).issubset(set('abcde')))

        # Length = 1
        sample = provider.random_sample('abcde', 1)
        self.assertEqual(len(sample), 1)
        self.assertTrue(set(sample).issubset(set('abcde')))

        # Length = 0
        sample = provider.random_sample('abcde', 0)
        self.assertEqual(sample, [])
Example #3
0
    def test_random_number(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        number = provider.random_number(10, True)
        assert len(str(number)) == 10

        # Digits parameter < 0
        with self.assertRaises(ValueError):
            number = provider.random_number(-1, True)

        # Digits parameter < 1 with fix_len=True
        with self.assertRaises(ValueError):
            number = provider.random_number(0, True)
Example #4
0
    def add_provider_alias(self, provider_name: str, new_name: str) -> None:
        """
        Adds a copy of a provider, with a different name
        :param provider_name: Name of original provider
        :param new_name: New name
        :example:
        >>>add_provider_alias(provider_name="name", new_name="person")
        >>>self.faker.person()
        """
        original = getattr(self.faker, provider_name)

        new_provider = BaseProvider(self.faker)
        setattr(new_provider, new_name, original)
        self.faker.add_provider(new_provider)
Example #5
0
    def test_random_element(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        choices = ('a', 'b', 'c', 'd')
        pick = provider.random_element(choices)
        assert pick in choices

        choices = {'a': 5, 'b': 2, 'c': 2, 'd': 1}
        pick = provider.random_element(choices)
        assert pick in choices

        choices = {'a': 0.5, 'b': 0.2, 'c': 0.2, 'd': 0.1}
        pick = provider.random_element(choices)
        assert pick in choices
Example #6
0
    def test_random_element(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(None)

        choices = ('a', 'b', 'c', 'd')
        pick = provider.random_element(choices)
        self.assertTrue(pick in choices)

        choices = {'a': 5, 'b': 2, 'c': 2, 'd': 1}
        pick = provider.random_element(choices)
        self.assertTrue(pick in choices)

        choices = {'a': 0.5, 'b': 0.2, 'c': 0.2, 'd': 0.1}
        pick = provider.random_element(choices)
        self.assertTrue(pick in choices)
Example #7
0
    def test_random_element(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        choices = ('a', 'b', 'c', 'd')
        pick = provider.random_element(choices)
        assert pick in choices

        # dicts not allowed because they introduce dependency on PYTHONHASHSEED
        with self.assertRaises(ValueError):
            provider.random_element({})

        choices = OrderedDict([('a', 5), ('b', 2), ('c', 2), ('d', 1)])
        pick = provider.random_element(choices)
        self.assertTrue(pick in choices)

        choices = OrderedDict([('a', 0.5), ('b', 0.2), ('c', 0.2), ('d', 0.1)])
        pick = provider.random_element(choices)
        self.assertTrue(pick in choices)
Example #8
0
    def test_random_sample_unique(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(None)

        sample = provider.random_sample_unique('abcde', 3)
        self.assertEqual(len(sample), 3)
        self.assertTrue(sample.issubset(set('abcde')))

        # Same length
        sample = provider.random_sample_unique('abcde', 5)
        self.assertEqual(sample, set('abcde'))

        # Length = 1
        sample = provider.random_sample_unique('abcde', 1)
        self.assertEqual(len(sample), 1)
        self.assertTrue(sample.issubset(set('abcde')))

        # Length = 0
        sample = provider.random_sample_unique('abcde', 0)
        self.assertEqual(sample, set())

        # Length = 0
        self.assertRaises(ValueError, provider.random_sample_unique, 'abcde',
                          6)
Example #9
0
    def test_random_number(self):
        from faker.providers import BaseProvider
        provider = BaseProvider(self.generator)

        number = provider.random_number(10, True)
        assert len(str(number)) == 10
Example #10
0
 def setUp(self):
     generator = Generator()
     self.provider = BaseProvider(generator)
Example #11
0
def GetFakerDataFromList(arr):
    '''arr = ('a', 'b', 'c', 'd') , choose one from list
    '''
    provider = BaseProvider(None)
    data = pick = provider.random_element(arr)
    return
Example #12
0
 def setUp(self):
     self.factory = Faker('en_US')
     Faker.seed(0)
     self.provider = BaseProvider(generator=self.factory)
Example #13
0
def write_docs(*args, **kwargs):
    from faker import Faker, documentor
    from faker.config import DEFAULT_LOCALE, AVAILABLE_LOCALES
    from faker.providers import BaseProvider

    fake = Faker(locale=DEFAULT_LOCALE)
    doc = documentor.Documentor(fake)

    # Write docs for fakers.providers.BaseProvider
    base_provider = BaseProvider(fake)
    fname = os.path.join(DOCS_ROOT, 'providers', 'BaseProvider.rst')
    with open(fname, 'wb') as fh:
        write_base_provider(fh, doc, base_provider)

    # Write docs for default locale providers
    base_provider_formatters = list(dir(BaseProvider))
    formatters = doc.get_formatters(with_args=True,
                                    with_defaults=True,
                                    excludes=base_provider_formatters)
    for provider, fakers in formatters:
        provider_name = doc.get_provider_name(provider)
        fname = os.path.join(DOCS_ROOT, 'providers', '%s.rst' % provider_name)
        with open(fname, 'wb') as fh:
            write(fh, ':github_url: hide\n\n')
            write_provider(fh, doc, provider, fakers)

    # Write providers index page
    with open(os.path.join(DOCS_ROOT, 'providers.rst'), 'wb') as fh:
        write(fh, ':github_url: hide\n\n')
        write(fh, 'Providers\n')
        write(fh, '=========\n')
        write(fh, '.. toctree::\n')
        write(fh, '   :maxdepth: 2\n\n')
        write(fh, '   providers/BaseProvider\n')
        [
            write(fh, '   providers/%s\n' % doc.get_provider_name(provider))
            for provider, fakers in formatters
        ]

    # Write docs for locale-specific providers
    AVAILABLE_LOCALES = sorted(AVAILABLE_LOCALES)
    for lang in AVAILABLE_LOCALES:
        fname = os.path.join(DOCS_ROOT, 'locales', '%s.rst' % lang)
        with open(fname, 'wb') as fh:
            write(fh, ':github_url: hide\n\n')
            title = 'Language {}\n'.format(lang)
            write(fh, title)
            write(fh, '=' * len(title))
            write(fh, '\n')
            fake = Faker(locale=lang)
            d = documentor.Documentor(fake)

            for p, fs in d.get_formatters(with_args=True,
                                          with_defaults=True,
                                          locale=lang,
                                          excludes=base_provider_formatters):
                write_provider(fh, d, p, fs)

    # Write locales index page
    with open(os.path.join(DOCS_ROOT, 'locales.rst'), 'wb') as fh:
        write(fh, ':github_url: hide\n\n')
        write(fh, 'Locales\n')
        write(fh, '=======\n')
        write(fh, '.. toctree::\n')
        write(fh, '   :maxdepth: 2\n\n')
        [write(fh, '   locales/%s\n' % lang) for lang in AVAILABLE_LOCALES]