def test_select_reservation_ok(service: RegisterReservationInOdoo, 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().reservation == reservation
def test_select_reservation_fail(service: RegisterReservationInOdoo, 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_select_reservation_error(service: RegisterReservationInOdoo, 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_save_reservation_ok(service: RegisterReservationInOdoo, context: Context, house, reservation):
    _reservation = attr.evolve(reservation, guest_contact_id=123)
    service._reservations_repo = Mock(save=Mock(return_value=(Some(_reservation), True)))
    context.house = house
    context.reservation = reservation

    result = service.save_reservation(context)
    assert is_successful(result)
    assert result.unwrap().reservation == _reservation
def test_save_reservation_fail(service: RegisterReservationInOdoo, context: Context, house, reservation):
    service._reservations_repo = Mock(save=Mock(return_value=(Nothing, False)))
    context.house = house
    context.reservation = reservation

    result = service.save_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.save
    assert result.failure().error.startswith('Error save Reservation')
def test_success(mocker, service: RegisterReservationInOdoo, house, reservation, user):
    _reservation = attr.evolve(reservation, guest_contact_id=123)
    api = Mock(create_opportunity=Mock(return_value=Some(333)), create_quotation=Mock(return_value=Some(444)))
    contact_call = mocker.patch(
        'contacts.usecases.CreateContact.execute', return_value=Success(Contact(id=123, name='XXX'))
    )
    service._houses_repo = Mock(get=Mock(return_value=Some(house)))
    service._members_repo = Mock(get_user_by_id=Mock(return_value=Some(user)))
    service._reservations_repo = Mock(
        get=Mock(return_value=Some(reservation)), save=Mock(return_value=(Some(_reservation), True))
    )
    service.get_rpc_api = Mock(return_value=api)

    result = service.execute(house.id, reservation.id, user_id=user.id)
    assert is_successful(result)

    contact_call.assert_called_once()
    api.create_opportunity.assert_called_once()
    api.create_quotation.assert_called_once()