Exemple #1
0
def test_find_found_in_id_map_found_in_DB(monkeypatch):
    # Test Data
    unexpected = Timeslot(11, 52, 'date2', 'block2', 'id2', 'id2')
    expected = Timeslot(1, 5, 'date', 'block', 'id', 'id')

    # Mock
    def id_find(_, __):
        return expected

    def tdg_find(_):
        return [[
            unexpected.getId(),
            unexpected.getStartTime(),
            unexpected.getEndTime(),
            unexpected.getDate(),
            unexpected.getBlock(),
            unexpected.getId()
        ]]

    monkeypatch.setattr(IdMap, 'find', id_find)
    monkeypatch.setattr(TimeslotTDG, 'find', tdg_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val.getStartTime() is expected.getStartTime())
    assert (val.getEndTime() is expected.getEndTime())
    assert (val.getDate() is expected.getDate())
    assert (val.getBlock() is expected.getBlock())
    assert (val.getId() is expected.getId())
Exemple #2
0
def test_find_not_found_in_id_map_found_in_DB(monkeypatch):
    # Test Data
    expected = Timeslot(1, 5, 'date', 'block', 'id', 'id')

    # Mock
    def no_find(_, __):
        return

    def yes_find(_):
        return [[
            expected.getId(),
            expected.getStartTime(),
            expected.getEndTime(),
            expected.getDate(),
            expected.getBlock(),
            expected.getId(),
            expected.getId()
        ]]

    monkeypatch.setattr(IdMap, 'find', no_find)
    monkeypatch.setattr(TimeslotTDG, 'find', yes_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val.getStartTime() is expected.getStartTime())
    assert (val.getEndTime() is expected.getEndTime())
    assert (val.getDate() is expected.getDate())
    assert (val.getBlock() is expected.getBlock())
    assert (val.getId() is expected.getId())
Exemple #3
0
def find(reservationId):
    result = ReservationTDG.find(reservationId)
    if not result:
        return None
    else:
        # must make a reference to timeslottable and create a timeslot object
        room = RoomMapper.find(result[0][1])
        holder = UserMapper.find(result[0][3])
        timeslot = TimeslotMapper.find(result[0][4])
        equipment = EquipmentMapper.find(result[0][5])
        return Reservation(room, holder, timeslot, result[0][2], equipment, result[0][0])
Exemple #4
0
def findAll():
    result = ReservationTDG.findAll()
    allReservations = []
    if not result:
        return []
    else:
        for index, r in enumerate(result):
            room = RoomMapper.find(result[index][1])
            holder = UserMapper.find(result[index][3])
            timeslot = TimeslotMapper.find(result[index][4])
            equipment = EquipmentMapper.find(result[index][5])
            reservation = Reservation(room, holder, timeslot, result[index][2], equipment, result[index][0])
            allReservations.append(reservation)
        return allReservations
Exemple #5
0
def test_find_not_found_in_id_map_not_found_in_DB(monkeypatch):
    # Mock
    def no_find(_):
        return

    def no_find2(_, __):
        return

    monkeypatch.setattr(IdMap, 'find', no_find2)
    monkeypatch.setattr(TimeslotTDG, 'find', no_find)

    # Execute
    val = TimeslotMapper.find(1)

    # Verify
    assert (val is None)
Exemple #6
0
    def modify_reservation(self, data, reservationId):
        startTime = int(data['timeslot']['startTime'])
        endTime = int(data['timeslot']['endTime'])
        date = str(data['timeslot']['date'])
        dateList = date.split('/')
        roomId = data['roomId']
        username = data['username']
        description = str(data['description'])
        laptop = int(data['equipment']['laptop'])
        board = int(data['equipment']['board'])
        projector = str(data['equipment']['projector'])

        old_booking = ReservationMapper.find(reservationId)
        if not old_booking:
            old_booking = WaitingMapper.find(reservationId)
            if not old_booking:
                response = jsonify({'makeNewReservation error': 'The specified reservation or waiting to modify does not exist.'})
                response.status_code = STATUS_CODE['NOT_FOUND']
                return response
            type = 'waiting'
        else:
            type = 'reservation'

        if not UserMapper.find(username) or not RoomMapper.find(roomId):
            response = jsonify({'makeNewReservation error': 'Either the room or user does not exist.'})
            response.status_code = STATUS_CODE['NOT_FOUND']
            return response

        # delete old but save information first
        old_timeslot = TimeslotMapper.find(old_booking.getTimeslot().getId())
        old_equipment = EquipmentMapper.find(old_booking.getEquipment().getId())
        if type == 'reservation':
            ReservationMapper.delete(old_booking.getId())
            ReservationMapper.done()
        if type == 'waiting':
            WaitingMapper.delete(old_booking.getId())
            WaitingMapper.done()
        TimeslotMapper.delete(old_booking.getTimeslot().getId())
        EquipmentMapper.delete(old_booking.getEquipment().getId())
        TimeslotMapper.done()
        EquipmentMapper.done()

        self.update_internal_lists()

        # no use for `block` parameter, for now, just passing empty strin
        timeslot = TimeslotMapper.makeNew(startTime, endTime, datetime(int(dateList[0]), int(dateList[1]), int(dateList[2])), '', username, str(uuid4()))
        room = RoomMapper.find(roomId)
        user = UserMapper.find(username)
        equipment = EquipmentMapper.makeNew(laptop, projector, board, str(uuid4()))


        duration = int(timeslot.getEndTime()) - int(timeslot.getStartTime())
        if self.find_total_reserved_time_for_user_for_a_given_week(user.getId(), timeslot.getDate().strftime('%Y/%m/%d')) + duration > 3:
            # delete newly create things
            TimeslotMapper.delete(timeslot.getId())
            EquipmentMapper.delete(equipment.getId())

            # add the old reservation back
            TimeslotMapper.makeNew(old_timeslot.getStartTime(), old_timeslot.getEndTime(), old_timeslot.getDate(),
                                   old_timeslot.getBlock(), old_timeslot.getUserId(), old_timeslot.getId())
            EquipmentMapper.makeNew(old_equipment.getNumLaptops(), old_equipment.getNumProjectors(),
                                    old_equipment.getNumWhiteboards(), old_equipment.getId())

            if type == 'reservation':
                ReservationMapper.makeNew(old_booking.getRoom(), old_booking.getUser(), old_booking.getTimeslot(),
                                          old_booking.getDescription(), old_booking.getEquipment(), old_booking.getId())
            if type == 'waiting':
                WaitingMapper.makeNew(old_booking.getRoom(), old_booking.getUser(), old_booking.getTimeslot(),
                                      old_booking.getDescription(), old_booking.getEquipment(), old_booking.getId())

            TimeslotMapper.done()
            EquipmentMapper.done()
            ReservationMapper.done()
            WaitingMapper.done()

            response = jsonify({'error': 'You have already booked for your maximum amount of time this week. Aborting new reservation, replacing old reservation.'})
            response.status_code = STATUS_CODE['UNPROCESSABLE']
            return response

        TimeslotMapper.done()
        EquipmentMapper.done()

        if not self.isTimeslotAvailableforRoom(room, timeslot):
            response = jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is a timeslot conflict: added to the waiting list'))
        elif not self.isEquipmentAvailableForTimeSlot(timeslot, equipment):
            response = jsonify(self.commit_new_waiting(room, user, timeslot, description, equipment, 'There is not enough equipment: added to the waiting list'))
        else:
            # Successfully adding a reservation
            response = jsonify(self.commit_new_reservation(room, user, timeslot, description, equipment))

        self.update_internal_lists()
        self.update_waiting_to_reserved()
        self.update_internal_lists()

        return response