Skip to content

Commit 69a3ce1

Browse files
committed
model: reword error messages for assoc. sets
I want to print out precise messages, so I needed to replace the standard dict with my very own implementation called Declarations because otherwise I would have to add try..except everywhere. The class Declarations just overwrites the method __getitem__ (the operator square braces []) where I catch the KeyError exception and raise my own KeyError exception with an appropriate message.
1 parent e592063 commit 69a3ce1

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pyodata/v2/model.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,18 @@ def add_complex_type(self, ctype):
560560
collection_type_name = 'Collection({})'.format(ctype.name)
561561
self.complex_types[collection_type_name] = Collection(ctype.name, ctype)
562562

563+
class Declarations(dict):
564+
565+
def __getitem__(self, key):
566+
try:
567+
return super(Schema.Declarations, self).__getitem__(key)
568+
except KeyError:
569+
raise KeyError('There is no Schema Namespace {}'.format(key))
570+
563571
def __init__(self):
564572
super(Schema, self).__init__()
565573

566-
self._decls = dict()
574+
self._decls = Schema.Declarations()
567575

568576
def __str__(self):
569577
return "{0}({1})".format(self.__class__.__name__, ','.join(self.namespaces))
@@ -725,13 +733,14 @@ def association_set_by_association(self, association_name, namespace=None):
725733
for association_set in list(self._decls[namespace].association_sets.values()):
726734
if association_set.association_type.name == association_name:
727735
return association_set
728-
raise KeyError('Association set with association type {} does not exist in namespace {}'.format(
736+
raise KeyError('Association Set for Association {} does not exist in Schema Namespace {}'.format(
729737
association_name, namespace))
730738
for decl in list(self._decls.values()):
731739
for association_set in list(decl.association_sets.values()):
732740
if association_set.association_type.name == association_name:
733741
return association_set
734-
raise KeyError('Association set with association type {} does not exist'.format(association_name))
742+
raise KeyError('Association Set for Association {} does not exist in any Schema Namespace'.format(
743+
association_name))
735744

736745
def association_set(self, set_name, namespace=None):
737746
if namespace is not None:

tests/test_model_v2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ def test_edmx_associations(schema):
204204
association_set = schema.association_set_by_association('CustomerOrders')
205205
assert str(association_set) == 'AssociationSet(CustomerOrder_AssocSet)'
206206

207+
# error handling: without namespace
208+
with pytest.raises(KeyError) as typ_ex_info:
209+
assert schema.association_set_by_association('FooBar')
210+
assert typ_ex_info.value.args[0] == 'Association Set for Association FooBar does not exist in any Schema Namespace'
211+
212+
# error handling: with unknown namespace
213+
with pytest.raises(KeyError) as typ_ex_info:
214+
assert schema.association_set_by_association('FooBar', namespace='BLAH')
215+
assert typ_ex_info.value.args[0] == 'There is no Schema Namespace BLAH'
216+
217+
# error handling: with namespace
218+
with pytest.raises(KeyError) as typ_ex_info:
219+
assert schema.association_set_by_association('FooBar', namespace='EXAMPLE_SRV')
220+
assert typ_ex_info.value.args[0] == 'Association Set for Association FooBar does not exist in Schema Namespace EXAMPLE_SRV'
221+
207222

208223
def test_edmx_navigation_properties(schema):
209224
"""Test parsing of navigation properties"""

0 commit comments

Comments
 (0)