Skip to content

Commit 35a862c

Browse files
committed
Update tests\
1 parent f5d0fb1 commit 35a862c

File tree

7 files changed

+4753
-4644
lines changed

7 files changed

+4753
-4644
lines changed

pyhelpers/data/user-agent-strings.json

Lines changed: 4475 additions & 4475 deletions
Large diffs are not rendered by default.

tests/data/dat.xlsx

1 Byte
Binary file not shown.

tests/images/store-save_fig-demo.emf

0 Bytes
Binary file not shown.

tests/images/store-save_fig-demo.svg

Lines changed: 17 additions & 17 deletions
Loading

tests/test__cache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Test the module ``_cache.py``"""
2+
3+
import pytest
4+
5+
6+
def test__check_dependency():
7+
from pyhelpers._cache import _check_dependency
8+
9+
sqlalchemy_dialects = _check_dependency(name='dialects', package='sqlalchemy')
10+
assert sqlalchemy_dialects.__name__ == 'sqlalchemy.dialects'
11+
12+
13+
def test__check_rel_pathname():
14+
from pyhelpers._cache import _check_rel_pathname
15+
16+
pathname = ""
17+
pathname_ = _check_rel_pathname(pathname=pathname)
18+
assert pathname_ == pathname
19+
20+
21+
if __name__ == '__main__':
22+
pytest.main()

tests/test_ops.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import os
5+
import tempfile
56
import typing
67
import warnings
78

@@ -17,6 +18,25 @@
1718
from pyhelpers.dirs import cd
1819

1920

21+
def test_confirmed(monkeypatch):
22+
from pyhelpers.ops import confirmed
23+
24+
assert confirmed(confirmation_required=False)
25+
26+
monkeypatch.setattr('builtins.input', lambda _: "Yes")
27+
assert confirmed()
28+
29+
monkeypatch.setattr('builtins.input', lambda _: "")
30+
assert not confirmed()
31+
32+
monkeypatch.setattr('builtins.input', lambda _: "no")
33+
assert not confirmed(resp=True)
34+
35+
prompt = "Testing if the function works?"
36+
monkeypatch.setattr('builtins.input', lambda _: "Yes")
37+
assert confirmed(prompt=prompt, resp=False)
38+
39+
2040
def test_eval_dtype(capfd):
2141
from pyhelpers.ops import eval_dtype
2242

@@ -48,6 +68,38 @@ def test_parse_size():
4868
assert parse_size(size=129446707, binary=False, precision=2) == '129.45 MB'
4969

5070

71+
@pytest.mark.parametrize('chunk_size_limit', [0, None, 1, 0.1])
72+
def test_get_number_of_chunks(chunk_size_limit):
73+
from pyhelpers.ops import get_number_of_chunks
74+
75+
temp_file_ = tempfile.NamedTemporaryFile()
76+
temp_file_path = temp_file_.name + ".txt"
77+
with open(temp_file_path, 'w') as f:
78+
f.write(", ".join(map(str, range(10 ** 5))))
79+
80+
number_of_chunks = get_number_of_chunks(temp_file_path, chunk_size_limit=chunk_size_limit)
81+
if chunk_size_limit:
82+
assert number_of_chunks >= 1
83+
else:
84+
assert number_of_chunks is None
85+
86+
os.remove(temp_file_path)
87+
88+
89+
def test_get_relative_path():
90+
from pyhelpers.ops import get_relative_path
91+
92+
rel_pathname = get_relative_path(pathname="")
93+
assert rel_pathname == ''
94+
95+
rel_pathname = get_relative_path(pathname=os.path.join(os.getcwd(), "tests"))
96+
assert rel_pathname == 'tests'
97+
98+
# On Windows OS
99+
rel_pathname = get_relative_path(pathname="C:/Windows")
100+
assert rel_pathname == "C:/Windows"
101+
102+
51103
def test_hash_password():
52104
from pyhelpers.ops import hash_password, verify_password
53105

@@ -453,31 +505,30 @@ def test_get_user_agent_string():
453505
_ = get_user_agent_string(fancy='Chrome')
454506

455507

456-
def test_fake_requests_headers():
508+
@pytest.mark.parametrize('randomized', [False, True])
509+
def test_fake_requests_headers(randomized):
457510
from pyhelpers.ops import fake_requests_headers
458511

459-
fake_headers_1 = fake_requests_headers()
460-
assert 'user-agent' in fake_headers_1
461-
462-
fake_headers_2 = fake_requests_headers(randomized=False)
463-
assert 'user-agent' in fake_headers_2
512+
fake_headers_ = fake_requests_headers(randomized=randomized)
513+
assert 'user-agent' in fake_headers_
464514

465515

466516
def test_download_file_from_url(capfd):
467517
from pyhelpers.ops import download_file_from_url
468518

469519
logo_url = 'https://www.python.org/static/community_logos/python-logo-master-v3-TM.png'
470-
path_to_img = cd("tests\\images", "ops-download_file_from_url-demo.png")
520+
# path_to_img = cd("tests\\images", "ops-download_file_from_url-demo.png")
521+
path_to_img_ = tempfile.NamedTemporaryFile()
522+
path_to_img = path_to_img_.name + ".png"
471523

472524
# Download the .png file
473-
download_file_from_url(logo_url, path_to_img)
474-
475-
# If download is successful, check again:
476-
assert os.path.exists(path_to_img)
477-
478525
download_file_from_url(logo_url, path_to_img, verbose=True)
526+
out, _ = capfd.readouterr()
527+
528+
assert os.path.isfile(path_to_img)
479529

480-
assert os.path.exists(path_to_img)
530+
os.remove(path_to_img_.name)
531+
os.remove(path_to_img)
481532

482533

483534
if __name__ == '__main__':

0 commit comments

Comments
 (0)