Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pytest-mock dependency #753

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
pip-tools
pytest-cov
pytest-django
pytest-mock
pytest-playwright
pytest-randomly
pytest
Expand All @@ -15,5 +14,6 @@ tox
# See https://github.com/microsoft/playwright-python/issues/2190
git+https://github.com/microsoft/playwright-python.git@d9cdfbb1e178b6770625e9f857139aff77516af0#egg=playwright

# coverage 7.6.2 dropped support for Python 3.8, so pinning it for now.
# These dependencies dropped support for Python 3.8, so pinning them for now.
coverage==7.6.1
pytest-randomly==3.15.0
9 changes: 3 additions & 6 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ asgiref==3.8.1
# django
bagit==1.8.1
# via -r requirements.txt
boto3==1.35.45
boto3==1.35.49
# via -r requirements.txt
botocore==1.35.45
botocore==1.35.49
# via
# -r requirements.txt
# boto3
Expand Down Expand Up @@ -258,7 +258,6 @@ pytest==8.3.3
# pytest-base-url
# pytest-cov
# pytest-django
# pytest-mock
# pytest-playwright
# pytest-randomly
pytest-base-url==2.1.0
Expand All @@ -267,8 +266,6 @@ pytest-cov==5.0.0
# via -r requirements-dev.in
pytest-django==4.9.0
# via -r requirements-dev.in
pytest-mock==3.14.0
# via -r requirements-dev.in
pytest-playwright==0.5.2
# via -r requirements-dev.in
pytest-randomly==3.15.0
Expand Down Expand Up @@ -387,7 +384,7 @@ zope-event==5.0
# via
# -r requirements.txt
# gevent
zope-interface==7.1.0
zope-interface==7.1.1
# via
# -r requirements.txt
# gevent
Expand Down
4 changes: 2 additions & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ brotli
Django>=4.2,<5
django-csp
django-tastypie
dj-database-url
gunicorn
importlib_resources
jsonfield
Expand All @@ -31,7 +30,8 @@ django-cas-ng
# Required for OpenID Connect authentication
mozilla-django-oidc

# gevent 24.10.1 dropped support for Python 3.8, so pinning it for now.
# These dependencies dropped support for Python 3.8, so pinning them for now.
dj-database-url==2.2.0
django-auth-ldap==5.0.0
gevent==24.2.1
pyparsing==3.1.4
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ asgiref==3.8.1
# via django
bagit==1.8.1
# via -r requirements.in
boto3==1.35.45
boto3==1.35.49
# via -r requirements.in
botocore==1.35.45
botocore==1.35.49
# via
# boto3
# s3transfer
Expand Down Expand Up @@ -219,7 +219,7 @@ zipp==3.20.2
# via importlib-resources
zope-event==5.0
# via gevent
zope-interface==7.1.0
zope-interface==7.1.1
# via gevent

# The following packages are considered to be unsafe in a requirements file:
Expand Down
7 changes: 4 additions & 3 deletions tests/administration/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@pytest.mark.parametrize(
"check_output,expected_result",
"output,expected_result",
[
(
b"d9c93f388a770287cf6337d4f9bcbbe60c25fdb8\n",
Expand All @@ -19,8 +19,9 @@
],
ids=["success", "error"],
)
def test_get_git_commit(check_output, expected_result, mocker):
mocker.patch("subprocess.check_output", side_effect=[check_output])
@mock.patch("subprocess.check_output")
def test_get_git_commit(check_output, output, expected_result):
check_output.side_effect = [output]

assert get_git_commit() == expected_result

Expand Down
7 changes: 5 additions & 2 deletions tests/common/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from unittest import mock

import pytest


@pytest.fixture(autouse=True)
def do_not_reset_the_root_logger(mocker):
def do_not_reset_the_root_logger():
# Some management commands suppress logging from models/package.py
# by calling logging.config.dictConfig directly which breaks the caplog
# fixture. See https://github.com/pytest-dev/pytest/discussions/11011
#
# This avoids breaking the caplog fixture when those management command
# modules are imported or called during tests.
mocker.patch("logging.config")
with mock.patch("logging.config"):
yield
75 changes: 43 additions & 32 deletions tests/common/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pathlib
import shutil
import subprocess
import tarfile
from collections import namedtuple
from io import StringIO
Expand Down Expand Up @@ -160,10 +159,10 @@ def test_get_tool_info_command(compression, command):
)
@mock.patch("subprocess.check_output")
def test_get_compression_event_detail(
mock_subprocess, compression, cmd_output, expected_detail
check_output, compression, cmd_output, expected_detail
):
# subprocess.check_output returns bytes in python3
mock_subprocess.return_value = cmd_output.encode("utf8")
check_output.return_value = cmd_output.encode("utf8")
detail = utils.get_compression_event_detail(compression)

assert (
Expand Down Expand Up @@ -299,17 +298,19 @@ def test_package_is_file(package_path, is_file):
ExTarCase(path="/a/b/c", isdir=True, raises=True, expected="fail"),
],
)
def test_extract_tar(mocker, path, will_be_dir, sp_raises, expected):
@mock.patch("pathlib.Path.rename")
@mock.patch("pathlib.Path.unlink")
@mock.patch("pathlib.Path.is_dir")
@mock.patch("subprocess.check_output")
def test_extract_tar(
check_output, is_dir, unlink, rename, path, will_be_dir, sp_raises, expected
):
if sp_raises:
mocker.patch.object(subprocess, "check_output", side_effect=OSError("gotcha!"))
else:
mocker.patch.object(subprocess, "check_output")
mocker.patch.object(pathlib.Path, "rename")
mocker.patch.object(pathlib.Path, "unlink")
check_output.side_effect = OSError("gotcha!")
if will_be_dir:
mocker.patch.object(pathlib.Path, "is_dir", return_value=True)
is_dir.return_value = True
else:
mocker.patch.object(pathlib.Path, "is_dir", return_value=False)
is_dir.return_value = False
path = pathlib.Path(path)
tarpath_ext = path.with_suffix(".tar")
dirname = tarpath_ext.parent
Expand All @@ -322,11 +323,9 @@ def test_extract_tar(mocker, path, will_be_dir, sp_raises, expected):
ret = utils.extract_tar(path)
assert f"Failed to extract {path}: gotcha!" == str(excinfo.value)
tarpath_ext.rename.assert_any_call(path)
assert not pathlib.Path.unlink.called
unlink.assert_not_called()
path.rename.assert_any_call(tarpath_ext)
subprocess.check_output.assert_called_once_with(
["tar", "-xf", tarpath_ext, "-C", dirname]
)
check_output.assert_called_once_with(["tar", "-xf", tarpath_ext, "-C", dirname])


@pytest.mark.parametrize(
Expand Down Expand Up @@ -390,17 +389,29 @@ def test_extract_tar(mocker, path, will_be_dir, sp_raises, expected):
),
],
)
@mock.patch("pathlib.Path.rename")
@mock.patch("shutil.rmtree")
@mock.patch("pathlib.Path.is_file")
@mock.patch("tarfile.is_tarfile")
@mock.patch("subprocess.check_output")
def test_create_tar(
mocker, path, will_be_file, will_be_tar, sp_raises, expected, extension
check_output,
is_tarfile,
is_file,
rmtree,
rename,
path,
will_be_file,
will_be_tar,
sp_raises,
expected,
extension,
):
if sp_raises:
mocker.patch.object(subprocess, "check_output", side_effect=OSError("gotcha!"))
else:
mocker.patch.object(subprocess, "check_output")
mocker.patch.object(pathlib.Path, "is_file", return_value=will_be_file)
mocker.patch.object(tarfile, "is_tarfile", return_value=will_be_tar)
mocker.patch.object(pathlib.Path, "rename")
mocker.patch.object(shutil, "rmtree")
check_output.side_effect = OSError("gotcha!")
is_file.return_value = will_be_file
is_tarfile.return_value = will_be_tar

fixed_path = pathlib.Path(path)
tarpath = fixed_path.with_suffix(".tar")
if expected == "success":
Expand All @@ -416,8 +427,8 @@ def test_create_tar(
f"Failed to create a tarfile at {tarpath} for dir at {fixed_path}"
== str(excinfo.value)
)
assert not shutil.rmtree.called
assert not pathlib.Path.rename.called
rmtree.assert_not_called()
rename.assert_not_called()
if not sp_raises:
tarpath.is_file.assert_called_once()
if will_be_file:
Expand Down Expand Up @@ -480,11 +491,12 @@ def test_strip_quad_dirs_from_path(input_path, expected_path):
(["tagmanifest-md5.txt"], "tagmanifest-md5.txt"),
],
)
def test_find_tagmanifest(mocker, tmp_path, dir_listing, tagmanifest_file):
@mock.patch("pathlib.Path.iterdir")
def test_find_tagmanifest(iterdir, tmp_path, dir_listing, tagmanifest_file):
aip_path = tmp_path / "aip"
aip_path.mkdir()
mock_files = [aip_path / file_ for file_ in dir_listing]
mocker.patch.object(pathlib.Path, "iterdir", return_value=mock_files)
iterdir.return_value = mock_files

if tagmanifest_file is None:
assert utils.find_tagmanifest(aip_path) is None
Expand All @@ -496,16 +508,15 @@ def test_find_tagmanifest(mocker, tmp_path, dir_listing, tagmanifest_file):
assert utils.find_tagmanifest(file_path) is None


def test_generate_checksum_uncompressed_aip(mocker, tmp_path):
@mock.patch("common.utils.find_tagmanifest")
@mock.patch("pathlib.Path.is_dir", return_value=True)
def test_generate_checksum_uncompressed_aip(is_dir, find_tag_manifest, tmp_path):
aip_path = tmp_path / "aip"
aip_path.mkdir()
tagmanifest = aip_path / "tagmanifest-md5.txt"
tagmanifest.write_text("some test data")

mocker.patch.object(pathlib.Path, "is_dir", return_value=True)
find_tag_manifest = mocker.patch(
"common.utils.find_tagmanifest", return_value=tagmanifest
)
find_tag_manifest.return_value = tagmanifest

utils.generate_checksum(aip_path)
find_tag_manifest.assert_called_once()
Expand Down
Loading