def test_liststatements_with_shape_on_its_own_line_fields_with_none_are_implicit():
    """Fields with value None (here: 'annot' and 'start') are simply implicit."""
    csvrows_list = [
        {"shape_id": "@a", "prop_id": None, "value_type": None},
        {"shape_id": None, "prop_id": "dct:creator", "value_type": "URI"},
        {"shape_id": None, "prop_id": "dct:subject", "value_type": "URI"},
    ]
    assert list_statements(csvrows_list) == [
        Statement(
            start=True,
            shape_id="@a",
            prop_id="dct:creator",
            annot=None,
            value_type="URI",
        ),
        Statement(
            start=True,
            shape_id="@a",
            prop_id="dct:subject",
            prop_label=None,
            value_type="URI",
        ),
    ]
    assert list_statements(csvrows_list) == [
        Statement(start=True, shape_id="@a", prop_id="dct:creator", value_type="URI"),
        Statement(start=True, shape_id="@a", prop_id="dct:subject", value_type="URI"),
    ]
Esempio n. 2
0
def test_statement_initialized_by_assignment_with_some_none():
    """Statement instance fields created by assignment, others have default None."""
    shap = Statement()
    shap.prop_id = "dcterms:creator"
    shap.value_type = "URI"
    assert shap == Statement(shape_id=None, prop_id="dcterms:creator", value_type="URI")
    assert not shap.shape_id
    assert not shap.mand
def test_liststatements_with_missing_value_type():
    """Should parse even if input lacks field 'value_type'."""
    as_input = [
        {
            "shape_id": "@book",
            "prop_id": "",
            "prop_label": "Book",
            "constraint_value": "",
            "annot": "",
        },
        {
            "shape_id": "",
            "prop_id": "rdf:type",
            "prop_label": "instance of",
            "constraint_value": "sdo:Book",
            "annot": "must be schema.org/Book",
        },
        {
            "shape_id": "",
            "prop_id": "rdf:type",
            "prop_label": "instance of",
            "constraint_value": "wd:Q571",
            "annot": "must be wikidata Book",
        },
    ]
    expected = [
        Statement(
            start=True,
            shape_id="@book",
            shape_label=None,
            prop_id="rdf:type",
            prop_label="instance of",
            mand=None,
            repeat=None,
            value_type=None,
            value_datatype=None,
            constraint_value="sdo:Book",
            constraint_type=None,
            shape_ref=None,
            annot="must be schema.org/Book",
        ),
        Statement(
            start=True,
            shape_id="@book",
            shape_label=None,
            prop_id="rdf:type",
            prop_label="instance of",
            mand=None,
            repeat=None,
            value_type=None,
            value_datatype=None,
            constraint_value="wd:Q571",
            constraint_type=None,
            shape_ref=None,
            annot="must be wikidata Book",
        ),
    ]
    assert list_statements(as_input) == expected
def test_liststatements_with_shape_on_its_own_line():
    """If shape IDs used previously, used for subsequent statements if no Shape ID."""
    csvrows_list = [
        {"shape_id": "@a", "prop_id": None, "value_type": None},
        {"shape_id": None, "prop_id": "dct:creator", "value_type": "URI"},
        {"shape_id": None, "prop_id": "dct:subject", "value_type": "URI"},
    ]
    assert list_statements(csvrows_list) == [
        Statement(start=True, shape_id="@a", prop_id="dct:creator", value_type="URI"),
        Statement(start=True, shape_id="@a", prop_id="dct:subject", value_type="URI"),
    ]
Esempio n. 5
0
def test_listshapes_one_shape_and_shape_label():
    """One Shape with shape label."""
    as_input = [
        Statement(
            start=True,
            shape_id="@a",
            shape_label="Author",
            prop_id="dct:creator",
            value_type="URI",
        ),
        Statement(
            start=True,
            shape_id="@a",
            shape_label="Author",
            prop_id="dct:date",
            value_type="String",
        ),
    ]
    assert list_shapes(as_input) == [
        Shape(
            start=True,
            shape_id="@a",
            shape_label="Author",
            shape_statements=[
                {
                    "prop_id": "dct:creator",
                    "value_type": "URI",
                    "prop_label": None,
                    "mand": None,
                    "repeat": None,
                    "value_datatype": None,
                    "constraint_value": None,
                    "constraint_type": None,
                    "shape_ref": None,
                    "annot": None,
                },
                {
                    "prop_id": "dct:date",
                    "value_type": "String",
                    "prop_label": None,
                    "mand": None,
                    "repeat": None,
                    "value_datatype": None,
                    "constraint_value": None,
                    "constraint_type": None,
                    "shape_ref": None,
                    "annot": None,
                },
            ],
        )
    ]
def test_liststatements():
    """Turn list of dictionaries into list of Statement objects."""
    csvrows_list = [
        {"shape_id": "@a", "prop_id": "dct:creator", "value_type": "URI"},
        {"shape_id": "@a", "prop_id": "dct:subject", "value_type": "URI"},
        {"shape_id": "@a", "prop_id": "dct:date", "value_type": "String"},
        {"shape_id": "@b", "prop_id": "foaf:name", "value_type": "String"},
    ]
    assert list_statements(csvrows_list) == [
        Statement(start=True, shape_id="@a", prop_id="dct:creator", value_type="URI"),
        Statement(start=True, shape_id="@a", prop_id="dct:subject", value_type="URI"),
        Statement(start=True, shape_id="@a", prop_id="dct:date", value_type="String"),
        Statement(start=False, shape_id="@b", prop_id="foaf:name", value_type="String"),
    ]
Esempio n. 7
0
def test_check():
    """Check CSV file structure for anomalies."""
    statement = Statement(
        start=True,
        shape_id="@default",
        shape_label=None,
        prop_id="wdt:P31",
        prop_label=None,
        mand=None,
        repeat=None,
        value_type="URI",
        value_datatype=None,
        constraint_value="wd:",
        constraint_type="URIStem",
        shape_ref=None,
        annot=None,
    )
    assert statement._is_uristem_used_correctly()
def test_liststatements_without_shape_ids():
    """Not shape IDs specified, so shape is '@default'."""
    csvrows_list = [
        {"prop_id": "dct:creator", "value_type": "URI"},
        {"prop_id": "dct:subject", "value_type": "URI"},
        {"prop_id": "dct:date", "value_type": "String"},
    ]
    assert list_statements(csvrows_list) == [
        Statement(
            start=True, shape_id="@default", prop_id="dct:creator", value_type="URI"
        ),
        Statement(
            start=True, shape_id="@default", prop_id="dct:subject", value_type="URI"
        ),
        Statement(
            start=True, shape_id="@default", prop_id="dct:date", value_type="String"
        ),
    ]
def test_liststatements_with_shape_ids_specified_as_none():
    """Shape IDs specified as 'None', so shape is '@default'.
    When "shape_id" specified as 'None', row["shape_id"] will be False.
    General case: when field specified as None."""
    csvrows_list = [
        {"shape_id": None, "prop_id": "dct:creator", "value_type": "URI"},
        {"shape_id": None, "prop_id": "dct:subject", "value_type": "URI"},
        {"shape_id": None, "prop_id": "dct:date", "value_type": "String"},
    ]
    assert list_statements(csvrows_list) == [
        Statement(
            start=True, shape_id="@default", prop_id="dct:creator", value_type="URI"
        ),
        Statement(
            start=True, shape_id="@default", prop_id="dct:subject", value_type="URI"
        ),
        Statement(
            start=True, shape_id="@default", prop_id="dct:date", value_type="String"
        ),
    ]
Esempio n. 10
0
def test_statement_initialized_by_assignment():
    """Statement instance fields created by assignment."""
    shap = Statement(shape_id="@photo", prop_id="dcterms:creator", value_type="URI")
    shap2 = Statement()
    shap2.shape_id = "@photo"
    shap2.prop_id = "dcterms:creator"
    shap2.value_type = "URI"
    assert shap == shap2
Esempio n. 11
0
def test_statement_bad_field_initialized_by_assignment():
    """Attempted assignment to bad field raises TypeError."""
    shap = Statement()
    shap.foobar = "@photo"
    shap.prop_id = "dcterms:creator"
    shap.value_type = "URI"
    with pytest.raises(TypeError):
        assert shap == Statement(
            foobar="@photo", prop_id="dcterms:creator", value_type="URI"
        )
Esempio n. 12
0
def test_statement_initialized_with_just_one_field():
    """Statement instance initialized with just one field."""
    shap = Statement(prop_id="dcterms:creator")
    assert not shap.start
    assert shap.shape_id is None
    assert shap.shape_label is None
    assert shap.prop_id == "dcterms:creator"
    assert shap.prop_label is None
    assert shap.mand is None
    assert shap.repeat is None
    assert shap.value_type is None
    assert shap.value_datatype is None
    assert shap.constraint_value is None
    assert shap.constraint_type is None
    assert shap.shape_ref is None
    assert shap.annot is None
Esempio n. 13
0
def test_statement_initialized_without_property_id():
    """Statement instance initialized without property ID. Shouldn't this fail?"""
    shap = Statement()
    assert not shap.start
    assert shap.shape_id is None
    assert shap.shape_label is None
    assert shap.prop_id is None
    assert shap.prop_label is None
    assert shap.mand is None
    assert shap.repeat is None
    assert shap.value_type is None
    assert shap.value_datatype is None
    assert shap.constraint_value is None
    assert shap.constraint_type is None
    assert shap.shape_ref is None
    assert shap.annot is None
Esempio n. 14
0
def test_statement_fields_individually_addressable():
    """Statement instance fields individually addressable."""
    shap = Statement(shape_id="@photo", prop_id="dcterms:creator", value_type="URI")
    assert shap.shape_id == "@photo"
    assert shap.prop_id == "dcterms:creator"
    assert shap.value_type == "URI"
Esempio n. 15
0
def test_statement_initialized_from_named_arguments_and_order_is_insignficant():
    """Order of arguments is insignificant (just a reminder to self)."""
    assert Statement(
        shape_id="@photo", prop_id="dcterms:creator", value_type="URI"
    ) == Statement(prop_id="dcterms:creator", shape_id="@photo", value_type="URI")
Esempio n. 16
0
"""Turn list of Statements into list of Shapes."""

from csv2shex.mkshapes import list_shapes, Shape
from csv2shex.mkstatements import Statement

LIST_OF_STATEMENT_OBJECTS = [
    Statement(start=True,
              shape_id="@a",
              prop_id="dct:creator",
              value_type="URI"),
    Statement(start=True,
              shape_id="@a",
              prop_id="dct:subject",
              value_type="URI"),
    Statement(start=True,
              shape_id="@a",
              prop_id="dct:date",
              value_type="String"),
    Statement(start=False,
              shape_id="@b",
              prop_id="foaf:name",
              value_type="String"),
]


def test_listshapes_one_shape():
    """Turn list of Statement objects into list with one Shape."""
    as_input = [
        Statement(start=True,
                  shape_id="@a",
                  prop_id="dct:creator",