Passed
Push — main ( ed7d21...87238c )
by Douglas
01:43
created

TestCommonTools.test_only()   B

Complexity

Conditions 6

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 16
rs 8.6666
c 0
b 0
f 0
cc 6
nop 1
1
from dataclasses import dataclass
2
from typing import Any, Self
3
4
import numpy as np
5
import pytest
6
from pocketutils.core.exceptions import MultipleMatchesError
7
from pocketutils.core.mocks import MockCallable, MockWritable, WritableCallable
8
from pocketutils.tools.common_tools import CommonTools
9
10
11
@dataclass(frozen=True, slots=True)
12
class Mammal:
13
    species: Any
14
15
16
def outside_lambda(x):
17
    return x
18
19
20
def non_lambda():
21
    pass
22
23
24
class TestCommonTools:
25
    def test_is_lambda(self: Self) -> None:
26
        f = CommonTools.is_lambda
27
        assert f(lambda: None)
28
        assert f(lambda x: None)
29
        assert f(lambda x, y: None)
30
        assert f(outside_lambda)
31
        assert not f(None)
32
        assert not f(non_lambda)
33
34
        def yes():
35
            return None
36
37
        assert not f(yes)
38
39
        class X:
40
            pass
41
42
        assert not f(X())
43
        assert not f(X)
44
        assert not f(1)
45
46
    def test_only(self: Self) -> None:
47
        only = CommonTools.only
48
        with pytest.raises(TypeError):
49
            # noinspection PyTypeChecker
50
            only(1)
51
        assert only(["a"]) == "a"
52
        assert only("a") == "a"
53
        assert only({"ab"}) == "ab"
54
        with pytest.raises(MultipleMatchesError):
55
            only(["a", "b"])
56
        with pytest.raises(MultipleMatchesError):
57
            only("ab")
58
        with pytest.raises(LookupError):
59
            only([])
60
        with pytest.raises(LookupError):
61
            only("")
62
63
    def test_to_true_iterable(self: Self) -> None:
64
        f = CommonTools.to_true_iterable
65
        assert f(1) == [1]
66
        assert f("abc") == ["abc"]
67
        assert f(bytes(5)) == [bytes(5)]
68
        assert f([1, 2]) == [1, 2]
69
        assert f(list(np.array([1, 2]))) == list(np.array([1, 2]))
70
71
    def test_look(self: Self) -> None:
72
        f = CommonTools.look
73
        with pytest.raises(TypeError):
74
            # noinspection PyTypeChecker
75
            f(1, 1)
76
        assert f(Mammal("cat"), "species") == "cat"
77
        assert f(Mammal("cat"), "owner") is None
78
        # assert f(Mammal(Mammal('cat')), 'species') == Mammal('cat')
79
        assert f(Mammal(Mammal("cat")), "species.species") == "cat"
80
        assert str(f(Mammal(Mammal("cat")), "species")) == "Mammal(species='cat')"
81
        assert f(Mammal(Mammal("cat")), lambda m: m.species.species) == "cat"
82
83
    def test_get_log_function(self: Self) -> None:
84
        from pocketutils.core import logger
85
86
        f = CommonTools.get_log_function
87
        assert f("INFO") == logger.info
88
        assert f("WARNING") == logger.warning
89
        assert f(30) == logger.warning
90
        w = MockWritable()
91
        f(w)("testing")
92
        assert w.data == "write:testing"
93
        w = MockCallable()
94
        f(w)("testing")
95
        assert w.data == "call:testing"
96
        w = WritableCallable()
97
        f(w)("testing")
98
        assert w.data == "call:testing"
99
100
101
if __name__ == "__main__":
102
    pytest.main()
103