summaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 2ae04fcf4c5daf5d208e7ed0a0b1d5fde05ca875 (plain)
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
"""
Fixtures for pytest.
"""

import locale
from typing import Iterator

import hypothesis
import pytest
from natsort.compat.locale import dumb_sort


# This disables the "too slow" hypothesis heath check globally.
# For some reason it thinks that the text/binary generation is too
# slow then causes the tests to fail.
hypothesis.settings.register_profile(
    "slow-tests", suppress_health_check=[hypothesis.HealthCheck.too_slow]
)


def load_locale(x: str) -> None:
    """Convenience to load a locale."""
    locale.setlocale(locale.LC_ALL, str("{}.UTF-8".format(x)))


@pytest.fixture()
def with_locale_en_us() -> Iterator[None]:
    """Convenience to load the en_US locale - reset when complete."""
    orig = locale.getlocale()
    load_locale("en_US")
    yield
    locale.setlocale(locale.LC_ALL, orig)


@pytest.fixture()
def with_locale_de_de() -> Iterator[None]:
    """
    Convenience to load the de_DE locale - reset when complete - skip if missing.
    """
    orig = locale.getlocale()
    try:
        load_locale("de_DE")
    except locale.Error:
        pytest.skip("requires de_DE locale to be installed")
    else:
        yield
    finally:
        locale.setlocale(locale.LC_ALL, orig)


@pytest.fixture()
def with_locale_cs_cz() -> Iterator[None]:
    """
    Convenience to load the cs_CZ locale - reset when complete - skip if missing.
    """
    orig = locale.getlocale()
    try:
        load_locale("cs_CZ")
        if dumb_sort():
            pytest.skip("requires a functioning locale library to run")
    except locale.Error:
        pytest.skip("requires cs_CZ locale to be installed")
    else:
        yield
    finally:
        locale.setlocale(locale.LC_ALL, orig)