Skip to content

Commit

Permalink
Update tests\
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeqfu committed Nov 10, 2022
1 parent f5d0fb1 commit 35a862c
Show file tree
Hide file tree
Showing 7 changed files with 4,753 additions and 4,644 deletions.
8,950 changes: 4,475 additions & 4,475 deletions pyhelpers/data/user-agent-strings.json

Large diffs are not rendered by default.

Binary file modified tests/data/dat.xlsx
Binary file not shown.
Binary file modified tests/images/store-save_fig-demo.emf
Binary file not shown.
34 changes: 17 additions & 17 deletions tests/images/store-save_fig-demo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions tests/test__cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Test the module ``_cache.py``"""

import pytest


def test__check_dependency():
from pyhelpers._cache import _check_dependency

sqlalchemy_dialects = _check_dependency(name='dialects', package='sqlalchemy')
assert sqlalchemy_dialects.__name__ == 'sqlalchemy.dialects'


def test__check_rel_pathname():
from pyhelpers._cache import _check_rel_pathname

pathname = ""
pathname_ = _check_rel_pathname(pathname=pathname)
assert pathname_ == pathname


if __name__ == '__main__':
pytest.main()
77 changes: 64 additions & 13 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import os
import tempfile
import typing
import warnings

Expand All @@ -17,6 +18,25 @@
from pyhelpers.dirs import cd


def test_confirmed(monkeypatch):
from pyhelpers.ops import confirmed

assert confirmed(confirmation_required=False)

monkeypatch.setattr('builtins.input', lambda _: "Yes")
assert confirmed()

monkeypatch.setattr('builtins.input', lambda _: "")
assert not confirmed()

monkeypatch.setattr('builtins.input', lambda _: "no")
assert not confirmed(resp=True)

prompt = "Testing if the function works?"
monkeypatch.setattr('builtins.input', lambda _: "Yes")
assert confirmed(prompt=prompt, resp=False)


def test_eval_dtype(capfd):
from pyhelpers.ops import eval_dtype

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


@pytest.mark.parametrize('chunk_size_limit', [0, None, 1, 0.1])
def test_get_number_of_chunks(chunk_size_limit):
from pyhelpers.ops import get_number_of_chunks

temp_file_ = tempfile.NamedTemporaryFile()
temp_file_path = temp_file_.name + ".txt"
with open(temp_file_path, 'w') as f:
f.write(", ".join(map(str, range(10 ** 5))))

number_of_chunks = get_number_of_chunks(temp_file_path, chunk_size_limit=chunk_size_limit)
if chunk_size_limit:
assert number_of_chunks >= 1
else:
assert number_of_chunks is None

os.remove(temp_file_path)


def test_get_relative_path():
from pyhelpers.ops import get_relative_path

rel_pathname = get_relative_path(pathname="")
assert rel_pathname == ''

rel_pathname = get_relative_path(pathname=os.path.join(os.getcwd(), "tests"))
assert rel_pathname == 'tests'

# On Windows OS
rel_pathname = get_relative_path(pathname="C:/Windows")
assert rel_pathname == "C:/Windows"


def test_hash_password():
from pyhelpers.ops import hash_password, verify_password

Expand Down Expand Up @@ -453,31 +505,30 @@ def test_get_user_agent_string():
_ = get_user_agent_string(fancy='Chrome')


def test_fake_requests_headers():
@pytest.mark.parametrize('randomized', [False, True])
def test_fake_requests_headers(randomized):
from pyhelpers.ops import fake_requests_headers

fake_headers_1 = fake_requests_headers()
assert 'user-agent' in fake_headers_1

fake_headers_2 = fake_requests_headers(randomized=False)
assert 'user-agent' in fake_headers_2
fake_headers_ = fake_requests_headers(randomized=randomized)
assert 'user-agent' in fake_headers_


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

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

# Download the .png file
download_file_from_url(logo_url, path_to_img)

# If download is successful, check again:
assert os.path.exists(path_to_img)

download_file_from_url(logo_url, path_to_img, verbose=True)
out, _ = capfd.readouterr()

assert os.path.isfile(path_to_img)

assert os.path.exists(path_to_img)
os.remove(path_to_img_.name)
os.remove(path_to_img)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 35a862c

Please sign in to comment.