def drivers(self): drv1 = Driver(id='Tom & Jerry', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv1.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv1.skills = ['calm', 'angry'] drv1.service_regions = [ ServiceRegionPolygon(lat_lng_pairs=[(0, 0), (0, 1), (1, 1)]) ] drv2 = Driver(id='Sam & Max', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv2.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv2.skills = ['pirate', 'ninja'] drv2.service_regions = [ ServiceRegionPolygon(lat_lng_pairs=[(0, 0), (0, 1), (1, 1)]), ServiceRegionPolygon(lat_lng_pairs=[(1, 1), (1, 2), (2, 2.5)]), ] drv3 = Driver(id='rantanplan', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv3.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv3.skills = ['woofing', 'barking'] return drv1, drv2, drv3
def test_allowed_overtime(self, cls_name): ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2.5 with pytest.raises(TypeError) as excinfo: ws.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'allowed_overtime', (int, long), float) assert err_msg == str(excinfo.value)
def test_service_regions_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = ['calm', 'angry'] drv.speed_factor = 1.5 drv.service_regions = 'A region' with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'service_regions', (list, tuple), str) assert err_msg == str(excinfo.value) drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = ['calm', 'angry'] drv.speed_factor = 1.5 drv.service_regions = [ ServiceRegionPolygon(lat_lng_pairs=[(0, 1), (1, 1), (1, 2)]), 'Invaild Object' ] with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = "'{}.service_regions' must contain elements of type {}"\ .format(cls_name, 'ServiceRegionPolygon') assert err_msg == str(excinfo.value)
def test_break(self, cls_name): ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 ws.break_ = 42 with pytest.raises(TypeError) as excinfo: ws.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'break_', Break, int) assert err_msg == str(excinfo.value)
def test_skills_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = 3 with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'skills', (list, tuple), int) assert err_msg == str(excinfo.value) drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = [2] with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = "'{}.skills' must contain elements of type str".format( cls_name) assert err_msg == str(excinfo.value)
def test_fixed_cost_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = ['calm', 'angry'] drv.fixed_cost = '30' with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'fixed_cost', Number, str) assert err_msg == str(excinfo.value)
def test_is_valid(self): ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 assert ws.validate() is None ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 ws.break_ = Break(earliest_start=dtime, latest_start=dtime, duration=5) ws.unavailable_times = [TimeWindow(start_time=dtime, end_time=dtime)] assert ws.validate() is None assert jsonify(ws) == ( '{"workTimeFrom": "2014-12-05T08:00", "break": {"breakStartTo": ' '"2014-12-05T08:00", "breakStartFrom": "2014-12-05T08:00", ' '"breakDuration": 5}, "unavailableTimes": [{"timeFrom": ' '"2014-12-05T08:00", "timeTo": "2014-12-05T08:00"}], "workTimeTo": ' '"2014-12-05T08:00", "allowedOvertime": 2}') assert WorkShiftValidator.validate(dictify(ws)) is None
def test_is_valid(self): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = [WorkShift(start_work=dtime, end_work=dtime)] drv.skills = ['calm', 'angry'] drv.speed_factor = 1.5 drv.cost_per_hour = 50 drv.cost_per_hour_for_overtime = 100 drv.cost_per_km = 60 drv.fixed_cost = 20 assert drv.validate() is None assert jsonify(drv) == ( '{"fixedCost": 20, "workShifts": [{"workTimeFrom": "2014-12-05T08:00", ' '"workTimeTo": "2014-12-05T08:00"}], "startLat": 3, "costPerHourForOvertime": 100, ' '"costPerHour": 50, "serviceRegions": [], "costPerKm": 60, "id": "3", "endLon": 5, ' '"skills": ["calm", "angry"], "endLat": 4, "speedFactor": 1.5, "startLon": 4}' ) assert DriverValidator.validate(dictify(drv)) is None
def test_is_valid(self): ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 assert ws.validate() is None ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 ws.break_ = Break(earliest_start=dtime, latest_start=dtime, duration=5) ws.unavailable_times = [TimeWindow(start_time=dtime, end_time=dtime)] assert ws.validate() is None assert jsonify(ws) == ( '{"workTimeFrom": "2014-12-05T08:00", "break": {"breakStartTo": ' '"2014-12-05T08:00", "breakStartFrom": "2014-12-05T08:00", ' '"breakDuration": 5}, "unavailableTimes": [{"timeFrom": ' '"2014-12-05T08:00", "timeTo": "2014-12-05T08:00"}], "workTimeTo": ' '"2014-12-05T08:00", "allowedOvertime": 2}' ) assert WorkShiftValidator.validate(dictify(ws)) is None
def route_plan(): from decimal import Decimal d1 = datetime.datetime(year=2014, month=12, day=5, hour=8, minute=0) d2 = datetime.datetime(year=2014, month=12, day=5, hour=14, minute=0) ws = WorkShift(d1, d2) drv = Driver('123', Decimal('53.350046'), Decimal('-6.274655'), Decimal('53.341191'), Decimal('-6.260402')) drv.work_shifts.append(ws) order1 = Order('123', Decimal('53.343204'), Decimal('-6.269798'), 20) order2 = Order('456', Decimal('53.341820'), Decimal('-6.264991'), 25) routeplan = RoutePlan( request_id='4321', callback_url='https://callback.com/1234', status_callback_url='https://status.callback.com/1234') routeplan.drivers.append(drv) routeplan.orders.append(order1) routeplan.orders.append(order2) return routeplan
def test_end_work(self, cls_name): ws = WorkShift(start_work=dtime, end_work=3) with pytest.raises(TypeError) as excinfo: ws.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'end_work', datetime, int) assert err_msg == str(excinfo.value)
def test_unavailable_times(self, cls_name): ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 ws.break_ = Break(earliest_start=dtime, latest_start=dtime, duration=5) ws.unavailable_times = 3 with pytest.raises(TypeError) as excinfo: ws.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'unavailable_times', (list, tuple), int) assert err_msg == str(excinfo.value) ws = WorkShift(start_work=dtime, end_work=dtime) ws.allowed_overtime = 2 ws.break_ = Break(earliest_start=dtime, latest_start=dtime, duration=5) ws.unavailable_times = [3] with pytest.raises(TypeError) as excinfo: ws.validate() err_msg = "'{}.unavailable_times' must contain elements of type " \ "TimeWindow".format(cls_name) assert err_msg == str(excinfo.value)