From 581271ffef700b536117592186ab68e0ae1cf621 Mon Sep 17 00:00:00 2001 From: Sergei Rybakov Date: Mon, 23 Dec 2024 17:40:42 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20init=20instance=20tests=20?= =?UTF-8?q?and=20add=20init=20of=20a=20local=20instance=20in=20cwd=20(#917?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- lamindb_setup/_init_instance.py | 2 +- lamindb_setup/core/_hub_crud.py | 19 ++++----- lamindb_setup/core/_settings.py | 2 +- lamindb_setup/core/_settings_instance.py | 2 +- tests/hub-cloud/test_init_instance.py | 49 ++++++++++++++++++------ 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/lamindb_setup/_init_instance.py b/lamindb_setup/_init_instance.py index 38ce045c..1eda955b 100644 --- a/lamindb_setup/_init_instance.py +++ b/lamindb_setup/_init_instance.py @@ -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 diff --git a/lamindb_setup/core/_hub_crud.py b/lamindb_setup/core/_hub_crud.py index 9d3291c1..3fe2fcc0 100644 --- a/lamindb_setup/core/_hub_crud.py +++ b/lamindb_setup/core/_hub_crud.py @@ -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] diff --git a/lamindb_setup/core/_settings.py b/lamindb_setup/core/_settings.py index a04728e9..cad3617c 100644 --- a/lamindb_setup/core/_settings.py +++ b/lamindb_setup/core/_settings.py @@ -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: diff --git a/lamindb_setup/core/_settings_instance.py b/lamindb_setup/core/_settings_instance.py index e93c240e..ed9b352a 100644 --- a/lamindb_setup/core/_settings_instance.py +++ b/lamindb_setup/core/_settings_instance.py @@ -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 diff --git a/tests/hub-cloud/test_init_instance.py b/tests/hub-cloud/test_init_instance.py index b5bc3e9f..fe0bc415 100644 --- a/tests/hub-cloud/test_init_instance.py +++ b/tests/hub-cloud/test_init_instance.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os from pathlib import Path from uuid import UUID @@ -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, ) @@ -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 @@ -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, ) @@ -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():