Skip to content

Commit

Permalink
Rename asset functions, export from tiledb.cloud. (#567)
Browse files Browse the repository at this point in the history
* Rename asset functions, export from tiledb.cloud.

* Update change log

* Export asset (and array) from tiledb.cloud

Function names are restored to delete() and info().
  • Loading branch information
sgillies authored Jun 3, 2024
1 parent 7980cb2 commit 95a523e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
## Next (YYYY-MM-DD)

- Add a tiledb.cloud.asset module with functions that allow management of
assets of any type (gh-566).
assets of any type. This module is exported from tiledb.cloud (gh-566).
4 changes: 4 additions & 0 deletions src/tiledb/cloud/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file imports specifically to re-export.

from . import array
from . import asset
from . import compute
from . import dag
from . import file
Expand Down Expand Up @@ -50,6 +52,8 @@
UDFResultType = ResultFormat

__all__ = (
"array",
"asset",
"compute",
"dag",
"file",
Expand Down
14 changes: 7 additions & 7 deletions src/tiledb/cloud/asset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""An asset may be an array or a group."""

from functools import partial
from typing import Union
from typing import Callable, Mapping, Union

import tiledb # type: ignore

Expand All @@ -14,27 +14,27 @@
def delete(uri: str, recursive: bool = False) -> None:
"""Deregister the asset and remove its physical groups and arrays from storage.
:param uri: URI of the asset.
:param uri: tiledb URI of the asset.
:return: None.
"""
delete_map = {
delete_map: Mapping[str, Callable] = {
"array": array.delete_array,
"group": partial(groups.delete, recursive=recursive),
}
asset_type = tiledb.object_type(uri, ctx=tiledb.cloud.Ctx())
asset_type: str = tiledb.object_type(uri, ctx=tiledb.cloud.Ctx())
func = delete_map[asset_type]
return func(uri)


def info(uri: str) -> Union[ArrayInfo, GroupInfo]:
"""Retrieve information about an asset.
:param uri: URI of the asset.
:param uri: tiledb URI of the asset.
:return: ArrayInfo or GroupInfo.
"""
# Note: the URI can be either of the two forms, yes?
# tiledb://namespace/name or tiledb://namespace/UUID.
info_map = {"array": array.info, "group": groups.info}
asset_type = tiledb.object_type(uri, ctx=tiledb.cloud.Ctx())
info_map: Mapping[str, Callable] = {"array": array.info, "group": groups.info}
asset_type: str = tiledb.object_type(uri, ctx=tiledb.cloud.Ctx())
func = info_map[asset_type]
return func(uri)
35 changes: 16 additions & 19 deletions tests/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from unittest import mock

from tiledb.cloud.asset import delete # type: ignore
from tiledb.cloud.asset import info # type: ignore
from tiledb.cloud import asset # type: ignore
from tiledb.cloud.rest_api.models import ArrayInfo # type: ignore
from tiledb.cloud.rest_api.models import GroupInfo # type: ignore

Expand All @@ -12,55 +11,53 @@
@mock.patch("tiledb.cloud.array.delete_array")
def test_asset_delete_array_dispatch(delete_array, object_type):
"""Dispatch to array.array_delete when URI is an array."""
delete("a")
# Since delete() doesn't return a value, we assert on the implementation.
asset.delete("a")
# Since asset.delete() doesn't return a value, we assert on the implementation.
delete_array.assert_called_once_with("a")


@mock.patch("tiledb.object_type", return_value="group")
@mock.patch("tiledb.cloud.groups.delete")
def test_asset_delete_group_dispatch(delete_group, object_type):
"""Dispatch to groups.delete when URI is a group."""
delete("g")
# Since delete() doesn't return a value, we assert on the implementation.
asset.delete("g")
delete_group.assert_called_once_with("g", recursive=False)


@mock.patch("tiledb.object_type", return_value="group")
@mock.patch("tiledb.cloud.groups.delete")
def test_asset_delete_group_recursive(delete_group, object_type):
"""Dispatch to groups.delete, recursively, when URI is a group."""
delete("g", recursive=True)
# Since delete() doesn't return a value, we assert on the implementation.
asset.delete("g", recursive=True)
delete_group.assert_called_once_with("g", recursive=True)


@mock.patch("tiledb.object_type", return_value="array")
@mock.patch("tiledb.cloud.array.info", return_value=ArrayInfo(tiledb_uri="tiledb://a"))
def test_asset_info_array_dispatch(array_info, object_type):
"""Dispatch to array.info when URI is an array."""
asset_info = info("a")
assert isinstance(asset_info, ArrayInfo)
assert asset_info.tiledb_uri == "tiledb://a"
info = asset.info("a")
assert isinstance(info, ArrayInfo)
assert info.tiledb_uri == "tiledb://a"


@mock.patch("tiledb.object_type", return_value="group")
@mock.patch("tiledb.cloud.groups.info", return_value=GroupInfo(tiledb_uri="tiledb://g"))
def test_asset_info_group_dispatch(group_info, object_type):
"""Dispatch to groups.info when URI is a group."""
asset_info = info("g")
assert isinstance(asset_info, GroupInfo)
assert asset_info.tiledb_uri == "tiledb://g"
info = asset.info("g")
assert isinstance(info, GroupInfo)
assert info.tiledb_uri == "tiledb://g"


def test_public_array_asset_info():
"""Get info about a public production array."""
asset_info = info("tiledb://TileDB-Inc/cd89a0d6-c262-4729-9e75-d942879e1d7d")
assert asset_info.name == "documents"
assert asset_info.type == "sparse"
info = asset.info("tiledb://TileDB-Inc/cd89a0d6-c262-4729-9e75-d942879e1d7d")
assert info.name == "documents"
assert info.type == "sparse"


def test_public_group_asset_info():
"""Get info about a public production group."""
asset_info = info("tiledb://TileDB-Inc/52165567-040c-4e75-bb89-a3d06017f650")
assert asset_info.name == "langchain_documentation_huggingface"
info = asset.info("tiledb://TileDB-Inc/52165567-040c-4e75-bb89-a3d06017f650")
assert info.name == "langchain_documentation_huggingface"

0 comments on commit 95a523e

Please sign in to comment.