File: test_helpers.py

package info (click to toggle)
python-asdf 2.14.3-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,280 kB
  • sloc: python: 16,612; makefile: 124
file content (45 lines) | stat: -rw-r--r-- 1,157 bytes parent folder | download
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
import pytest

from asdf import types
from asdf.exceptions import AsdfConversionWarning, AsdfWarning
from asdf.tests.helpers import assert_roundtrip_tree


def test_conversion_error(tmp_path):
    class FooType(types.CustomType):
        name = "foo"

        def __init__(self, a, b):
            self.a = a
            self.b = b

        @classmethod
        def from_tree(cls, tree, ctx):
            raise TypeError("This allows us to test the failure")

        @classmethod
        def to_tree(cls, node, ctx):
            return dict(a=node.a, b=node.b)

        def __eq__(self, other):
            return self.a == other.a and self.b == other.b

    class FooExtension:
        @property
        def types(self):
            return [FooType]

        @property
        def tag_mapping(self):
            return []

        @property
        def url_mapping(self):
            return []

    foo = FooType(10, "hello")
    tree = dict(foo=foo)

    with pytest.raises(AsdfConversionWarning):
        with pytest.warns(AsdfWarning, match=r"Unable to locate schema file"):
            assert_roundtrip_tree(tree, tmp_path, extensions=FooExtension())