Skip to content

Commit

Permalink
SqliteZipStorage: Ensure the filepath is absolute and exists
Browse files Browse the repository at this point in the history
Otherwise, loading the storage will fail as soon as the working
directory is changed.
  • Loading branch information
sphuber committed Dec 13, 2023
1 parent f3d2521 commit 5eac8b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
11 changes: 10 additions & 1 deletion aiida/storage/sqlite_zip/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from zipfile import ZipFile, is_zipfile

from archive_path import ZipPath, extract_file_in_zip
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator
from sqlalchemy.orm import Session

from aiida import __version__
Expand Down Expand Up @@ -66,12 +66,21 @@ class SqliteZipBackend(StorageBackend): # pylint: disable=too-many-public-metho
"""

class Configuration(BaseModel):
"""Model describing required information to configure an instance of the storage."""

filepath: str = Field(
title='Filepath of the archive',
description='Filepath of the archive in which to store data for this backend.'
)

@field_validator('filepath')
@classmethod
def filepath_exists_and_is_absolute(cls, value: str) -> str:
"""Validate the filepath exists and return the resolved and absolute filepath."""
filepath = Path(value)
assert filepath.is_file(), f'The archive `{value}` does not exist.'
return str(filepath.resolve().absolute())

_read_only = True

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/storage/sqlite_zip/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
"""Tests for :mod:`aiida.storage.sqlite_zip`."""
20 changes: 19 additions & 1 deletion tests/storage/sqlite_zip/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# -*- coding: utf-8 -*-
"""Tests for :mod:`aiida.storage.sqlite_zip.backend.SqliteZipBackend`."""
"""Tests for :mod:`aiida.storage.sqlite_zip.backend`."""
import pathlib

from pydantic_core import ValidationError
import pytest

from aiida.storage.sqlite_zip.backend import SqliteZipBackend
from aiida.storage.sqlite_zip.migrator import validate_storage

Expand Down Expand Up @@ -45,3 +50,16 @@ def test_initialise_reset_false(tmp_path, aiida_caplog):
assert filepath_archive.exists()
validate_storage(filepath_archive)
assert any('Migrating existing SqliteZipBackend' in record.message for record in aiida_caplog.records)


@pytest.mark.usefixtures('chdir_tmp_path')
def test_configuration():
"""Test :class:`aiida.storage.sqlite_zip.backend.SqliteZipBackend.Configuration`."""
with pytest.raises(ValidationError, match=r'.*The archive `non-existent` does not exist.*'):
SqliteZipBackend.Configuration(filepath='non-existent')

filepath = pathlib.Path.cwd() / 'archive.aiida'
filepath.touch()

configuration = SqliteZipBackend.Configuration(filepath=filepath.name)
assert pathlib.Path(configuration.filepath).is_absolute()

0 comments on commit 5eac8b4

Please sign in to comment.