Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Oct 29, 2024
1 parent e3dca8e commit dccdb61
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 382 deletions.
15 changes: 12 additions & 3 deletions src/palace/manager/celery/tasks/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from tempfile import TemporaryFile
from typing import Any

import pyinstrument
from celery import shared_task
from pydantic import TypeAdapter

Expand Down Expand Up @@ -124,7 +123,7 @@ def marc_export_collection(
)
return

with ExitStack() as stack, task.session() as session, pyinstrument.profile():
with ExitStack() as stack, task.transaction() as session:
files = {
library: stack.enter_context(TemporaryFile())
for library in libraries_info
Expand Down Expand Up @@ -192,7 +191,11 @@ def marc_export_collection(

# Upload part to s3, if there is anything to upload
for library, tmp_file in files.items():
uploads[library].upload_part(tmp_file)
upload = uploads[library]
if not upload.upload_part(tmp_file):
task.log.warning(
f"No data to upload to s3 '{upload.context.s3_key}'."
)

if no_more_works:
# Task is complete. Finalize the s3 uploads and create MarcFile records in DB.
Expand All @@ -208,6 +211,12 @@ def marc_export_collection(
key=upload.context.s3_key,
since=library.last_updated if delta else None,
)
task.log.info(f"Completed upload for '{upload.context.s3_key}'")
else:
task.log.warning(
f"No upload for '{upload.context.s3_key}', "
f"because there were no records."
)

task.log.info(
f"Finished generating MARC records for collection '{collection_name}' ({collection_id}) "
Expand Down
35 changes: 27 additions & 8 deletions src/palace/manager/marc/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class UploadContext(BaseModel):
upload_uuid: uuid.UUID
s3_key: str
upload_id: str
upload_id: str | None = None
parts: list[MultipartS3UploadPart] = []


Expand Down Expand Up @@ -51,11 +51,9 @@ def __init__(
upload_uuid,
since_time,
)
upload_id = self.storage_service.multipart_create(
s3_key, content_type=Representation.MARC_MEDIA_TYPE
)
context = UploadContext(
upload_uuid=upload_uuid, s3_key=s3_key, upload_id=upload_id
upload_uuid=upload_uuid,
s3_key=s3_key,
)
self.context = context

Expand Down Expand Up @@ -114,17 +112,34 @@ def __exit__(
self._in_context_manager = False
return False

def begin_upload(self) -> str:
upload_id = self.storage_service.multipart_create(
self.context.s3_key, content_type=Representation.MARC_MEDIA_TYPE
)
self.context.upload_id = upload_id
return upload_id

def upload_part(self, data: IO[bytes] | bytes) -> bool:
if self._finalized:
raise MarcUploadException("Upload is already finalized.")

length = len(data) if isinstance(data, bytes) else data.tell()
if isinstance(data, bytes):
length = len(data)
else:
length = data.tell()
data.seek(0)

if length == 0:
return False

if self.context.upload_id is None:
upload_id = self.begin_upload()
else:
upload_id = self.context.upload_id

part_number = len(self.context.parts) + 1
upload_part = self.storage_service.multipart_upload(
self.context.s3_key, self.context.upload_id, part_number, data
self.context.s3_key, upload_id, part_number, data
)
self.context.parts.append(upload_part)
return True
Expand All @@ -133,7 +148,7 @@ def complete(self) -> bool:
if self._finalized:
raise MarcUploadException("Upload is already finalized.")

if not self.context.parts:
if self.context.upload_id is None or not self.context.parts:
self.abort()
return False

Expand All @@ -147,6 +162,10 @@ def abort(self) -> None:
if self._finalized:
return

if self.context.upload_id is None:
self._finalized = True
return

self.storage_service.multipart_abort(
self.context.s3_key, self.context.upload_id
)
Expand Down
12 changes: 2 additions & 10 deletions tests/fixtures/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def __init__(
self.collection2.libraries = [self.library1]
self.collection3.libraries = [self.library2]

self.test_marc_file_key = "test-file-1.mrc"

def integration(self) -> IntegrationConfiguration:
return self._db.integration_configuration(
MarcExporter, Goals.CATALOG_GOAL, name="MARC Exporter"
Expand All @@ -54,13 +52,13 @@ def work(self, collection: Collection | None = None) -> Work:
edition = self._db.edition()
self._db.licensepool(edition, collection=collection)
work = self._db.work(presentation_edition=edition)
work.last_update_time = utc_now()
work.last_update_time = utc_now() - datetime.timedelta(days=1)
return work

def works(self, collection: Collection | None = None) -> list[Work]:
return [self.work(collection) for _ in range(5)]

def configure_export(self, *, marc_file: bool = True) -> None:
def configure_export(self) -> None:
marc_integration = self.integration()
self._db.integration_library_configuration(
marc_integration,
Expand All @@ -77,12 +75,6 @@ def configure_export(self, *, marc_file: bool = True) -> None:
self.collection2.export_marc_records = True
self.collection3.export_marc_records = True

if marc_file:
self.marc_file(
key=self.test_marc_file_key,
created=utc_now() - datetime.timedelta(days=7),
)

def enabled_libraries(
self, collection: Collection | None = None
) -> Sequence[LibraryInfo]:
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def __init__(
if isinstance(content, bytes):
self.content = content
else:
content.seek(0)
self.content = content.read()


Expand Down
Loading

0 comments on commit dccdb61

Please sign in to comment.