Skip to content

Commit da1ffda

Browse files
committed
Auto-generate additional tests
1 parent a3bba8d commit da1ffda

6 files changed

Lines changed: 195 additions & 1 deletion

File tree

apis/python/remote_tests/carrara/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export CARRARA_TEST_TEAMSPACE="..." # Teamspace to use for tests
2323

2424
Run the tests with pytest:
2525
```bash
26-
pytest api/python/remotes_tests/carrara/
26+
pytest api/python/remotes_tests/carrara/ --carrara -v
2727
```
2828

2929
Objects will be created at the specified workspace/teamspace: `tiledb://${CARRARA_TEST_WORKSPACE}/${CARRARA_TEST_TEAMSPACE}/...`. The test should remove all created objects when completed; however, assets may remain in the case of a test failure.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) TileDB, Inc.
2+
# Licensed under the MIT License.
3+
4+
from __future__ import annotations
5+
6+
import pathlib
7+
import urllib.parse
8+
from typing import Any
9+
10+
11+
def parse_tiledb_uri(uri: str) -> tuple[str, str, str | None]:
12+
"""Given a tiledb:// URI, return (workspace, teamspace, path)."""
13+
p_uri = urllib.parse.urlparse(uri)
14+
workspace = p_uri.netloc
15+
16+
p_path = pathlib.Path(p_uri.path)
17+
if len(p_path.parts) < 2:
18+
raise ValueError(f"Not a tiledb URI - missing teamspace: {uri}")
19+
teamspace = p_path.parts[1]
20+
21+
path = None if len(p_path.parts) == 2 else str(pathlib.PurePath(*p_path.parts[2:]))
22+
23+
return workspace, teamspace, path
24+
25+
26+
def get_asset_info(uri: str) -> dict[str, Any]:
27+
import tiledb.client
28+
29+
_, teamspace, path = parse_tiledb_uri(uri)
30+
return tiledb.client.assets.get_asset(path, teamspace=teamspace).to_dict()
31+
32+
33+
def s3_from_tiledb_uri(tiledb_uri: str) -> str:
34+
"""Given a tiledb:// URI, return the object's S3 URL.
35+
36+
This relies on the teamspace bucket being owned by the test user."""
37+
import tiledb.client
38+
39+
assert tiledb_uri.startswith("tiledb://")
40+
41+
_, teamspace, path = parse_tiledb_uri(tiledb_uri)
42+
asset_info = tiledb.client.assets.get_asset(path, teamspace=teamspace)
43+
s3_url = asset_info.uri
44+
assert s3_url.startswith("s3://")
45+
46+
return s3_url

apis/python/remote_tests/carrara/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@
2121
TEST_FOLDER = os.getenv("CARRARA_TEST_FOLDER") or "remote_test"
2222
BASE_URI = f"tiledb://{WORKSPACE_NAME}/{TEAMSPACE_NAME}/{TEST_FOLDER}"
2323

24+
TESTS_INPUT_DIR = os.path.abspath(
25+
os.path.join(os.path.dirname(__file__), "../../../../libtiledbvcf/test/inputs")
26+
)
27+
28+
29+
def pytest_addoption(parser):
30+
parser.addoption(
31+
"--carrara", action="store_true", default=False, help="run Carrara tests"
32+
)
33+
34+
35+
def pytest_collection_modifyitems(config, items):
36+
if config.getoption("--carrara"):
37+
return
38+
skip_carrara = pytest.mark.skip(reason="need --carrara option to run")
39+
for item in items:
40+
if "carrara" in item.keywords:
41+
item.add_marker(skip_carrara)
42+
2443

2544
@pytest.fixture(scope="session")
2645
def carrara_login() -> None:
@@ -54,3 +73,9 @@ def carrara_group_path() -> Generator[str, None, None]:
5473
pass
5574

5675

76+
@pytest.fixture(scope="session")
77+
def vcf_sample_uris() -> list[str]:
78+
"""Returns paths to local VCF/BCF test files used for ingestion tests."""
79+
return [os.path.join(TESTS_INPUT_DIR, s) for s in ["small.bcf", "small2.bcf"]]
80+
81+

apis/python/remote_tests/carrara/test_01_setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
from __future__ import annotations
99

10+
import pytest
1011
import tiledbvcf
1112

13+
14+
@pytest.mark.carrara
1215
def test_dataset_create(carrara_login: None, carrara_group_path: str):
1316
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
1417
ds.create_dataset()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) TileDB, Inc.
2+
# Licensed under the MIT License.
3+
"""
4+
VCF sample ingestion with Carrara URIs.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import pytest
10+
import tiledbvcf
11+
12+
13+
@pytest.mark.carrara
14+
def test_ingest_samples(
15+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
16+
) -> None:
17+
"""Ingest two BCF samples into a remote Carrara dataset and verify total count."""
18+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
19+
ds.create_dataset()
20+
ds.ingest_samples(vcf_sample_uris)
21+
22+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
23+
assert ds.count() == 14
24+
25+
26+
@pytest.mark.carrara
27+
def test_ingest_and_region_query(
28+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
29+
) -> None:
30+
"""Ingest samples and verify a region-filtered count."""
31+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
32+
ds.create_dataset()
33+
ds.ingest_samples(vcf_sample_uris)
34+
35+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
36+
assert ds.count(regions=["1:12700-13400"]) == 6
37+
38+
39+
@pytest.mark.carrara
40+
def test_ingest_and_sample_query(
41+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
42+
) -> None:
43+
"""Ingest samples and verify a sample- and region-filtered count."""
44+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
45+
ds.create_dataset()
46+
ds.ingest_samples(vcf_sample_uris)
47+
48+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
49+
assert ds.count(samples=["HG00280"], regions=["1:12700-13400"]) == 4
50+
51+
52+
@pytest.mark.carrara
53+
def test_ingest_incremental(
54+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
55+
) -> None:
56+
"""Ingest one sample, verify count, then add a second sample and re-verify."""
57+
first, second = vcf_sample_uris[0], vcf_sample_uris[1]
58+
59+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
60+
ds.create_dataset()
61+
ds.ingest_samples([first])
62+
63+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
64+
count_after_first = ds.count()
65+
assert count_after_first > 0
66+
67+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
68+
ds.ingest_samples([second])
69+
70+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
71+
assert ds.count() == 14
72+
assert ds.count() > count_after_first
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) TileDB, Inc.
2+
# Licensed under the MIT License.
3+
"""
4+
Dataset and sample deletion with Carrara URIs.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import pytest
10+
import tiledb
11+
import tiledbvcf
12+
13+
14+
@pytest.mark.carrara
15+
def test_delete_dataset(
16+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
17+
) -> None:
18+
"""Create and ingest a dataset, then delete it and verify it no longer exists."""
19+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
20+
ds.create_dataset()
21+
ds.ingest_samples(vcf_sample_uris)
22+
23+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
24+
assert ds.count() == 14
25+
26+
with tiledb.Group(carrara_group_path, mode="m") as G:
27+
G.delete(recursive=True)
28+
29+
assert tiledb.object_type(carrara_group_path) is None
30+
31+
32+
@pytest.mark.carrara
33+
def test_delete_samples(
34+
carrara_login: None, carrara_group_path: str, vcf_sample_uris: list[str]
35+
) -> None:
36+
"""Ingest two samples, delete one, and verify only the other remains."""
37+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
38+
ds.create_dataset()
39+
ds.ingest_samples(vcf_sample_uris)
40+
41+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
42+
assert sorted(ds.samples()) == ["HG00280", "HG01762"]
43+
44+
with tiledbvcf.Dataset(carrara_group_path, mode="w") as ds:
45+
ds.delete_samples(["HG00280"])
46+
47+
with tiledbvcf.Dataset(carrara_group_path, mode="r") as ds:
48+
assert ds.samples() == ["HG01762"]

0 commit comments

Comments
 (0)