예제 #1
0
def test_loading():
    model_file = distribution().locate_file("test-models/test-model.gaphor")
    outfile = PseudoFile()

    generate(model_file, outfile)

    assert outfile.data == GENERATED, f'"""{outfile.data}"""'
예제 #2
0
 def handler():
     try:
         path = distribution().locate_file(
             "test-models/diagram-#4.gaphor")
         load(path, self.element_factory, self.modeling_language)
     finally:
         Gtk.main_quit()
예제 #3
0
    def test_load_and_save_of_a_model(self):
        path = distribution().locate_file("test-models/simple-items.gaphor")

        with open(path, "r") as ifile:
            storage.load(
                ifile,
                factory=self.element_factory,
                modeling_language=self.modeling_language,
            )

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        assert copy == orig, "Saved model does not match copy"
예제 #4
0
def element_factory(session):
    element_factory = session.get_service("element_factory")
    modeling_language = session.get_service("modeling_language")
    path = distribution().locate_file("test-models/issue_53.gaphor")
    load(path, element_factory, modeling_language)
    yield element_factory
    element_factory.shutdown()
예제 #5
0
 def handler():
     try:
         path = distribution().locate_file(
             "test-diagrams/diagram-#4.gaphor")
         load(path, self.element_factory)
     finally:
         Gtk.main_quit()
예제 #6
0
    def test_load_save(self):
        """Test loading and saving models"""

        path = distribution().locate_file("test-diagrams/simple-items.gaphor")

        with open(path, "r") as ifile:
            storage.load(ifile, factory=self.element_factory)

        pf = PseudoFile()

        storage.save(XMLWriter(pf), factory=self.element_factory)

        with open(path, "r") as ifile:

            orig = ifile.read()

        copy = pf.data

        with open("tmp.gaphor", "w") as ofile:

            ofile.write(copy)

        expr = re.compile('gaphor-version="[^"]*"')
        orig = expr.sub("%VER%", orig)
        copy = expr.sub("%VER%", copy)

        self.maxDiff = None
        assert copy == orig, "Saved model does not match copy"
예제 #7
0
def save_generator(writer, factory):
    """Save the current model using @writer, which is a
    gaphor.storage.xmlwriter.XMLWriter instance."""

    writer.startDocument()
    writer.startPrefixMapping("", NAMESPACE_MODEL)
    writer.startElementNS(
        (NAMESPACE_MODEL, "gaphor"),
        None,
        {
            (NAMESPACE_MODEL, "version"):
            FILE_FORMAT_VERSION,
            (NAMESPACE_MODEL, "gaphor-version"):
            application.distribution().version,
        },
    )

    size = factory.size()
    for n, e in enumerate(factory.values(), start=1):
        clazz = e.__class__.__name__
        assert e.id
        writer.startElement(clazz, {"id": str(e.id)})
        e.save(partial(save_element, writer=writer))
        writer.endElement(clazz)

        if n % 25 == 0:
            yield (n * 100) / size

    writer.endElementNS((NAMESPACE_MODEL, "gaphor"), None)
    writer.endPrefixMapping("")
    writer.endDocument()
예제 #8
0
def test_model_header(tmp_path):
    """Load a model with no relationships to test header."""
    path = distribution().locate_file("test-models/multiple-messages.gaphor")
    outfile = tmp_path / "profile.py"

    generate(path, outfile)

    assert header in outfile.read_text()
예제 #9
0
    def test_load_uml_metamodel(self):
        path = distribution().locate_file("models/UML.gaphor")

        with open(path) as ifile:
            storage.load(
                ifile,
                factory=self.element_factory,
                modeling_language=self.modeling_language,
            )
예제 #10
0
    def test_bug(self, case):
        """Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all
        updates).
        """
        path = distribution().locate_file("test-models/dbus.gaphor")
        load(path, case.element_factory, case.modeling_language)
예제 #11
0
    def test_load_uml_metamodel(self):
        """
        Test if the meta model can be loaded.
        """

        path = distribution().locate_file("gaphor/UML/uml2.gaphor")

        with open(path) as ifile:
            storage.load(ifile, factory=self.element_factory)
예제 #12
0
    def test_bug(self):
        """
        Load file.

        This does not nearly resemble the error, since the model should
        be loaded from within the mainloop (which will delay all updates).
        """
        path = distribution().locate_file("test-diagrams/diagram-#4.gaphor")
        load(path, self.element_factory)
예제 #13
0
    def test_loading_an_old_version(self):
        """Test loading and saving models"""

        path = distribution().locate_file(
            "test-diagrams/old-gaphor-version.gaphor")

        def load_old_model():
            with open(path, "r") as ifile:
                storage.load(ifile, factory=self.element_factory)

        self.assertRaises(ValueError, load_old_model)
예제 #14
0
def test_model_with_extension(tmp_path):
    """Load a model with an extension relationship."""
    path = distribution().locate_file("test-models/codegen-extension.gaphor")
    outfile = tmp_path / "profile.py"

    generate(path, outfile)

    extension = """
from gaphor.UML import Class
class NewStereotype(Class):
    pass
"""
    assert extension in outfile.read_text()
예제 #15
0
    def test_can_not_load_models_older_that_0_17_0(self):

        path = distribution().locate_file("test-models/old-gaphor-version.gaphor")

        def load_old_model():
            with open(path, "r") as ifile:
                storage.load(
                    ifile,
                    factory=self.element_factory,
                    modeling_language=self.modeling_language,
                )

        self.assertRaises(ValueError, load_old_model)
예제 #16
0
    def test_can_not_load_models_older_that_0_17_0(self, case):

        path = distribution().locate_file(
            "test-models/old-gaphor-version.gaphor")

        def load_old_model():
            with open(path, "r") as ifile:
                storage.load(
                    ifile,
                    factory=case.element_factory,
                    modeling_language=case.modeling_language,
                )

        with pytest.raises(ValueError):
            load_old_model()
def test_message_item_upgrade(element_factory, modeling_language):
    """"""
    path = distribution().locate_file("test-models/multiple-messages.gaphor")

    elements = parse(path)
    load_elements(elements, element_factory, modeling_language)

    diagram = element_factory.lselect(UML.Diagram)[0]
    items = diagram.canvas.get_root_items()
    message_items = [i for i in items if isinstance(i, diagramitems.MessageItem)]
    subjects = [m.subject for m in message_items]
    messages = element_factory.lselect(UML.Message)
    presentations = [m.presentation for m in messages]

    assert len(messages) == 10
    assert all(subjects), subjects
    assert len(message_items) == 10
    assert all(presentations), presentations
예제 #18
0
def element_factory(session):
    element_factory = session.get_service("element_factory")
    path = distribution().locate_file("test-diagrams/issue_53.gaphor")
    load(path, element_factory)
    yield element_factory
    element_factory.shutdown()
예제 #19
0
def save_generator(writer, factory):  # noqa: C901
    """
    Save the current model using @writer, which is a
    gaphor.storage.xmlwriter.XMLWriter instance.
    """
    def save_reference(name, value):
        """
        Save a value as a reference to another element in the model.
        This applies to both UML as well as canvas items.
        """
        if value.id:
            writer.startElement(name, {})
            writer.startElement("ref", {"refid": value.id})
            writer.endElement("ref")
            writer.endElement(name)

    def save_collection(name, value):
        """
        Save a list of references.
        """
        if len(value) > 0:
            writer.startElement(name, {})
            writer.startElement("reflist", {})
            for v in value:
                if v.id:
                    writer.startElement("ref", {"refid": v.id})
                    writer.endElement("ref")
            writer.endElement("reflist")
            writer.endElement(name)

    def save_value(name, value):
        """
        Save a value (attribute).
        """
        if value is not None:
            writer.startElement(name, {})
            writer.startElement("val", {})
            if isinstance(value, str):
                writer.characters(value)
            elif isinstance(value, bool):
                # Write booleans as 0/1.
                writer.characters(str(int(value)))
            else:
                writer.characters(str(value))
            writer.endElement("val")
            writer.endElement(name)

    def save_element(name, value):
        """
        Save attributes and references from items in the gaphor.UML module.
        A value may be a primitive (string, int), a gaphor.core.modeling.collection
        (which contains a list of references to other UML elements) or a
        gaphas.Canvas (which contains canvas items).
        """
        if isinstance(value, (Element, gaphas.Item)):
            save_reference(name, value)
        elif isinstance(value, collection):
            save_collection(name, value)
        elif isinstance(value, gaphas.Canvas):
            writer.startElement("canvas", {})
            value.save(save_canvas)
            writer.endElement("canvas")
        else:
            save_value(name, value)

    def save_canvas(value):
        """
        Save attributes and references in a gaphor.diagram.* object.
        The extra attribute reference can be used to force UML
        """
        assert isinstance(value, gaphas.Item)
        writer.startElement("item", {
            "id": value.id,
            "type": value.__class__.__name__
        })
        value.save(save_canvas_item)

        for child in value.canvas.get_children(value):
            save_canvas(child)

        writer.endElement("item")

    def save_canvas_item(name, value):
        """
        Save attributes and references in a gaphor.diagram.* object.
        The extra attribute reference can be used to force UML
        """
        if isinstance(value, collection):
            save_collection(name, value)
        elif isinstance(value, (Element, gaphas.Item)):
            save_reference(name, value)
        else:
            save_value(name, value)

    writer.startDocument()
    writer.startPrefixMapping("", NAMESPACE_MODEL)
    writer.startElementNS(
        (NAMESPACE_MODEL, "gaphor"),
        None,
        {
            (NAMESPACE_MODEL, "version"):
            FILE_FORMAT_VERSION,
            (NAMESPACE_MODEL, "gaphor-version"):
            application.distribution().version,
        },
    )

    size = factory.size()
    n = 0
    for e in list(factory.values()):
        clazz = e.__class__.__name__
        assert e.id
        writer.startElement(clazz, {"id": str(e.id)})
        e.save(save_element)
        writer.endElement(clazz)

        n += 1
        if n % 25 == 0:
            yield (n * 100) / size

    writer.endElementNS((NAMESPACE_MODEL, "gaphor"), None)
    writer.endPrefixMapping("")
    writer.endDocument()
예제 #20
0
    def test_it(self):
        """
        Test an issue when loading a freshly created action diagram.
        """
        ef = self.element_factory
        modeling_language = self.modeling_language
        path = distribution().locate_file("test-models/action-issue.gaphor")
        storage.load(path, ef, modeling_language)

        actions = ef.lselect(UML.Action)
        flows = ef.lselect(UML.ControlFlow)
        assert 3 == len(actions)
        assert 3 == len(flows)

        # Actions live in partitions:
        partitions = ef.lselect(UML.ActivityPartition)
        assert 2 == len(partitions)

        # Okay, so far the data model is saved correctly. Now, how do the
        # handles behave?
        diagrams = ef.lselect(UML.Diagram)
        assert 1 == len(diagrams)

        canvas = diagrams[0].canvas
        assert 9 == len(canvas.get_all_items())
        # Part, Part, Act, Act, Part, Act, Flow, Flow, Flow

        for e in actions + flows:
            assert 1 == len(e.presentation), e
        for i in canvas.select(lambda e: isinstance(e,
                                                    (FlowItem, ActionItem))):
            assert i.subject, i

        # Loaded as:
        #
        # actions[0] --> flows[0, 1]
        # flows[0, 2] --> actions[2]
        # flows[1] --> actions[1] --> flows[2]

        # start element:
        assert actions[0].outgoing[0] is flows[0]
        assert actions[0].outgoing[1] is flows[1]
        assert not actions[0].incoming

        (cinfo, ) = canvas.get_connections(
            handle=flows[0].presentation[0].head)
        assert cinfo.connected is actions[0].presentation[0]
        (cinfo, ) = canvas.get_connections(
            handle=flows[1].presentation[0].head)
        assert cinfo.connected is actions[0].presentation[0]

        # Intermediate element:
        assert actions[1].incoming[0] is flows[1]
        assert actions[1].outgoing[0] is flows[2]

        (cinfo, ) = canvas.get_connections(
            handle=flows[1].presentation[0].tail)
        assert cinfo.connected is actions[1].presentation[0]
        (cinfo, ) = canvas.get_connections(
            handle=flows[2].presentation[0].head)
        assert cinfo.connected is actions[1].presentation[0]

        # Final element:
        assert actions[2].incoming[0] is flows[0]
        assert actions[2].incoming[1] is flows[2]

        (cinfo, ) = canvas.get_connections(
            handle=flows[0].presentation[0].tail)
        assert cinfo.connected is actions[2].presentation[0]
        (cinfo, ) = canvas.get_connections(
            handle=flows[2].presentation[0].tail)
        assert cinfo.connected is actions[2].presentation[0]

        # Test the parent-child connectivity
        for a in actions:
            (p, ) = a.inPartition
            assert p
            assert canvas.get_parent(a.presentation[0])
            assert canvas.get_parent(a.presentation[0]) is p.presentation[0]