def test_success(service: UpdateRoomClose, context: Context, house, room,
                 reservation):
    _reservation = attr.evolve(reservation, is_verified=True)
    service._houses_repo = Mock(get=Mock(return_value=Some(house)))
    service._rooms_repo = Mock(get=Mock(return_value=Some(room)))
    service._reservations_repo = Mock(
        accept=Mock(return_value=Some(_reservation)),
        get=Mock(return_value=Some(reservation)),
        is_room_busy=Mock(return_value=False),
        save=Mock(
            return_value=(Some(attr.evolve(reservation, is_verified=False)),
                          True)),
    )

    result = service.execute(
        '111-222-1',
        house.id,
        room.id,
        context.start_date,
        context.end_date,
        RoomCloseReasons.MAINTENANCE,
        context.user,
    )
    assert is_successful(result), result.failure()

    ctx = result.unwrap()
    assert isinstance(ctx, ReservationUpdateContext)
    assert ctx.reservation == _reservation
    assert ctx.start_date == datetime.date.today()
    assert ctx.end_date == datetime.date.today() + datetime.timedelta(days=2)
def test_select_reservation_ok(service: UpdateRoomClose, context: Context,
                               house, reservation):
    service._reservations_repo = Mock(get=Mock(return_value=Some(reservation)))
    context.house = house

    result = service.select_reservation(context)
    assert is_successful(result)
    assert result.unwrap().source == reservation
def test_select_reservation_fail(service: UpdateRoomClose, context: Context,
                                 house):
    service._reservations_repo = Mock(get=Mock(return_value=Nothing))
    context.house = house

    result = service.select_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_reservation
def test_check_room_is_free_ok(service: UpdateRoomClose, context: Context,
                               house, room, reservation):
    service._reservations_repo = Mock(is_room_busy=Mock(return_value=False))
    context.house = house
    context.room = room
    context.source = reservation

    result = service.check_room_is_free(context)
    assert is_successful(result), result.failure()
def test_select_reservation_error(service: UpdateRoomClose, context: Context,
                                  house):
    service._reservations_repo = Mock(get=Mock(
        side_effect=RuntimeError('ERR')))
    context.house = house

    result = service.select_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_accept_reservation_fail(service: UpdateRoomClose, context: Context,
                                 house, room, reservation):
    service._reservations_repo = Mock(accept=Mock(return_value=Nothing))
    context.house = house
    context.room = room
    context.reservation = reservation

    result = service.accept_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.save
    assert result.failure().error.startswith("Error accept Reservation ID=")
def test_accept_reservation_ok(service: UpdateRoomClose, context: Context,
                               house, room, reservation):
    reservation = attr.evolve(reservation, is_verified=True)
    service._reservations_repo = Mock(accept=Mock(
        return_value=Some(reservation)))
    context.house = house
    context.room = room
    context.reservation = attr.evolve(reservation, is_verified=False)

    result = service.accept_reservation(context)
    assert is_successful(result)
    assert result.unwrap().reservation == reservation
def test_accept_reservation_error(service: UpdateRoomClose, context: Context,
                                  house, room, reservation):
    service._reservations_repo = Mock(accept=Mock(
        side_effect=RuntimeError("ERR")))
    context.house = house
    context.room = room
    context.reservation = reservation

    result = service.accept_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == "ERR"
def test_check_room_is_free_error(service: UpdateRoomClose, context: Context,
                                  house, room, reservation):
    service._reservations_repo = Mock(is_room_busy=Mock(
        side_effect=RuntimeError("ERR")))
    context.house = house
    context.room = room
    context.source = reservation

    result = service.check_room_is_free(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == "ERR"
def test_save_reservation_ok(service: UpdateRoomClose, context: Context, house,
                             room, reservation):
    _reservation = attr.evolve(reservation,
                               close_reason=RoomCloseReasons.TMP_CLOSE)
    service._reservations_repo = Mock(save=Mock(
        return_value=(Some(_reservation), True)))
    context.house = house
    context.room = room
    context.reservation = reservation

    result = service.save_reservation(context)
    assert is_successful(result)
    assert result.unwrap().reservation == _reservation