Skip to content

Commit

Permalink
Fixed get_all_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Semprini committed Dec 5, 2023
1 parent f94aa28 commit 0b560a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
44 changes: 39 additions & 5 deletions mdg/tests/test_uml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ def test_find_package(self):
self.assertEqual(UMLPackage, type(res))
self.assertEqual("child1", res.name)

def test_find_class(self):
res = self.root_package.find_by_id("3", SearchTypes.CLASS)
self.assertEqual(UMLClass, type(res))
self.assertEqual("class1", res.name)

def test_find_enumeration(self):
# Check that we don't find the wrong thing
res = self.root_package.find_by_id("3", SearchTypes.ENUM)
Expand Down Expand Up @@ -85,3 +80,42 @@ def test_attribute_get_type(self):
def test_get_all_(self):
self.assertEqual(len(self.root_package.get_all_classes()), 2)
self.assertEqual(len(self.root_package.get_all_enums()), 1)


class TestUMLClassFunctions(unittest.TestCase):
def setUp(self):
self.root_package = UMLPackage("1", "root")
child = UMLPackage("2", "child1", self.root_package)
self.root_package.children.append(child)

# Ceate class to be specialized by with an attribute
cls = UMLClass(child, "class1", "3")
child.classes.append(cls)
att = UMLAttribute(cls, "foo", 4)
cls.attributes.append(att)

# Create simple class with no attributes
cls = UMLClass(child, "class2", "4")
child.classes.append(cls)

# Create inheitance
child.classes[1].generalization = child.classes[0]
child.classes[0].specialized_by.append(child.classes[1]) # TODO: Setter function which does this linking

def test_find_class(self):
res = self.root_package.find_by_id("3", SearchTypes.CLASS)
self.assertEqual(UMLClass, type(res))
self.assertEqual("class1", res.name)

def test_get_all_attributes(self):
# Grab class which inheits fom a class
cls = self.root_package.children[0].classes[1]
self.assertEqual("class2", cls.name)

# Inherited class is not abstact so expect an empty result set
res = cls.get_all_attributes(abstract_only=True)
self.assertEqual([], res)

# Include attributes of non abstract class so expect an attribute
res = cls.get_all_attributes(abstract_only=False)
self.assertEqual(1, len(res))
17 changes: 5 additions & 12 deletions mdg/uml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,13 @@ def get_array_relationships(self) -> list:
def get_all_attributes(self, abstract_only=True) -> list[UMLAttribute]:
""" Returns attributes of this class and inherited attrs from generalised classes
"""
result = self.attributes
result = self.attributes.copy()
parent = self.generalization
#if parent is not None:
# print(f" {parent} | {parent.generalization}")

if parent is not None:
# print(self)
# for a in result:
# print(f" {a.name}")
print(self)
print(parent)
attrs = parent.get_all_attributes(abstract_only)

for a in attrs:
print(f" {a.name}")
if parent.is_abstract or abstract_only==False:
result += parent.get_all_attributes(abstract_only)

return result


Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def find_packages(srcdir):

setuptools.setup(
name='pymdg',
version='0.9a1',
version='0.9a2',
author='Semprini',
author_email='[email protected]',
description='Model driven genration - from UML to Code & Docs',
Expand All @@ -30,7 +30,7 @@ def find_packages(srcdir):
packages=["mdg", ] + pymdg_packages,
include_package_data=True,
package_data={
'': ['*.txt', '*.xml', '*.special', '*.jinja'],
'': ['*.txt', '*.xml', '*.special', '*.jinja', '*.j2'],
},
entry_points={
'console_scripts': [
Expand All @@ -48,5 +48,5 @@ def find_packages(srcdir):
"pyyaml",
"sqlalchemy",
],
python_requires='>=3.7',
python_requires='>=3.11',
)

0 comments on commit 0b560a5

Please sign in to comment.