def append(self, rescue: Rescue, overwrite: bool = False) -> None: """ Accept a Rescue object and attach it to the board Args: rescue (Rescue): Rescue object to attach overwrite(bool) : **overwrite** existing keys, if necessary. requires `rescue` to have a `board_index` set, otherwise it does nothing. Returns: None Raises: IndexNotFreeError: Attempt to write a rescue to a key that is already set. """ # if the rescue already has a board index defined if rescue.board_index is not None: # check if the board index is not in use, or the overwrite flag is set if overwrite or rescue.board_index not in self.rescues: # write the key,value self.rescues[rescue.board_index] = rescue else: raise IndexNotFreeError( f"Index {rescue.board_index} is in use. If you want to overwrite this you must" f"set the `overwrite` flag.") # we need to give it one else: rescue.board_index = self.next_free_index() self.rescues[rescue.board_index] = rescue
def test_rescue_board_index(rescue_plain_fx): """ Verifies board index is NOT set without a rescue attached """ # Do not set board index test_rescue = Rescue(uuid4(), 'Test_Client', 'AliothJr', 'TestClient') assert test_rescue.board_index is None # Add rescue fixture test_rescue = rescue_plain_fx assert test_rescue.board_index == 42 # Add a negative int for board index, ensuring ValueError is thrown with pytest.raises(ValueError): test_rescue.board_index = -99 # Add incorrect type, ensuring TypeError is thrown with pytest.raises(TypeError): test_rescue.board_index = 'Index!'