diff --git a/MANIFEST.in b/MANIFEST.in index 071336b..e21365c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -35,3 +35,19 @@ include .dockerignore include .editorconfig include .tx/config recursive-include json_merger *.po *.pot *.mo + +# added by check_manifest.py +include *.rst +include *.sh +include *.txt +include LICENSE +include babel.ini +include pytest.ini +recursive-include docs *.bat +recursive-include docs *.py +recursive-include docs *.rst +recursive-include docs Makefile +recursive-include examples *.py +recursive-include tests *.json +recursive-include tests *.py +recursive-include tests *.txt diff --git a/json_merger/__init__.py b/json_merger/__init__.py index 68cec09..5833890 100644 --- a/json_merger/__init__.py +++ b/json_merger/__init__.py @@ -27,6 +27,7 @@ from __future__ import absolute_import, print_function from .ext import JsonMerger +from .merger import merge_records from .version import __version__ -__all__ = ('__version__', 'JsonMerger') +__all__ = ('__version__', 'JsonMerger', 'merge_records') diff --git a/json_merger/merger.py b/json_merger/merger.py new file mode 100644 index 0000000..1cdeda6 --- /dev/null +++ b/json_merger/merger.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Inspirehep. +# Copyright (C) 2016 CERN. +# +# Inspirehep is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Inspirehep is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Inspirehep; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. + +"""Invenio module that is able to merge json record objects.""" + +from __future__ import absolute_import, print_function + + +def merge_records(src, update): + """Merge update upon src.""" + return src diff --git a/tests/conftest.py b/tests/conftest.py index a850370..69f58a6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,9 +27,14 @@ from __future__ import absolute_import, print_function +import json +import os + import pytest from flask import Flask +from json_merger import JsonMerger + @pytest.fixture() def app(): @@ -38,4 +43,28 @@ def app(): app.config.update( TESTING=True ) + JsonMerger(app) return app + + +@pytest.fixture() +def json_loader(): + class _Loader(object): + def __init__(self, basedir): + self.basedir = basedir + + def _read_file(self, test_name, file_name): + with open(os.path.join(self.basedir, test_name, file_name)) as f: + return f.read() + + def load_single(self, test_name, file_name): + return json.loads(self._read_file(test_name, file_name)) + + def load_test(self, test_name): + src = self.load_single(test_name, 'src.json') + update = self.load_single(test_name, 'update.json') + expected = self.load_single(test_name, 'expected.json') + desc = self._read_file(test_name, 'description.txt') + return src, update, expected, desc + + return _Loader('./tests/fixtures/') diff --git a/tests/fixtures/author_delete/description.txt b/tests/fixtures/author_delete/description.txt new file mode 100644 index 0000000..f95c3b1 --- /dev/null +++ b/tests/fixtures/author_delete/description.txt @@ -0,0 +1 @@ +Deleting an author should preserve affiliations for the other. diff --git a/tests/fixtures/author_delete/expected.json b/tests/fixtures/author_delete/expected.json new file mode 100644 index 0000000..ffb873f --- /dev/null +++ b/tests/fixtures/author_delete/expected.json @@ -0,0 +1,29 @@ +{ + "authors": [ + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_delete/src.json b/tests/fixtures/author_delete/src.json new file mode 100644 index 0000000..b44b12a --- /dev/null +++ b/tests/fixtures/author_delete/src.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_delete/update.json b/tests/fixtures/author_delete/update.json new file mode 100644 index 0000000..ed0f0b4 --- /dev/null +++ b/tests/fixtures/author_delete/update.json @@ -0,0 +1,9 @@ +{ + "authors": [ + { + "curated_relation": false, + "full_name": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_delete_and_typo/description.txt b/tests/fixtures/author_delete_and_typo/description.txt new file mode 100644 index 0000000..35d6e5e --- /dev/null +++ b/tests/fixtures/author_delete_and_typo/description.txt @@ -0,0 +1 @@ +Deleting an author and editing another should preserve affiliations for the second. diff --git a/tests/fixtures/author_delete_and_typo/expected.json b/tests/fixtures/author_delete_and_typo/expected.json new file mode 100644 index 0000000..8f82308 --- /dev/null +++ b/tests/fixtures/author_delete_and_typo/expected.json @@ -0,0 +1,29 @@ +{ + "authors": [ + { + "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": "Ooguri, Hirosi, Update", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_delete_and_typo/src.json b/tests/fixtures/author_delete_and_typo/src.json new file mode 100644 index 0000000..b44b12a --- /dev/null +++ b/tests/fixtures/author_delete_and_typo/src.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_delete_and_typo/update.json b/tests/fixtures/author_delete_and_typo/update.json new file mode 100644 index 0000000..ec06969 --- /dev/null +++ b/tests/fixtures/author_delete_and_typo/update.json @@ -0,0 +1,9 @@ +{ + "authors": [ + { + "curated_relation": false, + "full_name": "Ooguri, Hirosi, Update", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend/description.txt b/tests/fixtures/author_prepend/description.txt new file mode 100644 index 0000000..1db094e --- /dev/null +++ b/tests/fixtures/author_prepend/description.txt @@ -0,0 +1 @@ +Prepending a new author should preserve affiliations for all the others. diff --git a/tests/fixtures/author_prepend/expected.json b/tests/fixtures/author_prepend/expected.json new file mode 100644 index 0000000..dadb74c --- /dev/null +++ b/tests/fixtures/author_prepend/expected.json @@ -0,0 +1,41 @@ +{ + "authors": [ + { + "full_name": "New, Author" + }, + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend/src.json b/tests/fixtures/author_prepend/src.json new file mode 100644 index 0000000..b44b12a --- /dev/null +++ b/tests/fixtures/author_prepend/src.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend/update.json b/tests/fixtures/author_prepend/update.json new file mode 100644 index 0000000..f1d3b5b --- /dev/null +++ b/tests/fixtures/author_prepend/update.json @@ -0,0 +1,16 @@ +{ + "authors": [ + { + "full_name": "New, Author" + }, + { + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "curated_relation": false, + "full_name": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend_and_typo/description.txt b/tests/fixtures/author_prepend_and_typo/description.txt new file mode 100644 index 0000000..52496a4 --- /dev/null +++ b/tests/fixtures/author_prepend_and_typo/description.txt @@ -0,0 +1 @@ +Update typo in author plus adding a new record should preserve affiliations. diff --git a/tests/fixtures/author_prepend_and_typo/expected.json b/tests/fixtures/author_prepend_and_typo/expected.json new file mode 100644 index 0000000..3bf613d --- /dev/null +++ b/tests/fixtures/author_prepend_and_typo/expected.json @@ -0,0 +1,41 @@ +{ + "authors": [ + { + "full_name": "New, Author" + }, + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary" + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend_and_typo/src.json b/tests/fixtures/author_prepend_and_typo/src.json new file mode 100644 index 0000000..b44b12a --- /dev/null +++ b/tests/fixtures/author_prepend_and_typo/src.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_prepend_and_typo/update.json b/tests/fixtures/author_prepend_and_typo/update.json new file mode 100644 index 0000000..e4106f7 --- /dev/null +++ b/tests/fixtures/author_prepend_and_typo/update.json @@ -0,0 +1,16 @@ +{ + "authors": [ + { + "full_name": "New, Author" + }, + { + "curated_relation": false, + "full_name": "Horowitz, Gary" + }, + { + "curated_relation": false, + "full_name": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_typo/description.txt b/tests/fixtures/author_typo/description.txt new file mode 100644 index 0000000..854ef6a --- /dev/null +++ b/tests/fixtures/author_typo/description.txt @@ -0,0 +1 @@ +Update typo in author name should preserve affiliations. diff --git a/tests/fixtures/author_typo/expected.json b/tests/fixtures/author_typo/expected.json new file mode 100644 index 0000000..443a334 --- /dev/null +++ b/tests/fixtures/author_typo/expected.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary" + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_typo/src.json b/tests/fixtures/author_typo/src.json new file mode 100644 index 0000000..b44b12a --- /dev/null +++ b/tests/fixtures/author_typo/src.json @@ -0,0 +1,38 @@ +{ + "authors": [ + { + "affiliations": [ + { + "value": "UC, Santa Barbara" + } + ], + "curated_relation": false, + "full_name": "Horowitz, Gary T." + }, + { + "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": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/fixtures/author_typo/update.json b/tests/fixtures/author_typo/update.json new file mode 100644 index 0000000..b2f6433 --- /dev/null +++ b/tests/fixtures/author_typo/update.json @@ -0,0 +1,13 @@ +{ + "authors": [ + { + "curated_relation": false, + "full_name": "Horowitz, Gary" + }, + { + "curated_relation": false, + "full_name": "Ooguri, Hirosi", + "inspire_id": "INSPIRE-00113241" + } + ] +} diff --git a/tests/test_json_merger.py b/tests/test_ext.py similarity index 97% rename from tests/test_json_merger.py rename to tests/test_ext.py index a5e7125..df0afb5 100644 --- a/tests/test_json_merger.py +++ b/tests/test_ext.py @@ -28,7 +28,6 @@ from __future__ import absolute_import, print_function from flask import Flask -from flask_babelex import Babel from json_merger import JsonMerger diff --git a/tests/test_merger_acceptance.py b/tests/test_merger_acceptance.py new file mode 100644 index 0000000..9b91d18 --- /dev/null +++ b/tests/test_merger_acceptance.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Inspirehep. +# Copyright (C) 2016 CERN. +# +# Inspirehep is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# Inspirehep is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Inspirehep; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307, USA. +# +# In applying this license, CERN does not +# waive the privileges and immunities granted to it by virtue of its status +# as an Intergovernmental Organization or submit itself to any jurisdiction. + + +"""Acceptance scenarios for the merger.""" + +from __future__ import absolute_import, print_function + +import pytest + +from json_merger import merge_records + + +@pytest.mark.xfail +@pytest.mark.parametrize('scenario', [ + 'author_typo', + 'author_prepend', + 'author_delete', + 'author_prepend_and_typo', + 'author_delete_and_typo']) +def test_expected_outcome(json_loader, scenario): + src, update, expected, desc = json_loader.load_test(scenario) + assert merge_records(src, update) == expected, desc