Skip to content

Commit

Permalink
Fix check_for_access_directory client script
Browse files Browse the repository at this point in the history
  • Loading branch information
replaceafill committed Sep 25, 2023
1 parent 5e2a222 commit 474fa4d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/MCPClient/lib/clientScripts/check_for_access_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from fileOperations import rename


def something(
def main(
job,
SIPDirectory,
accessDirectory,
Expand All @@ -45,11 +45,10 @@ def something(
date,
copy=False,
):
# exitCode = 435
exitCode = 179
job.pyprint(SIPDirectory)
# For every file, & directory Try to find the matching file & directory in the objects directory
for path, dirs, files in os.walk(accessDirectory):
for path, _, files in os.walk(accessDirectory):
for file in files:
accessPath = os.path.join(path, file)
objectPath = accessPath.replace(accessDirectory, objectsDirectory, 1)
Expand Down Expand Up @@ -78,7 +77,7 @@ def something(
for objectUUID, objectPath in files.values_list(
"uuid", "currentlocation"
):
objectExtension = objectPath.replace(objectNameLike, "", 1)
objectExtension = objectPath.decode().replace(objectNameLike, "", 1)
job.pyprint(
objectName[objectNameExtensionIndex + 1 :],
objectExtension,
Expand Down Expand Up @@ -173,7 +172,7 @@ def call(jobs):
except:
job.pyprint("error creating DIP directory")

exitCode = something(
exitCode = main(

Check warning on line 175 in src/MCPClient/lib/clientScripts/check_for_access_directory.py

View check run for this annotation

Codecov / codecov/patch

src/MCPClient/lib/clientScripts/check_for_access_directory.py#L175

Added line #L175 was not covered by tests
job,
SIPDirectory,
accessDirectory,
Expand Down
121 changes: 121 additions & 0 deletions src/MCPClient/tests/test_check_for_access_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import check_for_access_directory
import pytest
from django.utils import timezone
from job import Job
from main.models import Event
from main.models import File
from main.models import SIP


@pytest.mark.django_db
def test_main(mocker, tmp_path):
job = mocker.Mock(spec=Job)
date = timezone.now()

sip_directory = tmp_path / "sip"
sip_directory.mkdir()

sip = SIP.objects.create(currentpath=sip_directory.as_posix())

access_directory = tmp_path / "access"
access_directory.mkdir()
(access_directory / "file1.txt").touch()
(access_directory / "file2.txt").touch()

objects_directory = tmp_path / "objects"
objects_directory.mkdir()
(objects_directory / "file1.txt").touch()
(objects_directory / "file2.txt").touch()
(objects_directory / "file3.txt").touch()

dip_directory = tmp_path / "dip"
dip_directory.mkdir()
(dip_directory / "objects").mkdir()

# Add access files to the database.
File.objects.create(
sip=sip,
originallocation=(access_directory / "file1.txt").as_posix().encode(),
currentlocation=(access_directory / "file1.txt").as_posix().encode(),
)
File.objects.create(
sip=sip,
originallocation=(access_directory / "file2.txt").as_posix().encode(),
currentlocation=(access_directory / "file2.txt").as_posix().encode(),
)
assert (
File.objects.filter(
sip=sip,
currentlocation__startswith=access_directory.as_posix(),
).count()
== 2
)

# Add objects files to the database.
File.objects.create(
sip=sip,
originallocation=(objects_directory / "file1.txt").as_posix().encode(),
currentlocation=(objects_directory / "file1.txt").as_posix().encode(),
)
File.objects.create(
sip=sip,
originallocation=(objects_directory / "file2.txt").as_posix().encode(),
currentlocation=(objects_directory / "file2.txt").as_posix().encode(),
)
File.objects.create(
sip=sip,
originallocation=(objects_directory / "file3.txt").as_posix().encode(),
currentlocation=(objects_directory / "file3.txt").as_posix().encode(),
)

# The DIP objects directory is initially empty.
assert (
File.objects.filter(
sip=sip,
currentlocation__startswith=(dip_directory / "objects").as_posix(),
).count()
== 0
)

# And there are no PREMIS "movement" events.
assert Event.objects.filter(event_type="movement").count() == 0

result = check_for_access_directory.main(
job,
sip_directory.as_posix(),
access_directory.as_posix(),
objects_directory.as_posix(),
dip_directory.as_posix(),
str(sip.uuid),
date,
)

# Files have been moved from the access directory to the DIP objects directory.
assert (
File.objects.filter(
sip=sip,
currentlocation__startswith=access_directory.as_posix(),
).count()
== 0
)
assert (
File.objects.filter(
sip=sip,
currentlocation__startswith=(dip_directory / "objects").as_posix(),
currentlocation__endswith=".txt",
).count()
== 2
)

# And PREMIS "movement" events are created accordingly.
assert (
Event.objects.filter(
event_type="movement",
event_outcome_detail__startswith=f'moved from="{access_directory.as_posix()}',
event_outcome_detail__contains=f'moved to="{(dip_directory / "objects").as_posix()}',
event_datetime__date=date,
).count()
== 2
)

assert result == 179
2 changes: 1 addition & 1 deletion src/archivematicaCommon/lib/fileOperations.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def updateFileLocation(
To suppress creation of an event, pass the createEvent keyword argument (for example, if the file moved due to the renaming of a parent directory and not the file itself).
"""
if not fileUUID or fileUUID == "None":
kwargs = {"removedtime__isnull": True, "currentlocation": src}
kwargs = {"removedtime__isnull": True, "currentlocation": src.encode()}

if sipUUID:
kwargs["sip_id"] = sipUUID
Expand Down
8 changes: 4 additions & 4 deletions src/dashboard/src/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ def __str__(self):
_("File %(uuid)s:%(originallocation)s now at %(currentlocation)s")
% {
"uuid": self.uuid,
"originallocation": self.originallocation,
"currentlocation": self.currentlocation,
"originallocation": self.originallocation.decode(),
"currentlocation": self.currentlocation.decode(),
}
)

Expand Down Expand Up @@ -812,8 +812,8 @@ def __str__(self):
_("Directory %(uuid)s: %(originallocation)s now at %(currentlocation)s")
% {
"uuid": self.uuid,
"originallocation": self.originallocation,
"currentlocation": self.currentlocation,
"originallocation": self.originallocation.decode(),
"currentlocation": self.currentlocation.decode(),
}
)

Expand Down

0 comments on commit 474fa4d

Please sign in to comment.