Exemplo n.º 1
0
def test_marc21_856_indicators():
    """Test MARC21 856 field special indicator values."""
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record
    from dojson.contrib.to_marc21 import to_marc21

    RECORD_8564 = '''
    <datafield tag="856" ind1="4" ind2=" ">
        <subfield code="s">272681</subfield>
        <subfield code="u">https://zenodo.org/record/17575/files/...</subfield>
        <subfield code="z">0</subfield>
    </datafield>
    '''
    RECORD_8567 = '''
    <datafield tag="856" ind1="7" ind2=" ">
        <subfield code="s">272681</subfield>
        <subfield code="u">https://zenodo.org/record/17575/files/...</subfield>
        <subfield code="z">0</subfield>
        <subfield code="2">Awesome access method</subfield>
    </datafield>
    '''

    expected_8564 = {
        'electronic_location_and_access': [
            {'public_note': ('0',),
             'access_method': 'HTTP',
             'uniform_resource_identifier': (
                 'https://zenodo.org/record/17575/files/...',),
             'file_size': ('272681',)}
        ]
    }
    expected_8567 = {
        'electronic_location_and_access': [
            {'public_note': ('0',),
             'access_method': 'Awesome access method',
             'uniform_resource_identifier': (
                 'https://zenodo.org/record/17575/files/...',),
             'file_size': ('272681',)}
        ]
    }

    blob = create_record(RECORD_8564)
    data = marc21.do(blob)
    assert expected_8564 == data
    back_blob = to_marc21.do(data)
    assert blob == back_blob

    blob = create_record(RECORD_8567)
    data = marc21.do(blob)
    assert expected_8567 == data
    back_blob = to_marc21.do(data)
    assert blob == back_blob
Exemplo n.º 2
0
def test_tomarc21_from_liberal_xml():
    """Test MARC21 loading and recreating from XML."""
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record
    from dojson.contrib.to_marc21 import to_marc21
    from dojson.errors import MissingRule

    def liberal(exc, output, key, value):
        """When a key cannot be translated, simply use the number instead."""
        if exc.__class__ is MissingRule:
            if key == '__order__':
                return
            if isinstance(output, list):
                output.append((key, value))
            else:
                output[key] = value
        else:
            raise exc

    for name, record in LIBERAL_RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob, False, {MissingRule: liberal})

        back_blob = to_marc21.do(data, False, {MissingRule: liberal})

        assert blob == back_blob, name
Exemplo n.º 3
0
def test_marc21_856_indicators():
    """Test MARC21 856 field special indicator values."""

    records = {
        8564: [
            '''
            <datafield tag="856" ind1="4" ind2=" ">
                <subfield code="s">272681</subfield>
                <subfield code="u">https://zenodo.org/record/17575/files/...</subfield>
                <subfield code="z">0</subfield>
            </datafield>
            ''',
            {
                '__order__': ['electronic_location_and_access'],
                'electronic_location_and_access': [
                    {
                        '__order__': ('file_size', 'uniform_resource_identifier', 'public_note', 'access_method', 'relationship'),
                        'public_note': ('0',),
                        'access_method': 'HTTP',
                        'relationship': 'No information provided',
                        'uniform_resource_identifier': (
                            'https://zenodo.org/record/17575/files/...',),
                        'file_size': ('272681',)
                    }
                ]
            }
        ],
        8567: [
            '''
            <datafield tag="856" ind1="7" ind2=" ">
                <subfield code="s">272681</subfield>
                <subfield code="u">https://zenodo.org/record/17575/files/...</subfield>
                <subfield code="z">0</subfield>
                <subfield code="2">Awesome access method</subfield>
            </datafield>
            ''',
            {
                '__order__': ['electronic_location_and_access'],
                'electronic_location_and_access': [
                    {
                        '__order__': ('file_size', 'uniform_resource_identifier', 'public_note', 'access_method', 'relationship'),
                        'public_note': ('0',),
                        'access_method': 'Awesome access method',
                        'relationship': 'No information provided',
                        'uniform_resource_identifier': (
                            'https://zenodo.org/record/17575/files/...',),
                        'file_size': ('272681',)
                    }
                ]
            }
        ]
    }

    for name, (record, expected) in records.items():
        blob = create_record(record)
        data = marc21.do(blob)
        # this ordering is required to use __eq__ from GroupableOrderedDict
        assert data == expected, name
        back_blob = to_marc21.do(data)
        assert blob == back_blob, name
Exemplo n.º 4
0
def test_tomarc21_from_liberal_xml():
    """Test MARC21 loading and recreating from XML."""
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record
    from dojson.contrib.to_marc21 import to_marc21
    from dojson.errors import MissingRule

    def liberal(exc, output, key, value):
        """When a key cannot be translated, simply use the number instead."""
        if exc.__class__ is MissingRule:
            if key == '__order__':
                return
            if isinstance(output, list):
                output.append((key, value))
            else:
                output[key] = value
        else:
            raise exc

    for name, record in LIBERAL_RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob, False, {MissingRule: liberal})

        back_blob = to_marc21.do(data, False, {MissingRule: liberal})

        assert blob == back_blob, name
def test_update_draft(app, service, identity_simple, metadata, metadata2):
    draft = service.create(identity=identity_simple, metadata=metadata)
    assert draft.id

    # Update draft content
    update_draft = service.update_draft(draft.id,
                                        identity=identity_simple,
                                        metadata=metadata2)

    # Check the updates where savedif "json" in data:

    read_draft = service.read_draft(id_=draft.id, identity=identity_simple)

    assert draft.id == update_draft.id
    _test_metadata(
        to_marc21.do(update_draft["metadata"]["json"]),
        to_marc21.do(read_draft["metadata"]["json"]),
    )
Exemplo n.º 6
0
    def test_invenio_collection_marc21_tag():
        """Test invenio-collection marc21 tag."""
        source = {'980__': [{'a': 'colla'}, {'b': 'collb'}]}
        data = marc21.do(source)

        assert data['collections'][0]['primary'] == 'colla'
        assert data['collections'][1]['secondary'] == 'collb'

        original = to_marc21.do(data)
        assert source['980__'] == list(original['980__'])
    def test_invenio_collection_marc21_tag():
        """Test invenio-collection marc21 tag."""
        source = {'980__': [{'a': 'colla'}, {'b': 'collb'}]}
        data = marc21.do(source)

        assert data['collections'][0]['primary'] == 'colla'
        assert data['collections'][1]['secondary'] == 'collb'

        original = to_marc21.do(data)
        assert source['980__'] == original['980__']
Exemplo n.º 8
0
def test_tomarc21_from_xml():
    """Test MARC21 loading and recreating from XML."""

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)

        back_blob = to_marc21.do(data)

        assert blob == back_blob, name
Exemplo n.º 9
0
def test_tomarc21_from_xml():
    """Test MARC21 loading and recreating from XML."""

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)

        back_blob = to_marc21.do(data)

        assert blob == back_blob, name
Exemplo n.º 10
0
def test_records_marc21_tojson_tomarc21():
    """Test records marc21 - json - marc21."""

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)
        text = json.dumps(data)

        back_blob = to_marc21.do(json.loads(text))
        assert blob == back_blob, name
Exemplo n.º 11
0
def test_records_marc21_tojson_tomarc21():
    """Test records marc21 - json - marc21."""

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)
        text = json.dumps(data)

        back_blob = to_marc21.do(json.loads(text))
        assert blob == back_blob, name
Exemplo n.º 12
0
def dumps_etree(pid, record, **kwargs):
    """Dump MARC21 compatible record.

    :param pid: The :class:`invenio_pidstore.models.PersistentIdentifier`
        instance.
    :param record: The :class:`invenio_records.api.Record` instance.
    :returns: A LXML Element instance.
    """
    from dojson.contrib.to_marc21 import to_marc21
    from dojson.contrib.to_marc21.utils import dumps_etree

    return dumps_etree(to_marc21.do(record["_source"]), **kwargs)
Exemplo n.º 13
0
def dumps_etree(pid, record, **kwargs):
    """Dump MARC21 compatible record.

    :param pid: The :class:`invenio_pidstore.models.PersistentIdentifier`
        instance.
    :param record: The :class:`invenio_records.api.Record` instance.
    :returns: A LXML Element instance.
    """
    from dojson.contrib.to_marc21 import to_marc21
    from dojson.contrib.to_marc21.utils import dumps_etree

    return dumps_etree(to_marc21.do(record['_source']), **kwargs)
Exemplo n.º 14
0
def test_records_marc21_tojson_tomarc21():
    """Test records marc21 - json - marc21."""
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record
    from dojson.contrib.to_marc21 import to_marc21

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)

        back_blob = to_marc21.do(data)

        assert blob == back_blob, name
Exemplo n.º 15
0
def test_tomarc21_from_xml():
    """Test MARC21 loading and recreating from XML."""
    from dojson.contrib.marc21 import marc21
    from dojson.contrib.marc21.utils import create_record
    from dojson.contrib.to_marc21 import to_marc21

    for name, record in RECORDS.items():
        blob = create_record(record)
        data = marc21.do(blob)

        back_blob = to_marc21.do(data)

        assert blob == back_blob, name
Exemplo n.º 16
0
def test_update_draft(app, service, identity_simple, metadata, metadata2):
    # Needs `app` context because of invenio_access/permissions.py#166
    draft = service.create(identity=identity_simple, metadata=metadata)
    assert draft.id

    # Update draft content
    update_draft = service.update_draft(
        draft.id, identity=identity_simple, metadata=metadata2
    )

    assert draft.id == update_draft.id
    _test_metadata(
        to_marc21.do(update_draft["metadata"]["json"]), create_record(metadata.xml)
    )
    # Check the updates where savedif "json" in data:

    read_draft = service.read_draft(id_=draft.id, identity=identity_simple)

    assert draft.id == update_draft.id
    _test_metadata(
        to_marc21.do(update_draft["metadata"]["json"]),
        to_marc21.do(read_draft["metadata"]["json"]),
    )
Exemplo n.º 17
0
def test_toxml_from_xml():
    """Test MARC21 loading from XML and recreating to XML."""

    for name, record in RECORDS.items():
        blob = create_record(record)
        rec = marc21.do(blob)
        back_blob = to_marc21.do(rec)

        assert blob == back_blob
        xml = dumps([back_blob])

        options = {'xml_declaration': True,
                   'encoding': 'utf8',
                   'pretty_print': True}

        recordxml = ('<collection xmlns="http://www.loc.gov/MARC21/slim">' +
                     record +
                     '</collection>')

        expected = etree.tostring(objectify.fromstring(recordxml), **options)
        actual = etree.tostring(objectify.fromstring(xml), **options)

        assert expected == actual
Exemplo n.º 18
0
def dumps_etree(pid, record, **kwargs):
    """Dump MARC21 compatible record."""
    from dojson.contrib.to_marc21 import to_marc21
    from dojson.contrib.to_marc21.utils import dumps_etree

    return dumps_etree(to_marc21.do(record['_source']), **kwargs)
Exemplo n.º 19
0
 def convert_xml(self, data, **kwargs):
     """Convert json into marc21 xml."""
     if "json" in data:
         data["xml"] = dumps(to_marc21.do(data["json"]))
     return data
def json_to_marc21(json):
    """Convert record into marc21 xml."""
    return to_marc21.do(json)