|
1
|
|
|
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils |
|
2
|
|
|
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/pocketutils |
|
3
|
|
|
# SPDX-License-Identifier: Apache-2.0 |
|
4
|
|
|
from dataclasses import dataclass |
|
5
|
|
|
from typing import Any, Self |
|
6
|
|
|
|
|
7
|
|
|
import numpy as np |
|
8
|
|
|
import pytest |
|
9
|
|
|
from pocketutils.core.exceptions import MultipleMatchesError, NoMatchesError |
|
10
|
|
|
from pocketutils.core.mocks import MockCallable, MockWritable, WritableCallable |
|
11
|
|
|
from pocketutils.tools.common_tools import CommonTools |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
@dataclass(frozen=True, slots=True) |
|
15
|
|
|
class Mammal: |
|
16
|
|
|
species: Any |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
outside_lambda_1 = lambda: True |
|
20
|
|
|
outside_lambda_2 = lambda x: x |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def non_lambda(): |
|
24
|
|
|
pass |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class TestCommon: |
|
28
|
|
|
def test_only(self: Self) -> None: |
|
29
|
|
|
only = CommonTools.only |
|
30
|
|
|
with pytest.raises(TypeError): |
|
31
|
|
|
# noinspection PyTypeChecker |
|
32
|
|
|
only(1) |
|
33
|
|
|
assert only("a") == "a" |
|
34
|
|
|
assert only(["a"]) == "a" |
|
35
|
|
|
assert only({"ab"}) == "ab" |
|
36
|
|
|
with pytest.raises(MultipleMatchesError): |
|
37
|
|
|
only(["a", "b"]) |
|
38
|
|
|
with pytest.raises(NoMatchesError): |
|
39
|
|
|
only([]) |
|
40
|
|
|
|
|
41
|
|
|
def test_look(self: Self) -> None: |
|
42
|
|
|
f = CommonTools.look |
|
43
|
|
|
with pytest.raises(TypeError): |
|
44
|
|
|
# noinspection PyTypeChecker |
|
45
|
|
|
f(1, 1) |
|
46
|
|
|
assert f(Mammal("cat"), "species") == "cat" |
|
47
|
|
|
assert f(Mammal("cat"), "owner") is None |
|
48
|
|
|
# assert f(Mammal(Mammal('cat')), 'species') == Mammal('cat') |
|
49
|
|
|
assert f(Mammal(Mammal("cat")), "species.species") == "cat" |
|
50
|
|
|
assert str(f(Mammal(Mammal("cat")), "species")) == "Mammal(species='cat')" |
|
51
|
|
|
assert f(Mammal(Mammal("cat")), lambda m: m.species.species) == "cat" |
|
52
|
|
|
|
|
53
|
|
|
def test_get_log_function(self: Self) -> None: |
|
54
|
|
|
from pocketutils.core import logger |
|
55
|
|
|
|
|
56
|
|
|
f = CommonTools.get_log_fn |
|
57
|
|
|
assert f("INFO") == logger.info |
|
58
|
|
|
assert f("WARNING") == logger.warning |
|
59
|
|
|
assert f(30) == logger.warning |
|
60
|
|
|
w = MockWritable() |
|
61
|
|
|
f(w)("testing") |
|
62
|
|
|
assert w.data == "write:testing" |
|
63
|
|
|
w = MockCallable() |
|
64
|
|
|
f(w)("testing") |
|
65
|
|
|
assert w.data == "call:testing" |
|
66
|
|
|
w = WritableCallable() |
|
67
|
|
|
f(w)("testing") |
|
68
|
|
|
assert w.data == "call:testing" |
|
69
|
|
|
|
|
70
|
|
|
def test_iterator_has_elements(self: Self): |
|
71
|
|
|
f = CommonTools.iterator_has_elements |
|
72
|
|
|
assert not f(iter([])) |
|
73
|
|
|
assert f(iter([1])) |
|
74
|
|
|
with pytest.raises(TypeError): |
|
75
|
|
|
# noinspection PyTypeChecker |
|
76
|
|
|
f(None) |
|
77
|
|
|
with pytest.raises(TypeError): |
|
78
|
|
|
# noinspection PyTypeChecker |
|
79
|
|
|
f([1]) |
|
80
|
|
|
|
|
81
|
|
|
def test_is_null(self: Self): |
|
82
|
|
|
is_null = CommonTools.is_null |
|
83
|
|
|
assert not is_null("a") |
|
84
|
|
|
assert not is_null([]) |
|
85
|
|
|
assert not is_null("None") |
|
86
|
|
|
assert not is_null([]) |
|
87
|
|
|
assert is_null(None) |
|
88
|
|
|
assert not is_null("") |
|
89
|
|
|
assert not is_null(0.0) |
|
90
|
|
|
assert not is_null(np.inf) |
|
91
|
|
|
assert is_null(np.nan) |
|
92
|
|
|
|
|
93
|
|
|
def test_is_empty(self: Self): |
|
94
|
|
|
f = CommonTools.is_empty |
|
95
|
|
|
assert not f("a") |
|
96
|
|
|
assert f([]) |
|
97
|
|
|
assert not f("None") |
|
98
|
|
|
assert f(None) |
|
99
|
|
|
assert f("") |
|
100
|
|
|
assert f([]) |
|
101
|
|
|
assert f({}) |
|
102
|
|
|
assert f(()) |
|
103
|
|
|
assert not f((5,)) |
|
104
|
|
|
assert not f(0.0) |
|
105
|
|
|
assert not f(np.inf) |
|
106
|
|
|
assert f(np.nan) |
|
107
|
|
|
|
|
108
|
|
|
def test_is_probable_null(self: Self): |
|
109
|
|
|
f = CommonTools.is_probable_null |
|
110
|
|
|
assert f(None) |
|
111
|
|
|
assert not f(0) |
|
112
|
|
|
assert f("nan") |
|
113
|
|
|
assert f("none") |
|
114
|
|
|
assert f("NoNe") |
|
115
|
|
|
assert f(np.NaN) |
|
116
|
|
|
assert not f(np.Inf) |
|
117
|
|
|
|
|
118
|
|
|
def test_unique(self: Self): |
|
119
|
|
|
f = CommonTools.unique |
|
120
|
|
|
assert f([1, 1, 2, 1, 3, 2]) == [1, 2, 3] |
|
121
|
|
|
assert f([]) == [] |
|
122
|
|
|
with pytest.raises(TypeError): |
|
123
|
|
|
# noinspection PyTypeChecker |
|
124
|
|
|
f(None) |
|
125
|
|
|
|
|
126
|
|
|
def test_first(self: Self): |
|
127
|
|
|
f = CommonTools.first |
|
128
|
|
|
assert f([2, 1]) == 2 |
|
129
|
|
|
assert f([Mammal("cat"), Mammal("dog"), Mammal("cat")]) == Mammal("cat") |
|
130
|
|
|
assert f("21") == "2" |
|
131
|
|
|
assert f([]) is None |
|
132
|
|
|
with pytest.raises(TypeError): |
|
133
|
|
|
# noinspection PyTypeChecker |
|
134
|
|
|
f(None) |
|
135
|
|
|
|
|
136
|
|
|
def test_multidict(self: Self): |
|
137
|
|
|
f = CommonTools.multidict |
|
138
|
|
|
expected = "{'cat': [Mammal(species='cat')], 'dog': [Mammal(species='dog')]}" |
|
139
|
|
|
assert str(dict(f([Mammal("cat"), Mammal("dog")], "species"))) == expected |
|
140
|
|
|
|
|
141
|
|
|
def test_longest(self: Self) -> None: |
|
142
|
|
|
f = CommonTools.longest |
|
143
|
|
|
assert f(["1", "abc", "xyz", "2"]) == "abc" |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
if __name__ == "__main__": |
|
147
|
|
|
pytest.main() |
|
148
|
|
|
|