Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
Signed-off-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Aug 25, 2022
1 parent dae2214 commit edbec1b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
22 changes: 11 additions & 11 deletions sonar/modules/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ def part_of_format(part_of):
:param part_of: Object representing partOf property
"""
items = []
contributions = []
document = part_of.get('document', {})
if document.get('title'):
items.append(document['title'])

if 'contribution' in document:
contribution = document['contribution'].pop(0);
if len(document['contribution']) > 0:
contributions.append(' / {value} ; '.format(value=contribution))
contributions.extend(
'{value}. '.format(value=contrib)
for contrib in document['contribution']
formated_contribs = []
first_contrib = document['contribution'][0]
other_contribs = document['contribution'][1:]
if other_contribs:
formated_contribs.append(f' / {first_contrib} ; ')
formated_contribs.extend(
f'{contrib}. ' for contrib in other_contribs
)
else:
contributions.append(' / {value}. '.format(value=contribution))
if contributions:
items.append(''.join(contributions))
if items and not contributions:
formated_contribs.append(f' / {first_contrib}. ')
items.append(''.join(formated_contribs))
elif items:
items.append('. ')

if 'publication' in document and 'statement' in document['publication']:
Expand Down
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

import copy
import os
import shutil
import sys
import tempfile
from datetime import date
from io import BytesIO

import pytest
from dotenv import load_dotenv
from flask_principal import ActionNeed
from invenio_access.models import ActionUsers, Role
from invenio_accounts.ext import hash_password
Expand Down Expand Up @@ -109,6 +112,43 @@ def es(appctx):
current_search_client.indices.delete_template('*')


@pytest.fixture(scope='module')
def instance_path():
"""Temporary instance path.
Scope: module
This fixture creates a temporary directory if the
environment variable ``INVENIO_INSTANCE_PATH`` is not be set.
This directory is then automatically removed.
"""
# load .env, .flaskenv
load_dotenv()
invenio_instance_path = os.environ.get('INVENIO_INSTANCE_PATH')
invenio_static_folder = os.environ.get('INVENIO_STATIC_FOLDER')
path = invenio_instance_path
# static folder
if not invenio_static_folder:
if invenio_instance_path:
os.environ['INVENIO_STATIC_FOLDER'] = os.path.join(
invenio_instance_path, 'static')
else:
os.environ['INVENIO_STATIC_FOLDER'] = os.path.join(
sys.prefix, 'var/instance/static')
# instance path
if not path:
path = tempfile.mkdtemp()
os.environ['INVENIO_INSTANCE_PATH'] = path
yield path
# clean static folder variable
if not invenio_static_folder:
os.environ.pop('INVENIO_STATIC_FOLDER', None)
# clean instance path variable and remove temp dir
if not invenio_instance_path:
os.environ.pop('INVENIO_INSTANCE_PATH', None)
shutil.rmtree(path)


@pytest.fixture(scope='module', autouse=True)
def app_config(app_config):
"""Define configuration for module."""
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/documents/serializers/test_document_json_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2021 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Test schema.org marshmallow schema."""


from sonar.modules.documents.marshmallow import DocumentMetadataSchemaV1


def test_partOf(document):
"""Test partOf serialization."""
document = {
'pid': '1',
'organisation': [{
'$ref': 'https://sonar.rero.ch/api/organisations/org'
}],
'partOf': [{
'document': {
'title': 'Host document',
'contribution': ['Muller']
}
}]
}
assert DocumentMetadataSchemaV1().dump(
document)['partOf'][0]['document']['contribution'] == ['Muller']

0 comments on commit edbec1b

Please sign in to comment.