Skip to content

Commit

Permalink
Unit tests for install_wheel (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerseale authored Aug 9, 2024
1 parent cf9b4d0 commit fb6bc2f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/tiledb/cloud/utilities/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any, List, Mapping, Optional, Sequence

import tiledb
from tiledb.cloud.client import Config
from tiledb.cloud.utilities import get_logger

logger = get_logger()
Expand Down Expand Up @@ -47,6 +48,8 @@ def upload_wheel(
:param wheel_path: Path to the local wheel file.
:param dest_uri: URI where the wheel filestore will be created or updated.
Ensure wheel file includes extension '.whl' or '.wheel' for it to work
with `install_wheel`.
:param config: TileDB config.
:param overwrite: Whether to overwrite a registered wheel if one exists.
"""
Expand All @@ -55,7 +58,7 @@ def upload_wheel(
# When installing the wheel, the original wheel file name will be recovered
# from metadata.
dest_uri = dest_uri.replace("+", ".")

config = config or Config()
with tiledb.scope_ctx(config):
# catch edge case if array registered, but array deleted from storage backend
try:
Expand Down Expand Up @@ -215,6 +218,7 @@ def install_wheel(
)

if installer.wheel_ext:
config = config or Config()
with tiledb.scope_ctx(config):
# Get the original wheel file name from metadata.
with tiledb.open(wheel_uri) as A:
Expand Down
Empty file added tests/utilities/__init__.py
Empty file.
Binary file not shown.
162 changes: 162 additions & 0 deletions tests/utilities/test_wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import os
import sys

import pytest

import tiledb
import tiledb.cloud
from tiledb.cloud.utilities import get_logger
from tiledb.cloud.utilities import install_wheel
from tiledb.cloud.utilities import upload_wheel
from tiledb.cloud.utilities.wheel import PipInstall

logger = get_logger()

_LOCAL_WHEEL = "tests/utilities/data/fake_unittest_wheel-0.1.0-py3-none-any.whl"
_ARRAY_NAME = os.path.basename(_LOCAL_WHEEL)
_NAMESPACE = tiledb.cloud.client.default_user().username
_S3_OBJECT_PATH = tiledb.cloud.client.default_user().default_s3_path
_FULL_URI = os.path.join(
"tiledb://",
_NAMESPACE,
_S3_OBJECT_PATH,
_ARRAY_NAME,
)
_TDB_URI = os.path.join("tiledb://", _NAMESPACE, _ARRAY_NAME)
_CONFIG = tiledb.cloud.Config()


@pytest.fixture
def array_teardown():
"""Handle setup and teardown of arrays.
Ensures array doesn't exist and deletes if it does.
Deletes array after tests complete.
"""

ctx = tiledb.Ctx(_CONFIG)

try:
tiledb.Array.delete_array(_FULL_URI, ctx=ctx)
except tiledb.TileDBError:
pass

yield None

tiledb.Array.delete_array(_FULL_URI, ctx=ctx)


def test_upload_wheel(array_teardown) -> None:
# first time uploading array
upload_wheel(
wheel_path=_LOCAL_WHEEL,
dest_uri=_FULL_URI,
overwrite=True,
config=_CONFIG,
)

info = tiledb.cloud.info(os.path.join("tiledb://", _NAMESPACE, _ARRAY_NAME))

assert info.name == _ARRAY_NAME
assert info.namespace == _NAMESPACE
assert info.uri == os.path.join(_S3_OBJECT_PATH, _ARRAY_NAME)

with pytest.raises(ValueError):
upload_wheel(
wheel_path=_LOCAL_WHEEL,
dest_uri=_FULL_URI,
overwrite=False,
config=_CONFIG,
)

upload_wheel(
wheel_path=_LOCAL_WHEEL,
dest_uri=_FULL_URI,
overwrite=True,
config=_CONFIG,
)


@pytest.fixture
def pip_install_tdb() -> PipInstall:
"""Creates a PipInstall object for tests.
Uploads a wheel for it to install.
Deleteds wheel array during teardown.
"""

# upload a wheel
upload_wheel(
wheel_path=_LOCAL_WHEEL,
dest_uri=_FULL_URI,
overwrite=True,
config=_CONFIG,
)

pi = PipInstall(
wheel=_TDB_URI,
in_venv=True,
)

yield pi

ctx = tiledb.Ctx(_CONFIG)
tiledb.Array.delete_array(_FULL_URI, ctx=ctx)


def test_pip_install_wheel_ext(pip_install_tdb):
assert pip_install_tdb.wheel_ext

pip_install_tdb.wheel = "django"

assert not pip_install_tdb.wheel_ext


def test_pip_install_rm_from_cache():
dep = "os"
observed = PipInstall.rm_from_cache(dep)

assert len(observed) == 1
assert dep in observed

# already removed
observed2 = PipInstall.rm_from_cache([dep])
assert not observed2

dep2 = [dep, "shutil"]
observed3 = PipInstall.rm_from_cache(dep2)
assert len(observed3) == 1
assert dep2[1] in observed3


@pytest.fixture
def pip_install_pypi() -> PipInstall:
pi = PipInstall(wheel="django", in_venv=True)

yield pi


def test_pip_install_install(pip_install_pypi):
# will always run in github actions since django not included
try:
import django # noqa: F401

logger.info("django already imported, either manually delete or ignore")
except ModuleNotFoundError:
pip_install_pypi.install()
import django # noqa: F401

assert "django" in sys.modules
pip_install_pypi.install(deps_to_refresh="django")
assert "django" not in sys.modules


def test_install_wheel(pip_install_tdb):
# install from tiledb cloud array
assert "fake_unittest_wheel" not in sys.modules

install_wheel(wheel_uri=_TDB_URI, verbose=True)

import fake_unittest_wheel # noqa: F401

assert "fake_unittest_wheel" in sys.modules

0 comments on commit fb6bc2f

Please sign in to comment.