|
| 1 | +from dataclasses import dataclass |
1 | 2 | from pathlib import Path |
| 3 | +from typing import Any |
2 | 4 |
|
| 5 | +import iris.cube |
3 | 6 | import pytest |
4 | 7 | from prov.model import ProvDocument |
5 | 8 |
|
6 | 9 | from esmvalcore._provenance import ESMVALTOOL_URI_PREFIX, TrackedFile |
| 10 | +from esmvalcore.io.protocol import DataElement |
7 | 11 | from esmvalcore.local import LocalFile |
8 | 12 |
|
9 | 13 |
|
| 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 | + |
10 | 36 | @pytest.fixture |
11 | 37 | def tracked_input_file_nc(): |
12 | 38 | input_file_nc = LocalFile("/path/to/file.nc") |
@@ -39,6 +65,39 @@ def test_init_input_nc(tracked_input_file_nc): |
39 | 65 | tracked_input_file_nc.attributes # noqa: B018 |
40 | 66 |
|
41 | 67 |
|
| 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 | + |
42 | 101 | def test_init_output_nc(tracked_output_file_nc): |
43 | 102 | """Test `esmvalcore._provenance.TrackedFile.__init__`.""" |
44 | 103 | assert tracked_output_file_nc.filename == Path("/path/to/file.nc") |
|
0 commit comments