File: test_utils.py

package info (click to toggle)
faker 39.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,712 kB
  • sloc: python: 343,295; makefile: 183; sh: 20
file content (130 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
import unittest

from importlib import import_module
from pathlib import Path

import faker

from faker.config import META_PROVIDERS_MODULES, PROVIDERS
from faker.generator import random
from faker.typing import OrderedDictType
from faker.utils.checksums import calculate_luhn, luhn_checksum
from faker.utils.datasets import add_ordereddicts
from faker.utils.distribution import choices_distribution, choices_distribution_unique
from faker.utils.loading import find_available_locales, find_available_providers, get_path

TEST_DIR = Path(__file__).resolve().parent


class UtilsTestCase(unittest.TestCase):
    def test_choice_distribution(self):
        a = ("a", "b", "c", "d")
        p = (0.5, 0.2, 0.2, 0.1)

        sample = choices_distribution(a, p)[0]
        assert sample in a

        random_state = json.loads((TEST_DIR / "random_state.json").read_text())
        random_state[1] = tuple(random_state[1])

        random.setstate(random_state)
        samples = choices_distribution(a, p, length=100)
        a_pop = len([i for i in samples if i == "a"])
        b_pop = len([i for i in samples if i == "b"])
        c_pop = len([i for i in samples if i == "c"])
        d_pop = len([i for i in samples if i == "d"])

        boundaries = []
        tolerance = 5
        for probability in p:
            boundaries.append([100 * probability + tolerance, 100 * probability - tolerance])

        assert boundaries[0][0] > a_pop > boundaries[0][1]
        assert boundaries[1][0] > b_pop > boundaries[1][1]
        assert boundaries[2][0] > c_pop > boundaries[2][1]
        assert boundaries[3][0] > d_pop > boundaries[3][1]

    def test_choices_distribution_unique(self):
        a = ("a", "b", "c", "d")
        p = (0.25, 0.25, 0.25, 0.25)
        with self.assertRaises(AssertionError):
            choices_distribution_unique(a, p, length=5)

        samples = choices_distribution_unique(a, p, length=4)
        assert len(set(samples)) == len(samples)

    def test_get_path(self):
        result = get_path(faker)
        assert isinstance(result, str)

    def test_find_available_locales(self):
        result = find_available_locales(PROVIDERS)
        assert len(result) != 0

    def test_find_available_providers(self):
        modules = [import_module(path) for path in META_PROVIDERS_MODULES]
        providers = find_available_providers(modules)

        expected_providers = list(
            map(
                str,
                [
                    "faker.providers.address",
                    "faker.providers.automotive",
                    "faker.providers.bank",
                    "faker.providers.barcode",
                    "faker.providers.color",
                    "faker.providers.company",
                    "faker.providers.credit_card",
                    "faker.providers.currency",
                    "faker.providers.date_time",
                    "faker.providers.doi",
                    "faker.providers.emoji",
                    "faker.providers.file",
                    "faker.providers.geo",
                    "faker.providers.internet",
                    "faker.providers.isbn",
                    "faker.providers.job",
                    "faker.providers.lorem",
                    "faker.providers.misc",
                    "faker.providers.passport",
                    "faker.providers.person",
                    "faker.providers.phone_number",
                    "faker.providers.profile",
                    "faker.providers.python",
                    "faker.providers.sbn",
                    "faker.providers.ssn",
                    "faker.providers.user_agent",
                ],
            )
        )
        assert providers == expected_providers

    def test_luhn_checksum(self):
        """
        Tests if a valid checksum is generated
        Example from wiki: https://en.wikipedia.org/wiki/Luhn_algorithm
        """
        check_digit = calculate_luhn("7992739871")
        assert check_digit == 3

    def test_valid_luhn(self):
        """
        Tests if the number has a valid check digit
        Example from wiki https://en.wikipedia.org/wiki/Luhn_algorithm
        """
        assert luhn_checksum("79927398713") == 0

    def test_invalid_luhn(self):
        """
        Tests a number with an invalid check digit
        Example from wiki https://en.wikipedia.org/wiki/Luhn_algorithm
        """
        assert luhn_checksum("79927398714") != 0

    def test_add_ordereddicts(self):
        d1 = OrderedDictType([("a", 1), ("b", 2)])
        d2 = OrderedDictType([("c", 3), ("d", 4)])
        result = add_ordereddicts(d1, d2)
        assert result == OrderedDictType([("a", 1), ("b", 2), ("c", 3), ("d", 4)])