-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtest_model_parser.py
More file actions
175 lines (133 loc) · 4.81 KB
/
test_model_parser.py
File metadata and controls
175 lines (133 loc) · 4.81 KB
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
from io import StringIO
from unittest.mock import Mock
import pytest
from pydantic.v1 import Field
from cumulusci.utils.yaml.model_parser import CCIDictModel, CCIModel, ValidationError
class Foo(CCIModel):
bar: str = None
fields_ = Field([], alias="fields")
class Document(CCIModel):
__root__: Foo
class TestCCIModel:
def test_fields_property(self):
# JSON is YAML. Strange but true.
foo = Document.parse_from_yaml(StringIO("{bar: 'blah'}"))
assert isinstance(foo, Foo)
assert foo.fields_ == []
assert foo.fields == []
foo = Document.parse_from_yaml(StringIO("{bar: 'blah', fields: [1,2]}"))
assert foo.fields == [1, 2]
foo.fields = ["a", "b"]
assert foo.fields == ["a", "b"]
def test_parse_from_dict(self):
assert Document.parse_obj({"bar": "blah"})
def test_validate_data__success(self):
assert Document.validate_data({"bar": "blah"})
def test_validate_data__without_error_handler(self):
assert not Document.validate_data({"foo": "fail"}, context="pytest")
def test_validate_data__with_error_handler(self):
lf = Mock()
assert not Document.validate_data(
{"foo": "fail"}, context="pytest", on_error=lf
)
lf.assert_called()
assert "pytest" in str(lf.mock_calls[0][1][0])
assert "foo" in str(lf.mock_calls[0][1][0])
def test_validate_on_error_param(self):
with pytest.raises(Exception) as e:
assert not Document.validate_data({"qqq": "zzz"}, on_error="barn")
assert e.value.__class__ in [ValueError, TypeError]
def test_getattr_missing(self):
with pytest.raises(AttributeError):
x = Document.parse_obj({})
assert x
x.foo
def test_error_messages(self):
class FooWithError(CCIModel):
bar: int = None
class DocumentWithError(CCIModel):
__root__: FooWithError
s = StringIO("{bar: 'blah'}")
s.name = "some_filename"
with pytest.raises(ValidationError) as e:
DocumentWithError.parse_from_yaml(s)
assert "some_filename" in str(e.value)
def test_error_messages__nested(self):
class Foo(CCIModel):
bar: int # required
class Bar(CCIModel):
foo: Foo = None
class Baz(CCIModel):
bar: Bar = None
class Document(CCIModel):
__root__: Baz
s = StringIO("{bar: {foo: {}}}")
s.name = "some_filename"
with pytest.raises(ValidationError) as e:
Document.parse_from_yaml(s)
assert "some_filename" in str(e.value)
def test_fields_no_alias(self):
class Foo(CCIDictModel):
bar: str = None
x = Foo.parse_obj({})
assert x
with pytest.raises(AttributeError):
x.fields
def test_copy(self):
class Foo(CCIDictModel):
bar: str = None
x = Foo(bar="abc")
y = x.copy()
y.bar = "def"
assert x.bar == "abc"
assert y.bar == "def"
class TestCCIDictModel:
def test_fields_items(self):
class Foo(CCIDictModel):
bar: str = None
fields_ = Field([], alias="fields")
class Document(CCIDictModel):
__root__: Foo
# JSON is YAML. Strange but true.
foo = Document.parse_from_yaml(StringIO("{bar: 'blah'}"))
assert isinstance(foo, Foo)
assert foo["fields"] == []
foo = Document.parse_from_yaml(StringIO("{bar: 'blah', fields: [1,2]}"))
assert foo["fields"] == [1, 2]
foo["fields"] = ["a", "b"]
assert foo["fields"] == ["a", "b"]
def test_getitem_missing(self):
class Foo(CCIDictModel):
bar: str = None
fields_ = Field([], alias="fields")
x = Foo.parse_obj({})
assert x
with pytest.raises(IndexError):
x["foo"]
assert "bar" in x
assert "fields" in x
assert x["fields"] == []
def test_get(self):
class Foo(CCIDictModel):
bar: str = None
fields_ = Field([], alias="fields")
x = Foo.parse_obj({"bar": "q"})
assert x.get("bar") == x.bar == x["bar"] == "q"
assert x.get("xyzzy", 0) == 0
assert x.get("xyzzy") is None
assert x.get("fields") == []
def test_del(self):
class Foo(CCIDictModel):
bar: str = None
fields_ = Field([], alias="fields")
x = Foo.parse_obj({"bar": "q"})
assert x["bar"] == x.bar == "q"
assert "bar" in x
del x["bar"]
assert "bar" not in x
assert x.get("bar") is None
assert x["fields"] == x.fields == []
assert "fields" in x
del x["fields"]
assert "fields" not in x
assert x.get("fields") is None