Skip to content

Commit

Permalink
🧪 Added failing test for Project.import_data using a relative path
Browse files Browse the repository at this point in the history
where script and current working directory differ
  • Loading branch information
s-weigand committed Jan 8, 2023
1 parent 431ea76 commit c3e28e4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions glotaran/project/test/test_project.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

import sys
from importlib.metadata import distribution
from pathlib import Path
from shutil import rmtree
from textwrap import dedent
from typing import Literal

import pytest
from _pytest.monkeypatch import MonkeyPatch
from _pytest.recwarn import WarningsRecorder
from IPython.core.formatters import format_display_data

Expand All @@ -24,6 +26,7 @@
from glotaran.testing.simulated_data.sequential_spectral_decay import (
PARAMETERS as example_parameter,
)
from glotaran.utils.io import chdir_context


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -345,6 +348,27 @@ def test_generators_allow_overwrite(project_folder: Path, project_file: Path):
assert len(list(filter(lambda p: p.label.startswith("rates"), parameters.all()))) == 3


def test_relative_paths(tmp_path: Path, monkeypatch: MonkeyPatch):
"""Test project using relative paths where cwd and script folder differ."""
script_folder = tmp_path / "project"
script_folder.mkdir(parents=True, exist_ok=True)
with chdir_context(tmp_path), monkeypatch.context() as m:
# Pretend the currently running script is located at ``script_folder / "script.py"``
m.setattr(
sys.modules[globals()["__name__"]],
"__file__",
(script_folder / "script.py").as_posix(),
)
data_import_path = "import_data.nc"
save_dataset(example_dataset, data_import_path)
assert (tmp_path / data_import_path).is_file() is True

project = Project.open("project.gta")
assert (script_folder / "project.gta").is_file() is True
project.import_data(f"../{data_import_path}", name="dataset_1")
assert (script_folder / "data/dataset_1.nc").is_file() is True


def test_missing_file_errors(tmp_path: Path, project_folder: Path):
"""Error when accessing non existing files."""
with pytest.raises(FileNotFoundError) as exc_info:
Expand Down

0 comments on commit c3e28e4

Please sign in to comment.