Skip to content

Commit

Permalink
test/attr_tests: fix tests with setattr
Browse files Browse the repository at this point in the history
Since Python objects will dynamically add an attribute if one does not
exist when calling setattr(), we need to check that the attribute
actually exists before setting it. Otherwise, tests will pass even if
the attribute is not defined in the object being tested.

Signed-off-by: David Lechner <[email protected]>
  • Loading branch information
dlech committed Oct 5, 2023
1 parent 301a5f5 commit fa0404f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions test/attr_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def attribute_single_value_boolean(uri, classname, attr, value):
Value to write and read back from attribute
"""
bi = eval(classname + "(uri='" + uri + "')")

if not hasattr(bi, attr):
raise AttributeError(f"no attribute named: {attr}")

setattr(bi, attr, value)
rval = getattr(bi, attr)
del bi
Expand Down Expand Up @@ -258,6 +262,10 @@ def attribute_write_only_str(uri, classname, attr, value):
Value to write into attr property
"""
sdr = eval(classname + "(uri='" + uri + "')")

if not hasattr(sdr, attr):
raise AttributeError(f"no attribute named: {attr}")

try:
setattr(sdr, attr, value)
del sdr
Expand All @@ -284,6 +292,14 @@ def attribute_write_only_str_with_depends(uri, classname, attr, value, depends):
are properties and values are values to be written
"""
sdr = eval(classname + "(uri='" + uri + "')")

for p in depends:
if not hasattr(sdr, p):
raise AttributeError(f"no attribute named: {p}")

if not hasattr(sdr, attr):
raise AttributeError(f"no attribute named: {attr}")

for p in depends:
setattr(sdr, p, depends[p])
try:
Expand Down

0 comments on commit fa0404f

Please sign in to comment.