Skip to content

Commit 4c38531

Browse files
committed
split in code + test
1 parent 67fa728 commit 4c38531

File tree

5 files changed

+125
-111
lines changed

5 files changed

+125
-111
lines changed

src/conftest.py

+59-8
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ def insert(self, entity, service=None, service_path=None):
6868
return r
6969

7070
def update_attr(self, entity_id, attrs, service=None, service_path=None):
71-
r = requests.patch('{}/v2/entities/{}/attrs'.format(self.url, entity_id),
72-
data=json.dumps(attrs),
73-
headers=headers(service, service_path))
71+
r = requests.patch(
72+
'{}/v2/entities/{}/attrs'.format(self.url, entity_id),
73+
data=json.dumps(attrs),
74+
headers=headers(service, service_path))
7475
return r
7576

7677
def delete(self, entity_id, service=None, service_path=None):
@@ -173,7 +174,8 @@ def delete_entities(self, entity_type=None, fiware_service=None,
173174

174175
def entity_types(self, fiware_service=None, **kwargs):
175176
r = CrateTranslator.query_entity_types(
176-
self, entity_type=None, fiware_service=fiware_service, **kwargs)
177+
self, entity_type=None, fiware_service=fiware_service,
178+
**kwargs)
177179
try:
178180
self._refresh(r, fiware_service=fiware_service)
179181
except exceptions.ProgrammingError:
@@ -193,7 +195,9 @@ def clean(self, fiware_service=None, **kwargs):
193195
except exceptions.ProgrammingError:
194196
pass
195197

196-
with Translator(CrateConnectionData(host=crate_host, port=crate_port, db_user=crate_db_username, db_pass=crate_db_password)) as trans:
198+
with Translator(CrateConnectionData(host=crate_host, port=crate_port,
199+
db_user=crate_db_username,
200+
db_pass=crate_db_password)) as trans:
197201
yield trans
198202
trans.dispose_connection()
199203

@@ -226,7 +230,8 @@ def delete_entities(self, entity_type=None, fiware_service=None,
226230

227231
def entity_types(self, fiware_service=None, **kwargs):
228232
r = PostgresTranslator.query_entity_types(
229-
self, entity_type=None, fiware_service=fiware_service, **kwargs)
233+
self, entity_type=None, fiware_service=fiware_service,
234+
**kwargs)
230235
return r
231236

232237
def clean(self, fiware_service=None, **kwargs):
@@ -527,7 +532,10 @@ def ngsi_ld():
527532
"type": "GeoProperty",
528533
"value": {
529534
"type": "Point",
530-
"coordinates": [-3.164485591715449, 40.62785133667262]
535+
"coordinates": [
536+
-3.164485591715449,
537+
40.62785133667262
538+
]
531539
}
532540
},
533541
"areaServed": {
@@ -574,9 +582,52 @@ def ngsi_ld():
574582
}
575583
},
576584
"@context": [
577-
"https://schema.lab.fiware.org/ld/context",
585+
"https://smartdatamodels.org/context.jsonld",
578586
"https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
579587
]
580588
}
581589

582590
return entity
591+
592+
593+
@pytest.fixture
594+
def ngsi_ld_partially_expanded():
595+
"""
596+
:return: dict
597+
The NGSI LD model as received within an Orion notification.
598+
"""
599+
entity = {
600+
"@context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.3.jsonld",
601+
"id": "urn:ngsi-ld:Building:farm001",
602+
"type": "https://uri.fiware.org/ns/data-models#Building",
603+
"https://schema.org/address": {
604+
"type": "Property",
605+
"value": {
606+
"streetAddress": "Großer Stern 1",
607+
"addressRegion": "Berlin",
608+
"addressLocality": "Tiergarten",
609+
"postalCode": "10557"
610+
},
611+
"verified": {
612+
"type": "Property",
613+
"value": True
614+
}
615+
},
616+
"name": {
617+
"type": "Property",
618+
"value": "Victory Farm"
619+
},
620+
"https://uri.fiware.org/ns/data-models#category": {
621+
"type": "Property",
622+
"value": "farm"
623+
},
624+
"location": {
625+
"type": "GeoProperty",
626+
"value": {
627+
"type": "Point",
628+
"coordinates": [13.3505, 52.5144]
629+
}
630+
}
631+
}
632+
633+
return entity

src/ld/test.py

-101
This file was deleted.

src/ld/tests/test_ld.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from ld.utils import get_context, compute_attribute_mappings
2+
from ld.cached_aiohttp import cached_aiohttp_document_loader
3+
from pyld import jsonld
4+
5+
6+
def test_ld_compacted(ngsi_ld):
7+
jsonld.set_document_loader(cached_aiohttp_document_loader(timeout=30))
8+
context = get_context(ngsi_ld)
9+
assert context == ['https://smartdatamodels.org/context.jsonld', 'https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld']
10+
mappings = compute_attribute_mappings(ngsi_ld, context)
11+
assert mappings['location'] == 'https://uri.etsi.org/ngsi-ld/location'
12+
assert mappings['controllingMethod'] == 'https://smart-data-models.github.io/data-models/terms.jsonld#/definitions/controllingMethod'
13+
14+
15+
def test_ld_partially_expanded(ngsi_ld_partially_expanded):
16+
jsonld.set_document_loader(cached_aiohttp_document_loader(timeout=30))
17+
context = get_context(ngsi_ld_partially_expanded)
18+
assert context == 'https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.3.jsonld'
19+
mappings = compute_attribute_mappings(ngsi_ld_partially_expanded, context)
20+
assert mappings['name'] == 'https://uri.etsi.org/ngsi-ld/default-context/name'
21+
assert mappings['https://uri.fiware.org/ns/data-models#category'] == 'https://uri.fiware.org/ns/data-models#category'
22+
23+
24+
def test_ld_expand_partially_expanded(ngsi_ld_partially_expanded):
25+
jsonld.set_document_loader(cached_aiohttp_document_loader(timeout=30))
26+
expanded = jsonld.expand(ngsi_ld_partially_expanded)
27+
print(expanded)
28+
29+
30+
31+
32+
33+
34+
35+
36+

src/ld/utils.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from pyld import jsonld
2+
3+
4+
def get_context(doc: dict) -> dict:
5+
if doc and '@context' in doc:
6+
return doc['@context']
7+
else:
8+
return {}
9+
10+
11+
def expand_attribute(name: str, value: any, context: dict) -> (str, dict):
12+
attr = {
13+
name: value,
14+
'@context': context
15+
}
16+
expanded = jsonld.expand(attr)
17+
for item in expanded:
18+
for uri in item.keys():
19+
return uri, item[uri]
20+
return name, value
21+
22+
23+
def compute_attribute_mappings(compacted: dict, context: dict) -> dict:
24+
mapping = {}
25+
for key in compacted:
26+
uri, value = expand_attribute(key, compacted[key], context)
27+
mapping[key] = uri
28+
return mapping

src/reporter/reporter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def notify():
164164
# Validate entity update
165165
error = _validate_payload(entity)
166166
if error:
167-
# TODO in this way we return error for even if only one entity
168-
# is wrong
167+
# TODO in this way we return error even if only one entity
168+
# is wrong
169169
return error, 400
170170
# Add TIME_INDEX attribute
171171
custom_index = request.headers.get(TIME_INDEX_HEADER_NAME, None)

0 commit comments

Comments
 (0)