Skip to content

Commit

Permalink
using KeyError instead of ValueError; unit test added
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-beer committed Jul 24, 2024
1 parent 0b54c03 commit a2c657a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions resqpy/property/attribute_property_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __init__(self,
have their indexable element included in their key
multiple_handling (str, default 'warn'): either 'ignore', 'warn' ,or 'exception'; if 'warn' or 'ignore', and properties
exist that generate the same key, then only the first is visible in the attribute property set (and a warning is given
for each of the others in the case of 'warn'); if 'exception', a ValueError is raised if there are any duplicate keys
for each of the others in the case of 'warn'); if 'exception', a KeyError is raised if there are any duplicate keys
note:
at present, if the collection is being initialised from a property set, the support argument must also be specified;
Expand Down Expand Up @@ -270,7 +270,7 @@ def _make_attributes(self):
continue
if self.multiple_handling == 'ignore':
continue
raise ValueError(f'duplicate key in attribute property set: {key}')
raise KeyError(f'duplicate key in attribute property set: {key}')
aps_property = ApsProperty(self, part)
setattr(self, key, aps_property)

Expand Down
51 changes: 51 additions & 0 deletions tests/unit_tests/property/test_attribute_property_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,57 @@ def test_load_attribute_property_collection_pk(example_model_with_prop_ts_rels):
assert np.all(v == aps.saturation_water_t2.array_ref)


def test_load_attribute_property_collection_pk_duplicates(example_model_with_prop_ts_rels, capfd, caplog):
# Arrange
model = example_model_with_prop_ts_rels
grid = model.grid()
pc = grid.extract_property_collection()

# Act
aps = rqp.AttributePropertySet(support = grid)
facies_col = np.max(aps.facies.values, axis = 0)
pc.add_similar_to_imported_list(similar_uuid = aps.facies.uuid,
cached_array = facies_col,
indexable_element = 'columns')
pc.write_hdf5_for_imported_list()
pc.create_xml_for_imported_list_and_add_parts_to_model()

# check indexable used when needed in key
aps = rqp.AttributePropertySet(support = grid, indexable = 'cells')
assert aps is not None
assert len(aps) == 13
assert 'facies' in aps.keys()
assert 'facies_columns' in aps.keys()

# check duplicate ignored
aps = rqp.AttributePropertySet(support = grid, indexable = None, multiple_handling = 'ignore')
assert aps is not None
assert len(aps) == 13
key_list = list(aps.keys())
assert len(key_list) == 13 # all parts still exist in the PropertyCollection
assert len(
set(key_list)) == 12 # but two will have the same aps key (only the first is visible as AttributeProperty)
assert 'facies' in aps.keys()
assert 'facies_columns' not in aps.keys()

# check that multiple handling 'exception' raises key error
with pytest.raises(KeyError) as e_info:
aps = rqp.AttributePropertySet(support = grid, multiple_handling = 'exception')

# check that multiple handling 'warn' generates log warning
aps = rqp.AttributePropertySet(support = grid, multiple_handling = 'warn')
assert len(caplog.records) > 0
assert caplog.records[-1].getMessage().endswith(
"duplicate key in AttributePropertySet; only first instance included: facies")
assert aps is not None
assert len(aps) == 13
key_list = list(aps.keys())
assert len(key_list) == 13
assert len(set(key_list)) == 12
assert 'facies' in aps.keys()
assert 'facies_columns' not in aps.keys()


def test_load_attribute_property_collection_title(example_model_with_prop_ts_rels):
# Arrange
model = example_model_with_prop_ts_rels
Expand Down

0 comments on commit a2c657a

Please sign in to comment.