Skip to content

Commit bce7c5a

Browse files
committed
Move tests/unit/test_provenance.py to tests/unit/provenance and add more tests
1 parent 596bf2e commit bce7c5a

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

tests/unit/provenance/test_trackedfile.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1+
from dataclasses import dataclass
12
from pathlib import Path
3+
from typing import Any
24

5+
import iris.cube
36
import pytest
47
from prov.model import ProvDocument
58

69
from esmvalcore._provenance import ESMVALTOOL_URI_PREFIX, TrackedFile
10+
from esmvalcore.io.protocol import DataElement
711
from esmvalcore.local import LocalFile
812

913

14+
def test_set():
15+
assert {
16+
TrackedFile(Path("file1.nc"), attributes={}),
17+
TrackedFile(Path("file1.nc"), attributes={}),
18+
TrackedFile(Path("file2.nc"), attributes={}),
19+
} == {
20+
TrackedFile(Path("file1.nc"), attributes={}),
21+
TrackedFile(Path("file2.nc"), attributes={}),
22+
}
23+
24+
25+
def test_sort():
26+
file1 = TrackedFile(Path("file1.nc"), attributes={})
27+
file2 = TrackedFile(Path("file2.nc"), attributes={})
28+
assert sorted([file2, file1]) == [file1, file2]
29+
30+
31+
def test_equals():
32+
file = TrackedFile(Path("file.nc"), attributes={})
33+
assert file == TrackedFile(Path("file.nc"), attributes={})
34+
35+
1036
@pytest.fixture
1137
def tracked_input_file_nc():
1238
input_file_nc = LocalFile("/path/to/file.nc")
@@ -39,6 +65,39 @@ def test_init_input_nc(tracked_input_file_nc):
3965
tracked_input_file_nc.attributes # noqa: B018
4066

4167

68+
def test_provenance_file_nonpath_notimplemented() -> None:
69+
"""Test `esmvalcore._provenance.TrackedFile.provenance_file` with a DataElement."""
70+
71+
@dataclass
72+
class MockDataElement(DataElement):
73+
name: str
74+
facets: dict[str, Any]
75+
attributes: dict[str, Any]
76+
77+
def prepare(self) -> None:
78+
pass
79+
80+
def to_iris(
81+
self,
82+
ignore_warnings: list[dict[str, Any]] | None = None,
83+
) -> iris.cube.CubeList:
84+
return []
85+
86+
input_file = MockDataElement(
87+
name="/path/to/input_file.nc",
88+
facets={},
89+
attributes={},
90+
)
91+
tracked_file = TrackedFile(filename=input_file)
92+
93+
assert tracked_file.filename == input_file
94+
with pytest.raises(
95+
NotImplementedError,
96+
match=r"Saving provenance is only supported for pathlib.Path.*",
97+
):
98+
_ = tracked_file.provenance_file
99+
100+
42101
def test_init_output_nc(tracked_output_file_nc):
43102
"""Test `esmvalcore._provenance.TrackedFile.__init__`."""
44103
assert tracked_output_file_nc.filename == Path("/path/to/file.nc")

tests/unit/test_provenance.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)