Skip to content

Commit

Permalink
quality: Clean up every time the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Aug 21, 2024
1 parent f444104 commit c275ed6
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 241 deletions.
297 changes: 153 additions & 144 deletions tests/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,57 @@ async def test_acid(provider: BlobProvider) -> None:
path="scraping-test",
provider=provider,
) as client:
test_content_bytes = blob_content.encode(client.encoding)

# Check not exists
try:
await client.download_blob(blob_name)
raise AssertionError("Blob should not exist")
except BlobNotFoundError:
pass

# Upload test content
await client.upload_blob(
blob=blob_name,
data=test_content_bytes,
length=len(test_content_bytes),
overwrite=False,
)

# Check uploaded and downloaded content are the same
try:
# Get blob content
download_content = await client.download_blob(blob_name)
# Debug
logger.info("Downloaded content: %s", download_content)
assert download_content == blob_content, "Content mismatch"
except BlobNotFoundError as e:
raise AssertionError("Blob should exist") from e

# Check raise error on overwrite
try:
test_content_bytes = blob_content.encode(client.encoding)

# Check not exists
try:
await client.download_blob(blob_name)
raise AssertionError("Blob should not exist")
except BlobNotFoundError:
pass

# Upload test content
await client.upload_blob(
blob=blob_name,
data=test_content_bytes,
length=len(test_content_bytes),
overwrite=False,
)
raise AssertionError("Should raise error with overwrite disabled")
except BlobAlreadyExistsError:
pass

# Check overwrite
await client.upload_blob(
blob=blob_name,
data=test_content_bytes,
length=len(test_content_bytes),
overwrite=True,
)
# Check uploaded and downloaded content are the same
try:
# Get blob content
download_content = await client.download_blob(blob_name)
# Debug
logger.info("Downloaded content: %s", download_content)
assert download_content == blob_content, "Content mismatch"
except BlobNotFoundError as e:
raise AssertionError("Blob should exist") from e

# Check raise error on overwrite
try:
await client.upload_blob(
blob=blob_name,
data=test_content_bytes,
length=len(test_content_bytes),
overwrite=False,
)
raise AssertionError("Should raise error with overwrite disabled")
except BlobAlreadyExistsError:
pass

# Check overwrite
await client.upload_blob(
blob=blob_name,
data=test_content_bytes,
length=len(test_content_bytes),
overwrite=True,
)

# Clean up
await client.delete_container()
finally:
# Clean up
await client.delete_container()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -120,112 +122,118 @@ async def test_lease(provider: BlobProvider) -> None:
path="scraping-test",
provider=provider,
) as client:
# Write empty blob
await client.upload_blob(
blob=blob_name,
data=b"",
length=0,
overwrite=False,
)

# Check raise with wrong lease ID without lease
with pytest.raises(LeaseNotFoundError):
try:
# Write empty blob
await client.upload_blob(
blob=blob_name,
data=first_content.encode(client.encoding),
lease_id=str(uuid4()), # Wrong lease ID
length=len(first_content),
overwrite=True,
data=b"",
length=0,
overwrite=False,
)
raise AssertionError("Should raise error with wrong lease ID without lease")

# Start lease
async with client.lease_blob(
blob=blob_name,
lease_duration=15, # 15 secs
) as lease_id:

# Check raise error on double lease with the first non expired
with pytest.raises(LeaseAlreadyExistsError):
async with client.lease_blob(
# Check raise with wrong lease ID without lease
with pytest.raises(LeaseNotFoundError):
await client.upload_blob(
blob=blob_name,
lease_duration=15, # 15 secs
) as lease_id:
raise AssertionError(
"Should raise error on double lease with the first non expired"
)
data=first_content.encode(client.encoding),
lease_id=str(uuid4()), # Wrong lease ID
length=len(first_content),
overwrite=True,
)
raise AssertionError(
"Should raise error with wrong lease ID without lease"
)

second_content_bytes = _random_content().encode(client.encoding)
# Start lease
async with client.lease_blob(
blob=blob_name,
lease_duration=15, # 15 secs
) as lease_id:

# Check raise error on double lease with the first non expired
with pytest.raises(LeaseAlreadyExistsError):
async with client.lease_blob(
blob=blob_name,
lease_duration=15, # 15 secs
) as lease_id:
raise AssertionError(
"Should raise error on double lease with the first non expired"
)

second_content_bytes = _random_content().encode(client.encoding)

# Check raise error without lease ID
with pytest.raises(LeaseAlreadyExistsError):
await client.upload_blob(
blob=blob_name,
data=second_content_bytes,
length=len(second_content_bytes),
overwrite=True,
)
raise AssertionError("Should raise error without lease ID")

# Check raise error with wrong lease ID
with pytest.raises(LeaseAlreadyExistsError):
await client.upload_blob(
blob=blob_name,
data=second_content_bytes,
lease_id=str(uuid4()), # Wrong lease ID
length=len(second_content_bytes),
overwrite=True,
)
raise AssertionError("Should raise error with wrong lease ID")

# Check raise error without lease ID
with pytest.raises(LeaseAlreadyExistsError):
# Check upload with correct lease ID
await client.upload_blob(
blob=blob_name,
data=second_content_bytes,
lease_id=lease_id,
length=len(second_content_bytes),
overwrite=True,
)
raise AssertionError("Should raise error without lease ID")

# Check raise error with wrong lease ID
with pytest.raises(LeaseAlreadyExistsError):
third_content_bytes = _random_content().encode(client.encoding)

# Check raise error with wrong lease ID without lease
with pytest.raises(LeaseNotFoundError):
await client.upload_blob(
blob=blob_name,
data=second_content_bytes,
data=third_content_bytes,
lease_id=str(uuid4()), # Wrong lease ID
length=len(second_content_bytes),
length=len(third_content_bytes),
overwrite=True,
)
raise AssertionError("Should raise error with wrong lease ID")

# Check upload with correct lease ID
await client.upload_blob(
blob=blob_name,
data=second_content_bytes,
lease_id=lease_id,
length=len(second_content_bytes),
overwrite=True,
)

third_content_bytes = _random_content().encode(client.encoding)
raise AssertionError(
"Should raise error with wrong lease ID without lease"
)

# Check raise error with wrong lease ID without lease
with pytest.raises(LeaseNotFoundError):
# Check overwrite without lease ID
await client.upload_blob(
blob=blob_name,
data=third_content_bytes,
lease_id=str(uuid4()), # Wrong lease ID
length=len(third_content_bytes),
overwrite=True,
)
raise AssertionError("Should raise error with wrong lease ID without lease")

# Check overwrite without lease ID
await client.upload_blob(
blob=blob_name,
data=third_content_bytes,
length=len(third_content_bytes),
overwrite=True,
)

# Check open a new lease
async with client.lease_blob(
blob=blob_name,
lease_duration=15, # 15 secs
) as lease_id:
fourth_content_bytes = _random_content().encode(client.encoding)

# Check overwrite with correct lease ID
await client.upload_blob(

# Check open a new lease
async with client.lease_blob(
blob=blob_name,
data=fourth_content_bytes,
lease_id=lease_id,
length=len(fourth_content_bytes),
overwrite=True,
)
lease_duration=15, # 15 secs
) as lease_id:
fourth_content_bytes = _random_content().encode(client.encoding)

# Clean up
await client.delete_container()
# Check overwrite with correct lease ID
await client.upload_blob(
blob=blob_name,
data=fourth_content_bytes,
lease_id=lease_id,
length=len(fourth_content_bytes),
overwrite=True,
)

finally:
# Clean up
await client.delete_container()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -259,32 +267,33 @@ async def test_upload_many(provider: BlobProvider) -> None:
path="scraping-test",
provider=provider,
) as client:

# Upload blobs
tasks = [
client.upload_blob(
blob=name,
data=content.encode(client.encoding),
length=len(content.encode(client.encoding)),
overwrite=False,
)
for name, content in blobs
]
await asyncio.gather(*tasks)

# Check blobs content
async def _validate(
name: str,
original_content: str,
) -> None:
download_content = await client.download_blob(name)
assert download_content == original_content, "Content mismatch"

tasks = [_validate(name, content) for name, content in blobs]
await asyncio.gather(*tasks)

# Clean up
await client.delete_container()
try:
# Upload blobs
tasks = [
client.upload_blob(
blob=name,
data=content.encode(client.encoding),
length=len(content.encode(client.encoding)),
overwrite=False,
)
for name, content in blobs
]
await asyncio.gather(*tasks)

# Check blobs content
async def _validate(
name: str,
original_content: str,
) -> None:
download_content = await client.download_blob(name)
assert download_content == original_content, "Content mismatch"

tasks = [_validate(name, content) for name, content in blobs]
await asyncio.gather(*tasks)

finally:
# Clean up
await client.delete_container()


def _random_name() -> str:
Expand Down
Loading

0 comments on commit c275ed6

Please sign in to comment.