forked from CAVEconnectome/EMAnnotationSchemas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_schema_module.py
More file actions
75 lines (55 loc) · 2.41 KB
/
Copy pathtest_schema_module.py
File metadata and controls
75 lines (55 loc) · 2.41 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
import marshmallow as mm
import pytest
from emannotationschemas import get_schema, get_types
from emannotationschemas.errors import UnknownAnnotationTypeException
from emannotationschemas.flatten import create_flattened_schema
from emannotationschemas.schemas.base import AnnotationSchema, SpatialPoint
from emannotationschemas.schemas.nucleus_detection import NucleusDetection
def test_get_types():
types = get_types()
for type_ in types:
schema = get_schema(type_)
assert issubclass(schema, AnnotationSchema) or issubclass(schema, SpatialPoint)
def test_bad_type():
with pytest.raises(UnknownAnnotationTypeException):
get_schema("NOTAVALIDTYPE")
def test_flattened_schema_nulls():
"""Test that flattened schemas preserve required=False on nested fields"""
FlatSchema = create_flattened_schema(NucleusDetection)
flat_schema = FlatSchema()
assert flat_schema.fields['pt_position'].required == True
assert flat_schema.fields['bb_start_position'].required == False
assert flat_schema.fields['bb_end_position'].required == False
for field_name, field in flat_schema.fields.items():
print(f"{field_name}: required={field.required}")
def test_nested_field_required_preservation():
"""Test that flattened schemas preserve required=False on nested fields"""
class TestSchema(AnnotationSchema):
required_point = mm.fields.Nested(
SpatialPoint,
required=True,
description="A required spatial point"
)
optional_point = mm.fields.Nested(
SpatialPoint,
required=False,
description="An optional spatial point"
)
FlatSchema = create_flattened_schema(TestSchema)
flat_schema = FlatSchema()
assert flat_schema.fields['required_point_position'].required == True
assert flat_schema.fields['optional_point_position'].required == False
assert flat_schema.fields['optional_point_position'].allow_none == True
data = {
'required_point_position': [1, 2, 3],
}
try:
result = flat_schema.load(data)
assert result['optional_point_position'] is None
except mm.ValidationError as e:
pytest.fail(f"Validation failed for optional nested fields: {e}")
data = {
'optional_point_position': [1, 2, 3],
}
with pytest.raises(mm.ValidationError):
flat_schema.load(data)