class TestUMLModel(unittest.TestCase): def setUp(self): self.root_package = UMLPackage("1", "root") child = UMLPackage("2", "child1", self.root_package) self.root_package.children.append(child) cls = UMLClass(child, "class1", "3") child.classes.append(cls) def test_find_package(self): res = self.root_package.find_by_id("2") self.assertEqual(UMLPackage, type(res)) self.assertEqual("child1", res.name) def test_find_class(self): res = self.root_package.find_by_id("3") self.assertEqual(UMLClass, type(res)) self.assertEqual("class1", res.name) def test_string_to_multiplicity(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.string_to_multiplicity("0..1"), ("0", "1")) self.assertEqual(assoc.string_to_multiplicity("0..*"), ("0", "*")) self.assertEqual(assoc.string_to_multiplicity("*..1"), ("*", "1")) def test_association_cardinality(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_MANY) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_MANY) def test_association_type(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.association_type, UMLAssociationType.ASSOCIATION) assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1, UMLAssociationType.COMPOSITION) self.assertEqual(assoc.association_type, UMLAssociationType.COMPOSITION)
class TestUMLModel(unittest.TestCase): def setUp(self): self.root_package = UMLPackage("1", "root") child = UMLPackage("2", "child1", self.root_package) self.root_package.children.append(child) cls = UMLClass(child, "class1", "3") child.classes.append(cls) def test_find_package(self): res = self.root_package.find_by_id("2") self.assertEqual(UMLPackage, type(res)) self.assertEqual("child1", res.name) def test_find_class(self): res = self.root_package.find_by_id("3") self.assertEqual(UMLClass, type(res)) self.assertEqual("class1", res.name) def test_string_to_multiplicity(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.string_to_multiplicity("0..1"), ("0", "1")) self.assertEqual(assoc.string_to_multiplicity("0..*"), ("0", "*")) self.assertEqual(assoc.string_to_multiplicity("*..1"), ("*", "1")) def test_association_type(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.association_type, "OneToMany") assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.association_type, "OneToOne") assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.association_type, "ManyToOne")
def association_parse(package: UMLPackage, element, root): id = element.get("id") cell = element.find("mxCell") source: UMLClass = package.find_by_id(cell.get("source")) target: UMLClass = package.find_by_id(cell.get("target")) element_type = element.get("UMLType") association = UMLAssociation(package, source, target, id, UMLAssociationType[element_type.upper()]) # Extract multiplicities ret = root.findall('.//object[@UMLType="DestinationMultiplicity"]') for dest in ret: dm = dest.find('mxCell[@parent="{}"]'.format(association.id)) if dm is not None: label = dest.get("label").strip('<div>').strip('</div>') association.destination_multiplicity = association.string_to_multiplicity( label) break ret = root.findall('.//object[@UMLType="SourceMultiplicity"]') for dest in ret: dm = dest.find('mxCell[@parent="{}"]'.format(association.id)) if dm is not None: label = dest.get("label").strip('<div>').strip('</div>') association.source_multiplicity = association.string_to_multiplicity( label) break # Set association destination name destination_name = element.get("destination_name") if destination_name is not None: association.destination_name = destination_name else: association.destination_name = target.name if association.source_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.ASSOCIATION: association.destination_name += 's' elif association.destination_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.COMPOSITION: association.destination_name += 's' # Set association source name source_name = element.get("source_name") if source_name is not None: association.source_name = source_name else: association.source_name = source.name if association.destination_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.ASSOCIATION: association.source_name += 's' if association.source_multiplicity[ 1] == '*' and association.association_type == UMLAssociationType.COMPOSITION: association.source_name += 's'
def generalization_parse(package: UMLPackage, element, root): cell = element.find("mxCell") source: UMLClass = package.find_by_id(cell.get("source")) target: UMLClass = package.find_by_id(cell.get("target")) source.generalization = target
class TestUMLModel(unittest.TestCase): def setUp(self): self.root_package = UMLPackage("1", "root") child = UMLPackage("2", "child1", self.root_package) self.root_package.children.append(child) cls = UMLClass(child, "class1", "3") child.classes.append(cls) def test_find_package(self): res = self.root_package.find_by_id("2") self.assertEqual(UMLPackage, type(res)) self.assertEqual("child1", res.name) def test_find_class(self): res = self.root_package.find_by_id("3") self.assertEqual(UMLClass, type(res)) self.assertEqual("class1", res.name) def test_string_to_multiplicity(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.string_to_multiplicity("0..1"), ("0", "1")) self.assertEqual(assoc.string_to_multiplicity("0..*"), ("0", "*")) self.assertEqual(assoc.string_to_multiplicity("*..1"), ("*", "1")) def test_association_cardinality(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_MANY) assoc.source_multiplicity = ("0", "1") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.ONE_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "1") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_ONE) assoc.source_multiplicity = ("0", "*") assoc.destination_multiplicity = ("0", "*") self.assertEqual(assoc.cardinality, Cardinality.MANY_TO_MANY) def test_association_type(self): assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1) self.assertEqual(assoc.association_type, UMLAssociationType.ASSOCIATION) assoc = UMLAssociation(self.root_package, self.root_package.children[0].classes[0], self.root_package.children[0].classes[0], 1, UMLAssociationType.COMPOSITION) self.assertEqual(assoc.association_type, UMLAssociationType.COMPOSITION) def test_attribute_type(self): attr = UMLAttribute(None, "test", 123) attr.set_type('String') self.assertEqual(attr.dest_type, 'CharField') attr.set_type('String (123)') self.assertEqual(attr.dest_type, 'CharField') self.assertEqual(attr.length, 123) attr.set_type('decimal (12,2)') self.assertEqual(attr.dest_type, 'DecimalField') self.assertEqual(attr.precision, 12) self.assertEqual(attr.scale, 2) def test_attribute_get_type(self): attr = UMLAttribute(None, "test", 123) attr.set_type('String') self.assertEqual(attr.get_type('default'), 'string')