Skip to content

Commit

Permalink
Fix blob unit tests for Windows (#12483)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: b6042e54d5635321ca3562efc64dd65809d83f47
  • Loading branch information
jjmckee authored and Descartes Labs Build committed Mar 11, 2024
1 parent 7fa2bb9 commit af8fcfd
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions descarteslabs/core/catalog/tests/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# -*- coding: utf-8 -*-
import copy
import json
import os
import pytest
import responses

Expand Down Expand Up @@ -781,32 +782,45 @@ def test_download(self):
client=self.client,
)

with NamedTemporaryFile(delete=True) as temp:
result = b.download(temp.name)
assert result == temp.name
with NamedTemporaryFile(delete=False) as f1:
with NamedTemporaryFile(delete=False) as f2:
try:
f1.close()
f2.close()

with open(temp.name, "r") as handle:
line = handle.readlines()[0]
result = b.download(f1.name)

assert line == "This is mock download data. It can be any binary data."
assert result == f1.name

with NamedTemporaryFile(delete=True) as temp:
with open(temp.name, "wb") as temp:
result = b.download(temp)
with open(f1.name, "r") as handle:
line = handle.readlines()[0]

assert result == temp.name
assert (
line == "This is mock download data. It can be any binary data."
)

with open(temp.name, "r") as handle:
line = handle.readlines()[0]
with open(f2.name, "wb") as temp:
result = b.download(temp)

assert line == "This is mock download data. It can be any binary data."
assert result == f2.name

with pytest.raises(ValueError):
b.download(1)
with open(f2.name, "r") as handle:
line = handle.readlines()[0]

b._saved = False
with pytest.raises(ValueError):
b.download("wrong")
assert (
line == "This is mock download data. It can be any binary data."
)

with pytest.raises(ValueError):
b.download(1)

b._saved = False
with pytest.raises(ValueError):
b.download("wrong")

finally:
os.unlink(f1.name)
os.unlink(f2.name)

@patch.object(Blob, "namespace_id", _namespace_id)
def test_invalid_upload_data(self):
Expand Down Expand Up @@ -845,12 +859,19 @@ def test_invalid_upload(self):
client=self.client,
)

with pytest.raises(ValueError):
with NamedTemporaryFile(delete=True) as temp:
_ = b.upload(temp.name)
with NamedTemporaryFile(delete=False) as f1:
try:
f1.close()

b.name = None
b._save = False
with pytest.raises(ValueError):
with NamedTemporaryFile(delete=True) as temp:
_ = b.upload(temp)
with pytest.raises(ValueError):
_ = b.upload(f1.name)

b.name = None
b._save = False

with pytest.raises(ValueError):
with open(f1.name, "wb") as temp:
_ = b.upload(temp)

finally:
os.unlink(f1.name)

0 comments on commit af8fcfd

Please sign in to comment.