Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-advanced-user-tools
Commits
2e1b0e23
Commit
2e1b0e23
authored
1 year ago
by
Florian Spreckelsen
Browse files
Options
Downloads
Patches
Plain Diff
TST: Extend unit tests
parent
18e3ce3e
No related branches found
No related tags found
2 merge requests
!89
ENH: JsonSchemaExporter accepts do_not_create parameter.
,
!80
F simple schema export
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
unittests/test_json_schema_exporter.py
+136
-2
136 additions, 2 deletions
unittests/test_json_schema_exporter.py
with
136 additions
and
2 deletions
unittests/test_json_schema_exporter.py
+
136
−
2
View file @
2e1b0e23
...
@@ -22,6 +22,7 @@
...
@@ -22,6 +22,7 @@
import
linkahead
as
db
import
linkahead
as
db
from
jsonschema
import
FormatChecker
,
validate
,
ValidationError
from
pytest
import
raises
from
pytest
import
raises
from
caosadvancedtools.json_schema_exporter
import
recordtype_to_json_schema
as
rtjs
from
caosadvancedtools.json_schema_exporter
import
recordtype_to_json_schema
as
rtjs
...
@@ -57,12 +58,145 @@ def test_empty_rt():
...
@@ -57,12 +58,145 @@ def test_empty_rt():
def
test_rt_with_scalar_props
():
def
test_rt_with_scalar_props
():
pass
rt
=
db
.
RecordType
(
name
=
"
Test
"
)
rt
.
add_property
(
name
=
"
SimpleText
"
,
datatype
=
db
.
TEXT
,
description
=
"
This is a simple text
"
)
rt
.
add_property
(
name
=
"
ObligatoryDatetime
"
,
datatype
=
db
.
DATETIME
,
importance
=
db
.
OBLIGATORY
)
rt
.
add_property
(
name
=
"
JustDateNoTime
"
,
datatype
=
db
.
DATETIME
,
description
=
"
Only dates, no times
"
)
rt
.
add_property
(
name
=
"
ObligatoryInteger
"
,
datatype
=
db
.
INTEGER
,
importance
=
db
.
OBLIGATORY
)
rt
.
add_property
(
name
=
"
Double
"
,
datatype
=
db
.
DOUBLE
)
# Suggested shouldn't influence the result in any way.
rt
.
add_property
(
name
=
"
Boolean
"
,
datatype
=
db
.
BOOLEAN
,
importance
=
db
.
SUGGESTED
)
schema
=
rtjs
(
rt
,
additional_options_for_text_props
=
{
"
JustDateNoTime
"
:
{
"
format
"
:
"
date
"
}})
assert
"
properties
"
in
schema
props
=
schema
[
"
properties
"
]
assert
len
(
props
)
==
6
assert
"
required
"
in
schema
assert
len
(
schema
[
"
required
"
])
==
2
assert
"
ObligatoryDatetime
"
in
schema
[
"
required
"
]
assert
"
ObligatoryInteger
"
in
schema
[
"
required
"
]
assert
"
SimpleText
"
in
props
assert
props
[
"
SimpleText
"
][
"
type
"
]
==
"
string
"
assert
"
format
"
not
in
props
[
"
SimpleText
"
]
assert
"
description
"
in
props
[
"
SimpleText
"
]
assert
props
[
"
SimpleText
"
][
"
description
"
]
==
"
This is a simple text
"
assert
"
ObligatoryDatetime
"
in
props
assert
props
[
"
ObligatoryDatetime
"
][
"
type
"
]
==
"
string
"
assert
"
anyOf
"
in
props
[
"
ObligatoryDatetime
"
]
assert
len
(
props
[
"
ObligatoryDatetime
"
][
"
anyOf
"
])
==
2
fmts
=
[
fmt
[
"
format
"
]
for
fmt
in
props
[
"
ObligatoryDatetime
"
][
"
anyOf
"
]]
assert
"
date
"
in
fmts
assert
"
date-time
"
in
fmts
assert
"
JustDateNoTime
"
in
props
assert
props
[
"
JustDateNoTime
"
][
"
type
"
]
==
"
string
"
assert
"
anyOf
"
not
in
props
[
"
JustDateNoTime
"
]
assert
"
pattern
"
not
in
props
[
"
JustDateNoTime
"
]
assert
props
[
"
JustDateNoTime
"
][
"
format
"
]
==
"
date
"
assert
props
[
"
JustDateNoTime
"
][
"
description
"
]
==
"
Only dates, no times
"
assert
"
ObligatoryInteger
"
in
props
assert
props
[
"
ObligatoryInteger
"
][
"
type
"
]
==
"
integer
"
assert
"
Double
"
in
props
assert
props
[
"
Double
"
][
"
type
"
]
==
"
number
"
assert
"
Boolean
"
in
props
assert
props
[
"
Boolean
"
][
"
type
"
]
==
"
boolean
"
# test validation (we turst the jsonschema.validat function, so only test
# some more or less tricky cases with format or required).
example
=
{
"
SimpleText
"
:
"
something
"
,
"
ObligatoryInteger
"
:
23
,
"
ObligatoryDatetime
"
:
"
1900-01-01T12:34:56.0Z
"
,
"
JustDateNoTime
"
:
"
2023-10-13
"
}
# We need to explicitly enable the FormatChecker, otherwise format will be
# ignored
# (https://python-jsonschema.readthedocs.io/en/latest/validate/#validating-formats)
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
example
=
{
"
SimpleText
"
:
"
something
"
,
"
ObligatoryInteger
"
:
23
,
"
ObligatoryDatetime
"
:
"
1900-01-01
"
,
"
JustDateNoTime
"
:
"
2023-10-13
"
}
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
example
=
{
"
SimpleText
"
:
"
something
"
,
"
ObligatoryDatetime
"
:
"
1900-01-01T12:34:56.0Z
"
,
"
JustDateNoTime
"
:
"
2023-10-13
"
}
with
raises
(
ValidationError
):
# required missing
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
example
=
{
"
SimpleText
"
:
"
something
"
,
"
ObligatoryInteger
"
:
23
,
"
ObligatoryDatetime
"
:
"
1900-01-01T12:34:56.0Z
"
,
"
JustDateNoTime
"
:
"
2023-10-13T23:59:59.123Z
"
}
with
raises
(
ValidationError
):
# date expected in JustDateNoTime, but datetime given
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
def
test_rt_with_list_props
():
def
test_rt_with_list_props
():
pass
rt
=
db
.
RecordType
()
rt
.
add_property
(
name
=
"
ListOfIntegers
"
,
datatype
=
db
.
LIST
(
db
.
INTEGER
),
description
=
"
List of integers
"
)
rt
.
add_property
(
name
=
"
ListOfPatterns
"
,
datatype
=
db
.
LIST
(
db
.
TEXT
))
schema
=
rtjs
(
rt
,
additional_options_for_text_props
=
{
"
ListOfPatterns
"
:
{
"
pattern
"
:
"
[A-Z]+
"
}})
props
=
schema
[
"
properties
"
]
assert
"
ListOfIntegers
"
in
props
assert
props
[
"
ListOfIntegers
"
][
"
type
"
]
==
"
array
"
assert
"
items
"
in
props
[
"
ListOfIntegers
"
]
assert
props
[
"
ListOfIntegers
"
][
"
items
"
][
"
type
"
]
==
"
integer
"
assert
"
description
"
not
in
props
[
"
ListOfIntegers
"
][
"
items
"
]
assert
props
[
"
ListOfIntegers
"
][
"
description
"
]
==
"
List of integers
"
assert
"
ListOfPatterns
"
in
props
assert
props
[
"
ListOfPatterns
"
][
"
type
"
]
==
"
array
"
assert
"
items
"
in
props
[
"
ListOfPatterns
"
]
assert
props
[
"
ListOfPatterns
"
][
"
items
"
][
"
type
"
]
==
"
string
"
assert
props
[
"
ListOfPatterns
"
][
"
items
"
][
"
pattern
"
]
==
"
[A-Z]+
"
# Validation
example
=
{
"
ListOfIntegers
"
:
[
1
,
2
,
3
],
"
ListOfPatterns
"
:
[
"
A
"
,
"
BB
"
,
"
CCC
"
]
}
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
example
=
{
"
ListOfIntegers
"
:
1
,
"
ListOfPatterns
"
:
[
"
A
"
,
"
BB
"
,
"
CCC
"
]
}
with
raises
(
ValidationError
):
# No list
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
example
=
{
"
ListOfIntegers
"
:
[
1
,
2
,
3
],
"
ListOfPatterns
"
:
[
"
A
"
,
"
bb
"
,
"
CCC
"
]
}
with
raises
(
ValidationError
):
# Pattern doesn't match
validate
(
example
,
schema
,
format_checker
=
FormatChecker
())
def
test_rt_with_references
():
def
test_rt_with_references
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment