Connect with keep_artifacts_local=True

# this shouldn't run in a notebook, but in the unit tests
# currently still limited because of inability to load multiple instances

import os
import pytest
import lamindb_setup as ln_setup
from upath import UPath

name = f"keep-artifacts-local-setup-{os.environ['LAMIN_ENV']}"
storage = (UPath("s3://lamindb-ci") / name).as_posix()

ln_setup.login("testuser1")
ln_setup.init(storage=storage)

assert ln_setup.settings.instance.name == name
assert ln_setup.settings.instance.storage.type_is_cloud
assert ln_setup.settings.instance.storage.root_as_str == storage
assert (
    ln_setup.settings.instance._sqlite_file.as_posix()
    == f"{storage}/{ln_setup.settings.instance._id.hex}.lndb"
)
with pytest.raises(ValueError) as error:
    ln_setup.settings.instance.storage_local
assert (
    error.exconly()
    == "ValueError: `keep_artifacts_local` is not enabled for this instance."
)
ln_setup.settings.instance._keep_artifacts_local = True
with pytest.raises(ValueError) as error:
    ln_setup.settings.instance.storage_local
# now set local storage location
ln_setup.settings.instance.storage_local = "./my_storage_local"

assert (
    ln_setup.settings.instance.storage_local.root.as_posix()
    == UPath("./my_storage_local").resolve().as_posix()
)
assert (
    ln_setup.settings.instance.storage_local.root / ".lamindb/_is_initialized"
).read_text() == ln_setup.settings.instance.storage_local.uid
assert ln_setup.settings.instance.storage_local is not None
# the remote storage location is still in the regular slot
assert ln_setup.settings.instance.storage.root.as_posix() == storage

Another one:

ln_setup.settings.instance.storage_local = "./my_storage_local2"
assert (
    ln_setup.settings.instance.storage_local.root.as_posix()
    == UPath("./my_storage_local2").resolve().as_posix()
)
assert (
    ln_setup.settings.instance.storage_local.root / ".lamindb/_is_initialized"
).read_text() == ln_setup.settings.instance.storage_local.uid

See whether we can repeat this:

ln_setup.settings.instance.storage_local = "./my_storage_local2"

And back to the initial one:

ln_setup.settings.instance.storage_local = "./my_storage_local"

Add a test file:

test_file = ln_setup.settings.instance.storage_local.root / ".lamindb/test_file.txt"
test_file.write_text("test")
ln_setup.settings.instance.storage_local.root.view_tree()
with pytest.raises(ln_setup.core.upath.InstanceNotEmpty):
    ln_setup.delete(name, force=True)
test_file.unlink()
ln_setup.delete(name, force=True)