def test_1T1ENA_aggrPeriod(service, aggr_period, exp_index, ins_period):
    # Custom index to test aggrPeriod

    etype = f"test_1T1ENA_aggrPeriod_{aggr_period}"
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use a different entity type to store this test's rows in a
    # different table to avoid messing up global state---see also delete down
    # below.
    eid = "{}0".format(etype)

    for i in exp_index:
        base = dateutil.parser.isoparse(i)
        insert_test_data(service, [etype],
                         entity_id=eid,
                         index_size=3,
                         index_base=base,
                         index_period=ins_period)

    wait_for_insert([etype], service, 3 * len(exp_index))

    # aggrPeriod needs aggrMethod
    query_params = {
        'type': etype,
        'aggrPeriod': aggr_period,
    }
    h = {'Fiware-Service': service}

    r = requests.get(query_url(eid=eid), params=query_params, headers=h)
    assert r.status_code == 400, r.text

    # Check aggregation with aggrPeriod
    query_params = {
        'type': etype,
        'attrs': temperature + ',' + pressure,
        'aggrMethod': 'max',
        'aggrPeriod': aggr_period,
    }
    h = {'Fiware-Service': service}

    r = requests.get(query_url(eid=eid), params=query_params, headers=h)
    assert r.status_code == 200, r.text
    # Assert Results
    expected = {
        'entityId':
        eid,
        'entityType':
        etype,
        'index':
        exp_index,
        'attributes': [{
            'attrName': pressure,
            'values': [20., 20., 20.],
        }, {
            'attrName': temperature,
            'values': [2., 2., 2.],
        }]
    }
    obtained = r.json()
    assert_1T1ENA_response(obtained, expected)
    delete_test_data(service, [etype])
def test_no_type(service):
    """
    Specifying entity type is optional, provided that id is unique.
    """

    etype_1, etype_2 = 'test_no_type_RoomDevice', 'test_no_type_Car'
    etypes = [etype_1, etype_2]
    eid = "{}1".format(etype_1)
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use different entity types to store this test's rows in
    # different tables to avoid messing up global state---see also delete
    # down below.
    insert_test_data(service, etypes, n_entities=2, index_size=2)
    wait_for_insert(etypes, service, 2 * 2)

    h = {'Fiware-Service': service}

    # With type
    r = requests.get(query_url(eid=eid), params={'type': etype_1}, headers=h)
    assert r.status_code == 200, r.text
    res_with_type = r.json()

    # Without type
    r = requests.get(query_url(eid=eid), params={}, headers=h)
    assert r.status_code == 200, r.text
    res_without_type = r.json()

    assert res_with_type == res_without_type
    delete_test_data(service, etypes)
def check_time_index(service, input_index, expected_index=None):
    expected_index = expected_index or input_index

    entity_type, entity_id = 'Room', 'Room0'
    n0 = get_notification(entity_type, entity_id, 0, input_index[0])
    n1 = get_notification(entity_type, entity_id, 1, input_index[1])
    n2 = get_notification(entity_type, entity_id, 2, input_index[2])

    send_notifications(service='', notifications=[n0, n1, n2])
    wait_for_insert([entity_type], None, 3)

    # Query
    r = requests.get(query_1T1E1A(), params={'type': 'Room'})
    assert r.status_code == 200, r.text
    obtained = r.json()

    # Check Response
    expected = {
        'entityId': 'Room0',
        'entityType': 'Room',
        'attrName': 'temperature',
        'index': expected_index,
        'values': [0, 1, 2]
    }
    assert_1T1E1A_response(obtained, expected)
    delete_entity_type('', 'Room')
Example #4
0
def test_NTNENA_aggrPeriod(service, aggr_period, exp_index, ins_period):
    etype = 'test_NTNENA_aggrPeriod'
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use a different entity type to store this test's rows in a
    # different table to avoid messing up global state---see also delete down
    # below.
    eid = "{}0".format(etype)

    # Custom index to test aggrPeriod
    for i in exp_index:
        base = dateutil.parser.isoparse(i)
        insert_test_data(service, [etype],
                         index_size=5,
                         index_base=base,
                         index_period=ins_period)

    wait_for_insert([etype], service, 5 * len(exp_index))

    # aggrPeriod needs aggrMethod
    query_params = {
        'aggrPeriod': aggr_period,
    }
    r = query(params=query_params, service=service)
    assert r.status_code == 400, r.text

    # Check aggregation with aggrPeriod
    query_params = {
        'type': etype,
        'attrs': 'temperature',
        'aggrMethod': 'sum',
        'aggrPeriod': aggr_period,
    }
    r = query(params=query_params, service=service)
    # Assert
    assert r.status_code == 200, r.text
    obtained = r.json()

    delete_test_data(service, [etype])

    expected_temperatures = 0 + 1 + 2 + 3 + 4
    expected_entities = [
        {
            'entityId':
            eid,
            'index':
            exp_index,
            'values': [
                expected_temperatures, expected_temperatures,
                expected_temperatures
            ]
        },
    ]
    expected_types = [{'entities': expected_entities, 'entityType': etype}]
    expected_attrs = [{'attrName': 'temperature', 'types': expected_types}]

    expected = {'attrs': expected_attrs}

    assert obtained == expected
Example #5
0
def insert_data(notification: dict, headers: dict):
    res_post = requests.post('{}'.format(notify_url),
                             data=json.dumps(notification),
                             headers=headers)

    wait_for_insert([ENTITY_TYPE], SERVICE, len(notification['data']))

    assert res_post.status_code == 200
    assert res_post.json().startswith('Notification successfully processed')
Example #6
0
def insert_data(notification: dict, http_headers: dict, service: str):
    etype = type_of(notification['data'][0])
    res_post = requests.post('{}'.format(notify_url),
                             data=json.dumps(notification),
                             headers=http_headers)
    assert res_post.status_code == 200
    assert res_post.json().startswith('Notification successfully processed')

    wait_for_insert([etype], service, len(notification['data']))
def reporter_dataset():
    for service in services:
        insert_test_data(service, [entity_type], n_entities=1, index_size=30,
                         entity_id=entity_id)
        insert_test_data(service, [entity_type_1], n_entities=1, index_size=30,
                         entity_id=entity_id_1)
        wait_for_insert([entity_type, entity_type_1], service, 30)
    yield
    for service in services:
        delete_test_data(service, [entity_type, entity_type_1])
def reporter_dataset():
    for service in services:
        insert_test_data(service, [entity_type],
                         n_entities=3,
                         entity_id=entity_id,
                         index_size=n_days)
        wait_for_insert([entity_type], service, 3)
    yield
    for service in services:
        delete_test_data(service, [entity_type])
def setup_entities():
    notification_data = [{'data': [e]} for e in [entity_1, entity_2]]
    for service in services:
        send_notifications(service, notification_data)
        wait_for_insert([entity_type], service, 2)

    yield {}

    for service in services:
        delete_entity_type(service, entity_type)
Example #10
0
def test_different_time_indexes(service):
    """
    Each entity should have its time_index array.
    """
    etype = 'test_different_time_indexes'
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use a different entity type to store this test's rows in a
    # different table to avoid messing up global state---see also delete down
    # below.
    insert_test_data(service, [etype], entity_id='Room1', index_size=2)
    insert_test_data(service, [etype], entity_id='Room3', index_size=4)
    insert_test_data(service, [etype], entity_id='Room2', index_size=3)

    wait_for_insert([etype], service, 2 + 4 + 3)

    query_params = {
        'type': etype,
        'id': 'Room3,Room1,Room2',
    }
    h = {'Fiware-Service': service}

    r = requests.get(query_url(etype=etype), params=query_params, headers=h)
    assert r.status_code == 200, r.text

    expected_entities = [{
        'entityId':
        'Room3',
        'index':
        ['1970-01-{:02}T00:00:00+00:00'.format(i + 1) for i in range(4)],
        'values':
        list(range(4)),
    }, {
        'entityId':
        'Room1',
        'index':
        ['1970-01-{:02}T00:00:00+00:00'.format(i + 1) for i in range(2)],
        'values':
        list(range(2)),
    }, {
        'entityId':
        'Room2',
        'index':
        ['1970-01-{:02}T00:00:00+00:00'.format(i + 1) for i in range(3)],
        'values':
        list(range(3)),
    }]

    expected = {
        'entityType': etype,
        'attrName': attr_name,
        'entities': expected_entities
    }
    obtained = r.json()
    assert_1TNE1A_response(obtained, expected, etype=etype)
    delete_test_data(service, [etype])
Example #11
0
def test_1TNE1A_aggrPeriod(service, aggr_period, exp_index, ins_period):
    # Custom index to test aggrPeriod
    etype = f"test_1TNE1A_aggrPeriod_{aggr_period}"
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use a different entity type to store this test's rows in a
    # different table to avoid messing up global state---see also delete down
    # below.
    eid = '{}0'.format(etype)

    for i in exp_index:
        base = dateutil.parser.isoparse(i)
        insert_test_data(service,
                         [etype],
                         entity_id=eid,
                         index_size=5,
                         index_base=base,
                         index_period=ins_period)

    wait_for_insert([etype], service, 5 * len(exp_index))

    # aggrPeriod needs aggrMethod
    query_params = {
        'type': etype,
        'aggrPeriod': aggr_period,
    }
    h = {'Fiware-Service': service}

    r = requests.get(query_url(etype=etype), params=query_params, headers=h)
    assert r.status_code == 400, r.text

    # Check aggregation with aggrPeriod
    query_params = {
        'type': etype,
        'aggrMethod': 'sum',
        'aggrPeriod': aggr_period,
    }
    r = requests.get(query_url(etype=etype), params=query_params, headers=h)
    assert r.status_code == 200, r.text

    # Assert Results
    exp_sum = 0 + 1 + 2 + 3 + 4
    expected_entities = [
        {
            'entityId': eid,
            'index': exp_index,
            'values': [exp_sum, exp_sum, exp_sum],
        }
    ]
    obtained_data = r.json()
    assert isinstance(obtained_data, dict)
    assert obtained_data['entityType'] == etype
    assert obtained_data['attrName'] == attr_name
    assert obtained_data['entities'] == expected_entities
    delete_test_data(service, [etype])
def reporter_dataset_different_attribute_types():
    for service in services:
        insert_test_data_different_types(service, [entity_type],
                                         n_entities=1,
                                         index_size=4,
                                         entity_id=entity_id)
        wait_for_insert([entity_type], service, 4)

    yield
    for service in services:
        delete_test_data(service, [entity_type])
def reporter_dataset():
    entity_type = result_gen.formatter.entity_type
    sz = result_gen.time_index_size
    for service in services:
        insert_test_data(service, [entity_type], n_entities=1,
                         index_size=sz, entity_id=entity_id_1)
        insert_test_data(service, [entity_type], n_entities=1,
                         index_size=sz, entity_id=entity_id_2)
        wait_for_insert([entity_type], service, sz * 2)
    yield
    for service in services:
        delete_test_data(service, [entity_type])
def run_test(service, entity_type, attr_name):
    entity = mk_entity('d1', entity_type, attr_name)

    insert_entity(service, entity)
    wait_for_insert([entity_type], service, 1)

    query_result = query_entity(service, entity['id'], attr_name)
    query_result.pop('index', None)
    assert query_result == {
        'attrName': attr_name,
        'entityId': entity['id'],
        'entityType': entity_type,
        'values': [entity[attr_name]['value']]
    }

    delete_entity_type(service, entity_type)
Example #15
0
def test_none_service():
    service = None
    service_path = None
    alt_service_path = '/notdefault'
    insert_test_data(
        service,
        [entity_type],
        n_entities=1,
        index_size=30,
        service_path=service_path)
    insert_test_data(
        service,
        [entity_type],
        n_entities=1,
        index_size=15,
        service_path=alt_service_path)

    wait_for_insert([entity_type], service, 30 + 15)

    body = {
        'entities': [
            {
                'type': entity_type,
                'id': entity_id
            }
        ],
        'attrs': [
            'temperature',
            'pressure'
        ]
    }

    r = requests.post('{}'.format(query_url),
                      data=json.dumps(body),
                      headers=headers(service, service_path))
    assert r.status_code == 200, r.text
    assert r.json()[0]['temperature']['value'] == 29
    assert len(r.json()) == 1
    r = requests.post('{}'.format(query_url),
                      data=json.dumps(body),
                      headers=headers(service, alt_service_path))
    assert r.status_code == 200, r.text
    assert r.json()[0]['temperature']['value'] == 14
    assert len(r.json()) == 1
    delete_test_data(service, [entity_type], service_path=service_path)
    delete_test_data(service, [entity_type], service_path=alt_service_path)
def insert_test_data(service, service_path='/', entity_id=None):
    etypes = ['AirQualityObserved', 'Room', 'TrafficFlowObserved']
    # 3 entity types, 2 entities for each, 10 updates for each entity.
    for t in etypes:
        for e in range(2):
            for u in range(10):
                ei = entity_id or '{}{}'.format(t, e)
                notification = create_notification(t, ei)
                data = json.dumps(notification)
                h = {
                    'Content-Type': 'application/json',
                    'Fiware-Service': service
                }
                if service_path:
                    h['Fiware-ServicePath'] = service_path
                r = requests.post(notify_url, data=data, headers=h)
                assert r.status_code == 200, r.text

    wait_for_insert(etypes, service, 2 * 10)
def test_no_type_not_unique(service):
    # If id is not unique across types, you must specify type.

    etype_1, etype_2 = 'test_no_type_not_unique_RoomDevice', \
                       'test_no_type_not_unique_Car'
    etypes = [etype_1, etype_2]
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use different entity types to store this test's rows in
    # different tables to avoid messing up global state---see also delete
    # down below.
    shared_entity_id = "sharedId"

    insert_test_data(service,
                     etypes,
                     n_entities=2,
                     index_size=2,
                     entity_id=shared_entity_id)
    wait_for_insert(etypes, service, 2 * 2)

    url = "{qlUrl}/entities/{entityId}/attrs/temperature".format(
        qlUrl=QL_URL,
        entityId=shared_entity_id,
    )

    h = {'Fiware-Service': service}

    # With type
    r = requests.get(url, params={'type': etype_1}, headers=h)
    assert r.status_code == 200, r.text

    # Without type
    r = requests.get(url, params={}, headers=h)
    assert r.status_code == 400, r.text
    e = AmbiguousNGSIIdError(shared_entity_id)
    assert r.json() == {
        "error": "{}".format(type(e)),
        "description": str(e)
    }
    delete_test_data(service, etypes)
def insert_entities(service):
    entities = mk_entities()
    notification_data = [{'data': entities}]
    send_notifications(service, notification_data)
    wait_for_insert([entity_type], service, len(entities))
def test_NTNE1A_aggrPeriod(service, aggr_period, exp_index, ins_period):
    # Custom index to test aggrPeriod
    entity_type = 'test_NTNE1A_aggrPeriod'
    # The reporter_dataset fixture is still in the DB cos it has a scope of
    # module. We use a different entity type to store this test's rows in a
    # different table to avoid messing up global state---see also delete down
    # below.
    entity_id = "{}0".format(entity_type)
    attr_name = result_gen.attr_name()

    for i in exp_index:
        base = dateutil.parser.isoparse(i)
        insert_test_data(service,
                         [entity_type],
                         entity_id=entity_id,
                         index_size=5,
                         index_base=base,
                         index_period=ins_period)

    wait_for_insert([entity_type], service, 5 * len(exp_index))

    # aggrPeriod needs aggrMethod
    query_params = {
        'aggrPeriod': aggr_period,
    }
    h = {'Fiware-Service': service}
    r = requests.get(query_url(), params=query_params, headers=h)
    assert r.status_code == 400, r.text

    # Check aggregation with aggrPeriod
    query_params = {
        'type': entity_type,  # avoid picking up temperatures of entities
                              # in reporter_dataset fixture.
        'attrs': attr_name,
        'aggrMethod': 'sum',
        'aggrPeriod': aggr_period,
    }

    r = requests.get(query_url(), params=query_params, headers=h)

    delete_test_data(service, [entity_type])

    assert r.status_code == 200, r.text
    expected_temperatures = 0 + 1 + 2 + 3 + 4
    # Assert
    obtained = r.json()
    expected_entities = [
        {
            'entityId': entity_id,
            'index': exp_index,
            'values': [expected_temperatures, expected_temperatures,
                       expected_temperatures]
        }
    ]
    expected_types = [
        {
            'entities': expected_entities,
            'entityType': entity_type
        }
    ]
    expected = {
        'attrName': attr_name,
        'types': expected_types,
    }

    assert obtained == expected