File: test_history.py

package info (click to toggle)
python-asdf 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,032 kB
  • sloc: python: 24,068; makefile: 123
file content (275 lines) | stat: -rw-r--r-- 8,661 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import datetime
import fractions
import warnings

import pytest

import asdf
from asdf.exceptions import AsdfWarning, ValidationError
from asdf.extension import Converter, Extension, ExtensionProxy
from asdf.tags.core import HistoryEntry
from asdf.testing import helpers


def test_history():
    ff = asdf.AsdfFile()
    assert "history" not in ff.tree
    ff.add_history_entry(
        "This happened",
        {"name": "my_tool", "homepage": "http://nowhere.org", "author": "John Doe", "version": "2.0"},
    )
    assert len(ff.tree["history"]["entries"]) == 1

    with pytest.raises(ValidationError, match=r".* is not valid under any of the given schemas"):
        ff.add_history_entry("That happened", {"author": "John Doe", "version": "2.0"})
    assert len(ff.tree["history"]["entries"]) == 1

    ff.add_history_entry("This other thing happened")
    assert len(ff.tree["history"]["entries"]) == 2

    assert isinstance(ff.tree["history"]["entries"][0]["time"], datetime.datetime)


def test_history_to_file(tmp_path):
    file_path = tmp_path / "history.asdf"

    with asdf.AsdfFile() as ff:
        ff.add_history_entry(
            "This happened",
            {"name": "my_tool", "homepage": "http://nowhere.org", "author": "John Doe", "version": "2.0"},
        )
        ff.write_to(file_path)

    with asdf.open(file_path) as ff:
        assert "entries" in ff.tree["history"]
        assert "extensions" in ff.tree["history"]
        assert len(ff.tree["history"]["entries"]) == 1

        entry = ff.tree["history"]["entries"][0]
        assert entry["description"] == "This happened"
        assert entry["software"]["name"] == "my_tool"
        assert entry["software"]["version"] == "2.0"

        # Test the history entry retrieval API
        entries = ff.get_history_entries()
        assert len(entries) == 1
        assert isinstance(entries, list)
        assert isinstance(entries[0], HistoryEntry)
        assert entries[0]["description"] == "This happened"
        assert entries[0]["software"]["name"] == "my_tool"


def test_old_history():
    """Make sure that old versions of the history format are still accepted"""

    yaml = """
history:
  - !core/history_entry-1.0.0
    description: "Here's a test of old history entries"
    software: !core/software-1.0.0
      name: foo
      version: 1.2.3
    """

    buff = helpers.yaml_to_asdf(yaml)
    with asdf.open(buff) as af:
        assert len(af.tree["history"]) == 1

        # Test the history entry retrieval API
        entries = af.get_history_entries()
        assert len(entries) == 1
        assert isinstance(entries, list)
        assert isinstance(entries[0], HistoryEntry)
        assert entries[0]["description"] == "Here's a test of old history entries"
        assert entries[0]["software"]["name"] == "foo"


def test_get_history_entries(tmp_path):
    """
    Test edge cases for the get_history_entries API. Other cases tested above
    """
    file_path = tmp_path / "empty.asdf"

    with asdf.AsdfFile() as af:
        af.write_to(file_path)

    # Make sure this works when there is no history section at all
    with asdf.open(file_path) as af:
        assert len(af["history"]["extensions"]) > 0
        assert len(af.get_history_entries()) == 0


def test_extension_metadata(tmp_path):
    file_path = tmp_path / "extension.asdf"

    ff = asdf.AsdfFile()
    ff.write_to(file_path)

    with asdf.open(file_path) as af:
        assert len(af.tree["history"]["extensions"]) == 1
        metadata = af.tree["history"]["extensions"][0]
        assert metadata.extension_uri == "asdf://asdf-format.org/core/extensions/core-1.6.0"
        assert metadata.extension_class == "asdf.extension._manifest.ManifestExtension"
        assert metadata.software["name"] == "asdf"
        assert metadata.software["version"] == asdf.__version__


def test_missing_extension_warning():
    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: foo.bar.FooBar
      software: !core/software-1.0.0
        name: foo
        version: 1.2.3
    """

    buff = helpers.yaml_to_asdf(yaml)
    with pytest.warns(AsdfWarning, match=r"File was created with extension class 'foo.bar.FooBar'"), asdf.open(buff):
        pass


def test_extension_version_warning():
    uri = "asdf://somewhere.org/extensions/foo-1.0.0"
    package_name = "foo"
    file_package_version = "2.0.0"
    installed_package_version = "1.0.0"

    class FooExtension:
        extension_uri = uri

    yaml = f"""
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: {FooExtension.__qualname__}
      extension_uri: {uri}
      software: !core/software-1.0.0
        name: {package_name}
        version: {file_package_version}
    """

    buff = helpers.yaml_to_asdf(yaml)
    with asdf.config_context() as cfg:
        cfg.add_extension(ExtensionProxy(FooExtension(), package_name, installed_package_version))
        with (
            pytest.warns(
                AsdfWarning,
                match=f"older package \\({package_name}=={installed_package_version}\\)",
            ),
            asdf.open(buff),
        ):
            pass

        buff.seek(0)

        # Make sure suppressing the warning works too
        with warnings.catch_warnings():
            warnings.simplefilter("error")
            with asdf.open(buff, ignore_missing_extensions=True):
                pass


def test_strict_extension_check():
    yaml = """
history:
  extensions:
    - !core/extension_metadata-1.0.0
      extension_class: foo.bar.FooBar
      software: !core/software-1.0.0
        name: foo
        version: 1.2.3
    """

    buff = helpers.yaml_to_asdf(yaml)
    with (
        pytest.raises(
            RuntimeError,
            match=r"File was created with extension class .*, which is not currently installed",
        ),
        asdf.open(buff, strict_extension_check=True),
    ):
        pass

    # Make sure to test for incompatibility with ignore_missing_extensions
    buff.seek(0)
    with (
        pytest.raises(
            ValueError,
            match=r"'strict_extension_check' and 'ignore_missing_extensions' are incompatible options",
        ),
        asdf.open(buff, strict_extension_check=True, ignore_missing_extensions=True),
    ):
        pass


def test_metadata_with_custom_extension(tmp_path):
    class FractionConverter(Converter):
        tags = ["asdf://nowhere.org/tags/fraction-1.0.0"]
        types = [fractions.Fraction]

        def to_yaml_tree(self, obj, tag, ctx):
            return [obj.numerator, obj.denominator]

        def from_yaml_tree(self, node, tag, ctx):
            return fractions.Fraction(node[0], node[1])

    class FractionExtension(Extension):
        extension_uri = "asdf://nowhere.org/extensions/fraction-1.0.0"
        converters = [FractionConverter()]
        tags = FractionConverter.tags

    file_path = tmp_path / "custom_extension.asdf"

    with asdf.config_context() as config:
        config.add_extension(FractionExtension())

        tree = {"fraction": fractions.Fraction(2, 3)}

        with asdf.AsdfFile(tree) as ff:
            ff.write_to(file_path)

        # We expect metadata about both the Builtin extension and the custom one
        with asdf.open(file_path) as af:
            assert len(af["history"]["extensions"]) == 2

    with (
        pytest.warns(AsdfWarning, match=r"was created with extension"),
        asdf.open(
            file_path,
            ignore_unrecognized_tag=True,
        ),
    ):
        pass

    file_path_2 = tmp_path / "no_extension.asdf"

    # If we use the extension but we don't serialize any types that require it,
    # no metadata about this extension should be added to the file
    with asdf.config_context() as config:
        config.add_extension(FractionExtension())

        tree2 = {"x": list(range(10))}
        with asdf.AsdfFile(tree2) as ff:
            ff.write_to(file_path_2)

        with asdf.open(file_path_2) as af:
            assert len(af["history"]["extensions"]) == 1

    with warnings.catch_warnings():
        warnings.simplefilter("error")
        with asdf.open(file_path_2):
            pass

    file_path_3 = tmp_path / "custom_extension2.asdf"

    with asdf.config_context() as config:
        config.add_extension(FractionExtension())
        # Make sure that this works even when constructing the tree on-the-fly
        with asdf.AsdfFile() as ff:
            ff.tree["fraction"] = fractions.Fraction(4, 5)
            ff.write_to(file_path_3)

        with asdf.open(file_path_3) as af:
            assert len(af["history"]["extensions"]) == 2