Skip to content

Commit

Permalink
Fix load attachments for same shared cache/cache (#317)
Browse files Browse the repository at this point in the history
* Add failing test

* Add fix

* Improve docstrings
  • Loading branch information
hagenw authored May 29, 2024
1 parent 05e5020 commit 075b279
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions audb/core/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def _cached_versions(
# This fixes https://github.com/audeering/audb/issues/101
if cache_root is None and os.path.exists(default_cache_root(shared=True)):
df = pd.concat((df, cached(name=name, shared=True)))
# Ensure to remove duplicates,
# which can occur if cache and shared cache
# point to the same folder.
# Compare https://github.com/audeering/audb/issues/314
df = df[~df.index.duplicated(keep="first")]

cached_versions = []
for flavor_root, row in df.iterrows():
Expand Down
65 changes: 65 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os

import pytest

import audeer
import audformat

import audb


@pytest.fixture(scope="function", autouse=False)
def same_cache():
r"""Set shared cache to same folder as user cache."""
current_shared_cache = audb.config.SHARED_CACHE_ROOT
audb.config.SHARED_CACHE_ROOT = audb.config.CACHE_ROOT

yield

audb.config.SHARED_CACHE_ROOT = current_shared_cache


def test_loading_attachments_from_cache(tmpdir, repository, same_cache):
r"""Ensures loading attachments from cache works.
As reported in https://github.com/audeering/audb/issues/314,
loading an attachment for a database
fails to acquire a lock,
if the database has a previous version in the cache,
and ``audb.config.CACHE_ROOT`` and ``audb.config.SHARED_CACHE_ROOT``
point to the same folder.
"""
# Create version 1.0.0 of database,
# publish and load
db_name = "db"
db_version = "1.0.0"
db_root = audeer.mkdir(audeer.path(tmpdir, db_name))
db = audformat.Database(db_name)
db.description = f"Version {db_version} of database."
filename = "file.txt"
with open(audeer.path(db_root, filename), "w") as file:
file.write(f"{filename}\n")
db.attachments[filename] = audformat.Attachment(filename)
db.save(db_root)

audb.publish(db_root, db_version, repository)

db = audb.load(db_name, version=db_version, verbose=False)

# Create version 2.0.0 of database,
# and publish and load
db = audb.load_to(db_root, db_name, version=db_version)
db_version = "2.0.0"
db.description = f"Version {db_version} of database."
db.save(db_root)

audb.publish(db_root, db_version, repository)

db = audb.load(db_name, version=db_version, verbose=False)

# Ensure attachment file is loaded
assert list(db.attachments) == [filename]
for attachment in list(db.attachments):
for file in db.attachments[attachment].files:
assert os.path.exists(audeer.path(db.root, file))

0 comments on commit 075b279

Please sign in to comment.