Пример #1
0
def test_init_with_session():

    session = aiohttp.ClientSession()

    api = AquaIPy(session=session)

    api.close()
Пример #2
0
async def test_AquaIPy_validate_connection_fail():

    api = AquaIPy()

    with pytest.raises(ConnError):
        await api.async_connect("invalid-host")

    with pytest.raises(ConnError):
        api._validate_connection()

    await api._session.close()
Пример #3
0
async def test_AquaIPy_firmware_error(device):

    api = AquaIPy()
    task = TestHelper.async_process_request(
        device, api.async_connect(TestHelper.get_hostname(device)),
        '/api/identity', TestData.identity_hydra26hd_unsupported_firmware())

    with pytest.raises(FirmwareError):
        await task

    await api._session.close()
Пример #4
0
async def test_AquaIPy_init_raises_ConnectionError_server_error(device):

    api = AquaIPy()
    task = TestHelper.async_process_request(
        device, api.async_connect(TestHelper.get_hostname(device)),
        '/api/identity', TestData.server_error())

    with pytest.raises(ConnError):
        await task

    await api._session.close()
Пример #5
0
async def test_AquaIPy_init_raises_requests_MustBeParentError(device):

    api = AquaIPy()
    task = TestHelper.async_process_request(
        device, api.async_connect(TestHelper.get_hostname(device)),
        '/api/identity', TestData.identity_not_parent())

    with pytest.raises(MustBeParentError):
        await task

    await api._session.close()
Пример #6
0
def test_init_with_no_asyncio_loop_running():

    #Close current loop
    loop = asyncio.get_event_loop()
    loop.stop()
    pending_tasks = asyncio.Task.all_tasks()
    loop.run_until_complete(asyncio.gather(*pending_tasks))
    loop.close()

    api = AquaIPy()

    api.close()

    #Add new loop again
    new_loop = asyncio.new_event_loop()
    asyncio.set_event_loop(new_loop)
Пример #7
0
async def test_AquaIPy_init_raises_requests_ConnectionError_bad_hostname():

    api = AquaIPy()
    with pytest.raises(ConnError):
        await api.async_connect("invalid-host")

    await api._session.close()
Пример #8
0
async def test_AquaIPy_init_raises_InvalidURL():

    api = AquaIPy()
    with pytest.raises(ConnError):
        await api.async_connect("")

    await api._session.close()
Пример #9
0
    async def async_get_connected_instance(
        mock_device,
        identity=TestData.identity_hydra26hd(),
        power=TestData.power_hydra26hd()):
        api = AquaIPy()
        task = asyncio.ensure_future(
            api.async_connect(TestHelper.get_hostname(mock_device)))

        request = await mock_device.receive_request()
        assert request.path_qs == '/api/identity'
        mock_device.send_response(request, data=identity)

        request2 = await mock_device.receive_request()
        assert request2.path_qs == '/api/power'
        mock_device.send_response(request2, data=power)

        await task
        assert api._base_path is not None

        return api
Пример #10
0
def ai_instance(bound_socket, server_process):

    host = get_hostname()
    _, port = bound_socket.getsockname()

    api = AquaIPy()

    # The app runner/process takes a while to startup, so wait for it.
    for i in range(0, 10):

        try:
            api.connect("{0}:{1}".format(host, port))
        except Exception as e:
            assertIsInstance(e, ConnError)

    api._validate_connection()

    yield api

    api.close()
Пример #11
0
def test_init_get_event_loop_error(mock_get):

    with pytest.raises(RuntimeError):
        api = AquaIPy()
Пример #12
0
def test_init_with_specified_loop():

    loop = asyncio.get_event_loop()

    api = AquaIPy(loop=loop)
Пример #13
0
async def test_AquaIPy_init_with_name():

    api = AquaIPy("Test Name")
    assert api.name == "Test Name"

    await api._session.close()
Пример #14
0
def ai_instance():

    ai_instance = AquaIPy()
    ai_instance.connect(ENDPOINT)

    #Store current light state
    sched_state = ai_instance.get_schedule_state()
    color_state = ai_instance.get_colors_brightness()

    yield ai_instance

    #Restore light state
    ai_instance.set_schedule_state(sched_state)
    ai_instance.set_colors_brightness(color_state)

    ai_instance.close()