Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
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 Crawler
Commits
8edc1588
Commit
8edc1588
authored
5 months ago
by
Alexander Schlemmer
Browse files
Options
Downloads
Patches
Plain Diff
TST(validator): unit tests for new validator behavior
parent
55e6f39b
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!217
TST: Make NamedTemporaryFiles Windows-compatible
,
!201
Validator that checks created records using a json schema
Pipeline
#58339
passed
5 months ago
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caoscrawler/validator.py
+5
-4
5 additions, 4 deletions
src/caoscrawler/validator.py
unittests/test_validation.py
+7
-5
7 additions, 5 deletions
unittests/test_validation.py
with
12 additions
and
9 deletions
src/caoscrawler/validator.py
+
5
−
4
View file @
8edc1588
...
@@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b
...
@@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b
retval
=
[]
retval
=
[]
for
r
in
records
:
for
r
in
records
:
if
len
(
r
.
parents
)
!=
0
:
if
len
(
r
.
parents
)
!=
1
:
raise
RuntimeError
(
raise
RuntimeError
(
"
Schema validation is only supported if records have exactly one parent.
"
)
"
Schema validation is only supported if records have exactly one parent.
"
)
if
r
.
parents
[
0
]
not
in
schemas
:
parname
=
r
.
parents
[
0
].
name
if
parname
not
in
schemas
:
raise
RuntimeError
(
raise
RuntimeError
(
"
No schema for record type {} in schema dictionary.
"
.
format
(
r
.
par
ents
[
0
]
))
"
No schema for record type {} in schema dictionary.
"
.
format
(
par
name
))
try
:
try
:
jsonschema
.
validate
(
convert_record
(
r
),
schemas
[
r
.
par
ents
[
0
]
])
jsonschema
.
validate
(
convert_record
(
r
),
schemas
[
par
name
])
retval
.
append
((
True
,
None
))
retval
.
append
((
True
,
None
))
except
ValidationError
as
ex
:
except
ValidationError
as
ex
:
retval
.
append
((
False
,
ex
))
retval
.
append
((
False
,
ex
))
...
...
This diff is collapsed.
Click to expand it.
unittests/test_validation.py
+
7
−
5
View file @
8edc1588
...
@@ -51,7 +51,8 @@ def test_create_json_schema():
...
@@ -51,7 +51,8 @@ def test_create_json_schema():
pobj
=
convert_record
(
r
)
pobj
=
convert_record
(
r
)
# print(yaml.dump(pobj))
# print(yaml.dump(pobj))
# print(yaml.dump(json[0]))
# print(yaml.dump(json[0]))
jsonschema
.
validate
(
pobj
,
json
[
0
])
assert
"
Dataset
"
in
json
jsonschema
.
validate
(
pobj
,
json
[
"
Dataset
"
])
# Failing test:
# Failing test:
r
=
db
.
Record
()
r
=
db
.
Record
()
...
@@ -62,7 +63,7 @@ def test_create_json_schema():
...
@@ -62,7 +63,7 @@ def test_create_json_schema():
pobj
=
convert_record
(
r
)
pobj
=
convert_record
(
r
)
with
pytest
.
raises
(
ValidationError
,
match
=
"
.*
'
keywords
'
is a required property.*
"
):
with
pytest
.
raises
(
ValidationError
,
match
=
"
.*
'
keywords
'
is a required property.*
"
):
jsonschema
.
validate
(
pobj
,
json
[
0
])
jsonschema
.
validate
(
pobj
,
json
[
"
Dataset
"
])
def
test_validation
():
def
test_validation
():
...
@@ -83,6 +84,7 @@ def test_validation():
...
@@ -83,6 +84,7 @@ def test_validation():
valres
=
validate
([
r1
,
r2
],
json
)
valres
=
validate
([
r1
,
r2
],
json
)
assert
valres
[
0
][
0
]
is
True
assert
valres
[
0
][
0
]
is
True
assert
len
(
valres
[
0
][
1
])
==
1
assert
valres
[
0
][
1
]
is
None
assert
valres
[
0
][
1
][
0
]
==
json
[
0
]
assert
not
valres
[
1
][
0
]
assert
len
(
valres
[
1
][
1
])
==
0
assert
isinstance
(
valres
[
1
][
1
],
ValidationError
)
assert
valres
[
1
][
1
].
message
==
"'
keywords
'
is a required property
"
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