File: test_defaults.py

package info (click to toggle)
loguru 0.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,556 kB
  • sloc: python: 13,164; javascript: 49; makefile: 14
file content (66 lines) | stat: -rw-r--r-- 1,983 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytest

from loguru._defaults import env


@pytest.mark.parametrize("value", ["test", ""])
def test_string(value, monkeypatch):
    with monkeypatch.context() as context:
        key = "VALID_STRING"
        context.setenv(key, value)
        assert env(key, str) == value


@pytest.mark.parametrize("value", ["y", "1", "TRUE"])
def test_bool_positive(value, monkeypatch):
    with monkeypatch.context() as context:
        key = "VALID_BOOL_POS"
        context.setenv(key, value)
        assert env(key, bool) is True


@pytest.mark.parametrize("value", ["NO", "0", "false"])
def test_bool_negative(value, monkeypatch):
    with monkeypatch.context() as context:
        key = "VALID_BOOL_NEG"
        context.setenv(key, value)
        assert env(key, bool) is False


def test_int(monkeypatch):
    with monkeypatch.context() as context:
        key = "VALID_INT"
        context.setenv(key, "42")
        assert env(key, int) == 42


@pytest.mark.parametrize("value", ["", "a"])
def test_invalid_int(value, monkeypatch):
    with monkeypatch.context() as context:
        key = "INVALID_INT"
        context.setenv(key, value)
        with pytest.raises(
            ValueError,
            match=r"^Invalid environment variable 'INVALID_INT' \(expected an integer\): '[^']*'$",
        ):
            env(key, int)


@pytest.mark.parametrize("value", ["", "a"])
def test_invalid_bool(value, monkeypatch):
    with monkeypatch.context() as context:
        key = "INVALID_BOOL"
        context.setenv(key, value)
        with pytest.raises(
            ValueError,
            match=r"^Invalid environment variable 'INVALID_BOOL' \(expected a boolean\): '[^']*'$",
        ):
            env(key, bool)


def test_invalid_type(monkeypatch):
    with monkeypatch.context() as context:
        key = "INVALID_TYPE"
        context.setenv(key, "42.0")
        with pytest.raises(ValueError, match="^The requested type '[^']+' is not supported"):
            env(key, float)