def test_end_lat_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat='4', end_lng='5') with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'end_lat', Number, str) 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_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): 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_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_scheduled_driver(self, cls_name): si = SchedulingInfo(scheduled_at=dtime, scheduled_driver=1) with pytest.raises(TypeError) as excinfo: si.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'scheduled_driver', (basestring, Driver), int) assert err_msg == str(excinfo.value) # test it accepts a string as a driver si = SchedulingInfo(scheduled_at=dtime, scheduled_driver='bobos') assert si.validate() is None # test it accepts a driver object too drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) si = SchedulingInfo(scheduled_at=dtime, scheduled_driver=drv) assert si.validate() is None
def test_id_validation_fail(self, cls_name): drv = Driver(id=3, start_lat='3', start_lng='4', end_lat='4', end_lng='5') with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'id', basestring, int) assert err_msg == str(excinfo.value) drv = Driver(id='', start_lat='3', start_lng='4', end_lat='4', end_lng='5') with pytest.raises(ValueError) as excinfo: drv.validate() err_msg = "'{}.id' cannot be empty".format(cls_name) assert err_msg == str(excinfo.value)
def test_assigned_to(self, cls_name): order = Order(id='3', lat=5.2, lng=6.1, duration=7) order.time_window = TimeWindow(dtime, dtime) order.skills = ['handy', 'quiet'] order.assigned_to = 4 with pytest.raises(TypeError) as excinfo: order.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'assigned_to', (basestring, Driver), int) assert err_msg == str(excinfo.value) # test it accepts a string as a driver order.assigned_to = 'rantanplan' assert order.validate() is None # test it accepts a driver objects too drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) order.assigned_to = drv assert order.validate() is None
def test_is_valid(self): order = Order(id='3', lat=5.2, lng=6.1, duration=7) order.time_window = TimeWindow(start_time=dtime, end_time=dtime) order.skills = ['handy', 'quiet'] order.assigned_to = Driver(id='Tom & Jerry', start_lat=3, start_lng=4, end_lat=5, end_lng=6) order.scheduling_info = SchedulingInfo(scheduled_at=dtime, scheduled_driver='rantanplan') assert order.validate() is None assert jsonify(order) == ( '{"assignedTo": "Tom & Jerry", "skills": ["handy", "quiet"], "tw": ' '{"timeFrom": "2014-12-05T08:00", "timeTo": "2014-12-05T08:00"}, ' '"lon": 6.1, "priority": "M", "duration": 7, "lat": 5.2, ' '"schedulingInfo": {"scheduledAt": "2014-12-05T08:00", "locked": ' 'false, "scheduledDriver": "rantanplan"}, "id": "3"}') assert OrderValidator.validate(dictify(order)) is None
def test_is_valid(self): si = SchedulingInfo(scheduled_at=dtime, scheduled_driver='bobos') assert si.validate() is None assert jsonify(si) == ( '{"scheduledAt": "2014-12-05T08:00", "locked": false, ' '"scheduledDriver": "bobos"}') assert SchedulingInfoValidator.validate(dictify(si)) is None # test that if we give a Driver object instead of its string id it has # the same result drv = Driver(id='bobos', start_lat=3, start_lng=4, end_lat=4, end_lng=5) si = SchedulingInfo(dtime, drv) assert si.validate() is None assert jsonify(si) == ( '{"scheduledAt": "2014-12-05T08:00", "locked": false, ' '"scheduledDriver": "bobos"}') assert SchedulingInfoValidator.validate(dictify(si)) 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 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_work_shifts_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng=5) drv.work_shifts = 4 with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'work_shifts', (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 = [] with pytest.raises(ValueError) as excinfo: drv.validate() err_msg = "'{}.work_shifts' must contain at least 1 element".\ format(cls_name) 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 = [3] with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = "'{}.work_shifts' must contain elements of type WorkShift".\ format(cls_name) assert err_msg == str(excinfo.value)
def test_end_lng_validation_fail(self, cls_name): drv = Driver(id='3', start_lat=3, start_lng=4, end_lat=4, end_lng='5') with pytest.raises(TypeError) as excinfo: drv.validate() err_msg = TYPE_ERR_MSG.format(cls_name, 'end_lng', Number, str) 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_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 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