Exemplo n.º 1
0
def test_subjects_validation(running_app, minimal_record):
    """Tests data content validation."""
    # Valid id
    minimal_record["metadata"]["subjects"] = [{"id": "A-D000007"}]

    draft = RDMDraft.create(minimal_record)
    draft.commit()

    assert draft["metadata"]["subjects"] == [{"id": "A-D000007"}]

    # Invalid id
    minimal_record["metadata"]["subjects"] = [{"id": "invalid"}]

    pytest.raises(InvalidRelationValue, RDMDraft.create(minimal_record).commit)
def test_resource_type_validation(running_app, minimal_record):
    """Tests data content validation."""
    # Valid id
    minimal_record["metadata"]["resource_type"] = {"id": "image-photo"}

    draft = RDMDraft.create(minimal_record)
    draft.commit()
    db.session.commit()

    assert draft["metadata"]["resource_type"] == {"id": "image-photo"}

    # Invalid id
    minimal_record["metadata"]["resource_type"] = {"id": "invalid"}

    pytest.raises(InvalidRelationValue, RDMDraft.create(minimal_record).commit)
def test_languages_invalid(running_app, minimal_record, lang):
    """Should fail on invalid id's and invalid structure."""
    # The id "invalid" does not exists.
    minimal_record["metadata"]["languages"] = [{"id": "invalid"}]
    pytest.raises(InvalidRelationValue, RDMDraft.create(minimal_record).commit)

    # Not a list of objects
    minimal_record["metadata"]["languages"] = {"id": "eng"}
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # no additional keys are allowed
    minimal_record["metadata"]["languages"] = [{"test": "eng"}]
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # non-string types are not allowed as id values
    minimal_record["metadata"]["languages"] = [{"id": 1}]
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # Extra keys are not allowed
    minimal_record["metadata"]["languages"] = [{"id": "eng", "title": "rm"}]
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # No duplicates
    minimal_record["metadata"]["languages"] = [{"id": "eng"}, {"id": "eng"}]
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)
def test_contributor_affiliations_indexing(running_app,
                                           minimal_record_with_contributor):
    minimal_record = minimal_record_with_contributor
    draft = RDMDraft.create(minimal_record).commit()

    # Dump draft - dumps will dereference relations which inturn updates the
    # internal record dict so dump and record should be identical.
    dump = draft.dumps()
    assert dump["metadata"]["contributors"][0]["affiliations"] == [{
        "id":
        "cern",
        "name":
        "CERN",
        "@v":
        f"{running_app.affiliations_v._record.id}::1"
    }]

    # Load draft again - should produce an identical record.
    loaded_draft = RDMDraft.loads(dump)
    assert dict(draft) == dict(loaded_draft)

    # Calling commit() will clear the dereferenced relation.
    loaded_draft.commit()
    loaded_aff = loaded_draft["metadata"]["contributors"][0]["affiliations"]
    assert loaded_aff == [{"id": "cern"}]
def test_creator_affiliations_invalid(running_app, minimal_record):
    """Should fail on invalid id's and invalid structure."""
    # The id "invalid" does not exists.
    minimal_record["metadata"]["creators"][0]["affiliations"] = ([{
        "id":
        "invalid"
    }])
    pytest.raises(InvalidRelationValue, RDMDraft.create(minimal_record).commit)

    # Not a list of objects
    minimal_record["metadata"]["creators"][0]["affiliations"] = {"id": "cern"}
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # no additional keys are allowed
    minimal_record["metadata"]["creators"][0]["affiliations"] = ([{
        "test":
        "cern"
    }])
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # non-string types are not allowed as id values
    minimal_record["metadata"]["creators"][0]["affiliations"] = [{"id": 1}]
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)

    # No duplicates
    minimal_record["metadata"]["creators"][0]["affiliations"] = ([{
        "id": "cern"
    }, {
        "id": "cern"
    }])
    pytest.raises(ValidationError, RDMDraft.create, minimal_record)
Exemplo n.º 6
0
def test_languages_indexing(running_app, minimal_record):
    """Test languages relationship."""
    minimal_record["metadata"]["languages"] = [{"id": "eng"}]
    draft = RDMDraft.create(minimal_record).commit()

    # Dump draft - dumps will dereference relations which inturn updates the
    # internal record dict so dump and record should be identical.
    dump = draft.dumps()
    assert dump["metadata"]["languages"] == [{
        "id":
        "eng",
        "title": {
            "en": "English",
            "da": "Engelsk"
        },
        "@v":
        f"{running_app.languages_v._record.id}::1"
    }]

    # Load draft again - should produce an identical record.
    loaded_draft = RDMDraft.loads(dump)
    assert dict(draft) == dict(loaded_draft)

    # Calling commit() will clear the dereferenced relation.
    loaded_draft.commit()
    assert loaded_draft["metadata"]["languages"] == [{"id": "eng"}]
def test_languages_validation(running_app, minimal_record, lang):
    """Test languages relationship."""
    minimal_record["metadata"]["languages"] = [{"id": "eng"}]
    draft = RDMDraft.create(minimal_record)
    draft.commit()
    db.session.commit()
    assert minimal_record["metadata"]["languages"] == [{"id": "eng"}]

    lang = list(draft.relations.languages())[0]
    assert isinstance(lang, Vocabulary)
Exemplo n.º 8
0
def test_idempotence_dumps_loads(running_app, minimal_record):
    """Idempotence of dumps and loads."""
    # This simple test asserts a key property of the dumps and loads methods.
    # A record that's dumped, must when loaded produce exactly the same dict
    # representation of a record. This key property ensures that it doesn't
    # matter if a record is loaded from primary storage (database) or secondary
    # storages (index, files, ...). A record when loaded behaves like a normal
    # record.

    # If this tests fails likely either a system fields pre/post_dump/load
    # method is having an issue, or it might be an Elasticsearch dumper.

    # DO NOT CHANGE TEST UNLESS YOU ABSOLUTELY KNOW WHAT YOU'RE DOING
    draft = RDMDraft.create(minimal_record)
    loaded_draft = RDMDraft.loads(draft.dumps())
    assert dict(draft) == dict(loaded_draft)
def test_creator_affiliations_validation(running_app,
                                         minimal_record_with_creator):
    minimal_record = minimal_record_with_creator
    draft = RDMDraft.create(minimal_record)
    draft.commit()
    db.session.commit()

    # test it did not change
    creators = minimal_record_with_creator["metadata"]["creators"]
    affiliations = creators[0]["affiliations"]
    assert list(affiliations) == [{"id": "cern"}]

    # test it was saved properly
    aff = list(list(draft.relations.creator_affiliations())[0])[0]
    # since it is loaded it will contain more fields
    assert aff["id"] == "cern"
def test_community_integration(db, c, running_app, minimal_record):
    """Basic smoke test for communities integration."""
    draft = RDMDraft.create(minimal_record)
    draft.commit()
    db.session.commit()
    record = RDMRecord.publish(draft)
    record.commit()
    db.session.commit()
    record.parent.communities.add(c, default=True)
    record.parent.commit()
    record.commit()
    assert record.dumps()['parent']['communities'] == {
        'default': str(c.id),
        'ids': [str(c.id)],
    }
    db.session.commit()
def test_creator_affiliations_with_name_validation(
        running_app, minimal_record_with_creator):
    minimal_record = minimal_record_with_creator
    minimal_record["metadata"]["creators"][0]["affiliations"].append(
        {"name": "free-text"})
    draft = RDMDraft.create(minimal_record)
    draft.commit()
    db.session.commit()

    # test it did not change
    creators = minimal_record_with_creator["metadata"]["creators"]
    affiliations = creators[0]["affiliations"]
    assert list(affiliations) == [{"id": "cern"}, {"name": "free-text"}]

    # Length should be only 1, since free-text should not be saved
    aff_list = list(list(draft.relations.creator_affiliations())[0])
    assert len(aff_list) == 1
    aff = aff_list[0]
    # since it is loaded it will contain more fields
    assert aff["id"] == "cern"
def test_resource_types_indexing(running_app, minimal_record):
    """Test dereferencing characteristics/features really."""
    minimal_record["metadata"]["resource_type"] = {"id": "image-photo"}
    draft = RDMDraft.create(minimal_record).commit()

    # TODO/WARNING: draft.dumps() modifies draft
    dump = draft.dumps()
    assert dump["metadata"]["resource_type"] == {
        "id": "image-photo",
        "title": {
            "en": "Photo"
        },
        "@v": f"{running_app.resource_type_item._record.id}::1"
    }

    # Load draft again - should produce an identical record.
    loaded_draft = RDMDraft.loads(dump)
    assert dict(draft) == dict(loaded_draft)

    # Calling commit() will clear the dereferenced relation.
    loaded_draft.commit()
    assert loaded_draft["metadata"]["resource_type"] == {"id": "image-photo"}
Exemplo n.º 13
0
def test_subjects_indexing(running_app, minimal_record):
    """Test dereferencing characteristics/features really."""
    minimal_record["metadata"]["subjects"] = [{"id": "A-D000007"}]
    draft = RDMDraft.create(minimal_record).commit()

    # Dumping should return dereferenced representation
    dump = draft.dumps()
    assert dump["metadata"]["subjects"] == [{
        "id":
        "A-D000007",
        "title": {
            "en": "Abdominal Injuries"
        },
        "@v":
        f"{running_app.subject_v._record.id}::1"
    }]
    # NOTE/WARNING: draft.dumps() modifies the draft too
    assert draft["metadata"]["subjects"] == [{
        "id":
        "A-D000007",
        "title": {
            "en": "Abdominal Injuries"
        },
        "@v":
        f"{running_app.subject_v._record.id}::1"
    }]

    # Loading draft again should produce an identical record.
    loaded_draft = RDMDraft.loads(dump)
    assert dict(draft) == dict(loaded_draft)

    # Calling commit() should clear the dereferenced relation.
    draft.commit()
    assert draft["metadata"]["subjects"] == [{"id": "A-D000007"}]

    # subjects should be reachable through relations
    subject = next(draft.relations.subjects())
    assert "A-D000007" == subject["id"]