async def test_update_parking_lot_price(http_client, base_url): lot = ParkingLot(100, 'test', 1.0, Location(0.0, 1.0)) response = await http_client.fetch(base_url + '/spaces', method='POST', headers=HEADERS, body=serialize_model(lot)) assert ParkingLotCreationResponse(**json.loads(response.body)).id == 1 # TODO: Check that the ParkingLot price has actually been updated using a GET # request. body = serialize_model(ParkingLotPriceMessage(2.0)) response = await http_client.fetch(base_url + '/spaces/1/price', method='POST', headers=HEADERS, body=body) assert response.code == 200
async def test_create_parking_lot(http_client, base_url): lot = ParkingLot(100, 'test', 1.0, Location(0.0, 1.0)) response = await http_client.fetch(base_url + '/spaces', method='POST', headers=HEADERS, body=serialize_model(lot)) assert ParkingLotCreationResponse(**json.loads(response.body)).id == 1
async def test_parking_request_message(ws_url, user_id, mocker, engine): user_location = Location(0.0, 1.0) lot_location = Location(0.001, 1.0) lot_allocation = ParkingLotAllocation(100, "test_lot", 0, lot_location, 1, distance=50.0, availability=20) parking_request = ParkingRequestMessage(user_location) mocker.patch.object(engine, 'handle_allocation_request') engine.handle_allocation_request.return_value = return_async_value( lot_allocation) conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message(serialize_model(parking_request)) msg = deserialize_ws_message(await conn.read_message()) engine.handle_allocation_request.assert_called_once_with( user_id, parking_request) assert isinstance(msg, ParkingAllocationMessage) assert msg.lot == lot_allocation
async def update_price(self, lot_id: int, price: float): msgbody = serialize_model(rest_models.ParkingLotPriceMessage(price)) request = httpclient.HTTPRequest(f"{self.rest_url}/{lot_id}/price", body=msgbody, headers=HEADERS, method='POST') await self.client.fetch(request)
async def create_lot(self, lot: rest_models.ParkingLot): request = httpclient.HTTPRequest(self.rest_url, body=serialize_model(lot), headers=HEADERS, method='POST') response = await self.client.fetch(request) return rest_models.ParkingLotCreationResponse( **json.loads(response.body)).id
async def update_available(self, lot_id: int, available: int): msgbody = serialize_model( rest_models.ParkingLotAvailableMessage(available)) request = httpclient.HTTPRequest(f"{self.rest_url}/{lot_id}/available", body=msgbody, headers=HEADERS, method='POST') await self.client.fetch(request)
async def test_location_update_message(ws_url, user_id, mocker, usessions): conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) mocker.spy(usessions, 'update_user_location') location = Location(0.0, 1.0) await conn.write_message(serialize_model(LocationUpdateMessage(location))) await sleep(0.01) usessions.update_user_location.assert_called_once_with(user_id, location)
async def test_parking_rejection_message(ws_url, user_id, mocker, usessions): lot_id = 7 mocker.spy(usessions, "add_user_rejection") conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message(serialize_model(ParkingRejectionMessage(lot_id))) msg = deserialize_ws_message(await conn.read_message()) usessions.add_user_rejection.assert_called_once_with(user_id, lot_id) assert isinstance(msg, ConfirmationMessage)
async def hello(websocket, path): # receive a request m = await websocket.recv() print(m) # request = json.loads(request) # message = deserialize_ws_message(request) # send an allocation message = serialize_model( wsmodels.ParkingAllocationMessage( wsmodels.ParkingLot(1, "x", 1.0, wsmodels.Location(200.0, 300.0)))) await websocket.send(message) deallocation = wsmodels.ParkingCancellationMessage(1) while True: await websocket.recv() if random.randint(1, 10) == 1: await asyncio.sleep(1) await websocket.send(serialize_model(deallocation))
async def test_parking_cancellation_message(ws_url, user_id, dbaccess): lot_id = 7 dbaccess.delete_allocation.return_value = return_async_value(None) conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message( serialize_model(ParkingCancellationMessage(lot_id))) assert await conn.read_message( ) is None # assert connection was closed by server await sleep(0.01) dbaccess.delete_allocation.assert_called_once_with(user_id)
async def update_available(self, lot_id: int, available: int): msgbody = serialize_model( rest_models.ParkingLotAvailableMessage(available)) request = httpclient.HTTPRequest(f"{self.rest_url}/{lot_id}/available", body=msgbody, headers=HEADERS, method='POST') try: await self.client.fetch(request) except httpclient.HTTPError: logger.info( "server error while updating lot availability for lot number " + str(lot_id)) raise
async def test_parking_acceptance_message_happy_case(ws_url, user_id, mocker, dbaccess, engine): lot_id = 7 mocker.patch.object(dbaccess, "allocate_parking_lot") dbaccess.allocate_parking_lot.return_value = return_async_value(True) mocker.spy(engine, "commit_allocation") conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message(serialize_model(ParkingAcceptanceMessage(lot_id))) msg = deserialize_ws_message(await conn.read_message()) engine.commit_allocation.assert_called_once_with(user_id, lot_id) assert isinstance(msg, ConfirmationMessage)
async def test_parking_acceptance_message_failed_commit( ws_url, user_id, mocker, dbaccess, engine): lot_id = 7 mocker.patch.object(dbaccess, "allocate_parking_lot") dbaccess.allocate_parking_lot.return_value = return_async_value(False) mocker.spy(engine, "commit_allocation") conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message(serialize_model(ParkingAcceptanceMessage(lot_id))) msg = deserialize_ws_message(await conn.read_message()) engine.commit_allocation.assert_called_once_with(user_id, lot_id) assert WebSocketErrorType( msg.error) == WebSocketErrorType.ALLOCATION_COMMIT_FAIL
async def test_parking_request_message_no_parking_lot(ws_url, user_id, mocker, engine): user_location = Location(0.0, 1.0) parking_request = ParkingRequestMessage(user_location) mocker.patch.object(engine, 'handle_allocation_request') engine.handle_allocation_request.return_value = return_async_value(None) conn: WebSocketClientConnection = await tornado.websocket.websocket_connect( ws_url + user_id) await conn.write_message(serialize_model(parking_request)) msg = deserialize_ws_message(await conn.read_message()) engine.handle_allocation_request.assert_called_once_with( user_id, parking_request) assert WebSocketErrorType( msg.error) == WebSocketErrorType.NO_AVAILABLE_PARKING_LOT
def write_as_json(self, model: object) -> None: self.write_message(serialize_model(model))
async def post(self): lot = self.load_from_json_data(ParkingLot, self.json_args, 'Invalid parking lot data') pid = await self.dba.insert_parking_lot(lot) self.write(serialize_model(ParkingLotCreationResponse(id=pid)))
async def _send(self, message): if not isinstance(message, string_types): message = serialize_model(message) await self._ws.write_message(message)
async def _send(self, message): if not isinstance(message, string_types): message = serialize_model(message) await self._ws.write_message(message) logger.info("message sent: '{}'") # .format(message._type))
def test_serialize_model_raises_error(): with pytest.raises(ValueError): serialize_model(None)
def test_serialize_model(): json_str = '{"available": 10}' sam = ParkingLotAvailableMessage(10) assert serialize_model(sam) == json_str
def post(self): print(self.request.body.rstrip()) self.write(serialize_model(ParkingLotCreationResponse(1)))