File: test_optional.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 (44 lines) | stat: -rw-r--r-- 1,257 bytes parent folder | download | duplicates (2)
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
import pytest

from faker import Faker


class TestOptionalClass:
    def test_optional(self) -> None:
        fake = Faker()

        assert {fake.optional.boolean() for _ in range(10)} == {True, False, None}

    def test_optional_probability(self) -> None:
        """The probability is configurable."""
        fake = Faker()

        fake.optional.name(prob=0.1)

    def test_optional_arguments(self) -> None:
        """Other arguments are passed through to the function."""
        fake = Faker()

        fake.optional.pyint(1, 2, prob=0.4)

    def test_optional_valid_range(self) -> None:
        """Only probabilities in the range (0, 1]."""
        fake = Faker()

        with pytest.raises(ValueError, match=""):
            fake.optional.name(prob=0)

        with pytest.raises(ValueError, match=""):
            fake.optional.name(prob=1.1)

        with pytest.raises(ValueError, match=""):
            fake.optional.name(prob=-3)

    def test_functions_only(self):
        """Accessing non-functions through the `.optional` attribute
        will throw a TypeError."""

        fake = Faker()

        with pytest.raises(TypeError, match="Accessing non-functions through .optional is not supported."):
            fake.optional.locales