Skip to content

Commit

Permalink
Create test for fixity check signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed May 9, 2024
1 parent 55e5f22 commit b82cade
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
10 changes: 5 additions & 5 deletions storage_service/locations/models/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,27 +2062,27 @@ def get_fixity_check_report_send_signals(
# Build the response (to be a JSON object)
response = {
"success": success,
"message": message,
"message": str(message),
"failures": {"files": {"missing": [], "changed": [], "untracked": []}},
"timestamp": timestamp,
}
for failure in failures:
if isinstance(failure, bagit.FileMissing):
info = {"path": failure.path, "message": str(failure)}
info = {"path": failure.path, "message": failure}

Check warning on line 2071 in storage_service/locations/models/package.py

View check run for this annotation

Codecov / codecov/patch

storage_service/locations/models/package.py#L2071

Added line #L2071 was not covered by tests
response["failures"]["files"]["missing"].append(info)
if isinstance(failure, bagit.ChecksumMismatch):
info = {
"path": failure.path,
"expected": failure.expected,
"actual": failure.found,
"hash_type": failure.algorithm,
"message": str(failure),
"message": failure,
}
response["failures"]["files"]["changed"].append(info)
if isinstance(failure, bagit.UnexpectedFile):
info = {"path": failure.path, "message": str(failure)}
info = {"path": failure.path, "message": failure}

Check warning on line 2083 in storage_service/locations/models/package.py

View check run for this annotation

Codecov / codecov/patch

storage_service/locations/models/package.py#L2083

Added line #L2083 was not covered by tests
response["failures"]["files"]["untracked"].append(info)
report = json.dumps(response, default=str)
report = json.dumps(response)

# Trigger the signals (so ``FixityLog`` instances are created)
if success is False:
Expand Down
83 changes: 83 additions & 0 deletions tests/locations/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,89 @@ def test_move_uncompressed_aip(self):
assert pkg.current_location == dst_location


@pytest.fixture
@pytest.mark.django_db
def space(tmp_path):
space_dir = tmp_path / "space"
space_dir.mkdir()

staging_dir = tmp_path / "staging"
staging_dir.mkdir()

space = models.Space.objects.create(
uuid=uuid.uuid4(),
access_protocol=models.Space.LOCAL_FILESYSTEM,
path=space_dir,
staging_path=staging_dir,
)
models.LocalFilesystem.objects.create(space=space)
return space


@pytest.fixture
@pytest.mark.django_db
def location(space):
aipstore = models.Location.objects.create(
uuid=uuid.uuid4(),
space=space,
relative_path="fs-aips",
purpose="AS",
)
models.Location.objects.create(
space=space, purpose=models.Location.STORAGE_SERVICE_INTERNAL, relative_path=""
)
return aipstore


@pytest.fixture
@pytest.mark.django
def bag_fixture(tmp_path):
tmp_dir = tmp_path / "dir"
tmp_dir.mkdir()
src = os.path.join(FIXTURES_DIR, "working_bag.zip")
dst = os.path.join(tmp_dir, "")
return shutil.copy(src, dst)


@pytest.fixture
@pytest.mark.django_db
def package(location, bag_fixture):
return models.Package.objects.create(
uuid=uuid.uuid4(),
current_location=location,
current_path=bag_fixture,
package_type="AIP",
status="Uploaded",
)


@pytest.mark.django_db
@mock.patch(
"common.utils.generate_checksum",
return_value=mock.Mock(
**{
"hexdigest.return_value": "myhash",
}
),
)
def test_get_fixity_check_report_send_signals(generate_checksum, package):
"""
It verifies report of fixity check
"""
package.checksum = "incorrect"
package.save()

report, response = package.get_fixity_check_report_send_signals()

assert response == {
"success": False,
"message": "Incorrect package checksum",
"failures": {"files": {"missing": [], "changed": [], "untracked": []}},
"timestamp": None,
}
assert response["message"] == "Incorrect package checksum"


class TestTransferPackage(TestCase):
"""Test integration of transfer reading and indexing.
Expand Down

0 comments on commit b82cade

Please sign in to comment.