def test_basic_search(app, db, es): """Test basic search functionality.""" # The index should be empty assert len(ItemSearch().execute()) == 0 # Create item1, search for everything item1 = Item.create({}) item1.commit() record_indexer = RecordIndexer() record_indexer.index(item1) current_search.flush_and_refresh('_all') assert len(ItemSearch().execute()) == 1 # Create item2, search for everything again item2 = Item.create({'foo': 'bar'}) item2.commit() record_indexer.index(item2) current_search.flush_and_refresh('_all') assert len(ItemSearch().execute()) == 2 # Search for item2 assert len(ItemSearch().query('match', foo='bar').execute()) == 1 # Search for nonsense assert len(ItemSearch().query('match', foo='banana').execute()) == 0
def test_loan_item_marshmallow(app, db): """Use LoanItemSchema to validate loan_item parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = LoanItemSchema() circulation_event_schema.context['item'] = item # Valid arguments assert not circulation_event_schema.validate({}) assert not circulation_event_schema.validate({ 'start_date': datetime.date.today(), 'end_date': datetime.date.today() + datetime.timedelta(weeks=4), }) # Invalid start date errors = circulation_event_schema.validate({ 'start_date': datetime.date.today() + datetime.timedelta(days=1), 'end_date': datetime.date.today() + datetime.timedelta(weeks=4), }) assert 'start_date' in errors # Invalid duration errors = circulation_event_schema.validate({ 'start_date': datetime.date.today(), 'end_date': datetime.date.today() + datetime.timedelta(weeks=5), }) assert '_schema' in errors # Invalid item status item['_circulation']['status'] = 'foo' circulation_event_schema.context['item'] = item errors = circulation_event_schema.validate({}) assert '_schema' in errors
def test_item_find_by_holding(app, db, arguments, assertions): # Prepare the item item = Item.create({}) db.session.commit() # Create loan data la = LoanItemSchema() la.context['item'] = item # Prepare the loan data data = [] tmp = la.load({'user_id': 1}).data data.append(la.dump(tmp).data) end_date = datetime.date.today() + datetime.timedelta(weeks=2) tmp = la.load({'user_id': 2, 'end_date': end_date}).data data.append(la.dump(tmp).data) for d in data: # Loan item item.loan_item(**d) item.commit() db.session.commit() # Return item item.return_item() item.commit() db.session.commit() res = list(Item.find_by_holding(**arguments)) for assertion in assertions: assert assertion(res, item)
def test_item_request(app, db): item = Item.create({}) # Request the item item.request_item() assert item['_circulation']['status'] == ItemStatus.ON_SHELF assert len(item['_circulation']['holdings']) == 1
def test_extend_receiver(app, db, access_token): """Use the webhooks api to an item loan.""" item_uuid = uuid.uuid4() item_data = {} pid = circulation_item_minter(item_uuid, item_data) item = Item.create(item_data, id_=item_uuid) with app.test_request_context(): with app.test_client() as client: url = url_for('invenio_webhooks.event_list', receiver_id='circulation_loan') url += '?access_token=' + access_token data = {'item_id': pid.pid_value} res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 202 item = Item.get_record(item.id) assert item['_circulation']['status'] == ItemStatus.ON_LOAN url = url_for('invenio_webhooks.event_list', receiver_id='circulation_extend') url += '?access_token=' + access_token data = { 'item_id': pid.pid_value, 'requested_end_date': datetime.date.today().isoformat(), } res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 202 item = Item.get_record(item.id) assert item['_circulation']['status'] == ItemStatus.ON_LOAN
def test_loan_return_receiver(app, db, access_token): """Use the webhooks api to loan and return an item.""" item_uuid = uuid.uuid4() item_data = {} pid = circulation_item_minter(item_uuid, item_data) item = Item.create(item_data, id_=item_uuid) with app.test_request_context(): with app.test_client() as client: url = url_for('invenio_webhooks.event_list', receiver_id='circulation_loan') url += '?access_token=' + access_token data = {'item_id': pid.pid_value} res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 202 item = Item.get_record(item.id) assert item['_circulation']['status'] == ItemStatus.ON_LOAN assert len(item['_circulation']['holdings']) == 1 url = url_for('invenio_webhooks.event_list', receiver_id='circulation_return') url += '?access_token=' + access_token data = {'item_id': pid.pid_value} res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 202 item = Item.get_record(item.id) assert item['_circulation']['status'] == ItemStatus.ON_SHELF assert len(item['_circulation']['holdings']) == 0
def test_holdings(app, db): """Test basic Item.holdings functionality.""" item = Item.create({}) assert not item.holdings # Append an item id1 = str(uuid.uuid4()) holding = Holding.create(id_=id1) item.holdings.append(holding) assert len(item.holdings) == 1 assert id1 in item.holdings # Insert an item id2 = str(uuid.uuid4()) holding = Holding.create(id_=id2) item.holdings.insert(0, holding) assert len(item.holdings) == 2 assert [id2, id1] == [x['id'] for x in item.holdings] # Delete an item del item.holdings[id2] assert len(item.holdings) == 1 assert id2 not in item.holdings assert id1 in item.holdings
def test_item_extend_loan(app, db): item = Item.create({}) # Loan the item item.loan_item() # Extend the loan item.extend_loan() assert item['_circulation']['status'] == ItemStatus.ON_LOAN
def test_circulation_fetcher(db): """Test circulation_item_fetcher functionality.""" item = Item.create({'foo': 'bar'}) pid = circulation_item_minter(item.id, item) item.commit() db.session.commit() fetched = circulation_item_fetcher(item.id, item) assert pid.pid_value == fetched.pid_value
def test_blocking_holds(app, db, request_arguments, validate_arguments, assert_statement): item = Item.create({}) db.session.commit() circulation_event_schema = RequestItemSchema() circulation_event_schema.context['item'] = item item.request_item(**RequestItemSchema().dump(request_arguments).data) item.commit() errors = circulation_event_schema.validate(validate_arguments) assert assert_statement(errors)
def test_cancel_hold(app, db): item = Item.create({}) # Loan the item item.request_item() item.cancel_hold(item['_circulation']['holdings'][0]['id']) assert len(item['_circulation']['holdings']) == 0 with pytest.raises(Exception): item.cancel_hold(1)
def test_return_missing_item_marshmallow(app, db): """Use LoanItemSchema to validate return_missing_item parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = ReturnMissingItemSchema() circulation_event_schema.context['item'] = item # Item not missing errors = circulation_event_schema.validate({}) assert '_schema' in errors item.lose_item() assert not circulation_event_schema.validate({})
def test_request_item_marshmallow(app, db): """Use LoanItemSchema to validate request_item parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = RequestItemSchema() circulation_event_schema.context['item'] = item # Valid arguments assert not circulation_event_schema.validate({}) assert not circulation_event_schema.validate( { 'start_date': datetime.date.today(), 'end_date': datetime.date.today() + datetime.timedelta(weeks=4), }) # Valid arguments date in the future errors = circulation_event_schema.validate({ 'start_date': datetime.date.today() + datetime.timedelta(days=1), 'end_date': datetime.date.today() + datetime.timedelta(weeks=4), }) assert not errors # Invalid start date errors = circulation_event_schema.validate({ 'start_date': datetime.date.today() + datetime.timedelta(days=-1), 'end_date': datetime.date.today() + datetime.timedelta(weeks=4), }) assert 'start_date' in errors # Invalid duration errors = circulation_event_schema.validate({ 'start_date': datetime.date.today(), 'end_date': datetime.date.today() + datetime.timedelta(weeks=5), }) assert '_schema' in errors # Invalid item status item['_circulation']['status'] = ItemStatus.MISSING circulation_event_schema.context['item'] = item errors = circulation_event_schema.validate({}) assert '_schema' in errors
def test_extend_loan_marshmallow(app, db): """Use LoanItemSchema to validate extend_loan parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = ExtendItemSchema() circulation_event_schema.context['item'] = item # Valid arguments assert not circulation_event_schema.validate({}) # Date too far in the future requested_end_date = datetime.date.today() + datetime.timedelta(weeks=5) errors = circulation_event_schema.validate( {'requested_end_date': requested_end_date}) assert '_schema' in errors
def test_cancel_item_marshmallow(app, db): """Use LoanItemSchema to validate cancel_hold parameters.""" item = Item.create({}) item.loan_item() db.session.commit() hold_id = item.holdings[0]['id'] circulation_event_schema = CancelItemSchema() circulation_event_schema.context['item'] = item # No existing UUID errors = circulation_event_schema.validate({'hold_id': str(uuid.uuid4())}) assert '_schema' in errors # Valid argument assert not circulation_event_schema.validate({'hold_id': str(hold_id)})
def test_extend_loan_marshmallow(app, db): """Use LoanItemSchema to validate extend_loan parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = ExtendItemSchema() circulation_event_schema.context['item'] = item # Valid arguments assert not circulation_event_schema.validate({}) # Date too far in the future requested_end_date = datetime.date.today() + datetime.timedelta(weeks=5) errors = circulation_event_schema.validate({ 'requested_end_date': requested_end_date }) assert '_schema' in errors
def test_holding_values(app, db, action, schema): """Test holding values exist for loan_item/request_item.""" # Loan an item item = Item.create({}) assert not item.holdings action(item, schema().dump({}).data) item.commit() holding = item.holdings[0] assert len(item.holdings) == 1 assert 'start_date' in holding assert 'end_date' in holding assert 'waitlist' in holding assert 'delivery' in holding assert 'user_id' in holding
def test_item_lose(app, db): item = Item.create({}) # Lose the item item.lose_item() assert item['_circulation']['status'] == ItemStatus.MISSING # Return missing item item.return_missing_item() assert item['_circulation']['status'] == ItemStatus.ON_SHELF # Request the item item.request_item() # Lose the item item.lose_item() assert item['_circulation']['status'] == ItemStatus.MISSING assert len(item['_circulation']['holdings']) == 0
def test_item_circulation_loan(app, db): item = Item.create({}) # Loan the item item.loan_item() assert item['_circulation']['status'] == ItemStatus.ON_LOAN assert len(item['_circulation']['holdings']) == 1 # Return the item item.return_item() assert item['_circulation']['status'] == ItemStatus.ON_SHELF assert len(item['_circulation']['holdings']) == 0 # Loan the item item.loan_item() # Loan again with pytest.raises(PIDInvalidAction): item.loan_item(1)
def test_return_item_marshmallow(app, db): """Use LoanItemSchema to validate return_item parameters.""" item = Item.create({}) db.session.commit() circulation_event_schema = ReturnItemSchema() circulation_event_schema.context['item'] = item # No item to return arguments errors = circulation_event_schema.validate({}) assert '_schema' in errors # No active hold item.request_item(**RequestItemSchema().dump({ 'start_date': datetime.date.today() + datetime.timedelta(weeks=1), }).data) item.commit() errors = circulation_event_schema.validate({}) assert '_schema' in errors
def test_crud_read(app, db, es): """Test REST API get functionality.""" item = Item.create({'foo': 'bar'}) circulation_item_minter(item.id, item) item.commit() db.session.commit() record_indexer = RecordIndexer() record_indexer.index(item) current_search.flush_and_refresh('_all') with app.test_request_context(): with app.test_client() as client: url = url_for('circulation_rest.crcitm_item', pid_value=item['control_number']) res = client.get(url) fetched_item = json.loads(res.data.decode('utf-8'))['metadata'] assert fetched_item['control_number'] == item['control_number']
def test_rest_search(app, db, es, url_addition, count): """Test REST API search functionality.""" item = Item.create({'foo': 'bar'}) circulation_item_minter(item.id, item) item.commit() db.session.commit() record_indexer = RecordIndexer() record_indexer.index(item) current_search.flush_and_refresh('_all') with app.test_request_context(): with app.test_client() as client: base_url = url_for('circulation_rest.crcitm_list') url = base_url + url_addition res = client.get(url) hits = json.loads(res.data.decode('utf-8'))['hits']['hits'] assert len(hits) == count
def items(): """Create circulation items.""" from invenio_db import db from invenio_indexer.api import RecordIndexer from invenio_circulation.api import Item from invenio_circulation.minters import circulation_item_minter for x in range(10): item = Item.create({ 'foo': 'bar{0}'.format(x), 'title_statement': {'title': 'title{0}'.format(x)}, 'record': {'id': 1} }) circulation_item_minter(item.id, item) item.commit() record_indexer = RecordIndexer() record_indexer.index(item) db.session.commit()
def test_receiver_base(app, db, access_token): """Test ReceiverBase failure behavior.""" item_uuid = uuid.uuid4() item_data = {} pid = circulation_item_minter(item_uuid, item_data) item = Item.create(item_data, id_=item_uuid) with app.test_request_context(): with app.test_client() as client: url = url_for('invenio_webhooks.event_list', receiver_id='circulation_loan') url += '?access_token=' + access_token yesterday = datetime.date.today() + datetime.timedelta(days=-1) data = { 'item_id': pid.pid_value, 'start_date': str(yesterday), } res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 400
def test_cancel_item_marshmallow(app, db): """Use LoanItemSchema to validate cancel_hold parameters.""" item = Item.create({}) item.loan_item() db.session.commit() hold_id = item.holdings[0]['id'] circulation_event_schema = CancelItemSchema() circulation_event_schema.context['item'] = item # No existing UUID errors = circulation_event_schema.validate({ 'hold_id': str(uuid.uuid4()) }) assert '_schema' in errors # Valid argument assert not circulation_event_schema.validate({ 'hold_id': str(hold_id) })
def test_dry_run(app, db, access_token): """Use the webhooks api to loan and return an item.""" item_uuid = uuid.uuid4() item_data = {} pid = circulation_item_minter(item_uuid, item_data) item = Item.create(item_data, id_=item_uuid) with app.test_request_context(): with app.test_client() as client: # Successful request url = url_for('invenio_webhooks.event_list', receiver_id='circulation_loan') url += '?access_token=' + access_token data = { 'item_id': pid.pid_value, 'dry_run': True, } res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 204 item = Item.get_record(item.id) assert item['_circulation']['status'] == ItemStatus.ON_SHELF assert len(item['_circulation']['holdings']) == 0 # Invalid request url = url_for('invenio_webhooks.event_list', receiver_id='circulation_loan') url += '?access_token=' + access_token yesterday = datetime.date.today() + datetime.timedelta(days=-1) data = { 'item_id': pid.pid_value, 'start_date': str(yesterday), 'dry_run': True, } res = client.post(url, data=json.dumps(data), content_type='application/json') assert res.status_code == 400
def test_item_create(app, db): item = Item.create({}) assert item['_circulation']['status'] == ItemStatus.ON_SHELF assert len(item['_circulation']['holdings']) == 0