Skip to content

Commit

Permalink
Merge pull request #48 from ChiaraBi/fix_metadata_update
Browse files Browse the repository at this point in the history
models: don't override Type if the value is unkonwn
  • Loading branch information
Krzysztof Nowak authored May 4, 2018
2 parents 0d2eebf + c49a342 commit 6856975
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
35 changes: 23 additions & 12 deletions asclepias_broker/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# under the terms of the MIT License; see LICENSE file for more details.
"""Elasticsearch indexing module."""

import uuid
from copy import deepcopy
from itertools import chain

Expand Down Expand Up @@ -98,26 +99,36 @@ def index_documents(docs, bulk=False):

def build_doc(rel, src_grp=None, trg_grp=None, grouping=None):
"""Build the ES document for a relationship."""
if not src_grp:
if rel.type == GroupType.Identity:
# Fetch the supergroup (Version) of the Identity relations
src_grp = rel.source.supergroupsm2m[0].group
else:
src_grp = rel.source
src_meta = build_group_metadata(src_grp)

if not trg_grp:
trg_grp = rel.target
trg_meta = build_group_metadata(trg_grp)

if rel.type == GroupType.Identity:
# Fetch the supergroup (Version) of the Identity relations for metadata
src_meta = build_group_metadata(rel.source.supergroupsm2m[0].group)
if src_grp:
src_meta = build_group_metadata(src_grp)
elif rel.type == GroupType.Identity:
pass
else:
src_meta = build_group_metadata(rel.source)
rel_grp_int = rel.relation.value
# TODO: This this is the correct value, but there's a bug
# with superrelationshipsm2m being empty (shouldn't be the case)
# rel_grp_int = rel.superrelationshipsm2m[0].relationship_id.int

if trg_grp:
trg_meta = build_group_metadata(trg_grp)
else:
trg_meta = build_group_metadata(rel.target)
rel_grp_int = rel.id.int

# We deterministically recompute the document ID based on
# Source, Relationship and Target IDs
doc_uuid = uuid.UUID(int=(src_grp.id.int ^ rel_grp_int ^ trg_grp.id.int))

rel_meta = build_relationship_metadata(rel)
grouping = grouping or \
('identity' if rel.type == GroupType.Identity else 'version')
return {
'_id': 'v:{}'.format(str(rel.id)),
'_id': str(doc_uuid),
'_source': {
"ID": str(rel.id),
"Grouping": grouping,
Expand Down
12 changes: 10 additions & 2 deletions asclepias_broker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,11 @@ class GroupRelationshipM2M(db.Model, Timestamp):
nullable=False)

relationship = orm_relationship(GroupRelationship,
foreign_keys=[relationship_id])
foreign_keys=[relationship_id],
backref='subrelationshipsm2m')
subrelationship = orm_relationship(GroupRelationship,
foreign_keys=[subrelationship_id])
foreign_keys=[subrelationship_id],
backref='superrelationshipsm2m')

def __repr__(self):
"""String representation of the model."""
Expand Down Expand Up @@ -486,6 +488,8 @@ def update(self, payload, validate=True):
new_json = deepcopy(self.json or {})
for k in OVERRIDABLE_KEYS:
if payload.get(k):
if k == 'Type' and not _is_type_overridable(payload):
continue
new_json[k] = payload[k]
if validate:
jsonschema.validate(new_json, self.SCHEMA)
Expand All @@ -494,6 +498,10 @@ def update(self, payload, validate=True):
return self


def _is_type_overridable(new_json):
return new_json.get('Type', {}).get('Name', 'unknown') != 'unknown'


class GroupRelationshipMetadata(db.Model, Timestamp):
"""Metadata for a group relationship."""

Expand Down
7 changes: 7 additions & 0 deletions asclepias_broker/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
from .errors import PayloadValidationRESTError
from .models import Identifier

#
# UI Views
#

# TODO: we need this blueprint in order to run
# asclepias_broker.tasks.process_event
blueprint = Blueprint('asclepias_ui', __name__, template_folder='templates')
Expand All @@ -31,6 +35,9 @@ def ping():
return 'OK'


ping.talisman_view_options = {'force_https': False}


#
# REST API Views
#
Expand Down
32 changes: 32 additions & 0 deletions tests/model/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ def test_group_metadata(db):
)


def test_group_metadata_update_type(db):
g = Group(type=GroupType.Identity)
db.session.add(g)
db.session.commit()
minimal_gm = {'Type': {'Name': 'unknown'}}
gm = GroupMetadata(group_id=g.id, json=minimal_gm)
db.session.add(gm)
db.session.commit()
assert g.data == gm
assert g.data.json == gm.json

# Add Type
update_and_compare(gm, {'Title': 'Some other title',
'Type': {'Name': 'unknown'}})

# Change Type
update_and_compare(gm, {'Title': 'Some other title',
'Type': {'Name': 'software'}})

# Don't override Type
update_and_compare(gm, {'Type': {}}, {'Title': 'Some other title',
'Type': {'Name': 'software'}})
update_and_compare(gm, {'Type': {'Name': 'unknown'}},
{'Title': 'Some other title',
'Type': {'Name': 'software'}})

# Change Type
update_and_compare(gm, {'Type': {'Name': 'dataset'}},
{'Title': 'Some other title',
'Type': {'Name': 'dataset'}})


def test_group_relationship_metadata(db):
g_src = Group(type=GroupType.Identity)
g_trg = Group(type=GroupType.Identity)
Expand Down

0 comments on commit 6856975

Please sign in to comment.