Skip to content
Snippets Groups Projects

Resolve L2SS-247 "2021 07 06 branched from master attribute testing"

Merged Resolve L2SS-247 "2021 07 06 branched from master attribute testing"
All threads resolved!
All threads resolved!
@@ -535,62 +535,101 @@ class TestAttributeTypes(base.TestCase):
def readback_test(self, dev, dtype, test_type):
'''Test device'''
with DeviceTestContext(dev, process=True) as proxy:
#initialise
proxy.initialise()
proxy.on()
try:
with DeviceTestContext(dev, process=True) as proxy:
#initialise
proxy.initialise()
proxy.on()
if test_type == "scalar":
if dtype is str or dtype is numpy.str_:
val = '1'
else:
val = dtype(1)
proxy.scalar_RW = val
result_R = proxy.scalar_R
result_RW = proxy.scalar_RW
elif test_type == "spectrum":
if dtype is str or dtype is numpy.str_:
val = ['1','1','1','1']
else:
val = numpy.full((4,), dtype=dtype, fill_value=1)
proxy.spectrum_RW = val
result_R = proxy.spectrum_R
result_RW = proxy.spectrum_RW
elif test_type == "image":
if dtype is str or dtype is numpy.str_:
val = [['1','1'],['1','1'],['1','1']]
else:
val = numpy.full((3,2), dtype=dtype, fill_value=1)
# info += " write value: {}".format(val)
proxy.image_RW = val
result_R = proxy.image_R
result_RW = proxy.image_RW
# info += " value stored in RW: {} value read back in R {}".format(result_RW, result_R)
if test_type == "scalar":
if dtype is str or dtype is numpy.str_:
val = '1'
else:
val = dtype(1)
proxy.scalar_RW = val
result_R = proxy.scalar_R
result_RW = proxy.scalar_RW
elif test_type == "spectrum":
if dtype is str or dtype is numpy.str_:
val = ['1','1','1','1']
else:
val = numpy.full((4,), dtype=dtype, fill_value=1)
proxy.spectrum_RW = val
result_R = proxy.spectrum_R
result_RW = proxy.spectrum_RW
elif test_type == "image":
if dtype is str or dtype is numpy.str_:
val = [['1','1'],['1','1'],['1','1']]
else:
val = numpy.full((3,2), dtype=dtype, fill_value=1)
proxy.image_RW = val
result_R = proxy.image_R
result_RW = proxy.image_RW
# cant use all() for 2d arrays so instead compare the dimensions and then flatten to 2d
try:
self.assertEqual(result_R.shape, (3,2), "image R array dimensions got mangled. Expected (2,3), got {}".format(result_R.shape))
self.assertEqual(result_RW.shape, (3, 2), "image RW array dimensions got mangled. Expected (2,3), got {}".format(result_RW.shape))
except:
pass
else:
self.assertEqual(1,2, "{} is not a valid test_type. please use either scalar, spectrum or image".format(test_type))
try:
comparison = result_RW == val
equal_arrays = comparison.all()
self.assertTrue(equal_arrays, "Value could not be handled by the atrribute_wrappers internal RW storer")
except:
pass
try:
comparison = result_R == result_RW
equal_arrays = comparison.all()
self.assertTrue(equal_arrays, "written value not present in clients R attribute")
except:
pass
print("✅ Test passed! Managed write and read back a value: {}".format(val))
# if the test isn't scalar/spectrum or image its wrong
self.assertEqual(1,2, "{} is not a valid test_type. please use either scalar, spectrum or image".format(test_type))
if test_type == "spectrum":
if dtype == str:
self.assertEqual(len(result_RW), 4, "image RW str image dimensions got mangled. Expected 4, got {}".format(len(result_RW)))
self.assertEqual(len(result_R), 4, "image RW str image dimensions got mangled. Expected 4, got {}".format(len(result_R)))
else:
comparison = result_RW == val
equal_arrays = comparison.all()
self.assertTrue(equal_arrays, "Value could not be handled by the atrribute_wrappers internal RW storer")
comparison = result_R == result_RW
equal_arrays = comparison.all()
self.assertTrue(equal_arrays, "written value not present in clients R attribute")
elif test_type == "scalar":
comparison = result_RW == val
self.assertTrue(comparison, "Value could not be handled by the atrribute_wrappers internal RW storer")
comparison = result_R == result_RW
self.assertTrue(comparison, "written value not present in clients R attribute")
elif test_type == "image":
if dtype != str:
# images cant bee compared. instead just do the shape
self.assertEqual(result_R.shape, (3,2), "image R array dimensions got mangled. Expected (2,3), got {}".format(result_R.shape))
self.assertEqual(result_RW.shape, (3, 2), "image RW array dimensions got mangled. Expected (2,3), got {}".format(result_RW.shape))
else:
# str doesnt have shape, jsut comapre the lenghts
self.assertEqual(len(result_RW), 3,"image RW str image dimensions got mangled. Expected 3, got {}".format(len(result_RW)))
self.assertEqual(len(result_RW[0]), 2, "image RW str image dimensions got mangled. Expected 2, got {}".format(len(result_RW)))
self.assertEqual(len(result_R), 3, "image RW str image dimensions got mangled. Expected 3, got {}".format(len(result_R)))
self.assertEqual(len(result_R[0]), 2, "image RW str image dimensions got mangled. Expected 2, got {}".format(len(result_R)))
print("✅ Test passed! Managed write and read back a value: {}".format(val))
except Exception as e:
info = "Test failure in {} {} readback test \n\tW: {} \n\tRW: {} \n\tR: {}".format(test_type, dtype, val, result_RW, result_R)
raise Exception(info) from e
# def attribute_test_trampoline(test_type, attr_type, attr_class, dimensions):
# try:
# if test_type is 'read':
# test_r(attr_type, attr_class, dimensions)
# elif test_type is 'write':
# test_rw(attr_type, attr_class, dimensions)
# {{}}
# ...
# catch as err:
# """Example using extra_info annotation"""
# err.extra_info = "Error for type: {} on class: {} with dimensions: "
# "{}".format(attr_type, attr_class, dimensions)
#
# # Notice how this is not re-raising err, simply raising it
# raise
# """Example using Exception chaining (preferred?)"""
# raise Exception("Error for type: {} on class: {} with dimensions: "
# "{}".format(attr_type, attr_class, dimensions)) from err
"""
List of different types to be used with attributes testing, using any other
Loading