def test_add_model(self): """Test case for add_model Add a network model """ headers = { 'Accept': 'application/json', 'Content-Type': 'multipart/form-data', } cim_xml = open( "test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_DI.xml", "rb") modelname = "test_rootnet_full_ne_24j13h_di" response = self.client.open('/models', method='POST', headers=headers, data={ 'name': modelname, 'profiles': "DI", 'version': "cgmes_v2_4_15", 'files': cim_xml }, content_type='multipart/form-data') self.assert200(response, 'Response is: ' + response.data.decode('utf-8')) returned_model = ModelReply.from_dict(json.loads(response.get_data())) assert returned_model.name == modelname assert isinstance(returned_model.id, int)
def add_model(): """Add a new network model """ # parse the json request new_model = Model.from_dict(connexion.request.form) try: # parse the attached xml files req_files = connexion.request.files.getlist("files") files = [] for f in req_files: # Validate xml input filestr = f.stream.read() ElementTree.fromstring(filestr) f.stream.seek(0) files.append(f.stream) except ElementTree.ParseError: return Error(code=422, message="Invalid XML files"), 422 # create cimpy objects try: cimpy_data = cimpy.cim_import(files, new_model.version) except Exception: return Error(code=422, message="Invalid CIM files"), 422 new_id = model_db.put_model(new_model, cimpy_data, files) # Return the model as `ModelReply` return ModelReply.from_model(new_model, new_id)
def test_add_models(self): """Test case for add_model Add a network model """ cim_xml = [ open("test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_DI.xml", "rb"), open("test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_EQ.xml", "rb"), open("test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_SV.xml", "rb"), open("test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_TP.xml", "rb") ] modelname = "test_rootnet_full_ne_24j13h" response = self.client.open('/models', method='POST', data={ 'name': modelname, 'profiles': "DI,EQ,SV,TP", 'version': "cgmes_v2_4_15", 'files': cim_xml }, content_type='multipart/form-data') self.assert200(response, 'Response is: ' + response.data.decode('utf-8')) returned_model = ModelReply.from_dict(json.loads(response.get_data())) assert returned_model.name == modelname assert isinstance(returned_model.id, int)
def test_delete_model(self): """Test case for delete_model Delete a network model """ headers = { 'Accept': 'application/json', } testid = 1111 testname = "deltestrecord" # deleting an non-existing key faulty_response = self.client.open('/models/{id}'.format(id=testid), method='DELETE', headers=headers) self.assert404(faulty_response, 'Response is: ' + faulty_response.data.decode('utf-8')) db.models[str(testid)] = db.record(Model(testname, "DI", "cgmes_v2_4_15"), cimobj={}, files=None) response = self.client.open('/models/{id}'.format(id=testid), method='DELETE', headers=headers) self.assert200(response, 'Response is: ' + response.data.decode('utf-8')) assert str(testid) not in db.models returned_model = ModelReply.from_dict(json.loads(response.get_data())) assert returned_model.id == testid assert returned_model.name == testname assert returned_model.version == "cgmes_v2_4_15"
def test_add_and_delete_model(self): """Test case for delete_model Delete a network model """ headers = { 'Accept': 'application/json', } cim_xml = open( "test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_DI.xml", "rb") modelname = "test_rootnet_full_ne_24j13h_di" post_response = self.client.open('/models', method='POST', data={ 'name': modelname, 'profiles': "DI", 'version': "cgmes_v2_4_15", 'files': cim_xml }, content_type='multipart/form-data') self.assert200(post_response, 'Response is : ' + post_response.data.decode('utf-8')) response_model = ModelReply.from_dict( json.loads(post_response.get_data())) response = self.client.open( '/models/{id}'.format(id=response_model.id), method='DELETE', headers=headers) self.assert200(response, 'Response is: ' + response.data.decode('utf-8')) returned_model = ModelReply.from_dict(json.loads(response.get_data())) assert response_model == returned_model response = self.client.open( '/models/{id}'.format(id=response_model.id), method='DELETE', headers=headers) self.assert404(response, 'Response is: ' + response.data.decode('utf-8'))
def get_model(id_): """Get a network model This is only useful at the moment to get the name of the model :param id: Model id :type id: int :rtype: Model """ try: return ModelReply.from_model(model_db.get_model(id_), id_) except KeyError: return Error(code=404, message="Model not found"), 404
def test_add_and_get_model(self): """Test case for add_model Add a network model and then get it back by id. Must be the same data """ cim_xml = open( "test/sampledata/CIGRE_MV/Rootnet_FULL_NE_24J13h_DI.xml", "rb") modelname = "test_rootnet_full_ne_24j13h_di" post_response = self.client.open('/models', method='POST', data={ 'name': modelname, 'profiles': "DI", 'version': "cgmes_v2_4_15", 'files': cim_xml }, content_type='multipart/form-data') self.assert200(post_response, 'Response is : ' + post_response.data.decode('utf-8')) post_response_json = ModelReply.from_dict( json.loads(post_response.get_data())) # Check if model is avail headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', } get_response = self.client.open( '/models/{id}'.format(id=post_response_json.id), method='GET', headers=headers) self.assert200(get_response, 'Response is : ' + get_response.data.decode('utf-8')) get_response_json = ModelReply.from_dict( json.loads(get_response.get_data())) assert get_response_json == post_response_json
def get_model(id_): """Get a network model This is only useful at the moment to get the name of the model :param id: Model id :type id: int :rtype: Model """ try: record = model_db.get_record(id_) # Temporary Hack: manipulate the stored json so that it can be deserialized into a model again model_str = record.model.replace("\n", "").replace("'", '"') model = json.loads(model_str, object_hook=lambda d: SimpleNamespace(**d)) return ModelReply.from_model(model, id_) except KeyError: return Error(code=404, message="Model not found"), 404
def test_delete_model(self): """Test case for delete_model Delete a network model """ headers = { 'Accept': 'application/json', } testid = 1111 testname = "deltestrecord" # deleting an non-existing key faulty_response = self.client.open('/models/{id}'.format(id=testid), method='DELETE', headers=headers) self.assert404(faulty_response, 'Response is: ' + faulty_response.data.decode('utf-8')) # Create the entry in the database redis.sadd("models", str(testid)) redis.set(str(testid), Model(testname, "DL", "cgmes_v2_4_15").__repr__()) redis.set(str(testid) + "_files_len", "0") assert redis.get(str(testid)) is not None response = self.client.open('/models/{id}'.format(id=testid), method='DELETE', headers=headers) self.assert200(response, 'Response is: ' + response.data.decode('utf-8')) assert redis.get(str(testid)) is None returned_model = ModelReply.from_dict(json.loads(response.get_data())) assert returned_model.id == testid assert returned_model.name == testname assert returned_model.version == "cgmes_v2_4_15"