def test_path():
    # Check the path is read
    expected_path = 'http://schema.org/givenName'
    with open('tests/inputs/test_shape.ttl') as f:
        rdf_handler = RDFHandler(f)
    properties = rdf_handler.get_shape_no_root_iri()['properties']
    assert any(str(p['path'] == expected_path for p in properties))
Ejemplo n.º 2
0
def test_empty_file():
    with pytest.raises(Exception):
        with open('tests/inputs/empty_file.ttl') as f:
            rdf_handler = RDFHandler(f)
            shape = rdf_handler.get_shape_no_root_iri()
            generate_form(shape, form_destination='result.html')
            map_destination='result.ttl'
            rdf_handler.create_rdf_map(shape, map_destination)
def test_empty_shape():
    # Valid but empty shape
    expected_result = dict()
    expected_result['target_class'] = URIRef('http://schema.org/Person')
    expected_result['groups'] = list()
    expected_result['properties'] = list()
    expected_result['closed'] = False
    with open('tests/inputs/empty_shape.ttl') as f:
        rdf_handler = RDFHandler(f)
    result = rdf_handler.get_shape_no_root_iri()
    assert result == expected_result
Ejemplo n.º 4
0
def test_shape():
    # Contents of result can't be verified due to RDF and therefore the HTML result being unordered
    if os.path.exists('results'):
        shutil.rmtree('results')
    with open('tests/inputs/test_shape.ttl') as f:
        rdf_handler = RDFHandler(f)
        shape = rdf_handler.get_shape_no_root_iri()
        generate_form(shape, form_destination='results/result.html')
        map_destination='results/result.ttl'
        rdf_handler.create_rdf_map(shape, map_destination)
    assert os.path.exists('results/result.html')
def test_node_kind_missing_with_nested_properties():
    # Check that a property with nested properties is automatically assigned a value of sh:BlankNodeOrIRI when
    # sh:nodeKind isn't provided
    with open(
            'tests/inputs/node_kind/missing_nodekind_with_nested_properties.ttl'
    ) as f:
        shape = RDFHandler(f).get_shape_no_root_iri()
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty1':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'BlankNodeOrIRI'
def test_node_kind_missing_with_hasvalue_constraint():
    # Check that a property with a missing sh:nodeKind constraint is automatically assigned a value of sh:Literal when
    # the sh:hasValue constraint is present
    with open(
            'tests/inputs/node_kind/missing_nodekind_with_hasvalue_constraint.ttl'
    ) as f:
        shape = RDFHandler(f).get_shape_no_root_iri()
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'Literal'
def test_node_kind_iri_or_literal():
    # Check no warnings are raised when there are no nested properties
    with pytest.warns(None) as record:
        with open('tests/inputs/node_kind/iri_or_literal.ttl') as f:
            shape = RDFHandler(f).get_shape_no_root_iri()
    assert not record.list

    # Check the sh:nodeKind constraint was read correctly
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'IRIOrLiteral'
def test_shape():
    shape = RDFHandler(
        open('tests/inputs/test_shape.ttl')).get_shape_no_root_iri()
    # Run the following tests on the test shape to avoid getting it every time
    # They won't be automatically discovered by pytest without the test_ prefix
    constraint_generic_test(shape)
    constraint_name_test(shape)
    constraint_order_test(shape)
    constraint_mincount_test(shape)
    constraint_in_test(shape)
    constraint_languagein_test(shape)
    constraint_min_test(shape)
    constraint_max_test(shape)
    nested_properties_test(shape)
    node_test(shape)
    group_test(shape)
def test_node_iri_or_literal_has_value():
    # Check that a property with sh:nodeKind sh:IRIOrLiteral is changed to sh:Literal when the sh:hasValue constraint
    # is present
    with pytest.warns(UserWarning) as record:
        with open('tests/inputs/node_kind/iri_or_literal_hasvalue.ttl') as f:
            shape = RDFHandler(f).get_shape_no_root_iri()
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'Literal'

    # Check that a warning is raised
    assert record[0].message.args[0] == 'Property "testProperty" has constraint "sh:nodeKind" with value ' \
                                        '"http://www.w3.org/ns/shacl#IRIOrLiteral" which is incompatible with ' \
                                        'constraint sh:hasValue. Replacing with "http://www.w3.org/ns/shacl#Literal".'
def test_node_kind_invalid_with_nested_properties():
    # Check that a property with nested properties is automatically assigned a value of sh:BlankNodeOrIRI when
    # sh:nodeKind isn't a permitted value
    with pytest.warns(UserWarning) as record:
        with open(
                'tests/inputs/node_kind/invalid_nodekind_with_nested_properties.ttl'
        ) as f:
            shape = RDFHandler(f).get_shape_no_root_iri()
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty1':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'BlankNodeOrIRI'

    # Check that a warning is raised
    assert record[0].message.args[0] == 'Property "testProperty1" has constraint "sh:nodeKind" with invalid value ' \
                                        '"http://www.w3.org/ns/shacl#Invalid". Replacing with ' \
                                        '"http://www.w3.org/ns/shacl#BlankNodeOrIRI".'
def test_node_kind_literal_with_nested_properties():
    # Check a warning is raised when there are nested properties
    with pytest.warns(UserWarning) as record:
        with open('tests/inputs/node_kind/literal_with_nested_properties.ttl'
                  ) as f:
            shape = RDFHandler(f).get_shape_no_root_iri()

    # Check that the correct warning is raised
    assert record[0].message.args[0] == 'Property "testProperty1" has constraint "sh:nodeKind" with value ' \
                                        '"http://www.w3.org/ns/shacl#Literal". The property shapes provided in this ' \
                                        'property will be ignored.'

    # Check the sh:nodeKind constraint was read correctly
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty1':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'Literal'
def test_node_kind_invalid_with_hasvalue_constraint():
    # Check that a property with an invalid sh:nodeKind is automatically assigned a value of sh:Literal when the
    # sh:hasValue constraint is present
    with pytest.warns(UserWarning) as record:
        with open(
                'tests/inputs/node_kind/invalid_nodekind_with_hasvalue_constraint.ttl'
        ) as f:
            shape = RDFHandler(f).get_shape_no_root_iri()
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'Literal'

    # Check that a warning is raised
    assert record[0].message.args[0] == 'Property "testProperty" has constraint "sh:nodeKind" with invalid value ' \
                                        '"http://www.w3.org/ns/shacl#Invalid". Replacing with ' \
                                        '"http://www.w3.org/ns/shacl#Literal".'
def test_node_kind_blanknode_without_nested_properties():
    # Check a warning is raised when there are no nested properties
    with pytest.warns(UserWarning) as record:
        with open(
                'tests/inputs/node_kind/blanknode_without_nested_properties.ttl'
        ) as f:
            shape = RDFHandler(f).get_shape_no_root_iri()

    # Check that the correct warning is raised
    assert record[0].message.args[0] == 'Property "testProperty" has constraint "sh:nodeKind" with value ' \
                                        '"sh:BlankNode" but no property shapes are provided. This property will have ' \
                                        'no input fields.'

    # Check the sh:nodeKind constraint was read correctly
    node_kind = None
    for p in shape['properties']:
        if str(p['path']) == 'http://example.org/ex#testProperty':
            node_kind = p['nodeKind']
    assert node_kind == SHACL + 'BlankNode'
def test_no_target_class():
    # Checks behavior when the shape provided doesn't have a target class
    with open('tests/inputs/no_target_class.ttl') as f:
        rdf_handler = RDFHandler(f)
    with pytest.raises(Exception):
        rdf_handler.get_shape_no_root_iri()
def test_invalid_mincount():
    # Check that a non-integer minCount raises the appropriate error
    with open('tests/inputs/invalid_minCount.ttl') as f:
        rdf_handler = RDFHandler(f)
    with pytest.raises(Exception):
        rdf_handler.get_shape_no_root_iri()
def test_missing_group():
    # Checks to make sure an exception is thrown if a group is referenced but does not exist
    with open('tests/inputs/missing_group.ttl') as f:
        rdf_handler = RDFHandler(f)
    with pytest.raises(Exception):
        rdf_handler.get_shape_no_root_iri()
def test_empty_file():
    # Checks behaviour when an empty file is supplied
    with open('tests/inputs/empty_file.ttl') as f:
        rdf_handler = RDFHandler(f)
    assert rdf_handler.get_shape_no_root_iri() is None
def test_implicit_target_class():
    # Checks to make sure the target class is correctly identified when implicitly declared
    with open('tests/inputs/implicit_target_class.ttl') as f:
        rdf_handler = RDFHandler(f)
    shape = rdf_handler.get_shape_no_root_iri()
    assert str(shape['target_class']) == 'http://schema.org/Person'
def test_no_path():
    # Checks behaviour when a property doesn't have a compulsory path constraint
    with open('tests/inputs/no_path.ttl') as f:
        rdf_handler = RDFHandler(f)
    with pytest.raises(Exception):
        rdf_handler.get_shape_no_root_iri()
def test_recursion():
    # Checks to make sure recursion isn't accepted
    with open('tests/inputs/recursion.ttl') as f:
        rdf_handler = RDFHandler(f)
    with pytest.raises(Exception):
        rdf_handler.get_shape_no_root_iri()