Add initial test for partition upgrade in version 2.1.0

Signed-off-by: Dan Yeaw <dan@yeaw.me>
This commit is contained in:
Dan Yeaw 2020-11-25 14:13:26 -05:00
parent b3ba551fcd
commit 0e9d60cb15
No known key found for this signature in database
GPG Key ID: 77A923EF537B61A4
3 changed files with 157 additions and 0 deletions

View File

@ -10,7 +10,9 @@ import io
import logging
import os.path
import uuid
from collections import Iterable
from functools import partial
from typing import Optional
import gaphas
@ -200,6 +202,8 @@ def _load_elements_and_canvasitems(
for item in canvasitems:
item = upgrade_canvas_item_to_1_0_2(item)
item = upgrade_canvas_item_to_1_3_0(item)
if version_lower_than(gaphor_version, (2, 1, 0)):
item = upgrade_canvas_item_to_2_1_0(item)
if version_lower_than(gaphor_version, (1, 1, 0)):
item = upgrade_presentation_item_to_1_1_0(item)
cls = modeling_language.lookup_diagram_item(item.type)
@ -414,3 +418,21 @@ def upgrade_element_to_2_1_0(elem):
elem.references["comment"] = refids
del elem.references["ownedComment"]
return elem
def flatten(items):
"""Yield items from a nested iterable."""
for item in items:
if isinstance(item, Iterable):
yield from flatten(item)
else:
yield item
def upgrade_canvas_item_to_2_1_0(item: gaphas.Item) -> Optional[gaphas.Item]:
if item.type == "PartitionItem" and item.canvasitems:
item.canvasitems = list(flatten(item.canvasitems))
item.canvasitems = [
item for _ in item.canvasitems if item.type != "PartitionItem"
]
return item

View File

@ -1,3 +1,5 @@
import pytest
from gaphor import UML
from gaphor.application import distribution
from gaphor.storage.parser import parse
@ -23,3 +25,24 @@ def test_message_item_upgrade(element_factory, modeling_language):
assert all(subjects), subjects
assert len(message_items) == 10
assert all(presentations), presentations
@pytest.mark.xfail
def test_partition_item_upgrade(element_factory, modeling_language):
"""Test upgrade to version 2.1.0.
Previous versions had subpartitions nested within a main partition, version
2.1.0 removed the nesting.
"""
path = distribution().locate_file("test-models/partition-upgrade.gaphor")
elements = parse(path)
load_elements(elements, element_factory, modeling_language)
diagram = element_factory.lselect(UML.Diagram)[0]
items = diagram.canvas.get_all_items()
partitions_items = [i for i in items if isinstance(i, diagramitems.PartitionItem)]
assert len(partitions_items) == 1
action_items = [i for i in items if isinstance(i, diagramitems.ActionItem)]
assert len(action_items) == 1

View File

@ -0,0 +1,112 @@
<?xml version="1.0" encoding="utf-8"?>
<gaphor xmlns="http://gaphor.sourceforge.net/model" version="3.0" gaphor-version="2.0.1">
<Package id="4c23c750-2def-11eb-8f76-6d0910850668">
<name>
<val>New model</val>
</name>
<ownedDiagram>
<reflist>
<ref refid="4c23cffc-2def-11eb-8f76-6d0910850668"/>
</reflist>
</ownedDiagram>
</Package>
<Diagram id="4c23cffc-2def-11eb-8f76-6d0910850668">
<name>
<val>main</val>
</name>
<package>
<ref refid="4c23c750-2def-11eb-8f76-6d0910850668"/>
</package>
<canvas>
<item id="bb61f04c-2def-11eb-8f76-6d0910850668" type="PartitionItem">
<matrix>
<val>(1.0, 0.0, 0.0, 1.0, 157.0703125, 179.609375)</val>
</matrix>
<width>
<val>100.0</val>
</width>
<height>
<val>300.0</val>
</height>
<item id="bd6673a4-2def-11eb-8f76-6d0910850668" type="PartitionItem">
<matrix>
<val>(1.0, 0.0, 0.0, 1.0, 0.0, 30.0)</val>
</matrix>
<width>
<val>100.0</val>
</width>
<height>
<val>270.0</val>
</height>
<subject>
<ref refid="bd695ec0-2def-11eb-8f76-6d0910850668"/>
</subject>
<item id="c0b7c3be-2def-11eb-8f76-6d0910850668" type="ActionItem">
<matrix>
<val>(1.0, 0.0, 0.0, 1.0, 3.0, 116.7109375)</val>
</matrix>
<width>
<val>94.0</val>
</width>
<height>
<val>30.0</val>
</height>
<subject>
<ref refid="c0b79632-2def-11eb-8f76-6d0910850668"/>
</subject>
</item>
</item>
</item>
</canvas>
</Diagram>
<StyleSheet id="4c24a850-2def-11eb-8f76-6d0910850668">
<styleSheet>
<val>* {
background-color: transparent;
color: black;
font-family: sans;
font-size: 14;
highlight-color: rgba(0, 0, 255, 0.4);
line-width: 2;
padding: 0;
}
diagram {
background-color: white;
line-style: normal;
/* line-style: sloppy 0.3; */
}
</val>
</styleSheet>
</StyleSheet>
<ActivityPartition id="bd695ec0-2def-11eb-8f76-6d0910850668">
<name>
<val>Swimlane</val>
</name>
<node>
<reflist>
<ref refid="c0b79632-2def-11eb-8f76-6d0910850668"/>
</reflist>
</node>
<presentation>
<reflist>
<ref refid="bd6673a4-2def-11eb-8f76-6d0910850668"/>
</reflist>
</presentation>
</ActivityPartition>
<Action id="c0b79632-2def-11eb-8f76-6d0910850668">
<inPartition>
<reflist>
<ref refid="bd695ec0-2def-11eb-8f76-6d0910850668"/>
</reflist>
</inPartition>
<name>
<val>NewAction</val>
</name>
<presentation>
<reflist>
<ref refid="c0b7c3be-2def-11eb-8f76-6d0910850668"/>
</reflist>
</presentation>
</Action>
</gaphor>