Skip to content

Commit 94c1331

Browse files
committed
Disabled coverage for hard-to-test elements, and added new tests
1 parent 8084d4b commit 94c1331

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

domdf_python_tools/bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __deepcopy__(self, memodict={}):
6363
@property
6464
@abstractmethod
6565
def __dict__(self):
66-
return dict()
66+
return dict() # pragma: no cover (abc)
6767

6868
def __eq__(self, other) -> bool:
6969
if isinstance(other, self.__class__):

domdf_python_tools/dates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_utc_offset(
6363
if isinstance(tz, str):
6464
timezone = get_timezone(tz, date)
6565
else:
66-
timezone = tz
66+
timezone = tz # pragma: no cover (hard to test)
6767

6868
return date.replace(tzinfo=pytz.utc).astimezone(timezone).utcoffset()
6969

@@ -81,7 +81,7 @@ def get_timezone(tz: str, date: Optional[datetime.datetime] = None) -> Optional[
8181
"""
8282

8383
if date is None:
84-
date = datetime.datetime.utcnow()
84+
date = datetime.datetime.utcnow() # pragma: no cover (hard to test)
8585

8686
d = date.replace(tzinfo=None)
8787

@@ -94,7 +94,7 @@ def current_tzinfo() -> Optional[datetime.tzinfo]:
9494
:rtype: :class:`python:datetime.tzinfo`
9595
"""
9696

97-
return datetime.datetime.now().astimezone().tzinfo
97+
return datetime.datetime.now().astimezone().tzinfo # pragma: no cover (hard to test)
9898

9999
#
100100
# def datetime_to_utc_timestamp(datetime, current_tzinfo=None):

tests/test_typing.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
test_typing
3+
~~~~~~~~~~~~~~~
4+
5+
Test functions in typing.py
6+
7+
"""
8+
9+
# stdlib
10+
import os
11+
import pathlib
12+
13+
# 3rd party
14+
from typing import Dict, List, Sequence, Set, Tuple, Union
15+
16+
import pytest
17+
18+
# this package
19+
from domdf_python_tools.typing import check_membership, PathLike
20+
21+
22+
@pytest.mark.parametrize(
23+
"obj, type_",
24+
[
25+
("abc", Union[str, int, float, bytes]),
26+
(1234, Union[str, int, float, bytes]),
27+
(12.34, Union[str, int, float, bytes]),
28+
(b"\x0F", Union[str, int, float, bytes]),
29+
("abc", List[str]),
30+
(1234, Sequence[int]),
31+
(12.34, Set[float]),
32+
(1234, Tuple[int, float, str]), # todo: Position
33+
(12.34, Tuple[int, float, str]), # todo: Position
34+
("abc", Tuple[int, float, str]), # todo: Position
35+
(1234, Dict[int, float]), # todo: Position
36+
(12.34, Dict[int, float]), # todo: Position
37+
]
38+
)
39+
def test_check_membership_true(obj, type_):
40+
assert check_membership(obj, type_)
41+
42+
43+
@pytest.mark.parametrize(
44+
"obj, type_",
45+
[
46+
("abc", Union[float, bytes]),
47+
(1234, Union[str, float, bytes]),
48+
(12.34, Union[str, int]),
49+
(b"\x0F", Union[str, int, float]),
50+
("abc", List[int]),
51+
(1234, Sequence[bytes]),
52+
(12.34, Set[str]),
53+
(1234, Tuple[str, float, bytes]), # todo: Position
54+
(12.34, Tuple[int, bytes, str]), # todo: Position
55+
("abc", Tuple[int, float, bytes]), # todo: Position
56+
(1234, Dict[bytes, float]), # todo: Position
57+
(12.34, Dict[int, str]), # todo: Position
58+
]
59+
)
60+
def test_check_membership_false(obj, type_):
61+
assert not check_membership(obj, type_)
62+
63+
64+
class MyPathLike(os.PathLike):
65+
def __init__(self, directory, filename):
66+
self.directory = str(directory)
67+
self.filename = str(filename)
68+
69+
def __fspath__(self):
70+
os.path.join(self.directory, self.filename)
71+
72+
73+
class MyStr(str):
74+
pass
75+
76+
77+
class MyPath(type(pathlib.Path())): # type: ignore
78+
pass
79+
80+
81+
@pytest.mark.parametrize(
82+
"obj",
83+
[
84+
"/home/domdf/Python",
85+
"test_typing.py",
86+
pathlib.Path("/home/domdf/Python"),
87+
pathlib.Path("test_typing.py"),
88+
pathlib.PurePosixPath("test_typing.py"),
89+
pathlib.PureWindowsPath("test_typing.py"),
90+
MyPath("/home/domdf/Python"),
91+
MyPath("test_typing.py"),
92+
MyStr("/home/domdf/Python"),
93+
MyStr("test_typing.py"),
94+
MyPathLike("/home/domdf", "Python"),
95+
MyPathLike(".", "test_typing.py"),
96+
]
97+
)
98+
def test_pathlike_true(obj):
99+
assert check_membership(obj, PathLike)
100+
101+
102+
@pytest.mark.parametrize(
103+
"obj",
104+
[
105+
1234,
106+
12.34,
107+
[1, 2, 3, 4, 5],
108+
{1, 2, 3, 4, 5},
109+
(1, 2, 3, 4, 5),
110+
{"a": 1, "b": 2},
111+
]
112+
)
113+
def test_pathlike_false(obj):
114+
assert not check_membership(obj, PathLike)

tests/test_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,15 @@ def test_enquote_value(obj, expects):
376376
# def test_enquote_value_errors(obj, expects):
377377
# with pytest.raises(expects):
378378
# utils.enquote_value(obj)
379+
380+
def test_cmp():
381+
assert isinstance(utils.cmp(5, 20), int)
382+
assert utils.cmp(5, 20) < 0
383+
assert utils.cmp(5, 20) == -1
384+
385+
assert isinstance(utils.cmp(20, 5), int)
386+
assert utils.cmp(20, 5) > 0
387+
assert utils.cmp(20, 5) == 1
388+
389+
assert isinstance(utils.cmp(20, 20), int)
390+
assert utils.cmp(20, 20) == 0

0 commit comments

Comments
 (0)