From 85f4a5d4e2e33686d92fcd58cbc08b288c4e9ad3 Mon Sep 17 00:00:00 2001 From: Vivian Nguyen Date: Mon, 25 Nov 2024 13:30:02 -0800 Subject: [PATCH] Add unit tests --- apis/python/tests/test_context.py | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apis/python/tests/test_context.py b/apis/python/tests/test_context.py index 14d474abe5..65aeb7835e 100644 --- a/apis/python/tests/test_context.py +++ b/apis/python/tests/test_context.py @@ -1,6 +1,8 @@ import datetime import time from unittest import mock +import importlib +import typeguard import pytest @@ -122,3 +124,38 @@ def test_malformed_concurrency_config_value(): tiledbsoma.IntIndexer(np.arange(100, dtype=np.int64), context=ctx).get_indexer( np.array([0, 1]) ) + +@pytest.mark.skipif( + importlib.util.find_spec("tiledb") is not None, reason="TileDB-Py is installed" +) +def test_tiledb_ctx_without_tiledb(): + # Test that tiledb_ctx errors out as expected without tiledb-py + + with pytest.raises(ModuleNotFoundError): + tiledbsoma.SOMATileDBContext(tiledb_ctx="junk") + + sctx = tiledbsoma.SOMATileDBContext() + with pytest.raises(ModuleNotFoundError): + sctx.tiledb_ctx + + with pytest.raises(ModuleNotFoundError): + sctx.replace(tiledb_ctx="junk") + +@pytest.mark.skipif( + importlib.util.find_spec("tiledb") is None, reason="TileDB-Py is not installed" +) +def test_tiledb_ctx_with_tiledb(): + # If tiledb-py is installed, test that tiledb_ctx works to handle tiledb.Ctx + import tiledb + + # Default + sctx = tiledbsoma.SOMATileDBContext(tiledb_ctx=tiledb.Ctx()) + assert sctx.tiledb_ctx.config() == tiledb.Ctx().config() + + # Pass config + sctx = tiledbsoma.SOMATileDBContext(tiledb_ctx=tiledb.Ctx({"foo": "bar"})) + assert sctx.tiledb_ctx.config()["foo"] == "bar" + + # Replace config + sctx = sctx.replace(tiledb_ctx=tiledb.Ctx({"foo": "baz"})) + assert sctx.tiledb_ctx.config()["foo"] == "baz"