def test_execute(self): # create model model_name = "myModel" model = UMLModel() model.save_model(model_name + ".json", directory=os.getcwd() + "/") class_data = { "filename": model_name + ".json", "directory": os.getcwd() + "/", "class_name": "class1", "field_visibilities": ["private", "private"], "field_types": ["string", "int"], "field_names": ["name", "age"], "method_visibilities": ["public"], "method_types": ["string"], "method_names": ["getName"], "parameter_method_index": [], "parameter_types": [], "parameter_names": [] } cmd = CreateClassGUICommand(model, class_data) cmd.saveBackup() status, msg = cmd.execute() # ensure it passed self.assertTrue(status) # ensure components are in model self.assertTrue("class1" in model.classes) self.assertTrue(len(model.classes["class1"].fields) == 2)
def test_execute(self): # create model model_name = "myModel" model = UMLModel() model.create_class("class1") model.create_class("class2") model.create_relationship("composition", "class1", "class2") model.save_model(model_name + ".json", directory=os.getcwd() + "/") class_data = { "filename": model_name + ".json", "directory": os.getcwd() + "/", "class_name1": "class1", "class_name2": "class2" } cmd = DeleteRelationshipGUICommand(model, class_data) cmd.saveBackup() status, msg = cmd.execute() # ensure it passed self.assertTrue(status) # ensure relationship was deleted self.assertTrue(len(model.classes["class1"].relationships) == 0) self.assertTrue(len(model.classes["class2"].relationships) == 0) # ensure it fails when invalid cmd = DeleteRelationshipGUICommand(model, class_data) cmd.saveBackup() status, msg = cmd.execute() self.assertFalse(status)
def createModel(): # Ensure there was a POST request if request.method != "POST": # send error message as a flash message flash("Nothing sent in POST", "error") return redirect(url_for('models')) model_name:str = request.form.get("model_name") # Ensure new model name does not already exist if model_name in getModelNames(): flash(f"Model '{request.form.get('model_name')}' already exists", "error") return redirect(url_for('models')) # reject empty model names if model_name.strip() == "": flash(f"Model name cannot be empty", "error") return redirect(url_for('models')) # create new empty model file model = UMLModel() model.save_model(model_name + ".json", directory=DATA_FOLDER) flash(f"Model '{model_name}' created successfully", "success") return redirect(url_for("models"))
def test_execute(self): # create model model_name = "myModel" model = UMLModel() model.create_class("class1") model.create_class("class2") model.create_relationship("composition", "class1", "class2") model.save_model(model_name + ".json", directory=os.getcwd() + "/") class_data = { "filename": model_name + ".json", "directory": os.getcwd() + "/", "old_relationship_type": "composition", "old_class_name1": "class1", "old_class_name2": "class2", "relationship_type": "inheritance", "class_name1": "class1", "class_name2": "class2" } cmd = EditRelationshipGUICommand(model, class_data) cmd.saveBackup() status, msg = cmd.execute() # ensure it passed self.assertTrue(status) # relationship exists self.assertTrue(len(model.classes["class1"].relationships) == 1) self.assertTrue(len(model.classes["class2"].relationships) == 1) self.assertEqual(model.classes["class1"].relationships[0].type, RelationshipType.INHERITANCE) self.assertEqual(model.classes["class1"].relationships[0].other, "class2") self.assertEqual(model.classes["class2"].relationships[0].other, "class1") # ensure it fails when invalid class_data["class_name2"] = "invalid_class" cmd = EditRelationshipGUICommand(model, class_data) cmd.saveBackup() status, msg = cmd.execute() self.assertFalse(status)
def prompt_exit(model: UMLModel, directory=MODEL_DIRECTORY) -> Tuple[bool, str]: """Initiates the exit prompt Prompts user if they want to save before quitting If user types 'yes': - User is prompted for a filename to save to - Model is saved - Program exits If user types 'no': - program exits without saving If user types 'cancel': - model is not saved - returns from this function Params: - model (UMLModel) - the model to visibilityally save """ response = "" # Prompt user until we get a valid answer while response != "yes" and response != "no" and response != "cancel": print("Do you want to save current working project? (yes/no/cancel)") response = input().lower() # if user wants to save project, call the save_model function to save working project if response == "yes": print("Please enter the filename.") filename = input() model.save_model(filename, directory) elif response == "cancel": return (True, "Exit aborted") elif response == "no": print("Goodbye!") exit()
def test_save_model(self): model = UMLModel() model.create_class('class1') model.create_class('class2') model.create_class('class3') model.create_field('class1', "protected", "int", "number") model.create_method('class2', "public", "string", "getMsg") model.create_parameter('class2', 'getMsg', 'string', 'msg') # Test 1: Normal save model.save_model("test-save.json") # ensure data is in the json file data = None with open("code/server-data/test-save.json", 'r') as file: data = json.loads(file.read()) expectedData = { "class1" : { "name" : "class1", "fields" : [{ "visibility" : "protected", "type" : "int", "name" : "number" }], "methods" : [], "relationships" : [], "x" : 200, "y" : 0, "zindex" : 0 }, "class2" : { "name" : "class2", "fields" : [], "methods" : [{ "visibility" : "public", "type" : "string", "name" : "getMsg", "parameters" : [{ 'type' : 'string', 'name' : 'msg' }] }], "relationships" : [], "x" : 200, "y" : 0, "zindex" : 0 }, "class3" : { "name" : "class3", "fields" : [], "methods" : [], "relationships" : [], "x" : 200, "y" : 0, "zindex" : 0 } } self.assertEqual(data, expectedData) # Test 2: save to different directory directory = os.getcwd() + '/' model.save_model("test-save2.json", directory=directory) data = None passes = True try: with open("test-save2.json", 'r') as file: data = json.loads(file.read()) except FileNotFoundError: passes = False self.assertTrue(passes) self.assertEqual(data, expectedData)