Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions libraries/botbuilder-azure/botbuilder/azure/blob_storage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
from typing import Dict, List

from jsonpickle import encode
from jsonpickle.unpickler import Unpickler

from azure.storage.blob import BlockBlobService, Blob, PublicAccess
from botbuilder.core import Storage, StoreItem
from botbuilder.core import Storage

# TODO: sanitize_blob_name

Expand Down Expand Up @@ -59,7 +62,7 @@ async def read(self, keys: List[str]) -> Dict[str, object]:

return items

async def write(self, changes: Dict[str, StoreItem]):
async def write(self, changes: Dict[str, object]):
self.client.create_container(self.settings.container_name)
self.client.set_container_acl(
self.settings.container_name, public_access=PublicAccess.Container
Expand All @@ -71,10 +74,11 @@ async def write(self, changes: Dict[str, StoreItem]):
)
if e_tag:
item.e_tag = e_tag.replace('"', '\\"')
item_str = self._store_item_to_str(item)
self.client.create_blob_from_text(
container_name=self.settings.container_name,
blob_name=name,
text=str(item),
text=item_str,
if_match=e_tag,
)

Expand All @@ -95,8 +99,12 @@ async def delete(self, keys: List[str]):
container_name=self.settings.container_name, blob_name=key
)

def _blob_to_store_item(self, blob: Blob) -> StoreItem:
def _blob_to_store_item(self, blob: Blob) -> object:
item = json.loads(blob.content)
item["e_tag"] = blob.properties.etag
item["id"] = blob.name
return StoreItem(**item)
result = Unpickler().restore(item)
return result

def _store_item_to_str(self, item: object) -> str:
return encode(item)
14 changes: 7 additions & 7 deletions libraries/botbuilder-azure/tests/test_blob_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def test_blob_storage_read_should_return_data_with_valid_key(self):

data = await storage.read(["user"])
assert "user" in data
assert data["user"].counter == "1"
assert data["user"].counter == 1
assert len(data.keys()) == 1

@pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.")
Expand All @@ -57,7 +57,7 @@ async def test_blob_storage_read_update_should_return_new_etag(self):
data_result["test"].counter = 2
await storage.write(data_result)
data_updated = await storage.read(["test"])
assert data_updated["test"].counter == "2"
assert data_updated["test"].counter == 2
assert data_updated["test"].e_tag != data_result["test"].e_tag

@pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.")
Expand All @@ -77,7 +77,7 @@ async def test_blob_storage_write_should_add_new_value(self):

data = await storage.read(["user"])
assert "user" in data
assert data["user"].counter == "1"
assert data["user"].counter == 1

@pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.")
@pytest.mark.asyncio
Expand All @@ -89,7 +89,7 @@ async def test_blob_storage_write_should_overwrite_when_new_e_tag_is_an_asterisk

await storage.write({"user": SimpleStoreItem(counter=10, e_tag="*")})
data = await storage.read(["user"])
assert data["user"].counter == "10"
assert data["user"].counter == 10

@pytest.mark.skipif(not EMULATOR_RUNNING, reason="Needs the emulator to run.")
@pytest.mark.asyncio
Expand All @@ -107,9 +107,9 @@ async def test_blob_storage_write_batch_operation(self):
assert data["batch1"]
assert data["batch2"]
assert data["batch3"]
assert data["batch1"].counter == "1"
assert data["batch2"].counter == "1"
assert data["batch3"].counter == "1"
assert data["batch1"].counter == 1
assert data["batch2"].counter == 1
assert data["batch3"].counter == 1
assert data["batch1"].e_tag
assert data["batch2"].e_tag
assert data["batch3"].e_tag
Expand Down