Skip to content

Commit

Permalink
hal_hw_interface: Fix unit tests
Browse files Browse the repository at this point in the history
Tests became stale over time
  • Loading branch information
zultron committed Sep 20, 2021
1 parent 65d8642 commit 8cbb280
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 227 deletions.
17 changes: 9 additions & 8 deletions hal_hw_interface/src/hal_hw_interface/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ def mock_objs():


@pytest.fixture()
def mock_comp_obj():
def mock_comp_obj(request):
# Mock hal.component and returned object
# - Settable and readable pins
pin_value_map = dict(__default=0xDEADBEEF)
request.instance.pin_values = pin_values = dict(__default=0xDEADBEEF)

def get_pin(key):
if key in pin_value_map:
value = pin_value_map[key]
if key in pin_values:
value = pin_values[key]
print("Returning pin %s value=%s" % (key, value))
else:
value = pin_value_map['__default']
value = pin_values['__default']
print("Returning pin %s DEFAULT value=0x%x" % (key, value))
return value

def set_pin(key, value):
print("Setting pin %s value=%s" % (key, value))
pin_value_map[key] = value
pin_values[key] = value

mock_objs_dict['comp_name'] = 'test_comp'
comp_getprefix = MagicMock(side_effect=lambda: mock_objs_dict['comp_name'])
Expand Down Expand Up @@ -140,10 +140,10 @@ def log_side_effect(msg_fmt, *args):


@pytest.fixture()
def mock_redis_client_obj():
def mock_redis_client_obj(request):
# Mock redis_store.ConfigClient method and returned object
# - Settable and readable pins
key_value_map = dict(__default=0)
request.instance.key_value_map = key_value_map = dict(__default=0)

def get_key(key):
value = key_value_map.get(key, key_value_map['__default'])
Expand All @@ -158,6 +158,7 @@ def set_key(key, value):
mock_client_obj.configure_mock(
name='mock_redis_client_obj',
set_key=set_key, # Won't increment mock_calls
on_update_received=list(),
**{'get_param.side_effect': get_key, 'set_param.side_effect': set_key}
)

Expand Down
182 changes: 92 additions & 90 deletions hal_hw_interface/src/hal_hw_interface/tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,95 @@
keys2 = dict(pin1=False, pin2=1.88e42, pin4=0)


def test_mock_comp_obj_fixture(mock_comp_obj, mock_objs):
# Test hal.component returns mock_comp_obj
assert mock_objs['hal_comp']() is mock_comp_obj

# Set each pin (with out-of-band method) and check
for name, value in keys1.items():
mock_comp_obj.set_pin(name, value)
assert mock_comp_obj[name] == value

# Recheck
for name, value in keys1.items():
assert mock_comp_obj[name] == value

# Set each pin and check
for name, value in keys2.items():
mock_comp_obj[name] = value
assert mock_comp_obj[name] == value

# Recheck everything
pins = keys1.copy()
pins.update(keys2)
for name, value in pins.items():
assert mock_comp_obj[name] == value

# Default case
assert mock_comp_obj['bogus'] == 0xDEADBEEF


def test_mock_rospy_fixture(mock_rospy, mock_objs):
# Test mock rospy.get_param()
gp = mock_objs['rospy_get_param']
gp.set_key('foo', 1)
gp.set_key('bar', 2)
assert gp('foo') == 1
assert gp('bar') == 2
gp.set_key('baz', 3)
assert gp('foo') == 1
assert gp('bar') == 2
assert gp('baz') == 3

# Test rospy.Rate() returns expected object
assert mock_objs['rospy_Rate']() is mock_objs['rospy_Rate_obj']

# Test rospy.is_shutdown() returns True values, then False
found_false = False
for i in range(10):
val = mock_objs['rospy_is_shutdown']()
print("iter {} val {}".format(i, val))
if val is False:
found_false = True
if val is True:
break
else:
raise Exception("is_shutdown never returned True")
if not found_false:
raise Exception("is_shutdown never returned False")

# Test returned objects
for name in ('Subscriber', 'Publisher', 'Service'):
method = mock_objs['rospy_{}'.format(name)]
obj = mock_objs['rospy_{}_obj'.format(name)]
assert method() == obj


def test_mock_redis_client_obj(mock_redis_client_obj, mock_objs):
# Test redis_store.ConfigClient() returns object
assert mock_objs['redis_store']() is mock_redis_client_obj

# Set each param (with out-of-band method) and check
for name, value in keys1.items():
mock_redis_client_obj.set_key(name, value)
assert mock_redis_client_obj.get_param(name) == value

# Recheck
for name, value in keys1.items():
assert mock_redis_client_obj.get_param(name) == value

# Set each param and check
for name, value in keys2.items():
mock_redis_client_obj.set_param(name, value)
assert mock_redis_client_obj.get_param(name) == value

# Recheck everything
params = keys1.copy()
params.update(keys2)
for name, value in params.items():
assert mock_redis_client_obj.get_param(name) == value

# Default case
assert mock_redis_client_obj.get_param('bogus') == 0
class TestFixtures:

def test_mock_comp_obj_fixture(self, mock_comp_obj, mock_objs):
# Test hal.component returns mock_comp_obj
assert mock_objs['hal_comp']() is mock_comp_obj

# Set each pin (with out-of-band method) and check
for name, value in keys1.items():
mock_comp_obj.set_pin(name, value)
assert mock_comp_obj[name] == value

# Recheck
for name, value in keys1.items():
assert mock_comp_obj[name] == value

# Set each pin and check
for name, value in keys2.items():
mock_comp_obj[name] = value
assert mock_comp_obj[name] == value

# Recheck everything
pins = keys1.copy()
pins.update(keys2)
for name, value in pins.items():
assert mock_comp_obj[name] == value

# Default case
assert mock_comp_obj['bogus'] == 0xDEADBEEF


def test_mock_rospy_fixture(self, mock_rospy, mock_objs):
# Test mock rospy.get_param()
gp = mock_objs['rospy_get_param']
gp.set_key('foo', 1)
gp.set_key('bar', 2)
assert gp('foo') == 1
assert gp('bar') == 2
gp.set_key('baz', 3)
assert gp('foo') == 1
assert gp('bar') == 2
assert gp('baz') == 3

# Test rospy.Rate() returns expected object
assert mock_objs['rospy_Rate']() is mock_objs['rospy_Rate_obj']

# Test rospy.is_shutdown() returns True values, then False
found_false = False
for i in range(10):
val = mock_objs['rospy_is_shutdown']()
print("iter {} val {}".format(i, val))
if val is False:
found_false = True
if val is True:
break
else:
raise Exception("is_shutdown never returned True")
if not found_false:
raise Exception("is_shutdown never returned False")

# Test returned objects
for name in ('Subscriber', 'Publisher', 'Service'):
method = mock_objs['rospy_{}'.format(name)]
obj = mock_objs['rospy_{}_obj'.format(name)]
assert method() == obj


def test_mock_redis_client_obj(self, mock_redis_client_obj, mock_objs):
# Test redis_store.ConfigClient() returns object
assert mock_objs['redis_store']() is mock_redis_client_obj

# Set each param (with out-of-band method) and check
for name, value in keys1.items():
mock_redis_client_obj.set_key(name, value)
assert mock_redis_client_obj.get_param(name) == value

# Recheck
for name, value in keys1.items():
assert mock_redis_client_obj.get_param(name) == value

# Set each param and check
for name, value in keys2.items():
mock_redis_client_obj.set_param(name, value)
assert mock_redis_client_obj.get_param(name) == value

# Recheck everything
params = keys1.copy()
params.update(keys2)
for name, value in params.items():
assert mock_redis_client_obj.get_param(name) == value

# Default case
assert mock_redis_client_obj.get_param('bogus') == 0
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ def test_hal_io_comp_update(self, obj, pin_params, mock_comp_obj):
get_param, test_pins = pin_params
obj.setup_component()

# Put pins in dict and set values
# Set pin values
for pin in obj.pins:
mock_comp_obj.set_pin(pin.name, 1)
pin.last_value = 0

# Run update() and check that pins changed
print("----------- Running obj.update()")
obj.update()
for pin in obj.pins:
print("- pin %s" % pin)
assert pin.last_value == 1
pin.pub.publish.assert_called_with(1)
print(f"- pin {pin}")
pin.pub.publish.assert_called()
Loading

0 comments on commit 8cbb280

Please sign in to comment.