Skip to content

Commit

Permalink
Merge pull request #320 from jacquerie/add-author-builder
Browse files Browse the repository at this point in the history
builders: create an author builder
  • Loading branch information
jacquerie committed May 22, 2018
2 parents 963237a + 8291de0 commit 8ad58e4
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
37 changes: 37 additions & 0 deletions inspire_schemas/builders/authors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE.
# Copyright (C) 2014-2017 CERN.
#
# INSPIRE 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 3 of the License, or
# (at your option) any later version.
#
# INSPIRE 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 INSPIRE. If not, see <http://www.gnu.org/licenses/>.
#
# 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.

"""Author builder class and related code."""

from __future__ import absolute_import, division, print_function

from inspire_utils.name import normalize_name


class AuthorBuilder(object):
def __init__(self, author=None):
if author is None:
author = {}
self.obj = author

def set_name(self, value):
self.obj['name'] = {'value': normalize_name(value)}
83 changes: 83 additions & 0 deletions tests/unit/test_author_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE.
# Copyright (C) 2014-2017 CERN.
#
# INSPIRE 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 3 of the License, or
# (at your option) any later version.
#
# INSPIRE 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 INSPIRE. If not, see <http://www.gnu.org/licenses/>.
#
# 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.

from __future__ import absolute_import, division, print_function

from inspire_schemas.api import load_schema, validate
from inspire_schemas.builders.authors import AuthorBuilder


def test_author_builder_default_constructor():
expected = {}
result = AuthorBuilder()

assert expected == result.obj


def test_author_builder_copy_constructor():
expected = {'name': {'value': 'Torre, Riccardo'}}
result = AuthorBuilder({'name': {'value': 'Torre, Riccardo'}})

assert expected == result.obj


def test_author_builder_set_name():
schema = load_schema('authors')
subschema = schema['properties']['name']

author = AuthorBuilder()
author.set_name('Torre, Riccardo')

expected = {'value': 'Torre, Riccardo'}
result = author.obj['name']

assert validate(result, subschema) is None
assert expected == result


def test_author_builder_set_name_normalizes_name():
schema = load_schema('authors')
subschema = schema['properties']['name']

author = AuthorBuilder()
author.set_name('Riccardo Torre')

expected = {'value': 'Torre, Riccardo'}
result = author.obj['name']

assert validate(result, subschema) is None
assert expected == result


def test_author_builder_set_name_can_be_called_multiple_times():
schema = load_schema('authors')
subschema = schema['properties']['name']

author = AuthorBuilder()
author.set_name('Richard Tower')
author.set_name('Riccardo Torre')

expected = {'value': 'Torre, Riccardo'}
result = author.obj['name']

assert validate(result, subschema) is None
assert expected == result

0 comments on commit 8ad58e4

Please sign in to comment.