Exemplo n.º 1
0
def test_not_batched(mocker):
    client = Client(URL)
    client.auth_data = {"token": "some-token"}

    mocker.patch.object(client, '_execute')

    # success without data
    client._execute.return_value = (True, [{
        "success": True,
        "data": 7
    }])
    assert client.emit_event(REQUEST['eventName'])
    client._execute.assert_called_with([REQUEST])

    # success with data
    client._execute.return_value = (True, [{
        "success": True,
    }])
    assert client.emit_event(REQUEST['eventName'], REQUEST_WITH_DATA['data'])
    client._execute.assert_called_with([REQUEST_WITH_DATA])

    # false response with data
    client._execute.return_value = (False, [{"success": False}])
    assert not client.emit_event(
        REQUEST['eventName'], REQUEST_WITH_DATA['data']
    )
    client._execute.assert_called_with([REQUEST_WITH_DATA])
def test_batched():
    client = Client(URL)
    client.auth_data = {"token": "some-token"}
    client.start_batch()
    client.get_record("recordName")
    client.make_rpc("rpcName")
    client.emit_event("eventName")

    assert client._batch == [{
        'action': 'read',
        'recordName': 'recordName',
        'topic': 'record'
    }, {
        'action': 'make',
        'data': None,
        'rpcName': 'rpcName',
        'topic': 'rpc'
    }, {
        'action': 'emit',
        'data': None,
        'eventName': 'eventName',
        'topic': 'event'
    }]

    with requests_mock.mock() as m:
        res = {
            'result':
            "SUCCESS",
            'body': [{
                "success": True,
                "data": "data"
            }, {
                "success": True
            }, {
                "success": True
            }]
        }
        m.post(URL, status_code=200, json=res)

        assert client.execute_batch() == res['body']