def post_entity_context_update(oc: pyfiware.OrionConnector, entity_id: str, element_type: str, data: Dict): device = oc.get(entity_id=entity_id) if device: oc.patch(element_id=entity_id, element_type=element_type, attributes={ 'type': 'StructuredValue', 'value': data, "metadata": {} }) device = oc.get(entity_id=entity_id) print(device)
class TestFiwareManagerQueries(TestCase): url = "http://127.0.0.1:1026" def setUp(self): self.fiware_manager = OrionConnector(self.url) @patch.object( OrionConnector, "_request", Mock(return_value=DummyResponse( status=404, data= '{"error":"NotFound","description":"The requested entity has not been found. Check type and id"}' ))) def test_get_by_id_empty(self): response = self.fiware_manager.get("wrongID") self.assertIsNone(response, "Not empty response") @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=200, data='{"id":"CorrectID","type":"fake"}'))) def test_get_by_id_found(self): correct_id = "CorrectID" response = self.fiware_manager.get(correct_id) self.assertEqual(response["id"], correct_id, "Not correct element") @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=200, data='{"id":"CorrectID","type":"fake"}'))) def test_get_by_id_request(self): correct_id = "CorrectID" self.fiware_manager.get(correct_id) self.fiware_manager._request.assert_called_with( method='GET', url=self.url + '/v2/entities/' + correct_id, headers={'Accept': 'application/json'}, fields={}) @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=500, data='{"error":"Everything Blew up"}'))) def test_get_by_id_error(self): with self.assertRaises(FiException): self.fiware_manager.get("NOSENSE") @patch.object( OrionConnector, "_request", Mock(return_value=DummyResponse( status=404, data= '{"error":"NotFound","description":"The requested entity has not been found. Check type and id"}' ))) def test_get_no_result(self): # No Results response = self.fiware_manager.search(entity_type="WrongType") self.assertEqual(response, [], "Not empty response") @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=200, data='[{"id":"CorrectID","type":"fake"}]', headers={"fiware-total-count": 1}))) def test_get_single_result(self): response = self.fiware_manager.search(entity_type="fake") self.assertEqual(type(response), list, "Not a list") self.assertEqual(len(response), 1, "Not unique") @patch.object( OrionConnector, "_request", Mock(return_value=DummyResponse(status=200, data='[{"id":"1","type":"fake"},' + '{"id":"2","type":"fake"}]', headers={"fiware-total-count": 2}))) def test_get_multiple_result(self): response = self.fiware_manager.search() self.assertEqual(type(response), list, "Not a list") self.assertGreater(len(response), 1, "Unique results") @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=500, data='{"error":"Everything Blew up"}'))) def test_get_exception(self): # Raise exceptions with self.assertRaises(FiException): self.fiware_manager.search() @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=200, data='[{"id":"CorrectID","type":"fake"}]', headers={"fiware-total-count": 1}))) def test_get_queries_id_pattern(self): self.fiware_manager.search(id_pattern="id*/") self.fiware_manager._request.assert_called_with( method='GET', url=self.url + '/v2/entities', headers={'Accept': 'application/json'}, fields={ 'options': 'count', 'limit': 1000, 'idPattern': 'id*/' }) @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse( status=200, data='[{"id":"CorrectID","type":"fake"}]', headers={"fiware-total-count": 1}))) def test_get_queries_type(self): self.fiware_manager.search(query="something > 500") self.fiware_manager._request.assert_called_with( method='GET', url=self.url + '/v2/entities', headers={'Accept': 'application/json'}, fields={ 'options': 'count', 'limit': 1000, 'q': 'something > 500' })