Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cubicweb
cubes
jsonschema
Commits
68824684174a
Commit
37d0ac4e
authored
Mar 19, 2018
by
Denis Laxalde
Browse files
Add a test about selection score for jsonschema.item mappers for relations
parent
74f866411ac8
Changes
1
Hide whitespace changes
Inline
Side-by-side
test/test_mappers.py
View file @
68824684
...
...
@@ -34,6 +34,7 @@ from cubicweb.predicates import match_kwargs
from
cubicweb_jsonschema
import
CREATION_ROLE
,
EDITION_ROLE
,
VIEW_ROLE
from
cubicweb_jsonschema.mappers
import
(
JSONSchemaDeserializer
,
JSONSchemaMapper
,
JSONSchemaSerializer
,
CompoundMapper
,
yams_component_target
,
...
...
@@ -394,6 +395,45 @@ class RelatedCollectionMapperTC(CubicWebTC):
self
.
assertEqual
(
instance
,
expected
)
assert_jsonschema_validate
(
instance
,
schema
)
def
test_custom_mapper
(
self
):
"""Make sure a custom mapper with a yams_match selector wins selection
over default one.
"""
class
reverse_author_mapper
(
JSONSchemaMapper
):
__regid__
=
'jsonschema.item'
__select__
=
yams_match
(
rtype
=
'author'
,
role
=
'object'
)
def
schema_and_definitions
(
self
,
schema_role
=
None
):
return
{
'type'
:
'object'
,
'properties'
:
{
'name'
:
{
'type'
:
'string'
},
},
},
None
def
serialize
(
self
,
entity
):
return
{
'name'
:
entity
.
name
,
}
with
self
.
admin_access
.
cnx
()
as
cnx
:
author
=
cnx
.
create_entity
(
'Author'
,
name
=
u
'bob'
)
cnx
.
commit
()
with
self
.
temporary_appobjects
(
reverse_author_mapper
):
item_mapper
=
cnx
.
vreg
[
'mappers'
].
select
(
'jsonschema.item'
,
cnx
,
entity
=
author
,
rtype
=
'author'
,
role
=
'object'
)
collection_mapper
=
cnx
.
vreg
[
'mappers'
].
select
(
'jsonschema.collection'
,
cnx
,
etype
=
'Author'
,
rtype
=
'author'
,
role
=
'object'
)
self
.
assertIsInstance
(
item_mapper
,
reverse_author_mapper
)
self
.
assertEqual
(
item_mapper
.
serialize
(
author
),
{
'name'
:
'bob'
})
authors
=
collection_mapper
.
serialize
([
author
])
self
.
assertEqual
(
authors
,
[{
'name'
:
'bob'
}])
class
RelationMapperTC
(
CubicWebTC
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment