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 test_delete_entity(service): """ By default, delete all records of some entity. """ insert_test_data(service) entity_type = "AirQualityObserved" params = { 'type': entity_type, } h = {'Fiware-Service': service} url = '{}/entities/{}'.format(QL_URL, entity_type + '0') # Values are there r = requests.get(url, params=params, headers=h) assert r.status_code == 200, r.text assert r.text != '' # Delete them r = requests.delete(url, params=params, headers=h) assert r.status_code == 204, r.text # Values are gone time.sleep(1) r = requests.get(url, params=params, headers=h) assert r.status_code == 404, r.text # But not for other entities of same type url = '{}/entities/{}'.format(QL_URL, entity_type + '1') r = requests.get(url, params=params, headers=h) assert r.status_code == 200, r.text assert r.text != '' for t in ("AirQualityObserved", "Room", "TrafficFlowObserved"): delete_test_data(service, [t])
def reporter_dataset(): for service in services: insert_test_data(service, [entity_type], n_entities=1, index_size=30) time.sleep(SLEEP_TIME) yield for service in services: delete_test_data(service, [entity_type])
def reporter_dataset(): for service in services: insert_test_data(service, [entity_type], n_entities=3, index_size=n_days) yield for service in services: delete_test_data(service, [entity_type])
def test_no_type_not_unique(service): # If id is not unique across types, you must specify type. etype_1, etype_2 = 'RoomDevice', '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) 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 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 manage_db_entities(): for service in services: insert_entities(service) yield for service in services: delete_test_data(service, [entity_type])
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
def manage_db_entities(): for service in services: insert_entities(service) time.sleep(2 * SLEEP_TIME) yield for service in services: delete_test_data(service, [entity_type])
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) 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 reporter_dataset(): service = '' entity_type = result_gen.formatter.entity_type sz = result_gen.time_index_size 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) yield delete_test_data(service, [entity_type])
def reporter_dataset(): for service in services: insert_test_data(service, [entity_type], n_entities=1, index_size=30) for service in services: wait_for_insert([entity_type], service, 30) yield for service in services: delete_test_data(service, [entity_type])
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])
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 reporter_dataset(): for service in services: insert_test_data(service, [entity_type], n_entities=1, index_size=4, entity_id=entity_id) insert_test_data(service, [entity_type], n_entities=1, index_size=4, entity_id=entity_id_1) insert_test_data(service, [entity_type_1], entity_id=entity_id_1_1, index_size=3) wait_for_insert([entity_type], service, 4 * 2) wait_for_insert([entity_type_1], service, 3) yield for service in services: delete_test_data(service, [entity_type, entity_type_1])
def reporter_dataset(): service = '' 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, index_base=datetime(1980, 1, 1, 0, 0, 0, 0)) yield delete_test_data(service, [entity_type, entity_type_1])
def test_1T1E1A_aggrPeriod(service, aggr_period, exp_index, ins_period): etype = 'test_1T1E1A_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], entity_id=eid, index_size=4, index_base=base, index_period=ins_period) # 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, 'aggrMethod': 'avg', 'aggrPeriod': aggr_period, } r = requests.get(query_url(eid=eid), params=query_params, headers=h) delete_test_data(service, [etype]) assert r.status_code == 200, r.text # Assert Results obtained = r.json() exp_avg = (0 + 1 + 2 + 3) / 4. expected = { 'entityId': eid, 'entityType': etype, 'attrName': attr_name, 'index': exp_index, 'values': [exp_avg, exp_avg, exp_avg] } assert_1T1E1A_response(obtained, expected)
def reporter_dataset(): insert_test_data(default_service, [entity_type], n_entities=1, index_size=4, entity_id=entity_id) insert_test_data(default_service, [entity_type], n_entities=1, index_size=4, entity_id=entity_id_1) insert_test_data(service_1, [entity_type], entity_id=entity_id, index_size=3) insert_test_data(service_1, [entity_type_1], entity_id=entity_id_1_1, index_size=3) yield delete_test_data(default_service, [entity_type]) delete_test_data(service_1, [entity_type, entity_type_1])
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 test_no_type_not_unique(service): # If id is not unique across types, you must specify type. insert_test_data(service, entity_id='repeatedId') url = '{}/entities/{}'.format(QL_URL, 'repeatedId') h = {'Fiware-Service': service} # Without type r = requests.delete(url, params={}, headers=h) assert r.status_code == 409, r.text assert r.json() == { "error": "AmbiguousNGSIIdError", "description": str(AmbiguousNGSIIdError('repeatedId')) } # With type r = requests.delete(url, params={'type': 'AirQualityObserved'}, headers=h) assert r.status_code == 204, r.text for t in ("AirQualityObserved", "Room", "TrafficFlowObserved"): delete_test_data(service, [t])
def test_integration_with_orion(clean_mongo, service, entity): """ Make sure QL correctly handles headers in Orion's notification """ h = { 'Content-Type': 'application/json', 'Fiware-Service': service, 'Fiware-ServicePath': '/', } # Subscribe QL to Orion params = { 'orionUrl': ORION_URL_4QL, 'quantumleapUrl': QL_URL_4ORION, } r = requests.post(subscribe_url, params=params, headers=h) assert r.status_code == 201 # Insert values in Orion with Service and ServicePath data = json.dumps(entity) r = requests.post('{}/entities'.format(ORION_URL), data=data, headers=h) assert r.ok # Wait notification to be processed time.sleep(2) # Query WITH headers url = "{qlUrl}/entities/{entityId}".format( qlUrl=QL_URL, entityId=entity['id'], ) query_params = { 'type': entity['type'], } r = requests.get('{}'.format(url), params=query_params, headers=h) assert r.status_code == 200, r.text obtained = r.json() assert obtained['entityId'] == entity['id'] # Query WITHOUT headers r = requests.get(url, params=query_params) assert r.status_code == 404, r.text delete_test_data(service, ["Room"])
def test_delete_entities(service): """ By default, delete all historical records of all entities of some type. """ entity_type = "TrafficFlowObserved" params = { 'type': entity_type, } h = { 'Fiware-Service': service } insert_test_data(service) # Values are there for both entities for e in range(2): url = '{}/entities/{}'.format(QL_URL, '{}{}'.format(entity_type, e)) r = requests.get(url, params=params, headers=h) assert r.status_code == 200, r.text assert r.text != '' # 1 Delete call url = '{}/types/{}'.format(QL_URL, entity_type) r = requests.delete(url, params=params, headers=h) assert r.status_code == 204, r.text wait_for_delete(entity_type, service) # Values are gone for both entities for e in range(2): url = '{}/entities/{}'.format(QL_URL, '{}{}'.format(entity_type, e)) r = requests.get(url, params=params, headers=h) assert r.status_code == 404, r.text # But not for entities of other types url = '{}/entities/{}'.format(QL_URL, 'Room1') r = requests.get(url, params={'type': 'Room'}, headers=h) assert r.status_code == 200, r.text assert r.text != '' for t in ("AirQualityObserved", "Room"): delete_test_data(service, [t])
def test_default_service_path(service): service_path = '/' 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) time.sleep(SLEEP_TIME) 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 len(r.json()) == 1 assert r.json()[0]['temperature']['value'] == 29 r = requests.post('{}'.format(query_url), data=json.dumps(body), headers=headers(service, alt_service_path)) assert r.status_code == 200, r.text assert len(r.json()) == 1 assert r.json()[0]['temperature']['value'] == 14 delete_test_data(service, [entity_type], service_path=service_path) delete_test_data(service, [entity_type], service_path=alt_service_path)
def test_delete_no_type_with_multitenancy(service): """ A Car and a Truck with the same entity_id. Same thing in two different tenants (USA an EU). """ # You have a car1 car = json.dumps(create_notification("Car", "car1")) # In Default h_def = { 'Content-Type': 'application/json', 'Fiware-Service': service } r = requests.post(notify_url, data=car, headers=h_def) assert r.status_code == 200 # In EU h_eu = { 'Content-Type': 'application/json', 'Fiware-Service': 'EU' } r = requests.post(notify_url, data=car, headers=h_eu) assert r.status_code == 200 # In USA h_usa = { 'Content-Type': 'application/json', 'Fiware-Service': 'USA' } r = requests.post(notify_url, data=car, headers=h_usa) assert r.status_code == 200 time.sleep(SLEEP_TIME) # I could delete car1 from default without giving a type url = '{}/entities/{}'.format(QL_URL, 'car1') r = requests.delete(url, params={}, headers=h_def) assert r.status_code == 204, r.text # But it should still be in EU. r = requests.get(url, params={}, headers=h_eu) assert r.status_code == 200, r.text time.sleep(SLEEP_TIME) # I could delete car1 from EU without giving a type url = '{}/entities/{}'.format(QL_URL, 'car1') r = requests.delete(url, params={}, headers=h_eu) assert r.status_code == 204, r.text # But it should still be in USA. r = requests.get(url, params={}, headers=h_usa) assert r.status_code == 200, r.text delete_test_data(service, ["Car"]) delete_test_data('USA', ["Car"]) delete_test_data('EU', ["Car"])
def test_aggregation_is_per_instance(service): """ Attribute Aggregation works by default on a per-instance basis. Cross-instance aggregation not yet supported. It would change the shape of the response. """ etype = 'test_aggregation_is_per_instance' # 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='Room0', index_size=3) insert_test_data(service, [etype], entity_id='Room1', index_size=9) query_params = { 'type': etype, 'id': 'Room0,Room1', 'aggrMethod': 'sum' } h = {'Fiware-Service': service} r = requests.get(query_url(etype=etype), params=query_params, headers=h) assert r.status_code == 200, r.text # Assert Results expected_entities = [ { 'entityId': 'Room0', 'index': ['', ''], 'values': [sum(range(3))], }, { 'entityId': 'Room1', 'index': ['', ''], 'values': [sum(range(9))], } ] 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 # Index array in the response is the used fromDate and toDate query_params = { 'type': etype, 'id': 'Room0,Room1', 'aggrMethod': 'max', 'fromDate': datetime(1970, 1, 1, 0, 0, 0, 0, timezone.utc).isoformat(), 'toDate': datetime(1970, 1, 6, 0, 0, 0, 0, timezone.utc).isoformat(), } r = requests.get(query_url(etype=etype), params=query_params, headers=h) assert r.status_code == 200, r.text # Assert Results expected_entities = [{'entityId': 'Room0', 'index': ['1970-01-01T00:00:00+00:00', '1970-01-06T00:00:00+00:00'], 'values': [2], }, {'entityId': 'Room1', 'index': ['1970-01-01T00:00:00+00:00', '1970-01-06T00:00:00+00:00'], 'values': [5], }] 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 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