Exemplo n.º 1
0
    def test_find_field(self):
        model = UMLModel()
        model.create_class("class1")
        testClass = model.classes["class1"]

        # create some fields
        model.create_field("class1", "public", "void", "a1")
        model.create_field("class1", "private", "int", "num")

        # ensure fields are found in the right indices
        self.assertEqual(model.find_field("class1", "a1"), 0)
        self.assertEqual(model.find_field("class1", "num"), 1)

        # ensure we get correct output when field is not found
        self.assertEqual(model.find_field("class1", "nonexistent"), -1)
Exemplo n.º 2
0
    def test_move_down_field(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_field("class1", "public", "void", "a1")
        model.create_field("class1", "public", "void", "a2")
        model.create_field("class1", "public", "void", "a3")
        model.create_field("class1", "public", "void", "a4")
        # ensure the class is created
        testClass = model.classes["class1"]
        
        # Ensure the fields were created
        self.assertTrue(testClass.has_field("a1"))  
        self.assertTrue(testClass.has_field("a2"))
        self.assertTrue(testClass.has_field("a3"))
        self.assertTrue(testClass.has_field("a4"))
        
        # Ensure the fields'position  
        self.assertEqual(testClass.fields[0].name, "a1")
        self.assertEqual(testClass.fields[1].name, "a2")
        self.assertEqual(testClass.fields[2].name, "a3")
        self.assertEqual(testClass.fields[3].name, "a4")
        
        # Move down the field one position from the list
        model.move_down_field("class1", "a1") 
        
        # Ensure the field has a right position
        self.assertEqual(testClass.field_index("a2"), 0)
        self.assertEqual(testClass.field_index("a1"), 1)
        self.assertEqual(testClass.field_index("a3"), 2)
        self.assertEqual(testClass.field_index("a4"), 3)

        # Ensure the method don't move down further
        status, msg = model.move_down_field("class1", "a4")
        self.assertFalse(status)
        self.assertEqual(msg, "a4 can not move down any further in class1")

        # Ensure correct response is outputted when class doesn't exist
        status, msg = model.move_down_field("class8", "a4")
        self.assertFalse(status)
        self.assertEqual(msg, "class8 does not exist")
        
        # Ensure correct response is outputted when field doesn't exist
        status, msg = model.move_down_field("class1", "a7")
        self.assertFalse(status)
        self.assertEqual(msg, "a7 does not exist in class1")
Exemplo n.º 3
0
    def test_delete_field(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_field("class1", "public", "void", "a1")
        testClass = model.classes["class1"]

        # Ensure we get correct output when class doesn't exist
        status, msg = model.delete_field("class5", "a1")
        self.assertFalse(status)
        self.assertEqual(msg, "class5 does not exist")

        # Ensure we get correct output when field we wish to remove doesn't exist
        status, msg = model.delete_field("class1", "a3")
        self.assertFalse(status)
        self.assertEqual(msg, "a3 is not a field of class1")

        # Ensure field is deleted
        model.delete_field("class1", "a1")
        self.assertFalse(testClass.has_field("a1"))
Exemplo n.º 4
0
    def test_create_field(self):
        model = UMLModel()
        model.create_class("class1")
        testClass = model.classes["class1"]

        # Ensure field is created
        model.create_field("class1", "public", "void", "a1")
        self.assertTrue(testClass.has_field("a1"))

        # Ensure we get correct output when class does not exist
        status, msg = model.create_field("class5", "private", "void", "a1")
        self.assertFalse(status)
        self.assertEqual(msg, "class5 does not exist")
      
        # Ensure duplicate field is not created
        status, msg = model.create_field("class1", "public", "void", "a1")
        # ensure it failed
        self.assertFalse(status)
        self.assertEqual(msg, "field a1 already exists in class1")
    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, [])
Exemplo n.º 6
0
    def test_list_fields(self):
        model = UMLModel()
        model.create_class("class1")

        # variables used for testing equality
        message = model.list_fields("class3")[1]
        # test output equality with a non-existent class
        self.assertEqual(message, "class3 is not a class")

        # variables used for testing equality
        message = model.list_fields("class1")[1]
        # test output equality without inserting fields
        self.assertEqual(message, "Class 'class1' has no fields")

        # add some fields to class1
        model.create_field("class1", "public", "int", "year")
        model.create_field("class1", "private", "int", "salary")
        model.create_field("class1", "protected", "string", "SSN")

        # variables used for testing equality
        message = model.list_fields("class1")[1]
        outString = "".join(("Fields of class1\n",
                            "PUBLIC int year\n", 
                            "PRIVATE int salary\n", 
                            "PROTECTED string SSN"))
        # test output equality            
        self.assertEqual(message,outString)
Exemplo n.º 7
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)
Exemplo n.º 8
0
    def test_rename_field(self):
        model = UMLModel()
        model.create_class("class1")
        model.create_field("class1", "public", "void", "a1")

        # Ensure field is renamed
        model.rename_field("class1", "a1", "a2")
        self.assertEqual(model.classes["class1"].fields[0].name, "a2")

        # Ensure we get correct output when class doesn't exist
        status, msg = model.rename_field("class7", "a1", "a2")
        self.assertFalse(status)
        self.assertEqual(msg, "class7 does not exist.")

        # Ensure we get correct output when the field we wish to rename doesn't exist
        status, msg = model.rename_field("class1", "a7", "a3")
        self.assertFalse(status)
        self.assertEqual(msg, "field a7 does not exist in class1")

        # Ensure we get correct output when the new field already exist
        status, msg = model.rename_field("class1", "a2", "a2")
        self.assertFalse(status)
        self.assertEqual(msg, "field a2 already exists in class1")
Exemplo n.º 9
0
    def test_get_data(self):
        model = UMLModel()

        # Test 1: empty model
        data = model.get_data()
        self.assertEqual(data, {})

        # Test 2: model with data
        model.create_class('class1')
        model.create_class('class2')
        model.create_field('class1', 'private', 'string', 'name')
        expectedData = {
            "class1" : {
                "name" : "class1",
                "fields" : [{
                    "visibility" : 'private',
                    'type' : 'string',
                    'name' : 'name'
                }],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            },
            "class2" : {
                "name" : "class2",
                "fields" : [],
                "methods" : [],
                "relationships" : [],
                "x" : 200,
                "y" : 0,
                "zindex" : 0
            }
        }
        data = model.get_data()
        self.assertEqual(data, expectedData)
Exemplo n.º 10
0
    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)