def test_creators():
    """Test creators."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)

    assert s.load(
        d(creators=[
            dict(name="Doe, John",
                 affiliation="Atlantis",
                 orcid="0000-0002-1825-0097",
                 gnd="170118215"),
            dict(name="Smith, Jane", affiliation="Atlantis")
        ])).data['creators'] == [
            dict(name="Doe, John",
                 affiliation="Atlantis",
                 orcid="0000-0002-1825-0097",
                 gnd="170118215"),
            dict(name="Smith, Jane", affiliation="Atlantis")
        ]

    assert s.load(
        d(creators=[
            dict(name="Doe, John", affiliation=" "),
            dict(name="Smith, Jane", affiliation="")
        ])).data['creators'] == [
            dict(name="Doe, John"),
            dict(name="Smith, Jane")
        ]

    # Min length required
    pytest.raises(ValidationError,
                  legacyjson.LegacyMetadataSchemaV1(strict=True).load,
                  d(creators=[]))
def test_upload_type(app):
    """Test upload type deserialization."""
    s = legacyjson.LegacyMetadataSchemaV1(partial=[
        'upload_type', 'publication_type', 'image_type', 'communities'
    ],
                                          strict=True)
    assert s.load(d(
        upload_type='publication',
        publication_type='book',
    )).data['resource_type'] == {
        'subtype': 'book',
        'type': 'publication'
    }
    # Irrelevant extra key.
    assert s.load(
        d(
            upload_type='image',
            publication_type='book',
            image_type='photo',
        )).data['resource_type'] == {
            'subtype': 'photo',
            'type': 'image'
        }

    assert s.load(
        d(upload_type='software', openaire_type='foo:t1',
          communities=['c1'])).data['resource_type'] == {
              'type': 'software',
              'openaire_subtype': 'foo:t1'
          }
def test_communities_invalid(comms):
    """Test communities."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(communities=comms)
    )
def test_contributors():
    """Test contributors."""
    app = Flask(__name__)
    app.config.update(
        dict(DEPOSIT_CONTRIBUTOR_DATACITE2MARC=dict(
            Other='...',
            DataCurator='...',
        )))
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    with app.app_context():
        assert s.load(
            d(
                **{
                    'contributors': [
                        dict(name="Doe, John",
                             affiliation="Atlantis",
                             orcid="0000-0002-1825-0097",
                             gnd="170118215",
                             type="DataCurator"),
                        dict(name="Smith, Jane",
                             affiliation="Atlantis",
                             type="Other")
                    ]
                })).data['contributors'] == [
                    dict(name="Doe, John",
                         affiliation="Atlantis",
                         orcid="0000-0002-1825-0097",
                         gnd="170118215",
                         type="DataCurator"),
                    dict(name="Smith, Jane",
                         affiliation="Atlantis",
                         type="Other")
                ]
def test_license():
    """Test license."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(access_right='open',
                    license="cc-zero")).data['license'] == {
                        '$ref': 'https://dx.zenodo.org/licenses/cc-zero'
                    }
def test_embargo_date_invalid(dt):
    """Test embargo date."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(access_right='embargoed', embargo_date=dt)
    )
def test_access_rights_invalid(val):
    """Test creators."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(access_right=val)
    )
def test_keywords_invalid(keywords):
    """Test invalid keywords."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(keywords=keywords)
    )
def test_notes_invalid(desc):
    """Test invalid notes."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(notes=desc)
    )
def test_description_invalid(desc):
    """Test invalid description."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(description=desc)
    )
def test_publication_date_invalid(date):
    """Test creators."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(publication_date=date)
    )
def test_partof():
    """Test part of vs imprint."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(
        partof_pages="Some pages",
        partof_title="Some title",
    )).data['part_of'] == dict(
        pages="Some pages",
        title="Some title",
    )
    result = s.load(
        d(
            partof_pages="Some pages",
            partof_title="Some title",
            publication_date="2016-01-01",
            imprint_place="Some place",
            imprint_publisher="Some publisher",
            imprint_isbn="Some isbn",
        ))
    assert result.data['part_of'] == dict(
        pages="Some pages",
        title="Some title",
    )
    assert result.data['imprint'] == dict(
        place="Some place",
        publisher="Some publisher",
        isbn="Some isbn",
    )
def test_thesis():
    """Test creators and thesis supervisors."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(
        d(
            **{
                'thesis_supervisors': [
                    dict(name="Doe, John",
                         affiliation="Atlantis",
                         orcid="0000-0002-1825-0097",
                         gnd="170118215"),
                    dict(name="Smith, Jane", affiliation="Atlantis")
                ],
                'thesis_university':
                'Important'
            })).data['thesis'] == {
                'supervisors': [
                    dict(name="Doe, John",
                         affiliation="Atlantis",
                         orcid="0000-0002-1825-0097",
                         gnd="170118215"),
                    dict(name="Smith, Jane", affiliation="Atlantis")
                ],
                'university':
                'Important',
            }
def test_title_invalid(val):
    """Test invalid titles."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(title=val)
    )
def test_related_identifiers_invalid():
    """Test missing relation."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True).load
    # Missing relation
    pytest.raises(
        ValidationError, s,
        d(related_identifiers=[
            dict(identifier='10.1234/foo.bar2'),
        ]))
    # Invalid scheme
    pytest.raises(
        ValidationError, s,
        d(related_identifiers=[
            dict(identifier='10.1234/foo.bar2', scheme='isbn'),
        ]))
    # Invalid scheme
    pytest.raises(
        ValidationError, s,
        d(related_identifiers=[
            dict(identifier='10.1234/foo.bar2', scheme='invalid'),
        ]))
    # Missing identifier
    pytest.raises(
        ValidationError, s,
        d(related_identifiers=[
            dict(scheme='doi', relation='isCitedBy'),
        ]))
def test_creators_invalid(creator):
    """Test creators."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(creators=[creator])
    )
def test_related_alternate_identifiers():
    """Test related alternate identifiers."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)

    result = s.load(
        d(related_identifiers=[
            dict(identifier='10.1234/foo.bar2', relation='isCitedBy'),
            dict(identifier='10.1234/foo.bar3', relation='cites',
                 scheme='doi'),
            dict(identifier='2011ApJS..192...18K',
                 relation='isAlternateIdentifier'
                 ),  # Name difference on purpose
            dict(
                identifier='2011ApJS..192...18K',
                relation='isAlternativeIdentifier',  # Name difference on purpose
                scheme='ads'),
        ]))
    assert result.data['related_identifiers'] == [
        dict(identifier='10.1234/foo.bar2', relation='isCitedBy',
             scheme='doi'),
        dict(identifier='10.1234/foo.bar3', relation='cites', scheme='doi'),
    ]
    assert result.data['alternate_identifiers'] == [
        dict(identifier='2011ApJS..192...18K', scheme='ads'),
        dict(identifier='2011ApJS..192...18K', scheme='ads'),
    ]
def test_description(desc, expected):
    """Test descriptions.

    Note, we only do limited sanitize test because we use the bleach library
    to sanitize and it already have extensive tests.
    """
    assert legacyjson.LegacyMetadataSchemaV1(strict=True).load(
        d(description=desc)).data['description'] == (expected or desc)
def test_communities(communities):
    """Test communities."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(
        d(communities=[
            dict(identifier='zenodo'),
            dict(identifier='ecfunded'),
        ], )).data['communities'] == ['ecfunded', 'zenodo']
def test_related_identifiers_invalid_relations(relation):
    """Test invalid related identifiers."""
    pytest.raises(
        ValidationError,
        legacyjson.LegacyMetadataSchemaV1(strict=True).load,
        d(related_identifiers=[
            dict(identifier='10.1234/foo.bar2', relation=relation),
        ]))
def test_license_refresolver(app, db, license_record):
    """Test license."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True,
                                          context=dict(replace_refs=True))
    assert s.load(d(access_right='open',
                    license='CC0-1.0')).data['license'] == {
                        '$ref': 'https://dx.zenodo.org/licenses/CC0-1.0'
                    }
    pytest.raises(ValidationError, s.load,
                  d(access_right='open', license='invalid'))

    # Without ref resolving
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(access_right='open',
                    license='invalid')).data['license'] == {
                        '$ref': 'https://dx.zenodo.org/licenses/invalid'
                    }
def test_grants_refresolver(app, db, grant_record, license_record):
    """Test license."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True,
                                          context=dict(replace_refs=True))
    assert s.load(
        d(grants=[dict(id='282896'),
                  dict(id='10.13039/501100000780::282896')],
          license='CC0-1.0'))
    # Invalid grant raises
    pytest.raises(ValidationError, s.load,
                  d(grants=[dict(id='invalid')], license='CC0-1.0'))

    # Without ref resolving
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(
        grants=[dict(id='invalid')], license='CC0-1.0')).data['grants'] == [{
            '$ref':
            'https://dx.zenodo.org/grants/10.13039/501100000780::invalid'
        }]
def test_upload_type_invalid(app):
    """Test upload type deserialization."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)

    # Missing value
    obj = d()
    obj.pop('upload_type')
    pytest.raises(ValidationError, s.load, obj)

    # Invalid value
    obj.update(dict(upload_type='invalid'))
    pytest.raises(ValidationError, s.load, obj)

    # Missing subtype
    obj.update(dict(upload_type='publication'))
    pytest.raises(ValidationError, s.load, obj)

    # Invalid subtype
    obj.update(dict(upload_type='image', image_type='invalid'))
    pytest.raises(ValidationError, s.load, obj)

    # Subtype provided for type without possibility of subtype.
    obj.update(dict(upload_type='dataset', image_type='figure'))
    assert s.load(obj).data['resource_type'] == {'type': 'dataset'}

    obj.update(dict(upload_type='image', image_type='invalid'))
    pytest.raises(ValidationError, s.load, obj)

    # OpenAIRE subtype and community mismatch
    obj.update(
        dict(upload_type='software',
             openaire_type='foo:t1',
             communities=['foobar']))
    pytest.raises(ValidationError, s.load, obj)

    # OpenAIRE subtype invalid format (no prefix)
    obj.update(
        dict(upload_type='software',
             openaire_type='invalid',
             communities=['c1']))
    pytest.raises(ValidationError, s.load, obj)

    # OpenAIRE subtype not found (wrong prefix)
    obj.update(
        dict(upload_type='software',
             openaire_type='xxx:t1',
             communities=['c1']))
    pytest.raises(ValidationError, s.load, obj)

    # OpenAIRE subtype not found (good prefix, wrong type)
    obj.update(
        dict(upload_type='software',
             openaire_type='foo:invalid',
             communities=['c1']))
    pytest.raises(ValidationError, s.load, obj)
def test_identifier_schemes(app, db, es, locations, license_record,
                            sample_identifiers):
    """Test supported identifier schemes."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    result = s.load(
        d(related_identifiers=[{
            'identifier': _id,
            'scheme': scheme,
            'relation': 'references'
        } for scheme, (_id, _) in sample_identifiers.items()]))
    ZenodoDeposit.create(result.data).validate()
def test_references():
    """Test references."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(references=[
        "Reference 1",
        "  ",
        "Reference 2",
    ])).data['references'] == [
        dict(raw_reference="Reference 1"),
        dict(raw_reference="Reference 2"),
    ]
def test_grants():
    """Test grants."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    loaded_grants = s.load(
        d(grants=[dict(id='283595'),
                  dict(id='10.13039/501100000780::643410')], )).data['grants']
    assert {
        '$ref': 'https://dx.zenodo.org/grants/10.13039/501100000780::283595'
    } in loaded_grants
    assert {
        '$ref': 'https://dx.zenodo.org/grants/10.13039/501100000780::643410'
    } in loaded_grants
def test_contributors_invalid(contributor):
    """Test creators."""
    app = Flask(__name__)
    app.config.update(
        dict(DEPOSIT_CONTRIBUTOR_DATACITE2MARC=dict(
            Other='...',
            DataCurator='...',
        )))
    with app.app_context():
        pytest.raises(ValidationError,
                      legacyjson.LegacyMetadataSchemaV1(strict=True).load,
                      d(contributors=[contributor]))
def test_journal():
    """Test journal."""
    s = legacyjson.LegacyMetadataSchemaV1(strict=True)
    assert s.load(d(
        journal_issue="Some issue",
        journal_pages="Some pages",
        journal_title="Some journal name",
        journal_volume="Some volume",
    )).data['journal'] == dict(
        issue="Some issue",
        pages="Some pages",
        title="Some journal name",
        volume="Some volume",
    )
def test_upload_type():
    """Test upload type deserialization."""
    s = legacyjson.LegacyMetadataSchemaV1(
        partial=['upload_type', 'publication_type', 'image_type'],
        strict=True
    )
    s.load(d(
        upload_type='publication',
        publication_type='book',
    )).data['resource_type'] == {'subtype': 'book', 'type': 'publication'}
    # Irrelevant extra key.
    s.load(d(
        upload_type='image',
        publication_type='book',
        image_type='photo',
    )).data['resource_type'] == {'subtype': 'photo', 'type': 'image'}
def test_access_rights(val, removedkeys):
    """Test access rights."""
    data = dict(license='cc-by',
                embargo_date=(datetime.utcnow() +
                              timedelta(days=2)).date().isoformat(),
                access_conditions='TEST')

    result = legacyjson.LegacyMetadataSchemaV1(strict=True).load(
        d(access_right=val, **data))
    assert result.data['access_right'] == val
    # Make sure removed keys **are not** in output
    for k in removedkeys:
        assert k not in result.data
    # Make sure non-removed keys **are** in output
    for k in (set(data.keys()) - set(removedkeys)):
        assert k in result.data