-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
74 lines (53 loc) · 2.01 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# SPDX-FileCopyrightText: 2024 Helmholtz-Zentrum hereon GmbH
#
# SPDX-License-Identifier: LGPL-3.0-only
"""pytest configuration script for psy-ugrid."""
from itertools import cycle
from pathlib import Path
from typing import Any, Callable, List
import pytest # noqa: F401
@pytest.fixture
def get_test_file() -> Callable[[str], Path]:
"""Fixture to get the path to a test file."""
def get_file(basename: str) -> Path:
"""Get a file in the test folder
Parameters
----------
basename : str
The basename of the file, relative to the tests folder
Returns
-------
Path
The path to the file relative to the working directory
"""
test_folder = Path(__file__).parent / "tests"
return test_folder / basename
return get_file
@pytest.fixture
def same_upon_permutation() -> Callable[[List, List], bool]:
"""Get a comparison operator for two lists acknowledging the boundaries.
This fixture compares two lists that can be wrapped at the boundaries.
"""
def compare(test: List, ref: List, missing_value: Any = -999) -> bool:
"""Check if the order of the elements are the same.
This function is true, if the order of the elements are the same,
independent on the location of the boundary. I.e. the following
comparison results in a true condition::
assert compare([6, 4, 5, 8, 1], [1, 6, 4, 5, 8])
"""
def get_next(ref):
ref.pop(0)
ref.append(next(it))
return ref
# remove missing values
if missing_value in test:
test = test[: test.index(missing_value)]
if missing_value in ref:
ref = ref[: ref.index(missing_value)]
if len(test) != len(ref):
return False
it = cycle(list(ref))
# make a copy of the reference as we will modify it
ref = list(ref)
return any(test == get_next(ref) for i in range(len(ref)))
return compare