def testInsertCourier6(self): #create a courier that already exists in our database courier = Courier(courierId=1,lat=2.0,lon=3.0) courier.put() params = {'id': 1, 'lat': 5.1,'lon':2.0} response = self.testapp.post('/courier/new', params) self.assertEqual(302, response.status_int)
def post(self): c_id = self.request.get("id") c_lat = self.request.get('lat') c_lon = self.request.get('lon') #do some error handling try: c_id = int(c_id) c_lat = float(c_lat) c_lon = float(c_lon) #check if courier already exists pastCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = :1",c_id).get() if pastCourier is None: courier = Courier(courierId=c_id,lat = c_lat,lon=c_lon) courier.put() #when a courier is added, we assign it an order assign.assignDelivery() self.redirect('/courier/online') else: self.response.set_status(302,'Courier already exists') self.response.headers['Content-Type'] = 'text/html' template = jinja_env.get_template('courier.html') d={"error":"courier with id {} already exists".format(c_id)} self.response.out.write(template.render(d)) except ValueError: self.response.set_status(303,'Invalid Values') self.response.headers['Content-Type'] = 'text/html' template = jinja_env.get_template('courier.html') d={"error":"Invalid input parameters"} self.response.out.write(template.render(d))
def testPost3(self): #when courier comes online, assign it a package order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() order = Order(orderId=2,pickup_lat=10.0,pickup_lon=10.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() order = Order(orderId=3,pickup_lat=10.0,pickup_lon=1.0,dropoff_lat=12.0,dropoff_lon=2.0) order.put() order = Order(orderId=4,pickup_lat=1.0,pickup_lon=10.0,dropoff_lat=2.0,dropoff_lon=12.0) order.put() courier = Courier(courierId=7,lat=2.0,lon=3.0,online=False) courier.put() response = self.testapp.post('/courier/7/online') self.assertEqual(302,response.status_int) #check that courier 1 is assigned order 1 # courier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 7").get() # self.assertEqual(1,courier.orderId) # self.assertEqual(False,courier.online) #check order 1 is assigned to courier 1 posOrder = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual('enRoute',posOrder.state) self.assertEqual(7,posOrder.courierId) #check that the other orders are still unassigned for id in [2,3,4]: negOrder = db.GqlQuery("SELECT * FROM Order WHERE orderId = :1",id).get() self.assertEqual('needPickup',negOrder.state) self.assertEqual(None,negOrder.courierId)
def post(self): c_id = self.request.get("id") c_lat = self.request.get('lat') c_lon = self.request.get('lon') #do some error handling try: c_id = int(c_id) c_lat = float(c_lat) c_lon = float(c_lon) #check if courier already exists pastCourier = db.GqlQuery( "SELECT * FROM Courier WHERE courierId = :1", c_id).get() if pastCourier is None: courier = Courier(courierId=c_id, lat=c_lat, lon=c_lon) courier.put() #when a courier is added, we assign it an order assign.assignDelivery() self.redirect('/courier/online') else: self.response.set_status(302, 'Courier already exists') self.response.headers['Content-Type'] = 'text/html' template = jinja_env.get_template('courier.html') d = {"error": "courier with id {} already exists".format(c_id)} self.response.out.write(template.render(d)) except ValueError: self.response.set_status(303, 'Invalid Values') self.response.headers['Content-Type'] = 'text/html' template = jinja_env.get_template('courier.html') d = {"error": "Invalid input parameters"} self.response.out.write(template.render(d))
def testPost(self): #courier exists courier = Courier(courierId=7,lat=2.0,lon=3.0,online=True) courier.put() response = self.testapp.post('/courier/7/offline') self.assertEqual(302,response.status_int) result_courier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 7").get() self.assertEqual(False,result_courier.online)
def testPost3(self): #courier found, but order not found order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() courier = Courier(courierId=7,lat=2.0,lon=3.0) courier.put() response = self.testapp.post('/courier/7/complete') self.assertEqual(333,response.status_int)
def testPost2(self): #courier not found order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() courier = Courier(courierId=1,lat=2.0,lon=3.0,orderId=1) courier.put() self.assertEqual(1,len(Courier.all().fetch(20))) # if the courier is not found, nothing should be changed response = self.testapp.post('/courier/2/complete') self.assertEqual(333,response.status_int)
def testPost1(self): #courier and order exists order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() courier = Courier(courierId=7,lat=2.0,lon=3.0,orderId=7) courier.put() response = self.testapp.post('/courier/7/accept/1') self.assertEqual(200, response.status_int) #check that courier has properly changed result_courier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 7").get() #check order has properly changed result_order = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual('enRoute',result_order.state)
def testPost1(self): #courier and order found, with correct state order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0,state='enRoute') order.put() self.assertEqual('enRoute',order.state) courier = Courier(courierId=1,lat=2.0,lon=3.0,orderId=1) courier.put() self.testapp.post('/courier/1/complete') result_courier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() self.assertEqual(True, result_courier.online) result_order = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual('delivered',result_order.state)
def testAllCouriers(self): courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=4.0,lon=3.0,online=True) courier.put() c = assign.allCourierIds() self.assertEqual([1,3],c)
def testAvailableCouriers(self): courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=4.0,lon=3.0,online=True) courier.put() availCouriers = assign.availableCouriers() self.assertEqual(2,availCouriers.count())
def testPost3(self): #invalid input, other courier/order exists, make sure were not changed. #populate with courier and order courier = Courier(courierId=1,lat=2.0,lon=3.0) courier.put() order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() #submit invalid input params ={'id':'a','plat':3.0,'plon':4.0,'dlat':11.0,'dlon':11.0} response = self.testapp.post('/order/new',params) self.assertEqual(344, response.status_int) #check courier and order was unchanged posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() self.assertEqual(True, posCourier.online) posOrder = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual(None,posOrder.courierId) self.assertEqual('needPickup',posOrder.state)
def testInsertCourier5(self): #invalid inputs with previously existing courier/order, make sure they did not change #populate with courier/order courier = Courier(courierId=1,lat=2.0,lon=3.0) courier.put() order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() #post invalid input params = {'id': 1, 'lat': 'bar','lon':2.0} response = self.testapp.post('/courier/new', params) self.assertEqual(303, response.status_int) #check previous courier and order did not change posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() self.assertEqual(True, posCourier.online) posOrder = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual(None,posOrder.courierId) self.assertEqual('needPickup',posOrder.state)
def testFormMatrix(self): courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=14.0,lon=13.0,online=True) courier.put() order = Order(orderId=4,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() order = Order(orderId=5,pickup_lat=11.0,pickup_lon=11.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() orders = assign.idleOrders() couriers = assign.availableCouriers() matrix = self.mkres.form_matrix(orders, couriers)
def testInsertCourier(self): #create courier and there are no orders awaiting pickup params = {'id': 1, 'lat': 1.0,'lon':2.0} response = self.testapp.post('/courier/new', params) self.assertEqual(302,response.status_int) p2 = {'id': 2, 'lat': 11.0,'lon':22.0} response = self.testapp.post('/courier/new', p2) self.assertEqual(302,response.status_int) self.assertEqual(2, len(Courier.all().fetch(20))) courier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() self.assertNotEqual(None, courier)
def select_courier_by_id(courier_id): cursor = db_connection.cursor() cursor.execute(f"select * from couriers where courier_id = %s;", [courier_id]) result = cursor.fetchall()[0] cursor.close() time_intervals = [ TimeInterval(e[0] // 60, e[0] % 60, e[1] // 60, e[1] % 60) for e in result[3] ] return Courier(result[0], CourierType(result[1]), result[2], time_intervals)
def testAssignDelivery1(self): #have couriers but no orders available courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=4.0,lon=3.0,online=True) courier.put() orders = assign.idleOrders() self.assertEqual(0, orders.count()) assign.assignDelivery() posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() self.assertEqual(True,posCourier.online) posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 3").get() self.assertEqual(True,posCourier.online)
def testLowestCost(self): courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=14.0,lon=13.0,online=True) courier.put() order = Order(orderId=4,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() order = Order(orderId=5,pickup_lat=11.0,pickup_lon=11.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() orders = assign.idleOrders() couriers = assign.availableCouriers() indexes = self.mkres.lowest_cost(orders, couriers) self.assertEqual(2,len(indexes)) for elem in indexes: self.assertEqual(2,len(elem))
def testAssignDelivery3(self): #normal case where we have orders and couriers available courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=14.0,lon=13.0,online=True) courier.put() order = Order(orderId=4,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() order = Order(orderId=5,pickup_lat=11.0,pickup_lon=11.0,dropoff_lat=2.0,dropoff_lon=2.0) order.put() assign.assignDelivery() #order 4 is matched with courier 1 # posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() # self.assertEqual(4,posCourier.orderId) # self.assertEqual(False,posCourier.online) posOrder = db.GqlQuery("SELECT * FROM Order WHERE orderId = 4").get() self.assertEqual(1,posOrder.courierId) self.assertEqual('enRoute',posOrder.state)
def validate_courier(courier_dict): try: id = validate_id(courier_dict.get('courier_id')) except Exception as e: raise ValidationException('Поле courier_id - ' + str(e)) try: t = validate_type(courier_dict.get('courier_type')) except Exception as e: raise ValidationException('Поле courier_type - ' + str(e)) try: regions = validate_regions(courier_dict.get('regions')) except Exception as e: raise ValidationException('Поле regions - ' + str(e)) try: hours = validate_time_list(courier_dict.get('working_hours')) except Exception as e: raise ValidationException('Поле working_hours - ' + str(e)) return Courier(id, t, regions, hours)
def testPost2(self): #courier does not exists courier = Courier(courierId=1,lat=2.0,lon=3.0,online=False) courier.put() response = self.testapp.post('/courier/7/offline') self.assertEqual(300,response.status_int)
def testPost2(self): #courier exists, order does not exists courier = Courier(courierId=7,lat=2.0,lon=3.0,orderId=7) courier.put() response = self.testapp.post('/courier/7/accept/1') self.assertEqual(333, response.status_int)
def testPost(self): #populate couriers courier = Courier(courierId=1,lat=2.0,lon=3.0) courier.put() courier = Courier(courierId=2,lat=12.0,lon=13.0) courier.put() courier = Courier(courierId=3,lat=2.0,lon=13.0) courier.put() courier = Courier(courierId=4,lat=12.0,lon=3.0) courier.put() params ={'id':1,'plat':4.0,'plon':4.0,'dlat':11.0,'dlon':11.0} self.testapp.post('/order/new',params) order = db.GqlQuery("SELECT * FROM Order WHERE orderId = 1").get() self.assertEqual(1, order.courierId) #should have been assigned to courier 1 # posCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = 1").get() # self.assertEqual(False,posCourier.online) # self.assertEqual(1,posCourier.orderId) #check that the other couriers are available and have not been assigned for id in range(2,5): negCourier = db.GqlQuery("SELECT * FROM Courier WHERE courierId = :1",id).get() self.assertEqual(True, negCourier.online)
def testAvailableCourierId(self): order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0,state='enRoute',courierId= 3) order.put() courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=4.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=8,lat=4.0,lon=3.0,online=True) courier.put() self.assertEqual([1,8],assign.availableCourierId())
def testUnavailableCouriers(self): order = Order(orderId=1,pickup_lat=1.0,pickup_lon=1.0,dropoff_lat=2.0,dropoff_lon=2.0,state='enRoute',courierId= 3) order.put() courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=3,lat=4.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=4,lat=4.0,lon=3.0,online=True) courier.put() couriers = assign.unavailableCouriers() for c in couriers: self.assertIn(c.courierId, [3])
def testQuery(self): courier = Courier(courierId=1,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=2,lat=12.0,lon=13.0,online=False) courier.put() courier = Courier(courierId=14,lat=2.0,lon=3.0,online=True) courier.put() courier = Courier(courierId=51,lat=2.0,lon=3.0,online=True) courier.put() q = Query(Courier) q.filter("online = ", True) for courier in q: self.assertNotEqual(2, courier.courierId)