Skip to content

Commit

Permalink
Changes to bundle_utils/etc related to SMaHT ingestion work.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 9, 2023
1 parent 33b64ae commit 701384f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions dcicutils/validation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,14 @@ def identifying_properties(self, schema: Optional[JsonSchema] = None, schema_nam
return identifying_properties

@classmethod # This operation doesn't actually use the schemas so is safe as a class method
def identifying_value(cls, data_item: Dict[str, AnyJsonData], identifying_properties) -> AnyJsonData:
def identifying_value(cls, data_item: Dict[str, AnyJsonData],
identifying_properties, raise_exception: bool = False) -> AnyJsonData:
if not identifying_properties and raise_exception:
raise ValueError("No identifying properties were specified.")
for identifying_property in identifying_properties or []:
if identifying_property in data_item:
return data_item[identifying_property]
if False:
if not identifying_properties:
raise ValueError("No identifying properties were specified.")
for identifying_property in identifying_properties:
if identifying_property in data_item:
return data_item[identifying_property]
if raise_exception:
raise ValueError(f'{there_are(identifying_properties, just_are=True)}'
f' no {maybe_pluralize(identifying_properties, "identifying property")}'
f' {disjoined_list([repr(x) for x in identifying_properties])}'
Expand Down
8 changes: 4 additions & 4 deletions test/test_validation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ def test_schema_manager_with_schemas(schema_id):
def test_schema_manager_identifying_value():

with pytest.raises(ValueError) as exc:
assert SchemaManager.identifying_value({'any': 'thing'}, [])
assert SchemaManager.identifying_value({'any': 'thing'}, [], raise_exception=True)
assert str(exc.value) == "No identifying properties were specified."

person_named_fred = {'age': 33, 'name': 'Fred', 'favorite-color': 'yellow'}
assert SchemaManager.identifying_value(person_named_fred, ['uuid', 'name']) == 'Fred'
assert SchemaManager.identifying_value(person_named_fred, ['uuid', 'name'], raise_exception=True) == 'Fred'

person_nicknamed_fred = {'age': 33, 'nickname': 'Fred', 'favorite-color': 'yellow'}
with pytest.raises(ValueError) as exc:
SchemaManager.identifying_value(person_nicknamed_fred, ['uuid', 'name'])
SchemaManager.identifying_value(person_nicknamed_fred, ['uuid', 'name'], raise_exception=True)
assert str(exc.value) == ("""There are no identifying properties 'uuid' or 'name'"""
""" in {"age": 33, "nickname": "Fred", "favorite-color": "yellow"}.""")

with pytest.raises(ValueError) as exc:
SchemaManager.identifying_value(person_nicknamed_fred, ['name'])
SchemaManager.identifying_value(person_nicknamed_fred, ['name'], raise_exception=True)
assert str(exc.value) == ("""There is no identifying property 'name'"""
""" in {"age": 33, "nickname": "Fred", "favorite-color": "yellow"}.""")

Expand Down

0 comments on commit 701384f

Please sign in to comment.