Skip to content

Commit

Permalink
schema: add locations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Panero committed Oct 26, 2020
1 parent b049941 commit 02ea234
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 115 deletions.
8 changes: 5 additions & 3 deletions invenio_rdm_records/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ def create_fake_record():
}],
"description": fake.text(max_nb_chars=3000),
"locations": [{
"point": {
"lat": str(fake.latitude()),
"lon": str(fake.longitude())
"geometry": {
"type": "point",
"coordinates": [
str(fake.latitude()), str(fake.longitude())
]
},
"place": fake.location_on_land()[2],
"description": "Random place on land for random coordinates..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,18 @@
"type": "object",
"additionalProperties": false,
"properties": {
"point": {
"geometry": {
"type": "object",
"additionalProperties": false,
"properties": {
"lat": {"$ref": "#/definitions/latitude"},
"lon": {"$ref": "#/definitions/longitude"}
"type": {
"type": "string"
},
"coordinates": {
"type": "object"
}
}
},
"identifiers": {"$ref": "#/definitions/identifiers"},
"place": {
"description": "Place of the location",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,24 @@
},
"locations" : {
"properties" : {
"description" : {
"type" : "text"
},
"place" : {
"type" : "text"
},
"point" : {
"geometry": {
"properties" : {
"lat" : {
"type" : "double"
"type" : {
"type" : "keyword"
},
"lon" : {
"type" : "double"
"coordinates" : {
"type" : "object"
}
}
},
"place" : {
"type" : "text"
},
"identifiers" : {
"type" : "object"
},
"description" : {
"type" : "text"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,24 @@
},
"locations" : {
"properties" : {
"description" : {
"type" : "text"
},
"place" : {
"type" : "text"
},
"point" : {
"geometry": {
"properties" : {
"lat" : {
"type" : "double"
"type" : {
"type" : "keyword"
},
"lon" : {
"type" : "double"
"coordinates" : {
"type" : "object"
}
}
},
"place" : {
"type" : "text"
},
"identifiers" : {
"type" : "object"
},
"description" : {
"type" : "text"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,24 @@
},
"locations" : {
"properties" : {
"description" : {
"type" : "text"
},
"place" : {
"type" : "text"
},
"point" : {
"geometry": {
"properties" : {
"lat" : {
"type" : "double"
"type" : {
"type" : "keyword"
},
"lon" : {
"type" : "double"
"coordinates" : {
"type" : "object"
}
}
},
"place" : {
"type" : "text"
},
"identifiers" : {
"type" : "object"
},
"description" : {
"type" : "text"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,24 @@
},
"locations" : {
"properties" : {
"description" : {
"type" : "text"
},
"place" : {
"type" : "text"
},
"point" : {
"geometry": {
"properties" : {
"lat" : {
"type" : "double"
"type" : {
"type" : "keyword"
},
"lon" : {
"type" : "double"
"coordinates" : {
"type" : "object"
}
}
},
"place" : {
"type" : "text"
},
"identifiers" : {
"type" : "object"
},
"description" : {
"type" : "text"
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions invenio_rdm_records/services/schemas/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
validate, validates, validates_schema
from marshmallow_utils.fields import EDTFDateString, GenFunction, \
ISODateString, ISOLangString, LocalizedEDTFDateString, SanitizedUnicode
from marshmallow_utils.schemas import GeoJSONSchema

from .utils import validate_entry

Expand Down Expand Up @@ -433,8 +434,9 @@ class PointSchema(Schema):
class LocationSchema(Schema):
"""Location schema."""

point = fields.Nested(PointSchema)
geometry = fields.Nested(GeoJSONSchema, required=True)
place = SanitizedUnicode(required=True)
identifiers = fields.Dict()
description = SanitizedUnicode()


Expand Down Expand Up @@ -480,6 +482,6 @@ class Meta:
rights = fields.List(fields.Nested(RightsSchema))
description = SanitizedUnicode(validate=validate.Length(min=3))
additional_descriptions = fields.List(fields.Nested(DescriptionSchema))
# locations = fields.List(fields.Nested(LocationSchema))
locations = fields.List(fields.Nested(LocationSchema))
funding = fields.List(fields.Nested(FundingSchema))
references = fields.List(fields.Nested(ReferenceSchema))
14 changes: 9 additions & 5 deletions tests/records/full-record.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@
"lang": "eng"
}],
"locations": [{
"point": {
"lat": 1,
"lon": 2
"geometry": {
"type": "multipoint",
"coordinates": [-32.94682, -60.63932]
},
"place": "home",
"description": "test"
"place": "test location place",
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde"
},
"description": "test location description"
}],
"funding": [{
"funder": {
Expand Down
108 changes: 108 additions & 0 deletions tests/services/schemas/test_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 CERN.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Test location schema."""

import pytest
from marshmallow import ValidationError

from invenio_rdm_records.services.schemas.metadata import LocationSchema


def test_valid_full():
valid_full = {
"geometry": {
"type": "point",
"coordinates": [-32.94682, -60.63932]
},
"place": "test location place",
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde",
},
"description": "test location description"
}
assert valid_full == LocationSchema().load(valid_full)


def test_valid_minimal():
valid_minimal = {
"geometry": {
"type": "multipoint",
"coordinates": [
[-32.94682, -60.63932], [-32.94682, -60.63932, 10.0]
]
},
"place": "test location place"

}
assert valid_minimal == LocationSchema().load(valid_minimal)


def test_invalid_geometry_type():
invalid_location = {
"geometry": {
"type": "invalid",
"coordinates": [-32.94682, -60.63932]
},
"place": "test location place",
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde",
},
"description": "test location description"
}
with pytest.raises(ValidationError):
data = LocationSchema().load(invalid_location)


def test_invalid_wrong_geometry_type():
invalid_location = {
"geometry": {
"type": "multipoint",
"coordinates": [-32.94682, -60.63932] # This is a point
},
"place": "test location place",
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde",
},
"description": "test location description"
}
with pytest.raises(ValidationError):
data = LocationSchema().load(invalid_location)


def test_invalid_no_geometry():
invalid_location = {
"place": "test location place",
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde",
},
"description": "test location description"
}
with pytest.raises(ValidationError):
data = LocationSchema().load(invalid_location)


def test_invalid_no_place():
invalid_location = {
"geometry": {
"type": "multipoint",
"coordinates": [
[-32.94682, -60.63932], [-32.94682, -60.63932, 10.0]
]
},
"identifiers": {
"wikidata": "12345abcde",
"geonames": "12345abcde",
},
"description": "test location description"
}
with pytest.raises(ValidationError):
data = LocationSchema().load(invalid_location)
Loading

0 comments on commit 02ea234

Please sign in to comment.