Exemple #1
0
def test_batch_request_failed_changeset(service):
    """Check single response for changeset"""

    # pylint: disable=redefined-outer-name

    response_body = ('--batch_r1\n'
                     'Content-Type: application/http\n'
                     'Content-Transfer-Encoding: binary\n'
                     '\n'
                     'HTTP/1.1 400 Bad Request\n'
                     'Content-Type: application/json;charset=utf-8'
                     ''
                     '{"error": "this is error description"}'
                     '--batch_r1--')

    responses.add(
        responses.POST,
        '{0}/$batch'.format(URL_ROOT),
        body=response_body,
        content_type='multipart/mixed; boundary=batch_r1',
        status=202)

    batch = service.create_batch('batch1')

    chset = service.create_changeset('chset1')

    employee_request1 = service.entity_sets.Employees.get_entity(23)
    employee_request2 = service.entity_sets.Employees.get_entity(23)

    chset.add_request(employee_request1)
    chset.add_request(employee_request2)

    batch.add_request(chset)

    with pytest.raises(HttpError) as e_info:
        batch.execute()

    assert str(e_info.value).startswith('Changeset cannot be processed')
    assert isinstance(e_info.value, HttpError)
    assert e_info.value.response.status_code == 400
Exemple #2
0
def test_batch_request(service):
    """Batch requests"""

    # pylint: disable=redefined-outer-name

    response_body = (
        b'--batch_r1\n'
        b'Content-Type: application/http\n'
        b'Content-Transfer-Encoding: binary\n'
        b'\n'
        b'HTTP/1.1 200 OK\n'
        b'Content-Type: application/json\n'
        b'\n'
        b'{"d": {"ID": 23, "NameFirst": "Rob", "NameLast": "Ickes", "Address": { "ID": 456, "Street": "Baker Street", "City": "London"} }}'
        b'\n'
        b'--batch_r1\n'
        b'Content-Type: multipart/mixed; boundary=changeset_1\n'
        b'\n'
        b'--changeset_1\n'
        b'Content-Type: application/http\n'
        b'Content-Transfer-Encoding: binary\n'
        b'\n'
        b'HTTP/1.1 204 Updated\n'
        b'Content-Type: application/json\n'
        b'\n'
        b"{b'd': {'Sensor': 'Sensor-address', 'Date': datetime\'2017-12-24T18:00\', 'Value': 34}}"
        b'\n'
        b'--changeset_1--\n'
        b'\n'
        b'--batch_r1--')

    responses.add(responses.POST,
                  '{0}/$batch'.format(URL_ROOT),
                  body=response_body,
                  content_type='multipart/mixed; boundary=batch_r1',
                  status=202)

    batch = service.create_batch('batch1')

    chset = service.create_changeset('chset1')

    employee_request = service.entity_sets.Employees.get_entity(23)

    temp_request = service.entity_sets.TemperatureMeasurements.update_entity(
        Sensor='sensor1', Date=datetime.datetime(2017, 12, 24, 18,
                                                 0)).set(Value=34)

    batch.add_request(employee_request)

    chset.add_request(temp_request)

    batch.add_request(chset)

    response = batch.execute()

    assert len(response) == 2

    employee_response = response[0]
    assert isinstance(employee_response, pyodata.v2.service.EntityProxy)

    chset_response = response[1]
    assert isinstance(chset_response, list)
    assert len(chset_response) == 1
    assert chset_response[0] is None  # response to update request is None