Skip to content

Commit

Permalink
tests: extra acceptance scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Bivol <mihai.bivol@cern.ch>
  • Loading branch information
mihaibivol committed Apr 19, 2016
1 parent 98a70bb commit b121999
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 11 deletions.
24 changes: 24 additions & 0 deletions json_merger/comparator.py
Expand Up @@ -24,6 +24,9 @@

from __future__ import absolute_import, print_function

from .nothing import NOTHING
from .utils import get_obj_at_key_path


class BaseComparator(object):

Expand Down Expand Up @@ -62,6 +65,27 @@ def get_matches(self, src, src_idx):
if comparator(src_idx, trg_idx)]


class PrimaryKeyComparator(BaseComparator):
"""Renders two objects as equal if they have the same primary key.
If two objects have at least one of the configured primary_key_fields equal
then they are equal.
"""

primary_key_fields = ['pk']

def equal(self, idx_l1, idx_l2):
if self.l1[idx_l1] == self.l2[idx_l2]:
return True
for field in self.primary_key_fields:
key_path = tuple(k for k in field.split('.') if k)
o1 = get_obj_at_key_path(self.l1[idx_l1], key_path, NOTHING)
o2 = get_obj_at_key_path(self.l2[idx_l2], key_path, NOTHING)
if o1 != NOTHING and o2 != NOTHING and o1 == o2:
return True
return False


class DefaultComparator(BaseComparator):

def equal(self, idx_l1, idx_l2):
Expand Down
2 changes: 1 addition & 1 deletion json_merger/dict_merger.py
Expand Up @@ -50,7 +50,7 @@ def _get_list_fields(obj, res, key_path=()):
def patch_to_conflict_set(patch):
"""Translate a dictdiffer conflict into a json_merger one."""
patch_type, dotted_key, value = patch
key_path = tuple([k for k in dotted_key.split('.') if k])
key_path = tuple(k for k in dotted_key.split('.') if k)

conflicts = set()
if patch_type == REMOVE:
Expand Down
49 changes: 39 additions & 10 deletions tests/acceptance/test_merger_update.py
Expand Up @@ -30,18 +30,43 @@
import pytest

from json_merger import UpdateMerger, MergeError
from json_merger.comparator import BaseComparator
from json_merger.comparator import PrimaryKeyComparator
from json_merger.conflict import Conflict
from json_merger.list_unify import UnifierOps


class AuthorComparator(BaseComparator):
class AuthorComparator(PrimaryKeyComparator):

primary_key_fields = ['inspire_id']

def equal(self, l1_idx, l2_idx):
obj1 = self.l1[l1_idx]
obj2 = self.l2[l2_idx]
if 'inspire_id' in obj1 and 'inspire_id' in obj2:
return obj1['inspire_id'] == obj2['inspire_id']
return obj1['full_name'][:5] == obj2['full_name'][:5]
ret = super(AuthorComparator, self).equal(l1_idx, l2_idx)
if not ret:
return (self.l1[l1_idx]['full_name'][:5] ==
self.l2[l2_idx]['full_name'][:5])
else:
return True


class TitleComparator(PrimaryKeyComparator):

primary_key_fields = ['source']


class AffiliationComparator(PrimaryKeyComparator):

primary_key_fields = ['value']


COMPARATORS = {
'authors': AuthorComparator,
'authors.affiliations': AffiliationComparator,
'titles': TitleComparator
}
LIST_MERGE_OPS = {
'titles': UnifierOps.KEEP_UPDATE_AND_HEAD_ENTITIES_HEAD_FIRST,
'authors.affiliations': UnifierOps.KEEP_UPDATE_AND_HEAD_ENTITIES_HEAD_FIRST
}


@pytest.mark.parametrize('scenario', [
Expand All @@ -56,11 +81,15 @@ def equal(self, l1_idx, l2_idx):
'author_reorder_conflict',
'author_replace_and_single_curator_typo_fix',
'author_delete_and_double_curator_typo_fix',
'author_curator_collab_addition'])
'author_curator_collab_addition',
'author_affiliation_addition',
'title_addition',
'title_change'])
def test_author_typo_scenarios(update_fixture_loader, scenario):
comparators = {'authors': AuthorComparator}
root, head, update, exp, desc = update_fixture_loader.load_test(scenario)
merger = UpdateMerger(root, head, update, comparators=comparators)
merger = UpdateMerger(root, head, update,
comparators=COMPARATORS,
list_merge_ops=LIST_MERGE_OPS)
if exp.get('conflicts'):
with pytest.raises(MergeError) as excinfo:
merger.merge()
Expand Down
@@ -0,0 +1 @@
Preprint should add new affiliation and preserve the others.
@@ -0,0 +1,42 @@
{
"authors": [
{
"affiliations": [
{
"value": "UC, Santa Barbara"
}
],
"curated_relation": false,
"full_name": "Cox, Brian E."
},
{
"affiliations": [
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "UC, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "LBL, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "Santa Barbara, KITP"
},
{
"value": "BBC"
}
],
"curated_relation": false,
"full_name": "O'Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}

@@ -0,0 +1,38 @@
{
"authors": [
{
"affiliations": [
{
"value": "UC, Santa Barbara"
}
],
"curated_relation": false,
"full_name": "Cox, Brian E."
},
{
"affiliations": [
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "UC, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "LBL, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "Santa Barbara, KITP"
}
],
"curated_relation": false,
"full_name": "O'Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
@@ -0,0 +1,11 @@
{
"authors": [
{
"full_name": "Cox, Brian"
},
{
"full_name": "O Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
@@ -0,0 +1,19 @@
{
"authors": [
{
"full_name": "Cox, Brian"
},
{
"affiliations": [
{
"value": "Santa Barbara, KITP"
},
{
"value": "BBC"
}
],
"full_name": "O Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
@@ -0,0 +1 @@
Title addition in Update should preserve both titles with the HEAD first.
49 changes: 49 additions & 0 deletions tests/fixtures/update_scenarios/title_addition/expected.json
@@ -0,0 +1,49 @@
{
"titles": [
{
"title": "Effects of Stargazing, Live",
"source": "curator"
},
{
"title": "Effects of Stargazing, Live",
"source": "preprint"
}
],
"authors": [
{
"affiliations": [
{
"value": "UC, Santa Barbara"
}
],
"curated_relation": false,
"full_name": "Cox, Brian E."
},
{
"affiliations": [
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "UC, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "LBL, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "Santa Barbara, KITP"
}
],
"curated_relation": false,
"full_name": "O'Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}

44 changes: 44 additions & 0 deletions tests/fixtures/update_scenarios/title_addition/head.json
@@ -0,0 +1,44 @@
{
"titles": [
{
"title": "Effects of Stargazing, Live",
"source": "curator"
}
],
"authors": [
{
"affiliations": [
{
"value": "UC, Santa Barbara"
}
],
"curated_relation": false,
"full_name": "Cox, Brian E."
},
{
"affiliations": [
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "UC, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "LBL, Berkeley"
},
{
"record": {
"$ref": "http://localhost:5000/api/institutions/903889"
},
"value": "Santa Barbara, KITP"
}
],
"curated_relation": false,
"full_name": "O'Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
11 changes: 11 additions & 0 deletions tests/fixtures/update_scenarios/title_addition/root.json
@@ -0,0 +1,11 @@
{
"authors": [
{
"full_name": "Cox, Brian"
},
{
"full_name": "O Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
17 changes: 17 additions & 0 deletions tests/fixtures/update_scenarios/title_addition/update.json
@@ -0,0 +1,17 @@
{
"titles": [
{
"title": "Effects of Stargazing, Live",
"source": "preprint"
}
],
"authors": [
{
"full_name": "Cox, Brian"
},
{
"full_name": "O Brien, Dara",
"inspire_id": "INSPIRE-00113241"
}
]
}
@@ -0,0 +1 @@
Update title change should preserve both titles with the changed title for the second one.

0 comments on commit b121999

Please sign in to comment.