Exemplo n.º 1
0
def test_docs(conn):
    example = (b'Only the Knowledge Graph can unify all data types and '
               b'every data velocity into a single, coherent, unified whole.')

    # docstore
    docs = conn.docs()
    assert docs.size() == 0

    # add
    docs.add('doc', content.File('test/data/example.txt'))
    assert docs.size() == 1

    # get
    doc = docs.get('doc')
    assert doc == example

    with docs.get('doc', stream=True, chunk_size=1) as doc:
        assert b''.join(doc) == example

    # delete
    docs.delete('doc')
    assert docs.size() == 0

    # clear
    docs.add('doc', content.File('test/data/example.txt'))
    assert docs.size() == 1
    docs.clear()
    assert docs.size() == 0
Exemplo n.º 2
0
def test_content():

    r = content.Raw('content', content_types.TURTLE, 'zip', 'raw.ttl.zip')
    with r.data() as c:
        assert c == 'content'
        assert r.content_type == content_types.TURTLE
        assert r.content_encoding == 'zip'
        assert r.name == 'raw.ttl.zip'

    f = content.File('test/data/example.ttl')
    with f.data() as c:
        assert c.read() == b'<urn:subj> <urn:pred> <urn:obj> .'
        assert f.content_type == content_types.TURTLE
        assert f.content_encoding is None
        assert f.name == 'example.ttl'

    f = content.File('test/data/example.ttl.zip')
    assert f.content_type == content_types.TURTLE
    assert f.content_encoding == 'zip'
    assert f.name == 'example.ttl.zip'

    u = content.URL('https://www.w3.org/2000/10/rdf-tests/'
                    'RDF-Model-Syntax_1.0/ms_4.1_1.rdf')
    with u.data() as c:
        assert c == open('test/data/ms_4.1_1.rdf', 'rb').read()
        assert u.content_type == content_types.RDF_XML
        assert u.name == 'ms_4.1_1.rdf'
Exemplo n.º 3
0
def test_graphql():
    with stardog.admin.Admin() as admin:
        db = admin.new_database('graphql', {},
                                content.File('test/data/starwars.ttl'))

    with connection.Connection('graphql', username='******',
                               password='******') as c:
        gql = c.graphql()

        # query
        assert gql.query('{ Planet { system } }') == [{
            'system': 'Tatoo'
        }, {
            'system': 'Alderaan'
        }]

        # variables
        assert gql.query(
            'query getHuman($id: Integer) { Human(id: $id) {name} }',
            variables={'id': 1000}) == [{
                'name': 'Luke Skywalker'
            }]

        # schemas
        gql.add_schema('characters',
                       content=content.File('test/data/starwars.graphql'))

        assert len(gql.schemas()) == 1
        assert 'type Human' in gql.schema('characters')

        assert gql.query('{Human(id: 1000) {name friends {name}}}',
                         variables={'@schema': 'characters'}) == [{
                             'friends': [{
                                 'name': 'Han Solo'
                             }, {
                                 'name': 'Leia Organa'
                             }, {
                                 'name': 'C-3PO'
                             }, {
                                 'name': 'R2-D2'
                             }],
                             'name':
                             'Luke Skywalker'
                         }]

        gql.remove_schema('characters')
        assert len(gql.schemas()) == 0

        gql.add_schema('characters',
                       content=content.File('test/data/starwars.graphql'))
        gql.clear_schemas()
        assert len(gql.schemas()) == 0

    db.drop()
Exemplo n.º 4
0
def test_icv(conn):

    conn.begin()
    conn.clear()
    conn.add(content.File('test/data/icv-data.ttl'))
    conn.commit()

    icv = conn.icv()
    constraints = content.File('test/data/icv-constraints.ttl')

    # check/violations/convert
    assert not icv.is_valid(constraints)
    assert len(icv.explain_violations(constraints)) == 14
    assert 'SELECT DISTINCT' in icv.convert(constraints)

    # add/remove/clear
    icv.add(constraints)
    icv.remove(constraints)
    icv.clear()

    constraint = content.Raw(':Manager rdfs:subClassOf :Employee .',
                             content_types.TURTLE)

    # add/remove/clear
    icv.add(constraint)
    icv.remove(constraint)
    icv.clear()

    # nothing in the db yet so it should be valid
    assert icv.is_valid(constraint)

    # insert a triple that violates the constraint
    conn.begin()
    conn.add(content.Raw(':Alice a :Manager .', content_types.TURTLE))
    conn.commit()

    assert not icv.is_valid(constraint)

    assert len(icv.explain_violations(constraint)) == 2

    # make Alice an employee so the constraint is satisfied
    conn.begin()
    conn.add(content.Raw(':Alice a :Employee .', content_types.TURTLE))
    conn.commit()

    assert icv.is_valid(constraint)

    assert 'SELECT DISTINCT' in icv.convert(constraint)
Exemplo n.º 5
0
def test_databases(admin):
    assert len(admin.databases()) == 0

    # create database
    db = admin.new_database('db', {
        'search.enabled': True,
        'spatial.enabled': True
    })

    assert len(admin.databases()) == 1
    assert db.name == 'db'
    assert db.get_options('search.enabled', 'spatial.enabled') == {
        'search.enabled': True,
        'spatial.enabled': True
    }

    # change options
    db.offline()
    db.set_options({'spatial.enabled': False})
    db.online()

    assert db.get_options('search.enabled', 'spatial.enabled') == {
        'search.enabled': True,
        'spatial.enabled': False
    }

    # optimize
    db.optimize()

    # repair
    db.offline()
    db.repair()
    db.online()

    # bulk load
    contents = [
        content.Raw(
            '<urn:subj> <urn:pred> <urn:obj3> .',
            content_types.TURTLE,
            name='bulkload.ttl'),
        (content.File('test/data/example.ttl.zip'), 'urn:context'),
        content.URL('https://www.w3.org/2000/10/rdf-tests/'
                    'RDF-Model-Syntax_1.0/ms_4.1_1.rdf')
    ]

    bl = admin.new_database('bulkload', {}, *contents)

    with connection.Connection(
            'bulkload', username='******', password='******') as c:
        q = c.select('select * where { graph <urn:context> {?s ?p ?o}}')
        assert len(q['results']['bindings']) == 1
        assert c.size() == 7

    # clear
    db.drop()
    bl.drop()

    assert len(admin.databases()) == 0
Exemplo n.º 6
0
def test_virtual_graphs(admin):
    assert len(admin.virtual_graphs()) == 0

    options = {
        "namespaces": "stardog=tag:stardog:api",
        "jdbc.driver": "com.mysql.jdbc.Driver",
        "jdbc.username": "******",
        "jdbc.password": "******",
        "jdbc.url": "jdbc:mysql://localhost/support"
    }

    vg = admin.virtual_graph('test')

    # TODO add VG to test server
    with pytest.raises(
            exceptions.StardogException, match='java.sql.SQLException'):
        admin.new_virtual_graph('vg', content.File('test/data/r2rml.ttl'),
                                options)

    with pytest.raises(
            exceptions.StardogException, match='java.sql.SQLException'):
        vg.update('vg', content.File('test/data/r2rml.ttl'), options)

    with pytest.raises(
            exceptions.StardogException,
            match='Virtual Graph test Not Found!'):
        vg.available()

    with pytest.raises(
            exceptions.StardogException,
            match='Virtual Graph test Not Found!'):
        vg.options()

    with pytest.raises(
            exceptions.StardogException,
            match='Virtual Graph test Not Found!'):
        vg.mappings()

    with pytest.raises(
            exceptions.StardogException,
            match='Virtual Graph test Not Found!'):
        vg.delete()
Exemplo n.º 7
0
def test_backup_and_restore(admin):
    def check_db_for_contents(dbname, num_results):
        with connection.Connection(
                dbname, username='******', password='******') as c:
            q = c.select('select * where { ?s ?p ?o }')
            assert len(q['results']['bindings']) == num_results

    # determine the path to the backups
    now = datetime.datetime.now()
    date = now.strftime('%Y-%m-%d')
    stardog_home = os.getenv('STARDOG_HOME', '/data/stardog')
    restore_from = os.path.join(
        f"{stardog_home}", '.backup', 'backup_db', f"{date}")

    # make a db with test data loaded
    db = admin.new_database(
        'backup_db', {}, content.File('test/data/starwars.ttl'))

    db.backup()
    db.drop()

    # data is back after restore
    try:
        admin.restore(from_path=restore_from)
    except Exception as e:
        raise Exception(str(e) + ". Check whether $STARDOG_HOME is set")
    check_db_for_contents('backup_db', 87)

    # error if attempting to restore over an existing db without force
    with pytest.raises(
            exceptions.StardogException,
            match='Database already exists'):
        admin.restore(from_path=restore_from)

    # restore to a new db
    admin.restore(from_path=restore_from, name='backup_db2')
    check_db_for_contents('backup_db2', 87)

    # force to overwrite existing
    db.drop()
    db = admin.new_database('backup_db')
    check_db_for_contents('backup_db', 0)
    admin.restore(from_path=restore_from, force=True)
    check_db_for_contents('backup_db', 87)

    # the backup location can be specified
    db.backup(to=os.path.join(stardog_home, 'backuptest'))

    # clean up
    db.drop()
    admin.database('backup_db2').drop()
Exemplo n.º 8
0
def test_transactions(conn):
    data = content.Raw('<urn:subj> <urn:pred> <urn:obj> .',
                       content_types.TURTLE)

    # add
    conn.begin()
    conn.clear()
    conn.add(data)
    conn.commit()

    assert conn.size() == 1

    # remove
    conn.begin()
    conn.remove(content.File('test/data/example.ttl.zip'))
    conn.commit()

    assert conn.size() == 0

    # rollback
    conn.begin()
    conn.add(data)
    conn.rollback()

    assert conn.size() == 0

    # export
    conn.begin()
    conn.add(
        content.URL('https://www.w3.org/2000/10/rdf-tests/'
                    'RDF-Model-Syntax_1.0/ms_4.1_1.rdf'))
    conn.commit()

    assert b'<http://description.org/schema/attributedTo>' in conn.export()

    with conn.export(stream=True, chunk_size=1) as stream:
        assert b'<http://description.org/schema/attributedTo>' in b''.join(
            stream)

    # clear
    conn.begin()
    conn.clear()
    conn.commit()

    assert conn.size() == 0

    # add named graph
    conn.begin()
    conn.add(data, graph_uri='urn:graph')
    conn.commit()

    assert conn.size(exact=True) == 1

    # remove from default graph
    conn.begin()
    conn.remove(data)
    conn.commit()

    assert conn.size(exact=True) == 1

    # remove from named graph
    conn.begin()
    conn.remove(data, graph_uri='urn:graph')
    conn.commit()

    assert conn.size(exact=True) == 0
Exemplo n.º 9
0
def test_queries(conn):
    # add
    conn.begin()
    conn.clear()
    conn.add(content.File('test/data/starwars.ttl'))
    conn.commit()

    # select
    q = conn.select('select * {?s :name "Luke Skywalker"}')
    assert len(q['results']['bindings']) == 1

    # params
    q = conn.select('select * {?s a :Human}', offset=1, limit=10, timeout=1000)
    assert len(q['results']['bindings']) == 4

    # reasoning
    q = conn.select('select * {?s a :Character}', reasoning=True)
    assert len(q['results']['bindings']) == 7

    # no results with reasoning turned off
    q = conn.select('select * {?s a :Character}')
    assert len(q['results']['bindings']) == 0

    # the reasoning param on the query won't work in a transaction
    # that doesn't have reasoning enabled (the default)
    conn.begin()
    q = conn.select('select * {?s a :Character}', reasoning=True)
    assert len(q['results']['bindings']) == 0
    conn.rollback()

    # the query should return results if reasoning is on for the transaction
    conn.begin(reasoning=True)
    q = conn.select('select * {?s a :Character}', reasoning=True)
    assert len(q['results']['bindings']) == 7
    conn.rollback()

    # reasoning does not need to be specified in the query when it is
    # on in the transaction
    conn.begin(reasoning=True)
    q = conn.select('select * {?s a :Character}')
    assert len(q['results']['bindings']) == 7
    conn.rollback()

    # bindings
    q = conn.select('select * {?s :name ?o}',
                    bindings={'o': '"Luke Skywalker"'})
    assert len(q['results']['bindings']) == 1

    # paths
    q = conn.paths('paths start ?x = :luke end ?y = :leia via ?p')
    assert len(q['results']['bindings']) == 1

    # ask
    q = conn.ask('ask {:luke a :Droid}')
    assert not q

    # construct
    q = conn.graph('construct {:luke a ?o} where {:luke a ?o}')
    assert q.strip(
    ) == b'<http://api.stardog.com/luke> a <http://api.stardog.com/Human> .'

    # update
    q = conn.update('delete where {?s ?p ?o}')
    assert conn.size() == 0

    # explain
    q = conn.explain('select * {?s ?p ?o}')
    assert 'Projection(?s, ?p, ?o)' in q

    # query in transaction
    conn.begin()
    conn.add(content.File('test/data/starwars.ttl'))

    q = conn.select('select * {?s :name "Luke Skywalker"}')
    assert len(q['results']['bindings']) == 1

    conn.commit()

    # update in transaction
    conn.begin()
    q = conn.update('delete where {?s ?p ?o}')

    q = conn.select('select * {?s ?p ?o}')
    assert len(q['results']['bindings']) == 0

    conn.commit()