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 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 split_borders( range_obj: RangeDateTime ) -> List[Union[RangeDateTime, Range, None]]: starting_datetime = range_obj.starting_datetime ending_datetime = range_obj.ending_datetime delta = ending_datetime.date() - starting_datetime.date() if delta.days == 0: return [range_obj, None, None] time_obj = DateUtil.str_to_time('23:59') aux_ending_datetime = datetime.combine(starting_datetime.date(), time_obj) left_range_datetime = RangeDateTime(starting_datetime, aux_ending_datetime) time_obj = DateUtil.str_to_time('00:00') aux_starting_datetime = datetime.combine(ending_datetime.date(), time_obj) right_range_datetime = RangeDateTime(aux_starting_datetime, ending_datetime) body = None if delta.days > 1: body = Range(starting_datetime.date() + timedelta(days=1), ending_datetime.date() - timedelta(days=1)) return [left_range_datetime, body, right_range_datetime]
def test_operators(self): a = Range(0, 5) self.assertEqual(5, len(a)) # verify that len() is correct self.assertEqual('<Range:0-5>', str(a)) self.assertTrue(a == a) self.assertTrue(a <= a) self.assertTrue(a >= a) self.assertFalse(a < a) self.assertFalse(a > a) self.assertFalse(a != a) # Overlaps 'b'. b = Range(1, 6) self.assertTrue(a <= b) self.assertFalse(a < b) self.assertFalse(a >= b) self.assertFalse(a > b) self.assertFalse(a == b) self.assertTrue(a != b) # Does not overlap 'c'. c = Range(5, 7) self.assertTrue(a < c) self.assertFalse(a <= c) self.assertFalse(a > c) self.assertFalse(a >= c) self.assertFalse(a == c) self.assertTrue(a != c) self.assertFalse(a in c) self.assertFalse(c in a) # A strict superset of 'd'. d = Range(2, 4) self.assertTrue(a <= d) self.assertFalse(a < d) self.assertFalse(a >= d) self.assertFalse(a > d) self.assertFalse(a == d) self.assertTrue(a != d) self.assertTrue(d in a) self.assertFalse(a in d)
def get_differences(self): """ This function return the difference of range between the init range and the current range """ init_range = self.init_range range_obj = self.range_obj resp = {'was_deleted': [], 'was_created': []} if init_range.starting_date != range_obj.starting_date: min_date = min(init_range.starting_date, range_obj.starting_date) max_date = max(init_range.starting_date - timedelta(days=1), range_obj.starting_date - timedelta(days=1)) left_range = Range(min_date, max_date) if init_range.starting_date > range_obj.starting_date: resp['was_created'].append(left_range) else: resp['was_deleted'].append(left_range) if init_range.ending_date != range_obj.ending_date: min_date = min(init_range.ending_date + timedelta(days=1), range_obj.ending_date + timedelta(days=1)) max_date = max(init_range.ending_date, range_obj.ending_date) right_range = Range(min_date, max_date) if init_range.ending_date > range_obj.ending_date: resp['was_deleted'].append(right_range) else: resp['was_created'].append(right_range) return resp
def test_split_borders_1(self): range_datetime = RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 4, 10, 0, 0)) response = RangeDateTimeOperator.split_borders(range_datetime) expected = [ RangeDateTime(datetime(2019, 5, 1, 10, 0, 0), datetime(2019, 5, 1, 23, 59, 0)), Range(datetime(2019, 5, 2).date(), datetime(2019, 5, 3).date()), RangeDateTime(datetime(2019, 5, 4, 0, 0, 0), datetime(2019, 5, 4, 10, 0, 0)), ] assert response == expected
def remove(assign: AssignationProxy, starting_date: dateclass, ending_date: dateclass) -> Dict: """ Remove a range from a given assignation """ resp = {'delete': None, 'update': None, 'create': None} # type: Dict copy_range_obj = copy.copy(assign.range_obj) other_range_obj = Range(starting_date, ending_date) updated_range, new_range = copy_range_obj - other_range_obj if not updated_range: resp['delete'] = assign elif new_range: new_assign = AssignationOperator.copy(assign) new_assign.range_obj = new_range assign.range_obj = updated_range if new_assign.starting_date > assign.starting_date: new_assign.starting_day = AssignationOperator.\ simulate_starting_day( assign, new_assign.range_obj.starting_date) else: assign.starting_day = AssignationOperator.simulate_starting_day( new_assign, assign.range_obj.starting_date) resp['update'] = assign resp['create'] = new_assign elif updated_range.starting_date > assign.starting_date: new_starting_day = AssignationOperator.simulate_starting_day( assign, updated_range.starting_date) assign.range_obj = updated_range assign.starting_day = new_starting_day resp['update'] = assign elif updated_range.ending_date < assign.ending_date: assign.range_obj = updated_range resp['update'] = assign else: pass return resp
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 __init__(self, obj, *args, **kwargs): super(AssignationProxy, self).__init__(obj) self.range_obj = Range(self.starting_date, self.ending_date) self.init_range = copy.copy(self.range_obj) self.init_starting_day = getattr(self.obj, 'starting_day', None)
def covers(self, section): assert isinstance(section, Section64) return (Range(section.offset, section.offset + section.size) in Range(self.cryptoff, self.cryptoff + self.cryptsize))
def test_errors(self): self.assertRaises(ValueError, lambda: Range(-1, 2)) self.assertRaises(ValueError, lambda: Range(0, -1)) self.assertRaises(ValueError, lambda: Range(2, 1))
def _check_body(self, body_range: Range) -> bool: r1 = Range(self.day_off_assignation.starting_date, self.day_off_assignation.ending_date) return (RangeOperator.is_in(body_range, r1) and self.covers_a_whole_day())