Skip to content

Commit

Permalink
🐛 Fix init instance tests and add init of a local instance in cwd (#917)
Browse files Browse the repository at this point in the history
* fix init local instance in cwd

* try properly deleting cloud instances in test_init

* fix

* test init in cwd

* fix

* fix

* test double init

* test default storage
  • Loading branch information
Koncopd authored Dec 23, 2024
1 parent 0a1fddc commit 581271f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lamindb_setup/_init_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def infer_instance_name(
return str(db).split("/")[-1]
if storage == "create-s3":
raise ValueError("pass name to init if storage = 'create-s3'")
storage_path = UPath(storage)
storage_path = UPath(storage).resolve()
# not sure if name is ever ""
if storage_path.name != "":
name = storage_path.name
Expand Down
19 changes: 8 additions & 11 deletions lamindb_setup/core/_hub_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,14 @@ def select_collaborator(
def select_default_storage_by_instance_id(
instance_id: str, client: Client
) -> dict | None:
try:
data = (
client.table("storage")
.select("*")
.eq("instance_id", instance_id)
.eq("is_default", True)
.execute()
.data
)
except Exception:
return None
data = (
client.table("storage")
.select("*")
.eq("instance_id", instance_id)
.eq("is_default", True)
.execute()
.data
)
if len(data) == 0:
return None
return data[0]
Expand Down
2 changes: 1 addition & 1 deletion lamindb_setup/core/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __repr__(self) -> str:
repr = self.user.__repr__()
repr += f"\nAuto-connect in Python: {self.auto_connect}\n"
repr += f"Private Django API: {self.private_django_api}\n"
repr += f"Cache directory: {self.cache_dir}\n"
repr += f"Cache directory: {self.cache_dir.as_posix()}\n"
if self._instance_exists:
repr += self.instance.__repr__()
else:
Expand Down
2 changes: 1 addition & 1 deletion lamindb_setup/core/_settings_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def db(self) -> str:
sqlite_filepath = self.storage.cloud_to_local(
self._sqlite_file, error_no_origin=False
)
return f"sqlite:///{sqlite_filepath}"
return f"sqlite:///{sqlite_filepath.as_posix()}"
else:
return self._db

Expand Down
49 changes: 37 additions & 12 deletions tests/hub-cloud/test_init_instance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from pathlib import Path
from uuid import UUID

Expand All @@ -11,6 +12,7 @@
from lamindb_setup.core._hub_crud import (
Client,
select_account_by_handle,
select_default_storage_by_instance_id,
select_instance_by_name,
)

Expand Down Expand Up @@ -82,8 +84,30 @@ def test_init_instance_postgres_custom_name():
ln_setup.delete("mydata2", force=True)


def test_init_instance_cwd():
# can't make it via fixture because need to chnage dir back before ln_setup.delete
prev_wd = Path.cwd()
storage = Path("./mystorage_cwd")
storage.mkdir()
storage = storage.resolve()
os.chdir(storage)
assert Path.cwd() == storage
ln_setup.init(storage=".", _test=True)
assert ln_setup.settings.instance.name == "mystorage_cwd"
assert not ln_setup.settings.instance.storage.type_is_cloud
assert ln_setup.settings.instance.storage.root.as_posix() == Path.cwd().as_posix()
os.chdir(prev_wd)
ln_setup.delete("mystorage_cwd", force=True)


def test_init_instance_cloud_aws_us():
ln_setup.init(storage="s3://lamindb-ci/init_instance_cloud_aws_us", _test=True)
storage = (
f"s3://lamindb-ci/{os.environ['LAMIN_ENV']}_test/init_instance_cloud_aws_us"
)
ln_setup.init(storage=storage, _test=True)
# run for the second time
# just loads an already existing instance
ln_setup.init(storage=storage, _test=True)
hub = connect_hub_with_auth()
account = select_account_by_handle(
handle=ln_setup.settings.instance.owner, client=hub
Expand All @@ -93,27 +117,27 @@ def test_init_instance_cloud_aws_us():
name=ln_setup.settings.instance.name,
client=hub,
)
# test default storage record is correct
storage_record = select_default_storage_by_instance_id(instance["id"], hub)
assert storage_record["root"] == storage
# test instance settings
assert ln_setup.settings.instance._id == UUID(instance["id"])
assert ln_setup.settings.storage.type_is_cloud
assert (
str(ln_setup.settings.storage.root)
== "s3://lamindb-ci/init_instance_cloud_aws_us"
)
assert (
ln_setup.settings.storage.root_as_str
== "s3://lamindb-ci/init_instance_cloud_aws_us"
)
assert str(ln_setup.settings.storage.root) == storage
assert ln_setup.settings.storage.root_as_str == storage
assert ln_setup.settings.storage.region == "us-west-1"
assert (
str(ln_setup.settings.instance._sqlite_file)
== f"s3://lamindb-ci/init_instance_cloud_aws_us/{ln_setup.settings.instance._id.hex}.lndb"
== f"{storage}/{ln_setup.settings.instance._id.hex}.lndb"
)
ln_setup.delete("init_instance_cloud_aws_us", force=True)


def test_init_instance_cloud_aws_europe():
# do the same for an S3 bucket in Europe
storage = f"s3://lndb-setup-ci-eu-central-1/{os.environ['LAMIN_ENV']}_test"
ln_setup.init(
storage="s3://lndb-setup-ci-eu-central-1",
storage=storage,
name="lamindb-ci-europe",
_test=True,
)
Expand All @@ -122,8 +146,9 @@ def test_init_instance_cloud_aws_europe():
assert ln_setup.settings.instance.name == "lamindb-ci-europe"
assert (
str(ln_setup.settings.instance._sqlite_file)
== f"s3://lndb-setup-ci-eu-central-1/{ln_setup.settings.instance._id.hex}.lndb"
== f"{storage}/{ln_setup.settings.instance._id.hex}.lndb"
)
ln_setup.delete("lamindb-ci-europe", force=True)


def test_init_instance_sqlite():
Expand Down

0 comments on commit 581271f

Please sign in to comment.