Exemple #1
0
def test_record_pids(app, db, lang_type, lic_type):
    """Test record pid creation."""
    record = Vocabulary.create(
        {
            'id': 'eng',
            'title': {
                'en': 'English',
                'da': 'Engelsk'
            }
        },
        type=lang_type)
    Vocabulary.pid.create(record)
    assert record.type == lang_type
    assert record.pid.pid_value == 'eng'
    assert record.pid.pid_type == 'lng'
    assert Vocabulary.pid.resolve(('languages', 'eng'))

    record = Vocabulary.create(
        {
            'id': 'cc-by',
            'title': {
                'en': 'CC-BY',
                'da': 'CC-BY'
            }
        },
        type=lic_type)
    Vocabulary.pid.create(record)
    assert record.type == lic_type
    assert record.pid.pid_value == 'cc-by'
    assert record.pid.pid_type == 'lic'
    assert Vocabulary.pid.resolve(('licenses', 'cc-by'))
Exemple #2
0
def example_record(db, example_data, lang_type):
    """Example record."""
    record = Vocabulary.create(example_data, type=lang_type)
    Vocabulary.pid.create(record)
    record.commit()
    db.session.commit()
    return record
Exemple #3
0
def test_record_indexing(app, db, es, example_record, indexer, search_get):
    """Test indexing of a record."""
    # Index document in ES
    assert indexer.index(example_record)["result"] == "created"

    # Retrieve document from ES
    data = search_get(id=example_record.id)

    # Loads the ES data and compare
    record = Vocabulary.loads(data["_source"])
    assert record == example_record
    assert record.id == example_record.id
    assert record.revision_id == example_record.revision_id
    assert record.created == example_record.created
    assert record.updated == example_record.updated

    # Check system fields - i.e reading related type object from
    assert record == example_record
    assert record.type.id == 'languages'
    assert record.type.pid_type == 'lng'

    # Check that object was recrated without hitting DB
    assert inspect(record.type).persistent is False
    Vocabulary.type.session_merge(record)
    assert inspect(record.type).persistent is True
Exemple #4
0
def test_vocabulary_type(app, db):
    """Vocabulary creation."""
    vocabulary_type = VocabularyType(**{"name": "test-type"})
    db.session.add(vocabulary_type)
    db.session.commit()
    record = Vocabulary.create({},
                               metadata={
                                   "title": "test-item",
                                   "type": "test-type"
                               })
    assert record.metadata == {"title": "test-item", "type": "test-type"}
Exemple #5
0
def migrate_vocabularies():
    """Migrate old vocabularies."""
    print("Migrating old vocabularies...")
    # remove resource types
    try:
        # DO NOT DO THIS KIND OF QUERY FOR LARGE SETS
        rsrcts = PersistentIdentifier.query.filter_by(pid_type="rsrct")
        for rsrct in rsrcts:
            Vocabulary.get_record(id_=rsrct.object_uuid).delete(force=True)
        rsrcts.delete()  # delete from pidstore
        db.session.commit()
        print("resource_types vocabularies removed: OK.")
    except NoResultFound:
        print("No resource_types vocabularies found: OK.")
    try:
        rsrct = VocabularyType.query.filter_by(id="resource_types").one()
        db.session.delete(rsrct)
        db.session.commit()
        print("resource_types vocabulary type removed: OK.")
    except NoResultFound:
        print("No resource_types vocabulary found: OK.")

    # remove old affiliations
    try:
        schemes = VocabularyScheme.query.filter_by(
            parent_id="affiliations").all()
        for scheme in schemes:
            db.session.delete(scheme)
        db.session.commit()
        print("Affiliations schemes removed: OK.")
    except NoResultFound:
        print("No affiliations scheme not found: OK.")
    try:
        db.session.delete(
            VocabularyType.query.filter_by(id="affiliations").one())
        db.session.commit()
        print("Affiliations vocabulary type removed: OK.")
    except NoResultFound:
        print("No parent affiliations not found: OK.")

    print("Old vocabularies migrated.")
Exemple #6
0
def test_record_empty(app, db):
    """Test record creation."""
    # Empty record creation works, and injects a schema.
    record = Vocabulary.create({})
    db.session.commit()
    assert record.schema

    # JSONSchema validation works.
    pytest.raises(SchemaValidationError, Vocabulary.create,
                  {"metadata": {
                      "title": 1
                  }})
Exemple #7
0
def test_record_indexing(app, db, es, example_record, indexer):
    """Test indexing of a record."""
    # Index document in ES
    assert indexer.index(example_record)["result"] == "created"

    # Retrieve document from ES
    data = current_search_client.get("vocabularies-vocabulary-v1.0.0",
                                     id=example_record.id,
                                     doc_type="_doc")

    # Loads the ES data and compare
    record = Vocabulary.loads(data["_source"])
    assert record == example_record
    assert record.id == example_record.id
    assert record.revision_id == example_record.revision_id
    assert record.created == example_record.created
    assert record.updated == example_record.updated
    assert record.vocabulary_type_id == example_record.vocabulary_type_id

    # Check system fields
    assert record.metadata == example_record["metadata"]
Exemple #8
0
def test_vocabulary_type(app, db):
    """Vocabulary creation."""
    vocabulary_type = VocabularyType(**{"name": "test-type"})
    db.session.add(vocabulary_type)
    db.session.commit()
    record = Vocabulary.create({},
                               metadata={
                                   "title": {
                                       "en": "test-item"
                                   },
                                   "type": "test-type"
                               })
    record.commit()
    db.session.commit()
    assert record.metadata == {
        "title": {
            "en": "test-item"
        },
        "type": "test-type",
    }
    assert record.pid.status == "R"
    assert record.id
Exemple #9
0
def test_record_schema_validation(app, db, lang_type):
    """Record schema validation."""
    # Good data
    record = Vocabulary.create(
        {
            'id': 'eng',
            'title': {
                'en': 'English',
                'da': 'Engelsk'
            },
            'description': {
                'en': 'English',
                'da': 'Engelsk'
            },
            'icon': 'en',
            'props': {
                'akey': 'a value'
            },
        },
        type=lang_type)
    assert record.schema

    # Bad data
    examples = [
        # title/descriptions are objects of key/string.
        {
            'id': 'en',
            'title': 'not a string'
        },
        {
            'id': 'en',
            'title': {
                'en': 123
            }
        },
        {
            'id': 'en',
            'description': 'not a string'
        },
        # icon must be strings
        {
            'id': 'en',
            'icon': 123
        },
        # props values must be strings
        {
            'id': 'en',
            'props': {
                'key': 123
            }
        },
        {
            'id': 'en',
            'props': {
                'key': {
                    'test': 'test'
                }
            }
        },
        # Additional properties false
        {
            'id': 'en',
            'metadata': {
                'title': 'test'
            }
        },
    ]

    for ex in examples:
        pytest.raises(SchemaValidationError, Vocabulary.create, ex)
Exemple #10
0
def validates(data):
    """Validates affiliation data."""
    Vocabulary(data).validate()

    return True
Exemple #11
0
def test_record_via_field(app, db):
    """Record creation via field."""
    record = Vocabulary.create({}, metadata={"title": "test"})
    assert record.metadata == {"title": "test"}