示例#1
0
    def test_delete_class(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")

        # create relationship between classes
        model.create_relationship("composition", "class1", "class2")
        # Ensure relationship is created
        self.assertTrue(model.classes["class1"].has_relationship("class2"))
        self.assertTrue(model.classes["class2"].has_relationship("class1"))

        # Ensure deleted
        model.delete_class("class1")
        # assert dictionary key was removed
        self.assertTrue("class1" not in model.classes)

        # Ensure relationship was removed after deletion of class1
        status, msg = model.list_relationships("class2")
        self.assertTrue(status)
        self.assertEqual(msg, "Class 'class2' has no relationships")
        
        # Ensure no errors when class DNE
        status, msg = model.delete_class("class1")
        # ensure it failed
        self.assertFalse(status)
        self.assertEqual(msg, "class1 does not exist.")
    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)
示例#3
0
    def test_delete_relationship(self):
        model = UMLModel()
        model.create_class("c1")
        model.create_class("c2")
        model.create_relationship("realization", "c1", "c2")

        # Ensure we get correct output when class 1 doesn't exist
        status, msg = model.delete_relationship("c7","c2")
        self.assertFalse(status)
        self.assertEqual(msg, "c7 does not exist")

        # Ensure we get correct output when class 2 doesn't exist
        status, msg = model.delete_relationship("c1","c5")
        self.assertFalse(status)
        self.assertEqual(msg, "c5 does not exist")

        # Ensure relationship is deleted
        status, msg = model.delete_relationship("c1","c2")
        self.assertEqual(len(model.classes["c1"].relationships), 0)
        self.assertEqual(len(model.classes["c2"].relationships), 0)
        
        # Ensure we get correct output after relationship has been deleted
        self.assertTrue(status)
        self.assertEqual(msg, "Relationship between c1 and c2 has been deleted")

        # Ensure we get correct output if we try to delete relationship that doesn't exist
        status, msg = model.delete_relationship("c1","c2")
        self.assertFalse(status)
        self.assertEqual(msg, "Relationship between c1 and c2 does not exist.")
示例#4
0
    def test_rename_class(self):
        model = UMLModel()
        model.create_class("class1")

        # Ensure name is changed
        model.rename_class("class1", "classA")
        # assert dictionary key was changed
        self.assertTrue("classA" in model.classes)
        #assert class object name was changed
        self.assertEqual(model.classes["classA"].name, "classA")

        # Ensure unknown class is rejected
        status, msg = model.rename_class("class1", "classB")
        self.assertFalse(status)
        self.assertEqual(msg, "class1 does not exist.")

        # Ensure duplicate newname is rejected
        model.create_class("class1")
        status, msg = model.rename_class("classA", "class1")
        # ensure it failed
        self.assertFalse(status)
        self.assertEqual(msg, "class1 already exists.")

        # Ensure renamed class with relationship updates
        # relationship in both classes
        self.assertTrue(model.create_class("class2")[0])
        self.assertTrue(model.create_relationship("inheritance", "class1", "class2")[0])
        self.assertTrue(model.rename_class("class2", "class3")[0])
        self.assertEqual(model.classes["class1"].relationships[0].other, "class3")
    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 test_fetch_from_class(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")
        model.create_field("class1", "public", "string", "field1")
        model.create_field("class1", "public", "int", "field2")
        model.create_method("class1", "public", "int", "method1")
        model.create_method("class1", "public", "bool", "method2")
        model.create_relationship("inheritance", "class1", "class2")

        # Test 1: fetch fields
        fields = fetch_from_class(model, "class1", "fields")
        self.assertEqual(fields, ["field1", "field2"])

        # Test 2: fetch methods
        methods = fetch_from_class(model, "class1", "methods")
        self.assertEqual(methods, ["method1", "method2"])

        # Test 3: empty list
        fields = fetch_from_class(model, "class2", "fields")
        self.assertEqual(fields, [])
示例#7
0
    def test_create_relationship(self):
        model = UMLModel()
        model.create_class("c1")
        model.create_class("c2")

        # Ensure relationship is created
        model.create_relationship("composition", "c1", "c2")
        self.assertTrue(model.classes["c1"].has_relationship("c2"))
        self.assertTrue(model.classes["c2"].has_relationship("c1"))

        status, msg = model.create_relationship("invalidRealType", "c5", "c2")
        self.assertFalse(status)
        self.assertEqual(msg, "'invalidRealType' is not a valid relationship type.")

        # Ensure we get correct output when class 1 doesn't exist
        status, msg = model.create_relationship("realization", "c5", "c2")
        self.assertFalse(status)
        self.assertEqual(msg, "'c5' does not exist")

        # Ensure we get correct output when class 2 doesn't exist
        status, msg = model.create_relationship("realization", "c1", "c5")
        self.assertFalse(status)
        self.assertEqual(msg, "'c5' does not exist")

        # Ensure already existing rel
        status, msg = model.create_relationship("composition","c2","c1")
        # ensure it failed
        self.assertFalse(status)
        self.assertEqual(msg, "Relationship between 'c2' and 'c1' already exists.")
示例#8
0
    def test_list_relationships(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")

        # variables used for testing equality
        message = model.list_relationships("class5")[1]
        # test output equality with using a non-existent class name
        self.assertEqual(message, "class5 does not exist")
        
        # test with a parameter
        # variables used for testing equality
        message = model.list_relationships('class1')[1]
        # test output equality without creating a relationship
        self.assertEqual(message, "Class 'class1' has no relationships")

        # test without a parameter
        # variables used for testing equality
        message = model.list_relationships()[1]
        # test output equality without creating a relationship
        self.assertEqual(message, "No relationships exist for the current model")

        # create a relationship between the classes
        model.create_relationship("inheritance", "class1", "class2")

        # test with a parameter
        message = model.list_relationships("class1")[1]
        # test output equality
        outString = "".join(("Relationships for class1\n",
                             "class1 ---------> class2"))
        self.assertEqual(message, outString)

        # test without a parameter
        # variables used for testing equality
        message = model.list_relationships()[1]
        outString = "".join(("Listing all relationships\n",
                             "class1 ---------> class2\n",
                             "class2 <--------- class1"))
        # test output equality            
        self.assertEqual(message,outString)
示例#9
0
    def test_list_class(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_class("class2")
        model.create_class("class3")

        # variables used for testing equality
        message = model.list_class("class5")[1]
        # test output equality while using the wrong class name
        self.assertEqual(message, "'class5' is not a valid class")
        
        # add fields to class1
        model.create_field("class1", "public", "void", "a1")
        model.create_field("class1", "private", "int", "size")
        # add methods to class1 along with some parameters
        model.create_method("class1", "protected", "void", "setSpeed")
        model.create_method("class1", "public", "void", "walk")
        model.create_parameter("class1", "setSpeed", "int", "speed")
        model.create_parameter("class1", "walk", "int", "speed")
        model.create_parameter("class1", "walk", "double", "direction")
        # add relationships to class1
        model.create_relationship("inheritance", "class1", "class2")
        model.create_relationship("aggregation", "class3", "class1")

        # variables used for testing equality
        message = model.list_class("class1")[1]
        outString = "".join(("Class: class1\n", 
                    "=== Fields ======================\n", 
                    "public a1: void\n", 
                    "private size: int\n", 
                    "=== Methods =====================\n", 
                    "protected setSpeed(int speed): void\n", 
                    "public walk(int speed, double direction): void\n", 
                    "=== Relationships ===============\n", 
                    "class1 ---------> class2\n", 
                    "class1 <<>>------ class3\n", 
                    "================================="))
        # test output equality            
        self.assertEqual(message,outString)
示例#10
0
    def test_move_up_relationship(self):
        model = UMLModel()
        model.create_class("c1")
        model.create_class("c2")
        model.create_class("c3")
        model.create_class("c4")

        # Ensure relationship is created
        model.create_relationship("composition", "c1", "c2")
        self.assertTrue(model.classes["c1"].has_relationship("c2"))
        self.assertTrue(model.classes["c2"].has_relationship("c1"))

        # Ensure relationship is created
        model.create_relationship("aggregation", "c1", "c3")
        self.assertTrue(model.classes["c1"].has_relationship("c3"))
        self.assertTrue(model.classes["c3"].has_relationship("c1"))

        # Ensure relationship is created
        model.create_relationship("inheritance", "c1", "c4")
        self.assertTrue(model.classes["c1"].has_relationship("c4"))
        self.assertTrue(model.classes["c4"].has_relationship("c1"))

        # Ensure order of relationships for c1
        self.assertEqual(model.classes["c1"].relationship_index("c2"), 0)
        self.assertEqual(model.classes["c1"].relationship_index("c3"), 1)
        self.assertEqual(model.classes["c1"].relationship_index("c4"), 2)
        
        # Move c1's relationship with c4 up in c1
        model.move_up_relationship("c1", "c4")
        
        # Ensure the relationship was moved up 
        self.assertEqual(model.classes["c1"].relationship_index("c2"), 0)
        self.assertEqual(model.classes["c1"].relationship_index("c4"), 1)
        self.assertEqual(model.classes["c1"].relationship_index("c3"), 2)

        # Move c1's relationship with c4 up in c1
        model.move_up_relationship("c1", "c4")      

        # Ensure the relationship was moved up 
        self.assertEqual(model.classes["c1"].relationship_index("c4"), 0)  
        self.assertEqual(model.classes["c1"].relationship_index("c2"), 1)
        self.assertEqual(model.classes["c1"].relationship_index("c3"), 2)

        # Ensure correct response is outputted when we try to move a relationship
        # up when the relationship is at the top of the list
        status, msg = model.move_up_relationship("c1", "c4")
        self.assertFalse(status)
        self.assertEqual(msg, "The relationship with c4 can not move up any further in c1")

        # Ensure correct response is outputted when class 1 doesn't exist
        status, msg = model.move_up_relationship("c5", "c4")
        self.assertFalse(status)
        self.assertEqual(msg, "c5 does not exist")
        
        # Ensure correct response is outputted when class 2 doesn't exist
        status, msg = model.move_up_relationship("c1", "c7")
        self.assertFalse(status)
        self.assertEqual(msg, "c7 does not exist")

        # Ensure correct response is outputted when two classes don't have an
        # existing relationship
        status, msg = model.move_up_relationship("c2", "c3")
        self.assertFalse(status)
        self.assertEqual(msg, "Relationship between c2 and c3 does not exist.")