Skip to content

Commit a75eb3d

Browse files
committed
🧪 test: decorator & utils
1 parent d5a4ca5 commit a75eb3d

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

tests/test_decorator.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from usepy import (
2+
useCachedProperty, # noqa
3+
useCatchError,
4+
useListify,
5+
useRunInThread,
6+
useSingleton,
7+
useTimeIt,
8+
)
9+
10+
11+
def test_cached_property():
12+
class Foo(object):
13+
@useCachedProperty
14+
def bar(self):
15+
return 1
16+
17+
foo = Foo()
18+
assert foo.bar == 1
19+
assert foo.bar == 1
20+
21+
22+
def test_catch_error():
23+
@useCatchError()
24+
def foo():
25+
raise Exception('foo')
26+
27+
@useCatchError(return_val=1)
28+
def foo2():
29+
raise Exception('foo2')
30+
31+
assert foo() is None
32+
assert foo2() == 1
33+
34+
35+
def test_listify():
36+
@useListify()
37+
def foo():
38+
yield 1
39+
40+
@useListify()
41+
def foo2():
42+
yield 1
43+
yield 2
44+
45+
@useListify(dict)
46+
def foo3():
47+
yield 'a', 1
48+
yield 'b', 2
49+
50+
assert foo() == [1]
51+
assert foo2() == [1, 2]
52+
assert foo3() == {'a': 1, 'b': 2}
53+
54+
55+
def test_run_in_thread():
56+
@useRunInThread
57+
def foo():
58+
return 1
59+
60+
assert foo() is None
61+
62+
63+
def test_singleton():
64+
@useSingleton
65+
class Foo(object):
66+
pass
67+
68+
foo = Foo()
69+
foo2 = Foo()
70+
assert foo is foo2
71+
72+
73+
def test_timeit():
74+
@useTimeIt
75+
def foo():
76+
return 1
77+
78+
assert foo() == 1

tests/test_utils/test_is.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from usepy import useIs
2+
3+
4+
def test_is_token():
5+
assert useIs.is_token("token")
6+
assert not useIs.is_token("to")
7+
assert useIs.is_token("cd9bb741c5553b2243c6d39fb5dd3c34")
8+
9+
10+
def test_is_regexp():
11+
import re
12+
assert not useIs.is_regexp("regex")
13+
assert not useIs.is_regexp(".*?")
14+
assert useIs.is_regexp(re.compile(".*?"))
15+
16+
17+
def test_is_string():
18+
assert useIs.is_string("string")
19+
assert useIs.is_string(b"bytes")
20+
assert not useIs.is_string(123)

tests/test_utils/test_to.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from usepy import useTo
2+
3+
4+
def test_to_dict():
5+
assert useTo.cookie_to_dict(
6+
"code=1000; name=miclon; UID=359FEA9A6F1C7E97BFE909A1A700F5DE"
7+
) == {
8+
"code": "1000",
9+
"name": "miclon",
10+
"UID": "359FEA9A6F1C7E97BFE909A1A700F5DE",
11+
}
12+
13+
assert useTo.headers_to_dict(
14+
"""
15+
pragma: no-cache
16+
referer: https://github.com/
17+
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0
18+
""") == {
19+
"pragma": "no-cache",
20+
"referer": "https://github.com/",
21+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0",
22+
}
23+
assert useTo.data_to_dict("code=1000&name=miclon&UID=359FEA9A6F1C7E97BFE909A1A700F5DE") == {
24+
"code": "1000",
25+
"name": "miclon",
26+
"UID": "359FEA9A6F1C7E97BFE909A1A700F5DE",
27+
}
28+
29+
30+
def test_to_string():
31+
assert useTo.to_string("test") == "test"
32+
assert useTo.to_string(b"test") == "test"
33+
assert useTo.to_string(1000) == "1000"
34+
assert useTo.to_string(1000.0) == "1000.0"
35+
assert useTo.to_string(True) == "True"
36+
assert useTo.to_string(None) == "None"
37+
assert useTo.to_string([1, 2, 3]) == "[1, 2, 3]"
38+
assert useTo.to_string({"code": 1000}) == "{'code': 1000}"
File renamed without changes.

0 commit comments

Comments
 (0)