def test_json_ld_date_observed(service, notification): # example json-ld entity with custom index property notification['data'][0] = { "id": "urn:ngsi-ld:Device:filling001", "type": "FillingSensor", "dateObserved": { "type": "Property", "value": { "@type": "DateTime", "@value": "2018-08-07T12:00:00Z" } }, "filling": { "type": "Property", "value": 0.94, "unitCode": "C62", "observedAt": "2021-01-28T12:33:20.000Z", "modifiedAt": "2021-01-28T12:33:22.000Z" } } get_url = "{}/entities/urn:ngsi-ld:Device:filling001".format(QL_URL) url_new = '{}'.format(get_url) h = notify_header(service) h['Fiware-TimeIndex-Attribute'] = 'dateObserved' insert_data(notification, h, service) res_get = requests.get(url_new, headers=query_header(service)) assert res_get.status_code == 200 expected_value = [ notification['data'][0]['dateObserved']['value']['@value'] ] assert_equal_time_index_arrays([res_get.json()['index'][0]], expected_value) delete_entity_type(service, notification['data'][0]['type'])
def test_json_ld_date_modified_at_attribute(service, notification): # example json-ld entity with modifiedAt as attribute notification['data'][0] = { "id": "urn:ngsi-ld:Device:filling001", "type": "FillingSensor", "modifiedAt": "2021-01-28T12:33:22.000Z", "filling": { "type": "Property", "value": 0.94, "unitCode": "C62" } } url = '{}'.format(notify_url) get_url = "{}/entities/urn:ngsi-ld:Device:filling001".format(QL_URL) url_new = '{}'.format(get_url) r = requests.post(url, data=json.dumps(notification), headers=notify_header(service)) assert r.status_code == 200 # Give time for notification to be processed time.sleep(SLEEP_TIME) res_get = requests.get(url_new, headers=query_header(service)) assert res_get.status_code == 200 expected_value = [notification['data'][0]['modifiedAt']] assert_equal_time_index_arrays([res_get.json()['index'][0]], expected_value) delete_entity_type(service, notification['data'][0]['type'])
def test_json_ld_both_at_meta(service, notification): # example json-ld entity with modifiedAt and observedAt as metadata notification['data'][0] = { "id": "urn:ngsi-ld:Device:filling001", "type": "FillingSensor", "modifiedAt": "2021-01-28T12:33:22.000Z", "filling": { "type": "Property", "value": 0.94, "unitCode": "C62", "observedAt": "2021-01-28T12:33:20.000Z", "modifiedAt": "2021-01-28T12:33:22.000Z" } } url = '{}'.format(notify_url) get_url = "{}/entities/urn:ngsi-ld:Device:filling001".format(QL_URL) url_new = '{}'.format(get_url) insert_data(notification, notify_header(service), service) res_get = requests.get(url_new, headers=query_header(service)) assert res_get.status_code == 200 expected_value = [notification['data'][0]['filling']['observedAt']] assert_equal_time_index_arrays([res_get.json()['index'][0]], expected_value) delete_entity_type(service, notification['data'][0]['type'])
def assert_1T1ENA_response(obtained, expected): """ Check API responses for 1T1ENA """ # Assert time index obt_index = obtained.pop('index') exp_index = expected.pop('index') assert_equal_time_index_arrays(obt_index, exp_index) # Assert rest of data assert obtained == expected
def test_time_index(service): etype = 'test_time_index' # avoid interfering w/ other tests notification = create_notification(entity_type=etype) # If present, use entity-level dateModified as time_index global_modified = datetime(2000, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() modified = {'type': 'DateTime', 'value': global_modified} notification['data'][0]['dateModified'] = modified insert_data(notification, notify_header(service), service) entities_url = "{}/entities".format(QL_URL) r = requests.get(entities_url, params=None, headers=query_header(service)) entities = find_by_type(etype, r.json()) # ^ i.e. don't assume there's no other data in the DB! # some tests don't delete their data to speed up the test run. assert len(entities) == 1 assert_equal_time_index_arrays([entities[0]['index']], [global_modified]) # If not, use newest of changes notification['data'][0].pop('dateModified') temp = notification['data'][0]['temperature'] notification['data'][0]['pressure'] = copy.deepcopy(temp) older = datetime(2001, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() newer = datetime(2002, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() e = notification['data'][0] e['temperature']['metadata']['dateModified']['value'] = older e['pressure']['metadata']['dateModified']['value'] = newer insert_data(notification, notify_header(service), service) time.sleep(SLEEP_TIME) # still needed b/c of entity update w/ new attr r = requests.get(entities_url, params=None, headers=query_header(service)) entities = find_by_type(etype, r.json()) assert len(entities) == 1 obtained = [entities[0]['index']] assert_equal_time_index_arrays(obtained, [global_modified, newer]) # Otherwise, use current time. current = datetime.now() notification['data'][0]['pressure'].pop('metadata') notification['data'][0]['temperature'].pop('metadata') insert_data(notification, notify_header(service), service) time.sleep(SLEEP_TIME) # still needed b/c of entity update w/ new attr r = requests.get(entities_url, params=None, headers=query_header(service)) entities = find_by_type(etype, r.json()) assert len(entities) == 1 obtained = [entities[0]['index']] assert obtained[-1].startswith("{}".format(current.year)) delete_entity_type(service, etype)
def assert_1TNENA_response(obtained, expected, etype=entity_type, values_only=False): """ Check API responses for 1TNENA """ assert isinstance(obtained, dict) if not values_only: assert obtained['entityType'] == etype obt_entities_index = obtained['entities'][0]['index'] exp_entities_index = expected['entities'][0]['index'] else: obt_entities_index = obtained['values'][0]['index'] exp_entities_index = expected['values'][0]['index'] assert_equal_time_index_arrays(obt_entities_index, exp_entities_index) assert obtained == expected
def assert_1TNE1A_response(obtained, expected, etype=entity_type, values_only=False): """ Check API responses for 1TNE1A """ assert isinstance(obtained, dict) if not values_only: assert obtained['entityType'] == etype assert obtained['attrName'] == attr_name obt_entities = obtained['entities'] exp_entities = expected['entities'] else: obt_entities = obtained['values'] exp_entities = expected['values'] for oe, ee in zip(obt_entities, exp_entities): obt_index = oe.pop('index') exp_index = ee.pop('index') assert_equal_time_index_arrays(obt_index, exp_index) assert obtained == expected
def test_time_index(service, notification): # If present, use entity-level dateModified as time_index global_modified = datetime(2000, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() modified = {'type': 'DateTime', 'value': global_modified} notification['data'][0]['dateModified'] = modified r = requests.post('{}'.format(notify_url), data=json.dumps(notification), headers=notify_header(service)) assert r.status_code == 200 assert r.json().startswith('Notification successfully processed') time.sleep(SLEEP_TIME) entity_type = notification['data'][0]['type'] entities_url = "{}/entities".format(QL_URL) time.sleep(SLEEP_TIME) r = requests.get(entities_url, params=None, headers=query_header(service)) entities = r.json() assert len(entities) == 1 assert_equal_time_index_arrays([entities[0]['index']], [global_modified]) # If not, use newest of changes notification['data'][0].pop('dateModified') temp = notification['data'][0]['temperature'] notification['data'][0]['pressure'] = copy.deepcopy(temp) older = datetime(2001, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() newer = datetime(2002, 1, 2, 0, 0, 0, 0, timezone.utc).isoformat() e = notification['data'][0] e['temperature']['metadata']['dateModified']['value'] = older e['pressure']['metadata']['dateModified']['value'] = newer r = requests.post('{}'.format(notify_url), data=json.dumps(notification), headers=notify_header(service)) assert r.status_code == 200 assert r.json().startswith('Notification successfully processed') time.sleep(SLEEP_TIME) r = requests.get(entities_url, params=None, headers=query_header(service)) entities = r.json() assert len(entities) == 1 obtained = [entities[0]['index']] assert_equal_time_index_arrays(obtained, [global_modified, newer]) # Otherwise, use current time. current = datetime.now() notification['data'][0]['pressure'].pop('metadata') notification['data'][0]['temperature'].pop('metadata') r = requests.post('{}'.format(notify_url), data=json.dumps(notification), headers=notify_header(service)) assert r.status_code == 200 assert r.json().startswith('Notification successfully processed') time.sleep(SLEEP_TIME) r = requests.get(entities_url, params=None, headers=query_header(service)) entities = r.json() assert len(entities) == 1 obtained = [entities[0]['index']] assert obtained[-1].startswith("{}".format(current.year)) delete_entity_type(service, notification['data'][0]['type'])