|
2 | 2 |
|
3 | 3 | import datetime
|
4 | 4 | import os
|
| 5 | +import tempfile |
5 | 6 | import typing
|
6 | 7 | import warnings
|
7 | 8 |
|
|
17 | 18 | from pyhelpers.dirs import cd
|
18 | 19 |
|
19 | 20 |
|
| 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 | + |
20 | 40 | def test_eval_dtype(capfd):
|
21 | 41 | from pyhelpers.ops import eval_dtype
|
22 | 42 |
|
@@ -48,6 +68,38 @@ def test_parse_size():
|
48 | 68 | assert parse_size(size=129446707, binary=False, precision=2) == '129.45 MB'
|
49 | 69 |
|
50 | 70 |
|
| 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 | + |
51 | 103 | def test_hash_password():
|
52 | 104 | from pyhelpers.ops import hash_password, verify_password
|
53 | 105 |
|
@@ -453,31 +505,30 @@ def test_get_user_agent_string():
|
453 | 505 | _ = get_user_agent_string(fancy='Chrome')
|
454 | 506 |
|
455 | 507 |
|
456 |
| -def test_fake_requests_headers(): |
| 508 | +@pytest.mark.parametrize('randomized', [False, True]) |
| 509 | +def test_fake_requests_headers(randomized): |
457 | 510 | from pyhelpers.ops import fake_requests_headers
|
458 | 511 |
|
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_ |
464 | 514 |
|
465 | 515 |
|
466 | 516 | def test_download_file_from_url(capfd):
|
467 | 517 | from pyhelpers.ops import download_file_from_url
|
468 | 518 |
|
469 | 519 | 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" |
471 | 523 |
|
472 | 524 | # 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 |
| - |
478 | 525 | download_file_from_url(logo_url, path_to_img, verbose=True)
|
| 526 | + out, _ = capfd.readouterr() |
| 527 | + |
| 528 | + assert os.path.isfile(path_to_img) |
479 | 529 |
|
480 |
| - assert os.path.exists(path_to_img) |
| 530 | + os.remove(path_to_img_.name) |
| 531 | + os.remove(path_to_img) |
481 | 532 |
|
482 | 533 |
|
483 | 534 | if __name__ == '__main__':
|
|
0 commit comments