def test_shape_initialized_by_assignment():
    """TAPShape instances can be created by assignment."""
    shap = TAPShape()
    shap.shapeID = ":a"
    shap.shapeLabel = "Resource"
    shap.state_list = []
    shap.state_list.append({
        "propertyID": "dct:creator",
        "valueNodeType": "IRI"
    })
    shap.state_list.append({
        "propertyID": "dct:subject",
        "valueNodeType": "IRI"
    })
    shap.state_list.append({
        "propertyID": "dct:date",
        "valueNodeType": "Literal"
    })
    assert shap == SHAPE_OBJECT
Example #2
0
def _mkshape(row_dict=None, config_dict=None):
    """Populates shape fields of dataclass TAPShape object from dict for one row.

    Args:
        row_dict: Dictionary of all columns headers (keys) and cell values (values)
            found in a given row, with no distinction between shape elements and
            statement template elements.
        config_dict: Dictionary of settings, built-in or as read from config file.

    Returns:
        Unpopulated instance of dctap.tapclasses.TAPShape, by default:
        - TAPShape(shapeID='', shapeLabel='', state_list=[], shape_warns={}, state_extras={})
        - Plus extra TAPShape fields as per config settings.
    """
    (main_shems, xtra_shems) = get_shems(shape_class=TAPShape,
                                         settings=config_dict)
    tapshape_obj = TAPShape()
    for key in row_dict:
        if key in main_shems:
            setattr(tapshape_obj, key, row_dict[key])
        elif key in xtra_shems:
            tapshape_obj.shape_extras[key] = row_dict[key]
    return tapshape_obj
def test_mkshapes_returns_tapshape_object_even_in_absence_of_propertyID(
        tmp_path):
    """Populates TAPShape object even in the absence of a propertyID."""
    os.chdir(tmp_path)  # precaution to avoid interference among pytests
    config_dict = get_config()
    assert config_dict["shape_elements"] == ["shapeID", "shapeLabel"]
    one_row = {
        "shapeID": ":a",
        "shapeLabel": "Book",
    }
    assert _mkshape(row_dict=one_row,
                    config_dict=config_dict) == TAPShape(shapeID=':a',
                                                         shapeLabel='Book',
                                                         state_list=[],
                                                         shape_warns={},
                                                         shape_extras={})
def test_mkshape_recognizes_only_shape_elements_so_configured(tmp_path):
    """Populates TAPShape object but ignores any statement template elements in row."""
    os.chdir(tmp_path)  # precaution to avoid interference among pytests
    config_dict = get_config()
    config_dict["extra_shape_elements"] = ["closed"]
    one_row = {
        "shapeID": ":a",
        "shapeLabel": "Book",
        "closed": False,
        "start": True,
    }
    assert _mkshape(one_row, config_dict=config_dict) == TAPShape(
        shapeID=':a',
        shapeLabel='Book',
        state_list=[],
        shape_warns={},
        shape_extras={"closed": False})
def test_mkshape_extra_shape_elements_that_are_empty_are_passed_through(
        tmp_path):
    """Empty shape elements are passed through, but not unasserted elements."""
    os.chdir(tmp_path)  # precaution to avoid interference among pytests
    config_dict = get_config()
    assert config_dict["shape_elements"] == ["shapeID", "shapeLabel"]
    config_dict["extra_shape_elements"] = ["closed", "start"]
    one_row = {
        "shapeID": ":a",
        "shapeLabel": "",
        "closed": "",
    }
    assert _mkshape(row_dict=one_row, config_dict=config_dict) == TAPShape(
        shapeID=':a',
        shapeLabel='',
        state_list=[],
        shape_warns={},
        shape_extras={"closed": ""})
def test_mkshape_reads_all_extra_shape_elements_so_configured(tmp_path):
    """Reads all elements configured as extra shape elements."""
    os.chdir(tmp_path)  # precaution to avoid interference among pytests
    config_dict = get_config()
    assert config_dict["shape_elements"] == ["shapeID", "shapeLabel"]
    config_dict["extra_shape_elements"] = ["closed", "start"]
    one_row = {
        "shapeID": ":a",
        "shapeLabel": "Book",
        "closed": False,
        "start": True,
    }
    assert _mkshape(row_dict=one_row,
                    config_dict=config_dict) == TAPShape(shapeID=':a',
                                                         shapeLabel='Book',
                                                         state_list=[],
                                                         shape_warns={},
                                                         shape_extras={
                                                             "closed": False,
                                                             "start": True
                                                         })
def test_shape_initialized_with_no_shapeid_field_should_pass_for_now():
    """Shape initialized with no shapeID will use default shapeID."""
    config_dict = dict()
    config_dict["default_shape_identifier"] = "default"
    shap = TAPShape()
    shap.state_list = []
    shap.state_list.append({
        "propertyID": "dct:creator",
        "valueNodeType": "IRI"
    })
    shap.state_list.append({
        "propertyID": "dct:subject",
        "valueNodeType": "IRI"
    })
    shap.state_list.append({
        "propertyID": "dct:date",
        "valueNodeType": "Literal"
    })
    shap._normalize_default_shapeID(config_dict)
    assert shap == TAPShape(
        shapeID="default",
        state_list=[
            {
                "propertyID": "dct:creator",
                "valueNodeType": "IRI"
            },
            {
                "propertyID": "dct:subject",
                "valueNodeType": "IRI"
            },
            {
                "propertyID": "dct:date",
                "valueNodeType": "Literal"
            },
        ],
    )
"""TAPShape object holds statements sharing a common shapeID."""

import pytest
from dctap.tapclasses import TAPShape

SHAPE_OBJECT = TAPShape(
    shapeID=":a",
    shapeLabel="Resource",
    state_list=[
        {
            "propertyID": "dct:creator",
            "valueNodeType": "IRI"
        },
        {
            "propertyID": "dct:subject",
            "valueNodeType": "IRI"
        },
        {
            "propertyID": "dct:date",
            "valueNodeType": "Literal"
        },
    ],
)


def test_shape_fields_are_individually_addressable():
    """Fields of TAPShape instance are individually addressable."""
    shap = SHAPE_OBJECT
    assert shap.shapeID == ":a"
    assert shap.shapeLabel == "Resource"
    assert shap.state_list[1] == {