def test_get_volunteers_own_requests_correctly(self): session_id = 'sessionId' volunteer_id = '5f7c2d96e48e242b81178822' other_volunteer_id = '5f81ae36fa3b02a743177500' volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer request_1 = Request(requester='5f81ae776db502d353a84fdf', status=RequestStatus.CREATED, items=[], volunteer=None) request_2 = Request(requester='5f81ae776db502d353a84fdf', status=RequestStatus.IN_PROGRESS, items=[], volunteer=volunteer_id, submission_date=datetime.now()) request_3 = Request(requester='5f81ae776db502d353a84fdf', status=RequestStatus.IN_PROGRESS, items=[], volunteer=other_volunteer_id, submission_date=datetime.now()) request_1_id = self.requests_dao.store_one(request_1.to_db_object()) request_2_id = self.requests_dao.store_one(request_2.to_db_object()) request_3_id = self.requests_dao.store_one(request_3.to_db_object()) requests = self.request_handler.get_volunteers_own_requests(session_id) self.assertEqual(len(requests), 1) self.assertEqual(requests[0]['id'], request_2_id)
def test_add_request_to_time_frame_correctly(self): session_id = 'someId' volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer time_frame = TimeFrame(volunteer_id, datetime.fromisoformat('2020-10-25 12:00:00'), datetime.fromisoformat('2020-10-25 14:00:00'), []) time_frame.id = self.dao.store_one(time_frame.to_db_object()) request_1 = Request(requester=str(ObjectId()), status=RequestStatus.IN_PROGRESS, items=[], volunteer=volunteer_id) request_2 = Request(requester=str(ObjectId()), status=RequestStatus.IN_PROGRESS, items=[], volunteer=volunteer_id) request_1.id = self.requests_dao.store_one(request_1.to_db_object()) request_2.id = self.requests_dao.store_one(request_2.to_db_object()) self.time_frame_handler.add_request_to_time_frame( time_frame.id, request_1.id, session_id) self.time_frame_handler.add_request_to_time_frame( time_frame.id, request_2.id, session_id) time_frames = self.dao.get_time_frames_by_volunteer(volunteer_id) self.assertEqual(len(time_frames), 1) self.assertEqual(str(time_frames[0]['_id']), time_frame.id) self.assertEqual(len(time_frames[0]['requests']), 2) self.assertEqual(str(time_frames[0]['requests'][0]), request_1.id) self.assertEqual(str(time_frames[0]['requests'][1]), request_2.id)
def test_add_time_frame_incorrect_date_pattern(self): session_id = 'someId' volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer incorrect_date_pattern = '20201025,12,00,00' with self.assertRaises(ValueError): self.time_frame_handler.add_time_frame(incorrect_date_pattern, '2020-10-25 14:00:00', session_id)
def test_accept_request_with_unknown_object_id(self): session_id = 'sessionId' volunteer = Volunteer('login', 'pw', 'first', 'last', '5f81ae776db502d353a84fdf') self.volunteer_handler.active_user_sessions[session_id] = volunteer self.assertEqual(len(self.requests_dao.get_all()), 0) request_id = '5f7c28c6e979c6a33a1f3f79' with self.assertRaises(ObjectIdNotFoundError): self.request_handler.accept_request(request_id, session_id) self.assertEqual(len(self.requests_dao.get_all()), 0)
def test_get_open_requests_with_area_correctly(self): volunteer_point = (60.195382, 24.938703) closest_request_point = (60.195318, 24.940731) second_closest_request_point = (60.193371, 24.943177) out_of_range_request_point = (60.143397, 24.985745) range = 1 session_id = 'sessionId' volunteer = Volunteer('login', 'pw', 'first', 'last', 'userId') self.volunteer_handler.active_user_sessions[session_id] = volunteer out_of_range_request_date = datetime.fromisoformat( '2020-10-25 13:03:55.792200') second_closest_request_date = datetime.fromisoformat( '2020-10-26 13:03:55.792200') closest_request_date = datetime.fromisoformat( '2020-10-27 13:03:55.792200') closest_request = Request(requester='5f7c2d96e48e242b81178822', status=RequestStatus.SUBMITTED, items=[], volunteer=None, delivery_address=Address( '', '', '', closest_request_point[0], closest_request_point[1]), submission_date=closest_request_date) second_closest_request = Request( requester='5f7c2d96e48e242b81178822', status=RequestStatus.SUBMITTED, items=[], volunteer=None, delivery_address=Address('', '', '', second_closest_request_point[0], second_closest_request_point[1]), submission_date=second_closest_request_date) out_of_range_request = Request( requester='5f7c2d96e48e242b81178822', status=RequestStatus.SUBMITTED, items=[], volunteer=None, delivery_address=Address('', '', '', out_of_range_request_point[0], out_of_range_request_point[1]), submission_date=out_of_range_request_date) closest_request_id = self.requests_dao.store_one( closest_request.to_db_object()) second_closest_request_id = self.requests_dao.store_one( second_closest_request.to_db_object()) out_of_range_request_id = self.requests_dao.store_one( out_of_range_request.to_db_object()) area = { 'range': range, 'lat': volunteer_point[0], 'lng': volunteer_point[1] } requests = self.request_handler.get_open_requests(session_id, area) self.assertEqual(len(requests), 2) self.assertEqual(requests[0]['id'], second_closest_request_id) self.assertEqual(requests[1]['id'], closest_request_id)
def test_accept_request_with_other_status_than_submitted(self): session_id = 'sessionId' volunteer = Volunteer('login', 'pw', 'first', 'last', '5f81ae776db502d353a84fdf') self.volunteer_handler.active_user_sessions[session_id] = volunteer request = Request(requester='5f7c2d96e48e242b81178822', status=RequestStatus.CREATED, items=[], volunteer=None) request_id = self.requests_dao.store_one(request.to_db_object()) with self.assertRaises(UnexpectedRequestStatusError): self.request_handler.accept_request(request_id, session_id)
def test_add_request_to_time_frame_with_incorrect_request_id(self): session_id = 'someId' volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer time_frame = TimeFrame(volunteer_id, datetime.fromisoformat('2020-10-25 12:00:00'), datetime.fromisoformat('2020-10-25 14:00:00'), []) time_frame.id = self.dao.store_one(time_frame.to_db_object()) self.assertEqual(len(self.requests_dao.get_all()), 0) with self.assertRaises(ObjectIdNotFoundError): self.time_frame_handler.add_request_to_time_frame( time_frame.id, str(ObjectId()), session_id)
def test_add_request_to_time_frame_with_incorrect_time_frame_id(self): session_id = 'someId' volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer request = Request(requester=str(ObjectId()), status=RequestStatus.IN_PROGRESS, items=[], volunteer=volunteer_id) request.id = self.requests_dao.store_one(request.to_db_object()) self.assertEqual(len(self.dao.get_all()), 0) with self.assertRaises(ObjectIdNotFoundError): self.time_frame_handler.add_request_to_time_frame( str(ObjectId()), request.id, session_id)
def test_add_time_frame_correctly(self): session_id = 'someId' volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer start = '2020-10-25 12:00:00' end = '2020-10-25 14:00:00' id = self.time_frame_handler.add_time_frame(start, end, session_id) time_frames = self.dao.get_time_frames_by_volunteer(volunteer_id) self.assertEqual(len(time_frames), 1) self.assertEqual(str(time_frames[0]['_id']), id) self.assertEqual(str(time_frames[0]['volunteer']), volunteer_id) self.assertEqual(str(time_frames[0]['start']), start) self.assertEqual(str(time_frames[0]['end']), end) self.assertEqual(time_frames[0]['requests'], [])
def test_accept_request_correctly(self): session_id = 'sessionId' volunteer_id = '5f7c2d96e48e242b81178822' volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer request = Request(requester='5f81ae776db502d353a84fdf', status=RequestStatus.SUBMITTED, items=[], volunteer=None) request_id = self.requests_dao.store_one(request.to_db_object()) self.request_handler.accept_request(request_id, session_id) request = self.request_handler.from_db_object( self.requests_dao.get_all()[0]) self.assertEqual(request.status, RequestStatus.IN_PROGRESS) self.assertEqual(request.volunteer, volunteer_id)
def test_get_time_frames_correctly(self): session_id = 'someId' volunteer_id = str(ObjectId()) other_volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer time_frame_1 = TimeFrame(volunteer_id, datetime.fromisoformat('2020-10-25 12:00:00'), datetime.fromisoformat('2020-10-25 14:00:00'), []) time_frame_2 = TimeFrame(other_volunteer_id, datetime.fromisoformat('2020-10-25 12:00:00'), datetime.fromisoformat('2020-10-25 14:00:00'), []) time_frame_1_id = self.dao.store_one(time_frame_1.to_db_object()) time_frame_2_id = self.dao.store_one(time_frame_2.to_db_object()) time_frames = self.time_frame_handler.get_time_frames(session_id) self.assertEqual(len(time_frames), 1) self.assertEqual(time_frames[0]['id'], time_frame_1_id)
def test_get_open_requests_correctly(self): session_id = 'sessionId' volunteer = Volunteer('login', 'pw', 'first', 'last', 'userId') self.volunteer_handler.active_user_sessions[session_id] = volunteer request_1 = Request(requester='5f7c2d96e48e242b81178822', status=RequestStatus.CREATED, items=[], volunteer=None, submission_date=datetime.now()) request_2 = Request(requester='5f7c2d96e48e242b81178822', status=RequestStatus.SUBMITTED, items=[], volunteer=None, submission_date=datetime.now()) request_1_id = self.requests_dao.store_one(request_1.to_db_object()) request_2_id = self.requests_dao.store_one(request_2.to_db_object()) requests = self.request_handler.get_open_requests(session_id) self.assertEqual(len(requests), 1) self.assertEqual(requests[0]['id'], request_2_id)
def test_add_request_to_time_frame_with_incorrect_volunteer_id_at_request( self): session_id = 'someId' volunteer_id = str(ObjectId()) other_volunteer_id = str(ObjectId()) volunteer = Volunteer('login', 'pw', 'first', 'last', volunteer_id) self.volunteer_handler.active_user_sessions[session_id] = volunteer time_frame = TimeFrame(volunteer_id, datetime.fromisoformat('2020-10-25 12:00:00'), datetime.fromisoformat('2020-10-25 14:00:00'), []) time_frame.id = self.dao.store_one(time_frame.to_db_object()) request = Request(requester=str(ObjectId()), status=RequestStatus.IN_PROGRESS, items=[], volunteer=other_volunteer_id) request.id = self.requests_dao.store_one(request.to_db_object()) with self.assertRaises(UnauthorizedAccessError): self.time_frame_handler.add_request_to_time_frame( time_frame.id, request.id, session_id)