def test_should_raise_exception_when_color_and_state_invalid(usb_find_mock): # given device_mock = mock.MagicMock() device_mock.write.side_effect = TypeError() usb_find_mock.return_value = device_mock light = ClewareTrafficLight() # when & then with pytest.raises(TrafficLightError): light.set_led("foo", 1)
def test_should_raise_exception_when_light_is_disconneted(usb_find_mock): # given device_mock = mock.MagicMock() device_mock.write.side_effect = usb.core.USBError( "[Errno 5] Input/Output Error") usb_find_mock.return_value = device_mock light = ClewareTrafficLight() # when & then with pytest.raises(TrafficLightError): light.set_led(Color.RED, State.ON)
def test_should_turn_on_led(usb_find_mock, test_input, expected): # given device_mock = mock.MagicMock() usb_find_mock.return_value = device_mock light = ClewareTrafficLight() # when light.set_led(test_input[0], test_input[1]) # then device_mock.write.assert_called_once_with(0x02, [0x00, expected[0], expected[1]], timeout=1000)
def main(args=sys.argv[1:]): parser = argparse.ArgumentParser( prog="ctl", description='Turns the led of the cleware traffic light on or off' ) parser.add_argument('-r', '--red', choices=["on", "off"], help="Controlls the red led") parser.add_argument('-y', '--yellow', choices=["on", "off"], help="Controlls the yellow led") parser.add_argument('-g', '--green', choices=["on", "off"], help="Controlls the green led") parser.add_argument('-a', '--all', choices=["on", "off"], help="Controlls all leds") parser.add_argument('-adr', '--address', type=int, help="Specifies which traffic light should be used") args = parser.parse_args(args) if not vars(args)['all']: all_given_colors = [(k, v) for k, v in vars(args).items() if v is not None and k is not "address"] if not all_given_colors: parser.print_help() return 1 else: all_state = vars(args)["all"] all_given_colors = [ ('red', all_state), ('yellow', all_state), ('green', all_state) ] for color_name, state_name in all_given_colors: color = Color[color_name.upper()] state = State[state_name.upper()] address = vars(args)["address"] try: ClewareTrafficLight(address).set_led(color, state) except MultipleTrafficLightsError as exc: print(exc, file=sys.stderr) ClewareTrafficLight.print_devices() return 1 except TrafficLightError as exc: print("Error -> {}".format(exc.message), file=sys.stderr) return 1 return 0
def test_should_initialize_light_by_address(usb_find_mock): # when light = ClewareTrafficLight(address=10) # then assert light.address == 10 assert light.device is not None
def test_should_fail_for_no_connected_light(usb_find_mock): # given usb_find_mock.return_value = None # when & then with pytest.raises(TrafficLightError, match="Cleware traffic light not found!"): ClewareTrafficLight()
def test_should_fail_for_multiple_lights_whitout_address(usb_find_mock): # given usb_find_mock.return_value = [mock.MagicMock(), mock.MagicMock()] # when & then with pytest.raises( MultipleTrafficLightsError, match="No address is given and there are multiple devices conected! " "Use 'print_devices' to see a list of connected devices."): ClewareTrafficLight()
def test_should_fail_for_wrong_state_in_getattr(usb_find_mock): # when & then with pytest.raises( TrafficLightError, match="Either the given color or state could not be parsed"): ClewareTrafficLight().red_foo()