Skip to content

Commit

Permalink
Merge pull request #235 from dkpro/bugfix/234-cas_to_comparable_text-…
Browse files Browse the repository at this point in the history
…fails-with-null-arrays

#234 - cas_to_comparable_text fails with null arrays
  • Loading branch information
reckart committed Oct 1, 2021
2 parents 5f5bb56 + 39fe252 commit ee4dbfc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
8 changes: 5 additions & 3 deletions cassis/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def _render_feature_value(feature_value: any, fs_id_to_anchor: Dict[int, str]) -
elif isinstance(feature_value, list):
return [_render_feature_value(e, fs_id_to_anchor) for e in feature_value]
elif _is_array_fs(feature_value):
return [_render_feature_value(e, fs_id_to_anchor) for e in feature_value.elements]
if feature_value.elements is not None:
return [_render_feature_value(e, fs_id_to_anchor) for e in feature_value.elements]
elif _is_primitive_value(feature_value):
return feature_value
else:
Expand Down Expand Up @@ -227,8 +228,9 @@ def _feature_structure_hash(type_: Type, fs: FeatureStructure):
feature_value = getattr(fs, feature.name)

if _is_array_fs(feature_value):
for element in feature_value.elements:
hash_ = _feature_value_hash(feature_value, hash_)
if feature_value.elements is not None:
for element in feature_value.elements:
hash_ = _feature_value_hash(feature_value, hash_)
else:
hash_ = _feature_value_hash(feature_value, hash_)
return hash_
Expand Down
50 changes: 49 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
from cassis.typesystem import TYPE_NAME_ANNOTATION
from pathlib import Path

from cassis.typesystem import TYPE_NAME_ANNOTATION, TYPE_NAME_FS_ARRAY
from tests.fixtures import *
from tests.test_files.test_cas_generators import MultiFeatureRandomCasGenerator, MultiTypeRandomCasGenerator

FIXTURES = [
(pytest.lazy_fixture("small_xmi"), pytest.lazy_fixture("small_typesystem_xml")),
(pytest.lazy_fixture("cas_with_inheritance_xmi"), pytest.lazy_fixture("typesystem_with_inheritance_xml")),
(pytest.lazy_fixture("cas_with_collections_xmi"), pytest.lazy_fixture("typesystem_with_collections_xml")),
(pytest.lazy_fixture("cas_with_references_xmi"), pytest.lazy_fixture("webanno_typesystem_xml")),
(pytest.lazy_fixture("cas_with_nonindexed_fs_xmi"), pytest.lazy_fixture("dkpro_typesystem_xml")),
(pytest.lazy_fixture("cas_with_empty_array_references_xmi"), pytest.lazy_fixture("dkpro_typesystem_xml")),
(pytest.lazy_fixture("cas_with_reserved_names_xmi"), pytest.lazy_fixture("typesystem_with_reserved_names_xml")),
(pytest.lazy_fixture("cas_with_two_sofas_xmi"), pytest.lazy_fixture("small_typesystem_xml")),
(pytest.lazy_fixture("cas_with_smileys_xmi"), pytest.lazy_fixture("dkpro_typesystem_xml")),
(
pytest.lazy_fixture("cas_with_floating_point_special_values_xmi"),
pytest.lazy_fixture("typesystem_with_floating_points_xml"),
),
(
pytest.lazy_fixture("cas_has_fs_with_no_namespace_xmi"),
pytest.lazy_fixture("typesystem_has_types_with_no_namespace_xml"),
),
(
pytest.lazy_fixture("cas_with_multiple_references_allowed_string_array_xmi"),
pytest.lazy_fixture("typesystem_with_multiple_references_allowed_xml"),
),
]


def test_cas_to_comparable_text_on_minimal_cas():
cas = Cas()
Expand Down Expand Up @@ -66,3 +92,25 @@ def test_cas_to_comparable_text_excluding_types():
)

assert cas_to_comparable_text(cas, exclude_types=[TypeB.name]) == expected


def test_cas_to_comparable_text_with_null_arrays():
typesystem = TypeSystem()
FSArray = typesystem.get_type(TYPE_NAME_FS_ARRAY)
ArrayHolder = typesystem.create_type("type.ArrayHolder", supertypeName=TYPE_NAME_ANNOTATION)
typesystem.create_feature(
name="array", domainType=ArrayHolder, rangeType=TYPE_NAME_FS_ARRAY, elementType=TYPE_NAME_ANNOTATION
)
cas = Cas(typesystem=typesystem)
cas.sofa_string = "ABCDE"
cas.add(ArrayHolder(begin=0, end=5, array=FSArray(elements=None)))
cas_to_comparable_text(cas)


@pytest.mark.filterwarnings("ignore:Trying to add feature")
@pytest.mark.parametrize("xmi, typesystem_xml", FIXTURES)
def test_cas_to_comparable_text_on_fixtures(xmi, typesystem_xml):
typesystem = load_typesystem(typesystem_xml)
cas = load_cas_from_xmi(xmi, typesystem=typesystem)

cas_to_comparable_text(cas)

0 comments on commit ee4dbfc

Please sign in to comment.