def create_entity(oc: pyfiware.OrionConnector, entity_id: str, element_type: str, data: Dict): try: print("Deleting entity with id: {}".format(entity_id)) oc.delete(entity_id=entity_id) except pyfiware.FiException as err: print("Entity with id {} does not exist.".format(entity_id)) print(err.message) print("Creating entity with id: {}".format(entity_id)) oc.create(element_id=entity_id, element_type=element_type, attributes=data)
class TestFiwareManagerDeletions(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=''))) def test_delete_fails(self): with self.assertRaises(FiException): self.fiware_manager.delete(entity_id="1") @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse(status=404, data=''))) def test_delete_fails_silent(self): self.fiware_manager.delete(entity_id="1", silent=True) @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse(status=500, data=''))) def test_delete_fails_no_silent(self): with self.assertRaises(FiException): self.fiware_manager.delete(entity_id="1", silent=True) @patch.object(OrionConnector, "_request", Mock(return_value=DummyResponse(status=200, data=''))) def test_delete_success(self): self.fiware_manager.delete(entity_id="1") self.fiware_manager._request.assert_called_with( method='DELETE', url=self.url + '/v2/entities/' + "1", headers={'Accept': 'application/json'}, )