def test_get_difference8(self): workshifts_data = [ { 'id': 4, 'total_workshift_days': 8 } ] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-2-16', 'ending_date': '2019-2-16', 'workshift_id': 4, 'person_id': 1, 'starting_day': 8 } } assign = create_an_assignation(data, workshift_db) assign.starting_date = datetime(2019, 2, 16).date() assign.ending_date = datetime(2019, 2, 20).date() resp = assign.get_differences() resp_right = Range( datetime(2019, 2, 17).date(), datetime(2019, 2, 20).date()) assert ([resp_right] == resp['was_created'] and resp['was_deleted'] == [])
def test_simulate_starting_day3(self): workshifts_data = [ { 'id': 6, 'total_workshift_days': 8 } ] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-31', 'starting_day': 2, 'workshift_id': 6, } } assign = create_an_assignation(data, workshift_db) date_obj = datetime(2019, 1, 23).date() simulated_starting_day = AssignationOperator.simulate_starting_day( assign, date_obj) assert 8 == simulated_starting_day
def test_arent_compatible2(self): workshifts_data = [{'id': 4, 'total_workshift_days': 3}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-3', 'workshift_id': 4, 'person_id': 1, 'starting_day': 1 } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-4', 'ending_date': '2019-1-6', 'workshift_id': 4, 'person_id': 1, 'starting_day': 2 } } assign2 = create_an_assignation(data, workshift_db) assert not AssignationOperator.can_be_joined(assign1, assign2)
def test_get_difference3(self): workshifts_data = [ { 'id': 4, 'total_workshift_days': 8 } ] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-10', 'ending_date': '2019-1-15', 'workshift_id': 4, 'person_id': 1, 'starting_day': 8 } } assign = create_an_assignation(data, workshift_db) assign.starting_date = datetime(2019, 1, 10).date() assign.ending_date = datetime(2019, 1, 15).date() resp = assign.get_differences() assert [] == resp['was_deleted'] and resp['was_created'] == []
def test_can_be_joined5(self): workshifts_data = [{'id': 4, 'total_workshift_days': 6}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2018-12-28', 'ending_date': '2019-1-10', 'workshift_id': 4, 'person_id': 1, 'starting_day': None } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-6', 'workshift_id': 4, 'person_id': 1, 'starting_day': None } } assign2 = create_an_assignation(data, workshift_db) assert AssignationOperator.can_be_joined(assign1, assign2)
def test_remove3(self): workshifts_data = [{'id': 4, 'total_workshift_days': 8}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-10', 'workshift_id': 4, 'person_id': 1, 'starting_day': 6 } } assign = create_an_assignation(data, workshift_db) resp = AssignationOperator.remove(assign, datetime(2019, 1, 4).date(), datetime(2019, 1, 14).date()) resp_assign = resp['update'] assert (resp_assign.starting_date == datetime(2019, 1, 1).date() and resp_assign.ending_date == datetime(2019, 1, 3).date() and resp_assign.starting_day == 6 and resp['update'] == assign and resp['create'] is None and resp['delete'] is None)
def test_process_differences3(self): workshifts_data = [{'id': 6, 'total_workshift_days': 8}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'id': 1, 'starting_date': '2019-2-16', 'ending_date': '2019-2-25', 'workshift_id': 6, 'person_id': 1, 'starting_day': 1 } } assign1 = create_an_assignation(data, workshift_db) assignations = [assign1] assignation_db = AssignationDB(assignations, None) data = { 'assignation': { 'starting_date': '2019-2-19', 'ending_date': '2019-2-22', 'workshift_id': 6, 'person_id': 1, 'starting_day': 4 } } fake_assign = create_an_assignation(data, workshift_db) assignation_db.unassign(fake_assign) data = { 'assignation': { 'starting_date': '2019-2-21', 'ending_date': '2019-2-21', 'workshift_id': 6, 'person_id': 1, 'starting_day': 6 } } assign2 = create_an_assignation(data, workshift_db) assignation_db.assignate(assign2) differences_operator = DifferencesOperator(assignation_db) resp = differences_operator.process_differences() expected1 = Range( datetime(2019, 2, 19).date(), datetime(2019, 2, 20).date()) expected2 = Range( datetime(2019, 2, 22).date(), datetime(2019, 2, 22).date()) assert resp['1'] == [expected1, expected2]
def assignation_moves(data): workshifts_data = data['workshifts'] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) days_off_data = data['days_off'] day_off_assignations = create_proxy_day_off_assignation(days_off_data) day_off_assignations_db = DayOffAssignationDB(day_off_assignations, DayOffAssignationProxy) assignations_database_data = data['assignations_database'] database_assignations = create_assignations(assignations_database_data, workshift_db, day_off_assignations_db) assignation_db = AssignationDB(database_assignations, None) moves = data['moves'] assignations_data = moves['assignations'] assignations = create_assignations(assignations_data, workshift_db, day_off_assignations_db) deallocates_data = moves['deallocates'] deallocates = create_assignations(deallocates_data, workshift_db, day_off_assignations_db) for deallocate in deallocates: assignation_db.unassign(deallocate) for assignation in assignations: if not check_collisions(assignation, assignation_db): assignation_db.assignate(assignation) else: # Tiene colisiones, entonces lo ignoramos pass response = {'create': [], 'update': [], 'delete': []} for new_assignation in assignation_db.to_be_created: response['create'].append({ 'person_id': new_assignation.person_id, 'workshift_id': new_assignation.person_id, 'starting_date': f"{new_assignation.starting_date}", 'ending_date': f"{new_assignation.ending_date}", 'starting_day': new_assignation.starting_day, }) for update_assignation in assignation_db.to_be_updated: response['update'].append({ 'id': assignation.id, 'starting_date': f"{update_assignation.starting_date}", 'ending_date': f"{update_assignation.ending_date}", }) for delete_assignation in assignation_db.to_be_deleted: response['delete'].append(delete_assignation.id) return response
def test_get_candidates3(self): workshifts_data = [{'id': 4, 'total_workshift_days': 8}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-5', 'workshift_id': 4, 'person_id': 1, 'starting_day': 6 } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-7', 'ending_date': '2019-1-11', 'workshift_id': 4, 'person_id': 1, 'starting_day': 4 } } assign2 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-13', 'ending_date': '2019-1-16', 'workshift_id': 4, 'person_id': 1, 'starting_day': 2 } } assign3 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-12', 'ending_date': '2019-1-12', 'workshift_id': 4, 'person_id': 1, 'starting_day': 1 } } assign = create_an_assignation(data, workshift_db) assign_list = [assign1, assign2, assign3] best, others = AssignationOperator.get_candidates(assign, assign_list) assert best == assign2 and others == [assign3]
def test_cycle_date_iterator_1(self): workshifts_data = [{ 'id': 6, 'total_workshift_days': 3, 'workshift_type': 'cyclic', 'days': [{ 'day_number': 0, 'starting_time': '08:00', 'ending_time': '19:00' }, { 'day_number': 1, 'starting_time': '08:00', 'ending_time': '19:00' }, { 'day_number': 2, 'starting_time': None, 'ending_time': None }] }] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) assignation = { 'assignation': { 'person_id': 1, 'starting_day': 1, 'starting_date': '2020-2-1', 'ending_date': '2020-2-10', 'workshift_id': 6, } } assignation = create_an_assignation(assignation, workshift_db, None) iterator = CycleDateIterator(assignation, 0) dates = [] for date_obj in iterator: dates.append(date_obj) expected = [ datetime(2020, 2, 1).date(), datetime(2020, 2, 4).date(), datetime(2020, 2, 7).date(), datetime(2020, 2, 10).date(), ] assert expected == dates
def test_can_be_joined4(self): workshifts_data = [{'id': 4, 'total_workshift_days': 8}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-5', 'workshift_id': 4, 'person_id': 1, 'starting_day': 6 } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-7', 'ending_date': '2019-1-11', 'workshift_id': 4, 'person_id': 1, 'starting_day': 4 } } assign2 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2018-12-31', 'ending_date': '2019-1-8', 'workshift_id': 4, 'person_id': 1, 'starting_day': 5 } } assign = create_an_assignation(data, workshift_db) list_assigns = [assign1, assign2] resp = AssignationOperator.are_multiple_compatible( assign, list_assigns) assert resp == list_assigns
def proxy(): workshifts_data = [{'id': 10, 'total_workshift_days': 7}] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'person': 5, 'starting_date': '2019-1-1', 'starting_day': 1, 'ending_date': '2019-1-10', 'workshift_id': 10, 'fake_attr': 'badAttr' } } return create_an_assignation(data, workshift_db)
def test_add3(self): workshifts_data = [ { 'id': 4, 'total_workshift_days': 8 } ] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'starting_date': '2019-1-1', 'ending_date': '2019-1-22', 'workshift_id': 4, 'person_id': 1, 'starting_day': 1 } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'starting_date': '2019-1-22', 'ending_date': '2019-1-28', 'workshift_id': 4, 'person_id': 1, 'starting_day': 5 } } assign2 = create_an_assignation(data, workshift_db) assign1 += assign2 range_obj = assign1.range_obj assert (assign1.starting_date == datetime(2019, 1, 1).date() and assign1.ending_date == datetime(2019, 1, 22).date() and range_obj.starting_date == datetime(2019, 1, 1).date() and range_obj.ending_date == datetime(2019, 1, 22).date())
def test_process_differences7(self): workshifts_data = [ { 'id': 1, 'total_workshift_days': 8 }, { 'id': 2, 'total_workshift_days': 4 }, ] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) data = { 'assignation': { 'id': 1, 'starting_date': '2019-2-10', 'ending_date': '2019-2-15', 'workshift_id': 1, 'person_id': 1, 'starting_day': 1 } } assign1 = create_an_assignation(data, workshift_db) data = { 'assignation': { 'id': 2, 'starting_date': '2019-2-13', 'ending_date': '2019-2-20', 'workshift_id': 2, 'person_id': 1, 'starting_day': 4 } } assign2 = create_an_assignation(data, workshift_db) assignations = [assign1, assign2] assignation_db = AssignationDB(assignations, None) data = { 'assignation': { 'starting_date': '2019-2-14', 'ending_date': '2019-2-14', 'workshift_id': 1, 'person_id': 1, 'starting_day': 3 } } assign3 = create_an_assignation(data, workshift_db) assignation_db.assignate(assign3) data = { 'assignation': { 'starting_date': '2019-2-13', 'ending_date': '2019-2-17', 'workshift_id': 2, 'person_id': 1, 'starting_day': 3 } } assign4 = create_an_assignation(data, workshift_db) assignation_db.assignate(assign4) data = { 'assignation': { 'starting_date': '2019-2-14', 'ending_date': '2019-2-16', 'workshift_id': 1, 'person_id': 1, 'starting_day': 5 } } fake_assign = create_an_assignation(data, workshift_db) assignation_db.unassign(fake_assign) data = { 'assignation': { 'starting_date': '2019-2-13', 'ending_date': '2019-2-13', 'workshift_id': 2, 'person_id': 1, 'starting_day': 4 } } fake_assign = create_an_assignation(data, workshift_db) assignation_db.unassign(fake_assign) differences_operator = DifferencesOperator(assignation_db) resp = differences_operator.process_differences() expected = Range( datetime(2019, 2, 13).date(), datetime(2019, 2, 17).date()) assert resp['1'] == [expected]
def build_workshift_db(self): workshifts_data = self.data['workshifts'] workshifts = create_proxy_workshifts(workshifts_data) workshift_db = WorkShiftDB(workshifts, WorkShiftProxy) return workshift_db