Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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))
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
 def write_as_json(self, model: object) -> None:
     self.write_message(serialize_model(model))
Ejemplo n.º 16
0
 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)))
Ejemplo n.º 17
0
 async def _send(self, message):
     if not isinstance(message, string_types):
         message = serialize_model(message)
     await self._ws.write_message(message)
Ejemplo n.º 18
0
 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))
Ejemplo n.º 19
0
def test_serialize_model_raises_error():
    with pytest.raises(ValueError):
        serialize_model(None)
Ejemplo n.º 20
0
def test_serialize_model():
    json_str = '{"available": 10}'
    sam = ParkingLotAvailableMessage(10)
    assert serialize_model(sam) == json_str
Ejemplo n.º 21
0
 def post(self):
     print(self.request.body.rstrip())
     self.write(serialize_model(ParkingLotCreationResponse(1)))