def test_getrecord(app):
    schema = {
        "type": "object",
        "properties": {"title": {"type": "string"}, "field": {"type": "boolean"}},
        "required": ["title"],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({"title": "Test0", "$schema": schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()

        pid_value = pid.pid_value
        pid_updated = pid.updated

        with app.test_client() as c:
            result = c.get("/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc".format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            namespaces = {"x": NS_OAIPMH}
            assert len(tree.xpath("/x:OAI-PMH", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:header", namespaces=namespaces)) == 1
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:identifier", namespaces=namespaces)) == 1
            identifier = tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()", namespaces=namespaces)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath("/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()", namespaces=namespaces)
            assert datestamp == [datetime_to_datestamp(pid_updated)]
            assert len(tree.xpath("/x:OAI-PMH/x:GetRecord/x:metadata", namespaces=namespaces)) == 1
Example #2
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(1)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get(
                "/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc"
            )

        tree = etree.fromstring(result.data)

        namespaces = {'x': NS_OAIPMH}
        assert len(tree.xpath('/x:OAI-PMH', namespaces=namespaces)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=namespaces)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=namespaces)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=namespaces
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=namespaces
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)
Example #3
0
def test_getrecord(app):
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({'title': 'Test0', '$schema': schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()
        pid_value = pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'.
                format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord',
                           namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:header',
                           namespaces=NAMESPACES)) == 1
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:header/x:identifier',
                           namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(
                tree.xpath('/x:OAI-PMH/x:GetRecord/x:metadata',
                           namespaces=NAMESPACES)) == 1
Example #4
0
def test_getrecord(app):
    schema = {
        'type': 'object',
        'properties': {
            'title': {'type': 'string'},
            'field': {'type': 'boolean'},
        },
        'required': ['title'],
    }
    with app.test_request_context():
        with db.session.begin_nested():
            record = Record.create({'title': 'Test0', '$schema': schema}).model
            recid_minter(record.id, record.json)
            pid = oaiid_minter(record.id, record.json)

        db.session.commit()
        pid_value = pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'
                .format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:header',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier',
                namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:metadata',
                                  namespaces=NAMESPACES)) == 1
Example #5
0
def _migrate_oai(record):
    """Transform record OAI information."""
    if 'oai' not in record:
        return record

    oai = record.pop('oai')

    sets = oai.get('indicator', [])
    if isinstance(sets, string_types):
        sets = [sets]

    # OAI sets
    record['_oai'] = {
        'id': oai['oai'],
        'sets': sets,
        'updated': datetime_to_datestamp(datetime.utcnow()),
    }

    return record
Example #6
0
def _migrate_oai(record):
    """Transform record OAI information."""
    if 'oai' not in record:
        return record

    oai = record.pop('oai')

    sets = oai.get('indicator', [])
    if isinstance(sets, string_types):
        sets = [sets]

    # OAI sets
    record['_oai'] = {
        'id': oai['oai'],
        'sets': sets,
        'updated': datetime_to_datestamp(datetime.utcnow()),
    }

    return record
Example #7
0
def test_getrecord(app):
    """Test get record verb."""
    with app.test_request_context():
        pid_value = 'oai:legacy:1'
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {
                '_oai': {'id': pid_value},
                'title_statement': {'title': 'Test0'},
            }
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()
        assert pid_value == pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'
                .format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:record/x:header',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:identifier',
                namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [str(record.id)]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:record/x:metadata',
                                  namespaces=NAMESPACES)) == 1
Example #8
0
def test_getrecord(app):
    """Test get record verb."""
    with app.test_request_context():
        pid_value = 'oai:legacy:1'
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {
                '_oai': {'id': pid_value},
                'title_statement': {'title': 'Test0'},
            }
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()
        assert pid_value == pid.pid_value
        record_updated = record.updated
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=GetRecord&identifier={0}&metadataPrefix=oai_dc'
                .format(pid_value))
            assert 200 == result.status_code

            tree = etree.fromstring(result.data)

            assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:record/x:header',
                                  namespaces=NAMESPACES)) == 1
            assert len(tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:identifier',
                namespaces=NAMESPACES)) == 1
            identifier = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:identifier/text()',
                namespaces=NAMESPACES)
            assert identifier == [pid_value]
            datestamp = tree.xpath(
                '/x:OAI-PMH/x:GetRecord/x:record/x:header/x:datestamp/text()',
                namespaces=NAMESPACES)
            assert datestamp == [datetime_to_datestamp(record_updated)]
            assert len(tree.xpath('/x:OAI-PMH/x:GetRecord/x:record/x:metadata',
                                  namespaces=NAMESPACES)) == 1
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        "type": "object",
        "properties": {"title": {"type": "string"}, "field": {"type": "boolean"}},
        "required": ["title"],
    }
    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {"title": "Test0", "$schema": schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(1)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get("/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc")

        tree = etree.fromstring(result.data)

        namespaces = {"x": NS_OAIPMH}
        assert len(tree.xpath("/x:OAI-PMH", namespaces=namespaces)) == 1
        assert len(tree.xpath("/x:OAI-PMH/x:ListIdentifiers", namespaces=namespaces)) == 1
        assert len(tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header", namespaces=namespaces)) == 1
        identifier = tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier", namespaces=namespaces)
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath("/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp", namespaces=namespaces)
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)
Example #10
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        with db.session.begin_nested():
            db.session.add(OAISet(
                spec='test0',
                name='Test0',
                description='test desc 0',
                search_pattern='title_statement.title:Test0',
            ))
        db.session.commit()

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == record['_oai']['updated']

        # Check from_:until range
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                '&from_={0}&until={1}&set=test0'.format(
                    datetime_to_datestamp(record.updated - datetime.timedelta(
                        1)),
                    datetime_to_datestamp(record.updated + datetime.timedelta(
                        1)),
                )
            )

        tree = etree.fromstring(result.data)
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
Example #11
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    schema = {
        'type': 'object',
        'properties': {
            'title': {
                'type': 'string'
            },
            'field': {
                'type': 'boolean'
            },
        },
        'required': ['title'],
    }
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        with db.session.begin_nested():
            db.session.add(
                OAISet(
                    spec='test0',
                    name='Test0',
                    description='test desc 0',
                    search_pattern='title:Test0',
                ))
        db.session.commit()

    with app.test_request_context():
        indexer = RecordIndexer()

        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title': 'Test0', '$schema': schema}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        pid_value = pid.pid_value

        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc')

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                       namespaces=NAMESPACES)) == 1
        assert len(
            tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                       namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES)
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES)
        assert len(datestamp) == 1
        assert datestamp[0].text == datetime_to_datestamp(record.updated)

        # Check from_:until range
        with app.test_client() as c:
            result = c.get('/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                           '&from_={0}&until={1}&set=test0'.format(
                               datetime_to_datestamp(record.updated -
                                                     datetime.timedelta(1)),
                               datetime_to_datestamp(record.updated +
                                                     datetime.timedelta(1)),
                           ))

        tree = etree.fromstring(result.data)
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES)
        assert len(identifier) == 1
Example #12
0
def test_listidentifiers(app):
    """Test verb ListIdentifiers."""
    from invenio_oaiserver.models import OAISet

    with app.app_context():
        current_oaiserver.unregister_signals_oaiset()
        # create new OAI Set
        with db.session.begin_nested():
            oaiset = OAISet(
                spec='test0',
                name='Test0',
                description='test desc 0',
                search_pattern='title_statement.title:Test0',
            )
            db.session.add(oaiset)
        db.session.commit()

    run_after_insert_oai_set()

    with app.test_request_context():
        indexer = RecordIndexer()

        # create a new record (inside the OAI Set)
        with db.session.begin_nested():
            record_id = uuid.uuid4()
            data = {'title_statement': {'title': 'Test0'}}
            recid_minter(record_id, data)
            pid = oaiid_minter(record_id, data)
            record = Record.create(data, id_=record_id)

        db.session.commit()

        indexer.index_by_id(record_id)
        sleep(2)

        pid_value = pid.pid_value

        # get the lis of identifiers
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
            )

        tree = etree.fromstring(result.data)

        assert len(tree.xpath('/x:OAI-PMH', namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers',
                              namespaces=NAMESPACES)) == 1
        assert len(tree.xpath('/x:OAI-PMH/x:ListIdentifiers/x:header',
                              namespaces=NAMESPACES)) == 1
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1
        assert identifier[0].text == str(pid_value)
        datestamp = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:datestamp',
            namespaces=NAMESPACES
        )
        assert len(datestamp) == 1
        assert datestamp[0].text == record['_oai']['updated']

        # Check from:until range
        with app.test_client() as c:
            result = c.get(
                '/oai2d?verb=ListIdentifiers&metadataPrefix=oai_dc'
                '&from={0}&until={1}&set=test0'.format(
                    datetime_to_datestamp(record.updated - datetime.timedelta(
                        1)),
                    datetime_to_datestamp(record.updated + datetime.timedelta(
                        1)),
                )
            )

        tree = etree.fromstring(result.data)
        identifier = tree.xpath(
            '/x:OAI-PMH/x:ListIdentifiers/x:header/x:identifier',
            namespaces=NAMESPACES
        )
        assert len(identifier) == 1