This repository has been archived by the owner on May 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds the endpoint configuration for `Item.find_by_holding`. Signed-off-by: Martin Vesper <[email protected]>
- Loading branch information
Showing
7 changed files
with
343 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2017 CERN. | ||
# | ||
# Invenio 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. | ||
# | ||
# Invenio 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 Invenio; 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. | ||
|
||
"""Serializers for circulation search.""" | ||
import json | ||
|
||
from invenio_records_rest.serializers import JSONSerializer | ||
from invenio_records_rest.serializers.response import search_responsify | ||
from invenio_records_rest.serializers.schemas.json import RecordSchemaJSONV1 | ||
|
||
|
||
class RevisionSerializer(JSONSerializer): | ||
"""JSON serializer for items found by `Item.find_by_holding`.""" | ||
|
||
def serialize_search(self, pid_fetcher, search_result, links=None, | ||
item_links_factory=None): | ||
"""Serialize a search result. | ||
:param search_result: Elasticsearch search result. | ||
:param links: Dictionary of links to add to response. | ||
""" | ||
return json.dumps({ | ||
'hits': { | ||
'hits': [hit for hit in search_result['hits']['hits']], | ||
'total': search_result['hits']['total'], | ||
}, | ||
'links': links or {}, | ||
'aggregations': search_result.get('aggregations', {}), | ||
}, **self._format_args()) | ||
|
||
|
||
revision_serializer = search_responsify(RevisionSerializer(RecordSchemaJSONV1), | ||
'application/json') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of Invenio. | ||
# Copyright (C) 2017 CERN. | ||
# | ||
# Invenio 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. | ||
# | ||
# Invenio 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 Invenio; 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. | ||
|
||
|
||
"""Revision search tests.""" | ||
|
||
import datetime | ||
import json | ||
|
||
import pytest | ||
from elasticsearch_dsl.query import Bool, MultiMatch, Range | ||
|
||
from invenio_circulation.api import Item, ItemStatus, Location | ||
from invenio_circulation.search import ItemRevisionSearch | ||
from invenio_circulation.validators import LoanItemSchema | ||
|
||
|
||
def test_item_revision_search_result(): | ||
search_result = ['result1', 'result2'] | ||
result = ItemRevisionSearch.Results(search_result) | ||
|
||
assert result.hits.total == len(search_result) | ||
assert result.results == search_result | ||
assert result.to_dict() == { | ||
'hits': { | ||
'hits': search_result, | ||
'total': len(search_result) | ||
} | ||
} | ||
|
||
|
||
def test_item_revision_search_query(): | ||
# Testing the initial setup | ||
item_revision_search = ItemRevisionSearch() | ||
|
||
assert item_revision_search._index == [''] | ||
assert item_revision_search._query == {} | ||
|
||
# Testing a MultiMatch query | ||
item_revision_search = ItemRevisionSearch() | ||
mm = MultiMatch(fields=['foo'], query='bar') | ||
|
||
item_revision_search.query(mm) | ||
|
||
assert item_revision_search._query == {'foo': 'bar'} | ||
|
||
# Testing a range query | ||
item_revision_search = ItemRevisionSearch() | ||
r = Range(foo={'gte': 'bar', 'lte': u'baz'}) | ||
|
||
item_revision_search.query(r) | ||
|
||
assert item_revision_search._query == {'foo': ['bar', 'baz']} | ||
|
||
|
||
def test_item_revision_search_execute(app, db): | ||
# Prepare the item | ||
item = Item.create({'foo': 'bar'}) | ||
db.session.commit() | ||
|
||
# Create loan data | ||
la = LoanItemSchema() | ||
la.context['item'] = item | ||
|
||
# Prepare the loan data | ||
tmp = la.load({'user_id': 1}).data | ||
data = la.dump(tmp).data | ||
|
||
# Loan item | ||
item.loan_item(**data) | ||
item.commit() | ||
db.session.commit() | ||
|
||
# Return item | ||
item.return_item() | ||
item.commit() | ||
db.session.commit() | ||
|
||
# Prepare ItemRevisionSearch | ||
item_revision_search = ItemRevisionSearch() | ||
mm = MultiMatch(fields=['user_id'], query=1) | ||
|
||
result = item_revision_search.query(mm).execute() | ||
|
||
assert result.hits.total == 1 | ||
assert result.to_dict() == { | ||
'hits': { | ||
'hits': [item], | ||
'total': 1 | ||
} | ||
} | ||
|
||
|
||
def test_item_revision_search_dummies(): | ||
"""Tests necessary dummy implementations.""" | ||
item_revision_search = ItemRevisionSearch() | ||
|
||
assert item_revision_search == item_revision_search[0] | ||
assert item_revision_search == item_revision_search.params() |
Oops, something went wrong.