Skip to content
Snippets Groups Projects
Commit 2032bd27 authored by Torben Miny's avatar Torben Miny
Browse files

Merge branch 'enhancement/provider_get' into 'master'

model.provider: Add AbstractObjectStore.get() method

See merge request acplt/pyaas!12
parents 964399d1 1e5a0a16
Branches
Tags
1 merge request!12model.provider: Add AbstractObjectStore.get() method
Pipeline #248917 passed
......@@ -40,6 +40,19 @@ class AbstractObjectProvider(metaclass=abc.ABCMeta):
"""
pass
def get(self, identifier: Identifier, default: Optional[Identifiable] = None) -> Optional[Identifiable]:
"""
Find an object in this set by its identification, with fallback parameter
:param default: An object to be returned, if no object with the given identification is found
:return: The Identifiable object with the given identification in the provider. Otherwise the `default` object
or None, if none is given.
"""
try:
return self.get_identifiable(identifier)
except KeyError:
return default
_IT = TypeVar('_IT', bound=Identifiable)
......
......@@ -39,10 +39,13 @@ class ProvidersTest(unittest.TestCase):
self.assertEqual(2, len(object_store))
self.assertIs(self.aas1,
object_store.get_identifiable(model.Identifier("urn:x-test:aas1", model.IdentifierType.IRI)))
self.assertIs(self.aas1,
object_store.get(model.Identifier("urn:x-test:aas1", model.IdentifierType.IRI)))
object_store.discard(self.aas1)
object_store.discard(self.aas1)
with self.assertRaises(KeyError) as cm:
object_store.get_identifiable(model.Identifier("urn:x-test:aas1", model.IdentifierType.IRI))
self.assertIsNone(object_store.get(model.Identifier("urn:x-test:aas1", model.IdentifierType.IRI)))
self.assertEqual("Identifier(IRI=urn:x-test:aas1)", str(cm.exception))
self.assertIs(self.aas2, object_store.pop())
self.assertEqual(0, len(object_store))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment