Skip to content

Commit

Permalink
adds support for metadata during object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
CNDW committed Sep 13, 2024
1 parent 0d623b8 commit a9e2f4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/gcp_storage_emulator/handlers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def _create_resumable_upload(request, response, storage):
object_id,
content_type,
content_length,
metadata={**(request.data or {}), "name": object_id},
)
id = storage.create_resumable_upload(
request.params["bucket_name"],
Expand Down
43 changes: 24 additions & 19 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from gcp_storage_emulator.server import create_server
from gcp_storage_emulator.settings import STORAGE_BASE, STORAGE_DIR


TEST_TEXT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_text.txt")


Expand Down Expand Up @@ -162,9 +161,7 @@ def tearDown(self):
return super().tearDown()

def test_bucket_created(self):
self._server = create_server(
"localhost", 9023, in_memory=True, default_bucket="example.appspot.com"
)
self._server = create_server("localhost", 9023, in_memory=True, default_bucket="example.appspot.com")
self._server.start()
self._session = requests.Session()
self._client = _get_storage_client(self._session)
Expand Down Expand Up @@ -198,9 +195,7 @@ def test_upload_from_text_file(self):
self.assertEqual(read_content, expected_content)

def test_upload_from_bin_file(self):
test_binary = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "test_binary.png"
)
test_binary = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_binary.png")
bucket = self._client.create_bucket("testbucket")
blob = bucket.blob("binary.png")
with open(test_binary, "rb") as file:
Expand All @@ -227,9 +222,7 @@ def test_upload_from_bin_file_cr_lf(self):
self.assertEqual(read_content, content)

def test_upload_from_file_name(self):
test_binary = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "test_binary.png"
)
test_binary = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_binary.png")
file_name = "test_binary.png"

bucket = self._client.create_bucket("testbucket")
Expand All @@ -242,9 +235,7 @@ def test_upload_from_file_name(self):
self.assertEqual(temp_file.read(), orig_file.read())

def test_upload_from_file(self):
test_binary = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "test_binary.png"
)
test_binary = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_binary.png")
file_name = "test_binary.png"

bucket = self._client.create_bucket("testbucket")
Expand Down Expand Up @@ -463,9 +454,7 @@ def test_invalid_crc32c_hash(self):
blob.upload_from_string(content)

def test_download_binary_to_file(self):
test_binary = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "test_binary.png"
)
test_binary = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_binary.png")
bucket = self._client.create_bucket("testbucket")

blob = bucket.blob("binary.png")
Expand Down Expand Up @@ -832,9 +821,7 @@ def test_signed_url_download_with_content_disposition(self):
response = requests.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content)
self.assertEqual(
response.headers["content-disposition"], f"{response_disposition}"
)
self.assertEqual(response.headers["content-disposition"], f"{response_disposition}")
self.assertEqual(response.headers["content-type"], "text/mycustom")

def test_url_generation_for_browser(self):
Expand Down Expand Up @@ -944,6 +931,24 @@ def test_upload_from_file_content_type_json(self):
self.assertEqual(blob.name, file_name)
self.assertEqual(blob.download_as_bytes(), content)

def test_upload_from_file_with_metadata(self):
file_name = "test.json"
content = b'[{"a": 1}]'
bucket = self._client.create_bucket("testbucket")
blob = bucket.blob(file_name)
blob.metadata = {"foo": "bar"}

with NamedTemporaryFile() as temp_file:
temp_file.write(content)
temp_file.flush()
temp_file.seek(0)
blob.upload_from_file(temp_file, content_type="application/json")

blob = bucket.get_blob(file_name)
self.assertEqual(blob.name, file_name)
self.assertEqual(blob.download_as_bytes(), content)
self.assertEqual(blob.metadata, {"foo": "bar"})


class HttpEndpointsTest(ServerBaseCase):
"""Tests for the HTTP endpoints defined by server.HANDLERS."""
Expand Down

0 comments on commit a9e2f4a

Please sign in to comment.