def test_same_named_arguments_c_attributes(self): a1 = CAttribute(default="") a2 = CAttribute(type=str) n1 = "a" self.mcl.attributes = {n1: a1, "a": a2} eq_(set(self.mcl.attributes), {a2}) eq_(self.mcl.attribute_names, ["a"])
def test_set_cattribute_method_to_none(self): # setting the type/default to their default value (None) should work a = CAttribute() a.type = None a.default = None eq_(a.type, None) eq_(a.default, None)
def test_type_object_attribute_class_is_deleted_in_type_method(self): attr_cl = CClass(self.mcl, "AC") attr_cl.delete() try: a = CAttribute() a.type = attr_cl exception_expected_() except CException as e: eq_(e.value, "cannot access named element that has been deleted")
def test_default_object_attribute_is_deleted_in_default_method(self): attr_cl = CClass(self.mcl, "AC") default_obj = CObject(attr_cl) default_obj.delete() try: a = CAttribute() a.default = default_obj exception_expected_() except CException as e: eq_(e.value, "cannot access named element that has been deleted")
def test_delete_class_that_is_an_attribute_type(self): b1 = CBundle("B1") mcl = CMetaclass("MCL") cl1 = CClass(mcl, "CL1", bundles=b1) cl2 = CClass(mcl, "CL2", bundles=b1) cl3 = CClass(mcl, "CL3", bundles=b1) o3 = CObject(cl3, "O3") ea1 = CAttribute(type=cl3, default=o3) m = CMetaclass("M", bundles=b1, attributes={"o": ea1}) c = CClass(m) cl1.delete() cl3.delete() try: # we just use list here, in order to not get a warning that ea1.default has no effect list([ea1.default]) exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: # we just use list here, in order to not get a warning that ea1.type has no effect list([ea1.type]) exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.default = "3" exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = cl1 exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = cl2 exception_expected_() except CException as e: eq_("default value '' incompatible with attribute's type 'CL2'", e.value) try: c.set_value("o", CObject(cl2)) exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: c.get_value("o") exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value)
def test_use_enum_type_attribute(self): enum_values = ["A", "B", "C"] enum_obj = CEnum("ABCEnum", values=enum_values) ea1 = CAttribute(type=enum_obj, default="A") ea2 = CAttribute(type=enum_obj) self.mcl.attributes = {"letters1": ea1, "letters2": ea2} eq_(set(self.mcl.attributes), {ea1, ea2}) ok_(isinstance(ea1.type, CEnum)) self.mcl.attributes = {"letters1": ea1, "isBool": True, "letters2": ea2} bool_attr = self.mcl.get_attribute("isBool") l1 = self.mcl.get_attribute("letters1") eq_(set(self.mcl.attributes), {l1, ea2, bool_attr}) eq_(l1.default, "A") eq_(ea2.default, None)
def test_class_type_attribute(self): attribute_type = CMetaclass("AttrType") attribute_value = CClass(attribute_type, "attribute_value") self.stereotype.attributes = {"attrTypeCl": attribute_type} class_attribute = self.stereotype.get_attribute("attrTypeCl") class_attribute.default = attribute_value attributes = self.stereotype.attributes eq_(set(attributes), {class_attribute}) bool_attr = CAttribute(default=True) self.stereotype.attributes = { "attrTypeCl": class_attribute, "isBoolean": bool_attr } attributes = self.stereotype.attributes eq_(set(attributes), {class_attribute, bool_attr}) eq_(self.stereotype.attribute_names, ["attrTypeCl", "isBoolean"]) class_attribute = self.stereotype.get_attribute("attrTypeCl") eq_(class_attribute.type, attribute_type) default = class_attribute.default ok_(isinstance(default, CClass)) eq_(default, attribute_value) self.stereotype.attributes = { "attrTypeCl": attribute_value, "isBoolean": bool_attr } eq_(self.stereotype.attribute_names, ["attrTypeCl", "isBoolean"]) # using the CClass in attributes causes a new CAttribute to be created != class_attribute neq_(self.stereotype.get_attribute("attrTypeCl"), class_attribute)
def test_object_type_attribute(self): attribute_type = CClass(self.mcl, "AttrType") attribute_value = CObject(attribute_type, "attribute_value") self.stereotype.attributes = {"attrTypeObj": attribute_value} object_attribute = self.stereotype.get_attribute("attrTypeObj") attributes = self.stereotype.attributes eq_(set(attributes), {object_attribute}) bool_attr = CAttribute(default=True) self.stereotype.attributes = { "attrTypeObj": object_attribute, "isBoolean": bool_attr } attributes = self.stereotype.attributes eq_(set(attributes), {object_attribute, bool_attr}) eq_(self.stereotype.attribute_names, ["attrTypeObj", "isBoolean"]) object_attribute = self.stereotype.get_attribute("attrTypeObj") eq_(object_attribute.type, attribute_type) default = object_attribute.default ok_(isinstance(default, CObject)) eq_(default, attribute_value) self.stereotype.attributes = { "attrTypeObj": attribute_value, "isBoolean": bool_attr } eq_(self.stereotype.attribute_names, ["attrTypeObj", "isBoolean"]) # using the CObject in attributes causes a new CAttribute to be created != object_attribute neq_(self.stereotype.get_attribute("attrTypeObj"), object_attribute)
def test_define_enum_type_attribute(self): enum_values = ["A", "B", "C"] enum_obj = CEnum("ABCEnum", values=enum_values) CAttribute(type=enum_obj, default="A") CAttribute(default="A", type=enum_obj) try: CAttribute(type=enum_obj, default="X") exception_expected_() except CException as e: eq_( "default value 'X' incompatible with attribute's type 'ABCEnum'", e.value) try: CAttribute(default="X", type=enum_obj) exception_expected_() except CException as e: eq_( "default value 'X' incompatible with attribute's type 'ABCEnum'", e.value)
def test_type_and_default_on_attribute(self): CAttribute(default="", type=str) CAttribute(type=str, default="") try: CAttribute(default=1, type=str) exception_expected_() except CException as e: eq_( "default value '1' incompatible with attribute's type '<class 'str'>'", e.value) try: CAttribute(type=str, default=1) exception_expected_() except CException as e: eq_( "default value '1' incompatible with attribute's type '<class 'str'>'", e.value) a5 = CAttribute(type=int) eq_(a5.default, None)
def test_delete_enum_that_is_an_attribute_type(self): b1 = CBundle("B1") CBundle("B2") e1 = CEnum("E1", bundles=b1) e2 = CEnum("E2", bundles=b1) e3 = CEnum("E3", values=["1", "2"], bundles=b1) ea1 = CAttribute(type=e3, default="1") ea2 = CAttribute(type=e3) cl = CClass(self.mcl, attributes={"letters1": ea1, "letters2": ea2}) o = CObject(cl, "o") e1.delete() e3.delete() try: ea1.default = "3" exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = e1 exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.default = "3" exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = e1 exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = e2 exception_expected_() except CException as e: eq_("default value '1' incompatible with attribute's type 'E2'", e.value) try: o.set_value("letters1", "1") exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: o.get_value("letters1") exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value)
cart_item_relation = cart.association(item, "in cart: [cart] 1 -> [item in cart] *") item_product_relation = item.association( product, "product definition: [cart item] * -> [product] 1") order_status = CEnum("Order Status", values=["New", "Hold", "Shipped", "Delivered"]) order = CClass(domain_metaclass, "Order", attributes={ "id": str, "ordered": today, "shipped": date, "ship to": address, "status": CAttribute(type=order_status, default="New"), "total": float }) order_item_relation = CAssociation(order, item, "in order: [order] 1 -> [item in order] *") person = CClass(domain_metaclass, "Person", attributes={ "first name": str, "last name": str, "address": address, "email": str, "phone": str })
# stateless_service = CStereotype("Service", superclasses=service, # default_values={"stateless": True}) database = CStereotype("Database", extended=component) facade = CStereotype("Facade", extended=component) web_ui = CStereotype("Web UI", superclasses=facade) gateway = CStereotype("Gateway", extended=component) jdbc = CStereotype("JDBC", extended=connectors_relation) http_protocol = CEnum("HTTP Protocol", values=["HTTP", "HTTPS"]) restful_http = CStereotype("RESTful HTTP", extended=connectors_relation, attributes={ "protocol": CAttribute(type=http_protocol, default="HTTPS"), }) def print_stereotypes_and_extended_introspection(): print(f"component stereotypes = {component.stereotypes!s}") print(f"connector stereotypes = {connectors_relation.stereotypes!s}") print(f"facade extended = {facade.extended!s}") print(f"restful_http extended = {restful_http.extended!s}") print() # class model api_gateway = CClass(distributed_component, "API Gateway", stereotype_instances=[gateway, facade])
def test_delete_enum_from_bundle(self): b1 = CBundle("B1") e1 = CEnum("E1", bundles=b1) e1.delete() eq_(set(b1.get_elements(type=CEnum)), set()) e1 = CEnum("E1", bundles=b1) e2 = CEnum("E2", bundles=b1) e3 = CEnum("E3", values=["1", "2"], bundles=b1) ea1 = CAttribute(type=e3, default="1") ea2 = CAttribute(type=e3) cl = CClass(self.mcl, attributes={"letters1": ea1, "letters2": ea2}) o = CObject(cl, "o") e1.delete() eq_(set(b1.get_elements(type=CEnum)), {e2, e3}) e3.delete() eq_(set(b1.get_elements(type=CEnum)), {e2}) eq_(e3.name, None) eq_(e3.bundles, []) eq_(e3.values, []) eq_(set(cl.attributes), {ea1, ea2}) try: # we just use list here, in order to not get a warning that ea1.default has no effect list([ea1.default]) exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: # we just use list here, in order to not get a warning that ea1.type has no effect list([ea1.type]) exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.default = "3" exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = e1 exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: ea1.type = e2 exception_expected_() except CException as e: eq_("default value '1' incompatible with attribute's type 'E2'", e.value) try: o.set_value("letters1", "1") exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value) try: o.get_value("letters1") exception_expected_() except CException as e: eq_("cannot access named element that has been deleted", e.value)