Skip to content

Commit

Permalink
tests: Increase code coverage
Browse files Browse the repository at this point in the history
BETTER Mock functions responses to increase code coverage.

Signed-off-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze committed Jul 24, 2019
1 parent 272f6d3 commit 93a038e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
5 changes: 3 additions & 2 deletions sonar/modules/documents/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ def import_documents(institution, pages):
.format(url=url,
first_record=(current_page*10-9),
institution=key.upper()), stream=True)
response.raw.decode_content = True

if(response.status_code != 200):
if response.status_code != 200:
raise ClickException('Request to "{url}" failed'.format(url=url))

response.raw.decode_content = True

ids = []

for data in split_stream(response.raw):
Expand Down
33 changes: 28 additions & 5 deletions tests/ui/documents/test_documents_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

"""Test CLI for importing documents."""

import click
import pytest
from click.exceptions import ClickException
import requests
from click.testing import CliRunner
from pytest_invenio.fixtures import script_info

import sonar.modules.documents.cli as Cli
from sonar.modules.institutions.api import InstitutionRecord


def test_import_documents(app, script_info):
def test_import_documents(app, script_info, monkeypatch):
"""Test import documents."""
runner = CliRunner()

Expand All @@ -31,6 +28,17 @@ def test_import_documents(app, script_info):
"name": "Test"
}, dbcommit=True)

app.config.update(SONAR_DOCUMENTS_INSTITUTIONS_MAP=None)

result = runner.invoke(Cli.import_documents, ['test'], obj=script_info)
assert result.output.find(
'Institution map not found in configuration') != -1

app.config.update(SONAR_DOCUMENTS_INSTITUTIONS_MAP=dict(
usi='ticino.unisi',
hevs='valais.hevs'
))

result = runner.invoke(Cli.import_documents, ['test'], obj=script_info)
assert result.output.find(
'Institution map for "test" not found in configuration') != -1
Expand All @@ -46,3 +54,18 @@ def test_import_documents(app, script_info):
result = runner.invoke(
Cli.import_documents, ['usi', '--pages=1'], obj=script_info)
assert result.exit_code == 0

class MockResponse():
"""Mock response."""
def __init__(self):
self.status_code = 500

def mock_get(*args, **kwargs):
return MockResponse()

monkeypatch.setattr(requests, 'get', mock_get)

result = runner.invoke(
Cli.import_documents, ['usi', '--pages=1'], obj=script_info)
assert result.exit_code == 1
assert result.output.find('failed') != -1
71 changes: 69 additions & 2 deletions tests/ui/documents/test_marc21tojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""Test marc21 to json converter."""

import pytest
import requests
from dojson.contrib.marc21.utils import create_record

import sonar.modules.documents.dojson.contrib.marc21tojson.model as model
Expand All @@ -21,13 +22,35 @@ def test_remove_punctuation():
'lorem ipsum - / ; :,') == 'lorem ipsum'


def test_get_mef_person_link():
def test_get_mef_person_link(monkeypatch):
"""Test getting MEF link."""
assert model.get_mef_person_link(None, '', '') is None

assert model.get_mef_person_link(
'(RERO)A012327677', '', '')[:28] == 'https://mef.rero.ch/api/mef/'

class MockResponse():
"""Mock response."""
def __init__(self):
self.status_code = 200

@staticmethod
def json():
return {}

mock_response = MockResponse()

def mock_get(*args, **kwargs):
return mock_response

monkeypatch.setattr(requests, 'get', mock_get)
assert not model.get_mef_person_link('(RERO)A012327677', '', '')

mock_response.status_code = 500

monkeypatch.setattr(requests, 'get', mock_get)
assert not model.get_mef_person_link('(RERO)A012327677', '', '')


def test_marc21_to_type():
"""
Expand Down Expand Up @@ -256,9 +279,53 @@ def test_marc21_to_languages(app):
assert 'translatedFrom' not in data


def test_marc21_to_authors():
def test_marc21_to_authors(monkeypatch):
"""Test dojson marc21_to_authors."""

monkeypatch.setattr(
'sonar.modules.documents.dojson.contrib.marc21tojson.model.'
'get_mef_person_link',
lambda *args: 'link_to_reference')

marc21xml = """
<record>
<datafield tag="100" ind1=" " ind2=" ">
<subfield code="0">123456</subfield>
<subfield code="a">Jean-Paul</subfield>
<subfield code="b">II</subfield>
<subfield code="c">Pape</subfield>
<subfield code="d">1954-</subfield>
</datafield>
<datafield tag="700" ind1=" " ind2=" ">
<subfield code="a">Dumont, Jean</subfield>
<subfield code="c">Historien</subfield>
<subfield code="d">1921-2014</subfield>
</datafield>
<datafield tag="710" ind1=" " ind2=" ">
<subfield code="a">RERO</subfield>
</datafield>
</record>
"""
marc21json = create_record(marc21xml)
data = marc21tojson.do(marc21json)
authors = data.get('authors')
assert authors == [
{
'$ref': 'link_to_reference',
'type': 'person'
},
{
'name': 'Dumont, Jean',
'type': 'person',
'date': '1921-2014',
'qualifier': 'Historien'
},
{
'name': 'RERO',
'type': 'organisation'
}
]

marc21xml = """
<record>
<datafield tag="100" ind1=" " ind2=" ">
Expand Down

0 comments on commit 93a038e

Please sign in to comment.