Skip to content

Commit

Permalink
global: increase coverage
Browse files Browse the repository at this point in the history
* Adds some tests.
* Removes useless code.

Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d committed Aug 29, 2022
1 parent 2d8eaeb commit 2429dae
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 26 deletions.
3 changes: 1 addition & 2 deletions invenio_sip2/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def execute(self, message, client):
current_logger \
.debug(f'[PatronInformation]: handler response: {patron_account}')
# TODO: better way to begin session
# Begin session
# start patron session
client['patron_session'] = {
'patron_id': patron_id,
'language': message.i18n_language
Expand All @@ -284,7 +284,6 @@ def execute(self, message, client):
)

summary = SelfcheckSummary(message.summary)

# add optional fields
for optional_field in self.optional_fields:
# TODO: use custom handler to get specified summary field.
Expand Down
2 changes: 1 addition & 1 deletion invenio_sip2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def summary(self):
return self.get_fixed_field_value('summary')

def _parse_request(self):
"""Parse the request sended by the selfcheck."""
"""Parse the request sent by the selfcheck."""
txt = self.message_text[2:]
# try to extract sequence number and checksum
error_txt = txt[-9:]
Expand Down
20 changes: 0 additions & 20 deletions invenio_sip2/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
"""Invenio-SIP2 exceptions."""


# Actions
class InvalidSelfCheckActionError(Exception):
"""Action not found in sip2 configuration."""

def __init__(self, action=None, **kwargs):
"""Initialize exception."""
self.description = f"Invalid selfcheck '{action}'"
super().__init__(**kwargs)


class SelfcheckCirculationError(Exception):
"""Selfcheck Circulation error."""

Expand All @@ -37,16 +27,6 @@ def __init__(self, error, data, **kwargs):
super().__init__(error, **kwargs)


# Message
class InvalidSelfCheckMessageError(Exception):
"""Invalid SIP2 message."""

def __init__(self, message=None, **kwargs):
"""Initialize exception."""
self.description = f"InvalidSelfCheckMessageError '{message}'"
super().__init__(**kwargs)


class UnknownFieldIdMessageError(Exception):
"""Unknown SIP2 field id."""

Expand Down
2 changes: 1 addition & 1 deletion invenio_sip2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,4 @@ def __init__(self, text):

def is_needed(self, key):
"""Check if the given information is needed."""
return getattr(self, key) or False
return hasattr(self, key)
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def base_app(request):
CACHE_REDIS_URL='redis://localhost:6379/0',
SIP2_DATASTORE_HANDLER='invenio_sip2.datastore:Sip2RedisDatastore',
SIP2_DATASTORE_REDIS_URL='redis://localhost:6379/1',
SIP2_LOGGING_FS_LOGFILE='./log/sip2.log',
SIP2_ERROR_DETECTION=True,
SIP2_REMOTE_ACTION_HANDLERS=dict(
test_ils=dict(
login_handler=remote_login_handler,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def selfckeck_validate_patron_message(enable_patron_message):
@pytest.fixture(scope="module")
def patron_information_message():
"""Patron information message."""
return '6300020200716 211818 AOinstitution_id' \
return '6300020200716 211818Y AOinstitution_id' \
'|AApatron_identifier|ADpatron_pwd|AY3AZECEC'


Expand Down
14 changes: 14 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from invenio_sip2.actions.base import Action
from invenio_sip2.api import Message
from invenio_sip2.errors import CommandNotFound
from invenio_sip2.proxies import current_sip2


Expand All @@ -33,6 +34,14 @@ def test_sip2_actions_interface(app):
with pytest.raises(NotImplementedError):
action.execute()

with pytest.raises(CommandNotFound):
action = Action(
command='85', response='86', message='interface_message')
action.execute()

# test action from extension
current_sip2.sip2.execute('wrong command')


@mock.patch('invenio_sip2.actions.actions.selfcheck_login_handler',
mock.MagicMock(return_value={'authenticated': False}))
Expand Down Expand Up @@ -88,6 +97,11 @@ def test_patron_information(app, dummy_client, patron_information_message):
Message(request=patron_information_message),
client=dummy_client
)
data = response.dumps()
required_fields = app.config.get('SIP2_MESSAGE_TYPES').get('64')\
.get('required_fields')
for field in required_fields:
assert data[field]
assert str(response).startswith('64')


Expand Down
42 changes: 42 additions & 0 deletions tests/test_messages_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
#
# INVENIO-SIP2
# Copyright (C) 2020 UCLouvain
#
# 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/>.

"""Invenio-sip2 actions test."""

import pytest

from invenio_sip2.api import Message
from invenio_sip2.errors import CommandNotFound, UnknownFieldIdMessageError


def test_messages_api(app, patron_information_message):
"""Test invenio-sip2 Message api."""
# instantiate message class with wrong a message
with pytest.raises(CommandNotFound):
message = Message(request='bad sip2 message')

# use well-formed message
message = Message(request=patron_information_message)
patron_id = message.get_field_values('patron_id')
data = message.dumps()
assert data['patron_id'] in patron_id
for key in ['_sip2', 'message_type', 'institution_id', 'patron_id',
'checksum', 'sequence_number', 'transaction_date']:
assert data[key]
# test unknown message field
with pytest.raises(UnknownFieldIdMessageError):
field = message.get_field_values('message_type')
2 changes: 1 addition & 1 deletion tests/test_views_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_monitoring_clients(app, users):
assert res.status_code == 200


def test_get_server(app, users, server, dummy_client):
def test_get_server(app, users, server):
"""Test monitoring servers."""
with app.test_client() as client:
server_url = url_for(
Expand Down
2 changes: 2 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def remote_patron_account_handler(patron_id, **kwargs):
language='und',
currency_type='EUR'
)
response.get('hold_items', []).append('item_pid_1')
response.get('hold_items', []).append('item_pid_2')
return response


Expand Down

0 comments on commit 2429dae

Please sign in to comment.