def test_add_location_from_geojson(): m = DataModel("id", "type") location = Point((-0.5667, 44.8333)) m.add("location", location) assert m.json( ) == r'{"id": "id", "type": "type", "location": {"value": {"type": "Point", "coordinates": [-0.5667, 44.8333]}, "type": "geo:json"}}'
def test_add_location_invalid(): m = DataModel("id", "type") with pytest.raises(NgsiException, match=r".*JSON compliant.*"): m.add("location", ('A', -0.5667))
def test_add_field_date_from_datetime(): m = DataModel("id", "type") d = datetime(2019, 6, 1, 18, 30, 0) m.add("dateObserved", d) assert m.json( ) == r'{"id": "id", "type": "type", "dateObserved": {"value": "2019-06-01T18:30:00", "type": "DateTime"}}'
def test_add_location_from_tuple(): m = DataModel("id", "type") m.add("location", (44.8333, -0.5667)) assert m.json( ) == r'{"id": "id", "type": "type", "location": {"value": {"type": "Point", "coordinates": [-0.5667, 44.8333]}, "type": "geo:json"}}'
def test_add_field_date_from_str(): m = DataModel("id", "type") m.add_date("dateObserved", "2018-01-01T15:00:00") assert m.json( ) == r'{"id": "id", "type": "type", "dateObserved": {"value": "2018-01-01T15:00:00", "type": "DateTime"}}'
def test_add_field_url_from_str(): m = DataModel("id", "type") m.add_url("dataProvider", "https://www.fiware.org") assert m.json( ) == r'{"id": "id", "type": "type", "dataProvider": {"value": "https://www.fiware.org", "type": "URL"}}'
def test_add_field_float(): m = DataModel("id", "type") m.add("temperature", 37.2) assert m.json( ) == r'{"id": "id", "type": "type", "temperature": {"value": 37.2, "type": "Float"}}'
def side_effect(row, sink, datamodel): m = DataModel( id=f"Building:MainBuilding:Room:{datamodel['id']}", type="Room") sink.write(m.json()) return 1 # number of entities created in the side_effect function
def test_add_field_str(): m = DataModel("id", "type") m.add("projectName", "Pixel") assert m.json( ) == r'{"id": "id", "type": "type", "projectName": {"value": "Pixel", "type": "Text"}}'
def test_add_field_int(): m = DataModel("id", "type") m.add("temperature", 37) assert m.json( ) == r'{"id": "id", "type": "type", "temperature": {"value": 37, "type": "Integer"}}'
def test_add_relationship_bad_ref(): m = DataModel("id", "type") with pytest.raises(NgsiException, match=r".*Bad relationship.*"): m.add_relationship("store", "Shelf", "001")
def test_add_relationship(): # https://github.com/Fiware/tutorials.Entity-Relationships m = DataModel("id", "type") m.add_relationship("refStore", "Shelf", "001") assert m.json() == r'{"id": "id", "type": "type", ' \ r'"refStore": {"value": "urn:ngsi-ld:Shelf:001", "type": "Relationship"}}'
def build_entity(row: Row) -> DataModel: r = row.record m = DataModel(id=r["room"], type="Room") m.add("temperature", r["temperature"]) m.add("pressure", r["pressure"]) return m
def test_create(): m = DataModel("id", "type") assert m["id"] == "id" assert m["type"] == "type"
def test_add_field_bool(): m = DataModel("id", "type") m.add("loading", True) assert m.json( ) == r'{"id": "id", "type": "type", "loading": {"value": true, "type": "Boolean"}}'
def test_cannot_map_ngsi_type(): m = DataModel("id", "type") with pytest.raises(NgsiException, match=r".*Cannot map.*"): m.add("unknown", None)
def build_entity(row: Row) -> DataModel: id, temperature, pressure = row.record.split(';') m = DataModel(id=id, type="Room") m.add("temperature", float(temperature)) m.add("pressure", int(pressure)) return m