Skip to content

Commit

Permalink
use mock
Browse files Browse the repository at this point in the history
  • Loading branch information
djay committed Jul 7, 2024
1 parent dabf6d9 commit 0792e47
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/plone/restapi/tests/test_tus.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,44 +409,41 @@ def count_open(self, mode="r"):
blob_write += 1 if "w" in mode else 0
return old_open(self, mode)

ZODB.blob.Blob.open = count_open
with unittest.mock.patch.object(ZODB.blob.Blob, 'open', count_open):

pdf_file_path = os.path.join(os.path.dirname(__file__), UPLOAD_PDF_FILENAME)
pdf_file_size = os.path.getsize(pdf_file_path)
metadata = _prepare_metadata(UPLOAD_PDF_FILENAME, UPLOAD_PDF_MIMETYPE)
response = self.api_session.post(
self.upload_url,
headers={
"Tus-Resumable": "1.0.0",
"Upload-Length": str(pdf_file_size),
"Upload-Metadata": metadata,
},
)
self.assertEqual(response.status_code, 201)
location = response.headers["Location"]

# upload the data with PATCH
with open(pdf_file_path, "rb") as pdf_file:
response = self.api_session.patch(
location,
pdf_file_path = os.path.join(os.path.dirname(__file__), UPLOAD_PDF_FILENAME)
pdf_file_size = os.path.getsize(pdf_file_path)
metadata = _prepare_metadata(UPLOAD_PDF_FILENAME, UPLOAD_PDF_MIMETYPE)
response = self.api_session.post(
self.upload_url,
headers={
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "0",
"Tus-Resumable": "1.0.0",
"Upload-Length": str(pdf_file_size),
"Upload-Metadata": metadata,
},
data=pdf_file,
)
self.assertEqual(response.status_code, 204)

self.assertEqual(
blob_write,
1,
"Slow write to blob instead of os rename. Should be only 1 on init",
)
self.assertEqual(blob_read, 0, "Validation is reading the whole blob in memory")
# TODO: would be better test to patch file read instead and ensure its not called?

ZODB.blob.Blob.open = old_open
self.assertEqual(response.status_code, 201)
location = response.headers["Location"]

# upload the data with PATCH
with open(pdf_file_path, "rb") as pdf_file:
response = self.api_session.patch(
location,
headers={
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "0",
"Tus-Resumable": "1.0.0",
},
data=pdf_file,
)
self.assertEqual(response.status_code, 204)

self.assertEqual(
blob_write,
1,
"Slow write to blob instead of os rename. Should be only 1 on init",
)
self.assertEqual(blob_read, 0, "Validation is reading the whole blob in memory")

def test_tus_can_replace_pdf_file(self):
# Create a test file
Expand Down

0 comments on commit 0792e47

Please sign in to comment.