Skip to content

Commit

Permalink
🧪 Add test to ensure need field jsonschemas are correct (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Aug 26, 2024
1 parent 0e210b8 commit 005d393
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion sphinx_needs/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class CoreFieldParameters(TypedDict):
},
"section_name": {
"description": "Simply the first section.",
"schema": {"type": ["string", "null"], "default": ""},
"schema": {"type": "string", "default": ""},
},
"signature": {
"description": "Derived from a docutils desc_name node.",
Expand Down
5 changes: 1 addition & 4 deletions tests/__snapshots__/test_basic_doc.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
5 changes: 1 addition & 4 deletions tests/__snapshots__/test_export_id.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
10 changes: 2 additions & 8 deletions tests/__snapshots__/test_external.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down Expand Up @@ -1123,10 +1120,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
5 changes: 1 addition & 4 deletions tests/__snapshots__/test_import.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -4845,10 +4845,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
5 changes: 1 addition & 4 deletions tests/__snapshots__/test_need_constraints.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
10 changes: 2 additions & 8 deletions tests/__snapshots__/test_needextend.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down Expand Up @@ -1507,10 +1504,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
15 changes: 3 additions & 12 deletions tests/__snapshots__/test_needs_builder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down Expand Up @@ -1174,10 +1171,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down Expand Up @@ -1979,10 +1973,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
5 changes: 1 addition & 4 deletions tests/__snapshots__/test_service_github.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,7 @@
'default': '',
'description': 'Simply the first section.',
'field_type': 'core',
'type': list([
'string',
'null',
]),
'type': 'string',
}),
'sections': dict({
'default': list([
Expand Down
35 changes: 35 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ast
import inspect
from typing import ForwardRef

from sphinx_needs.data import NeedsCoreFields, NeedsInfoType

Expand All @@ -10,8 +11,42 @@ def test_consistent():
but I'm not sure this is possible (to encode both the static and dynamic data required).
So at least here, we check that they are consistent with each other.
"""
# check fields are consistent
assert set(NeedsCoreFields).issubset(set(NeedsInfoType.__annotations__))

# check field types are consistent with schema
for field, data in NeedsCoreFields.items():
if not (schema := data.get("schema")):
continue
type_ = NeedsInfoType.__annotations__[field]
assert isinstance(type_, ForwardRef)
type_str = type_.__forward_arg__
if type_str.startswith("Required"):
type_str = type_str[9:-1]
if type_str == "str" or type_str == "str | Text":
assert schema["type"] == "string", field
elif type_str == "int":
assert schema["type"] == "integer", field
elif type_str == "bool":
assert schema["type"] == "boolean", field
elif type_str in ("str | None", "None | str"):
assert schema["type"] == ["string", "null"], field
elif type_str in ("int | None", "None | int"):
assert schema["type"] == ["integer", "null"], field
elif type_str in ("bool | None", "None | bool"):
assert schema["type"] == ["boolean", "null"], field
elif type_str == "list[str]":
assert schema["type"] == "array", field
assert schema["items"]["type"] == "string", field
elif type_str == "dict[str, str]":
assert schema["type"] == "object", field
assert schema["additionalProperties"]["type"] == "string", field
elif type_str.startswith("dict["):
assert schema["type"] == "object", field
else:
raise ValueError(f"Unknown type: {type_str!r} for {field!r}")

# check descriptions are consistent
source = inspect.getsource(NeedsInfoType)
klass = ast.parse(source).body[0]
descriptions = {}
Expand Down

0 comments on commit 005d393

Please sign in to comment.