def test_check_reservation_room_close(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = attr.evolve(reservation, status=ReservationStatuses.CLOSE)

    result = service.check_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.room_close_reservation
def test_check_reservation_wrong_house(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = attr.evolve(reservation, house_id=999)

    result = service.check_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_reservation
def test_create_guest_contact_no_guest_name(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = attr.evolve(reservation, guest_name='', guest_surname='')

    result = service.create_guest_contact(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_guest
def test_select_reservation_missed_pk(service: RegisterReservationInOdoo, context: Context, house):
    context.house = house
    context.pk = None

    result = service.select_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_reservation
def test_select_user_ok(service: RegisterReservationInOdoo, context: Context, house, user):
    service._members_repo = Mock(get_user_by_id=Mock(return_value=Some(user)))
    context.house = house

    result = service.select_user(context)
    assert is_successful(result)
    assert result.unwrap().user == user
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_user_fail(service: RegisterReservationInOdoo, context: Context, house):
    service._members_repo = Mock(get_user_by_id=Mock(return_value=Nothing))
    context.house = house

    result = service.select_user(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_user
def test_select_user_error(service: RegisterReservationInOdoo, context: Context, house):
    service._members_repo = Mock(get_user_by_id=Mock(side_effect=RuntimeError('ERR')))
    context.house = house

    result = service.select_user(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_user
    assert str(result.failure().exc) == 'ERR'
def test_create_opportuniry_error(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = reservation
    context.api = Mock(create_opportunity=Mock(side_effect=RuntimeError('ERR')))

    result = service.create_opportunity(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_create_guest_contact_ok(mocker, service: RegisterReservationInOdoo, context: Context, house, reservation):
    mocker.patch('contacts.usecases.CreateContact.execute', return_value=Success(Contact(id=123, name='XXX')))
    context.house = house
    context.reservation = reservation

    result = service.create_guest_contact(context)
    assert is_successful(result)
    assert result.unwrap().reservation.guest_contact_id == 123
    assert result.unwrap().reservation.guest_contact_ids == [123]
def test_create_guest_contact_fail(mocker, service: RegisterReservationInOdoo, context: Context, house, reservation):
    mocker.patch('contacts.usecases.CreateContact.execute', return_value=Failure(CaseError(error='ERR')))
    context.house = house
    context.reservation = reservation

    result = service.create_guest_contact(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert result.failure().error == 'ERR'
def test_create_guest_contact_has_guest_contact_id(
    mocker, service: RegisterReservationInOdoo, context: Context, house, reservation
):
    mocker.patch('contacts.usecases.CreateContact.execute', return_value=Failure(CaseError(error='ERR')))
    context.house = house
    context.reservation = attr.evolve(reservation, guest_contact_id=123)

    result = service.create_guest_contact(context)
    assert is_successful(result)
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_create_opportunity_fail(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = reservation
    context.api = Mock(create_opportunity=Mock(return_value=Nothing))

    result = service.create_opportunity(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert result.failure().error.startswith('Error create Opportunity for Reservation ID=')
def test_save_reservation_error(service: RegisterReservationInOdoo, context: Context, house, reservation):
    service._reservations_repo = Mock(save=Mock(side_effect=RuntimeError('ERR')))
    context.house = house
    context.reservation = reservation

    result = service.save_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
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_create_quotation_has_quotation_id(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = attr.evolve(reservation, quotation_id=123)
    context.api = Mock(create_quotation=Mock(side_effect=RuntimeError('ERR')))

    result = service.create_quotation(context)
    assert is_successful(result)

    context.api.create_quotation.assert_not_called()
def test_create_quotation_fail(service: RegisterReservationInOdoo, context: Context, house, reservation, user):
    context.house = house
    context.user = user
    context.reservation = attr.evolve(reservation, guest_contact_id=222, opportunity_id=333)
    context.api = Mock(create_quotation=Mock(return_value=Nothing))

    result = service.create_quotation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert result.failure().error.startswith('Error create Quotation for Reservation ID=')
def test_create_quotation_error(service: RegisterReservationInOdoo, context: Context, house, reservation, user):
    context.house = house
    context.user = user
    context.reservation = attr.evolve(reservation, guest_contact_id=222, opportunity_id=333)
    context.api = Mock(create_quotation=Mock(side_effect=RuntimeError('ERR')))

    result = service.create_quotation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_create_quotation_ok(
    service: RegisterReservationInOdoo, context: Context, house, reservation, room_type, user
):
    context.house = house
    context.user = user
    context.reservation = attr.evolve(reservation, guest_contact_id=222, opportunity_id=333)
    context.api = Mock(create_quotation=Mock(return_value=Some(123)))

    result = service.create_quotation(context)
    assert is_successful(result)
    assert result.unwrap().reservation.quotation_id == 123

    context.api.create_quotation.assert_called_once_with(
        {
            'partner_id': 222,
            'partner_invoice_id': 222,
            'partner_shipping_id': 222,
            'user_id': user.odoo_id,
            'currency_id': house.currency.odoo_id,
            'note': '',
            'company_id': house.odoo_id,
            'team_id': CrmTeams.SALES.value,
            'opportunity_id': 333,
            'report_grids': True,
            'origin': reservation.id,
            'client_order_ref': '',
        },
        [
            {
                'price_unit': '1120',
                'order_partner_id': 222,
                'discount': 0.,
                'product_id': room_type.id,
                'product_uom_qty': 1,
                'item_date': reservation.checkin.strftime(DATE_FORMAT),
            },
            {
                'price_unit': '1120',
                'order_partner_id': 222,
                'discount': 0.,
                'product_id': room_type.id,
                'product_uom_qty': 1,
                'item_date': (reservation.checkin + datetime.timedelta(days=1)).strftime(DATE_FORMAT),
            },
            {
                'price_unit': '1120',
                'order_partner_id': 222,
                'discount': 0.,
                'product_id': room_type.id,
                'product_uom_qty': 1,
                'item_date': (reservation.checkin + datetime.timedelta(days=2)).strftime(DATE_FORMAT),
            },
        ]
    )
def test_create_opportunity_ok(service: RegisterReservationInOdoo, context: Context, house, reservation):
    context.house = house
    context.reservation = attr.evolve(reservation, guest_contact_id=222)
    context.api = Mock(create_opportunity=Mock(return_value=Some(123)))

    result = service.create_opportunity(context)
    assert is_successful(result)
    assert result.unwrap().reservation.opportunity_id == 123

    context.api.create_opportunity.assert_called_once_with(
        {
            'company_id': house.odoo_id,
            'name': reservation.get_opportunity_name(),
            'partner_id': 222,
            'planned_revenue': '3360',
            'stage_id': CrmStages.NEGOTIATION.value,
            'team_id': CrmTeams.SALES.value,
            'tag_ids': [CrmTags.DIRECT.value],
        },
        context={'default_type': 'opportunity'},
    )