Skip to content

Commit

Permalink
Start converting to class for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Dec 13, 2024
1 parent dd32f04 commit db9e6eb
Showing 1 changed file with 86 additions and 19 deletions.
105 changes: 86 additions & 19 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import pytest

import audeer
import audformat

Expand Down Expand Up @@ -57,27 +59,92 @@ def test_available_broken_dataset(private_and_public_repository):
assert "broken-dataset" not in df


def test_available_repositories(tmpdir):
"""Test repositories argument of available()."""
class TestAvailable:
r"""Test collecting available datasets."""

repositories = []
for n in range(2):
host = audeer.mkdir(tmpdir, f"host{n}")
repo = f"repo{n}"
repositories.append(audb.Repository(repo, host, "file-system"))
# Fake dataset by adding db.yaml file
audeer.touch(audeer.mkdir(host, repo, f"name{n}", "1.0.0"), "db.yaml")
# Look at all repositories
for repositories_argument in [None, repositories]:
df = audb.available(repositories=repositories_argument)
"""Repositories."""

@classmethod
@pytest.fixture(scope="class", autouse=True)
def setup(cls, tmpdir_factory):
r"""Create two repositories containing a single dataset.
Args:
request: request fixture to access params
tmpdir_factory: tmpdir_factory fixture
"""
current_repositories = audb.config.REPOSITORIES

for n in range(2):
host = str(tmpdir_factory.mktemp(f"host{n}"))
repo = f"repo{n}"
audeer.touch(audeer.mkdir(host, repo, f"name{n}", "1.0.0"), "db.yaml")
cls.repositories.append(audb.Repository(repo, host, "file-system"))

audb.config.REPOSITORIES = cls.repositories

yield

audb.config.REPOSITORIES = current_repositories

@pytest.fixture(scope="class", autouse=False)
def repository_with_empty_folder(self, tmpdir_factory):
"""Create repository with empty folder.
An empty folder in a repository
should not be detected as a database.
"""
repository = self.repositories[0]
empty_folder = audeer.mkdir(repository.host, repository.name, "no-database")
yield
audeer.rmdir(empty_folder)

def test_non_existing_database(self, repository_with_empty_folder):
"""Test having a database only given as a folder."""
df = audb.available()
assert len(df) == 2
for n, repository in enumerate(repositories):
# Test for string and list arguments
for repositories_argument in [repository, [repository]]:
df = audb.available(repositories=repositories_argument)
assert len(df) == 1
assert df.host.iloc[0] == repository.host
assert df.repository.iloc[0] == repository.name
assert df.index[0] == f"name{n}"
assert "no-database" not in df

def test_repositories_none(self):
"""Test default value of repositories argument."""
print(f"{audb.config.REPOSITORIES=}")
df = audb.available(repositories=None)
print(df)
assert len(df) == 2

def test_repositories_all(self):
"""Provide all repositories."""
df = audb.available(repositories=self.repositories)
assert len(df) == 2

@pytest.mark.parametrize("repository_index", [0, 1])
@pytest.mark.parametrize("as_list", [False, True])
def test_repositories_single(self, repository_index, as_list):
"""Test looking to single repositories."""
repository = self.repositories[repository_index]
if as_list:
repositories = [repository]
else:
repositories = repository
df = audb.available(repositories=repositories)
assert len(df) == 1
assert df.host.iloc[0] == repository.host
assert df.repository.iloc[0] == repository.name
assert df.index[0] == f"name{repository_index}"

def test_repositories_non_existing_path(self):
"""Test with non-existent path."""
invalid_repo = audb.Repository("invalid", "/nonexistent/path", "file-system")
df = audb.available(repositories=invalid_repo)
assert len(df) == 0

def test_repositories_malformed_repository(self):
"""Test with malformed repository (string instead of Repository object)."""
with pytest.raises(AttributeError):
audb.available(repositories=["not-a-repository-object"])


def test_versions(tmpdir, repository):
Expand Down

0 comments on commit db9e6eb

Please sign in to comment.