def test_load_save(self): """Test loading and saving models""" dist = importlib_metadata.distribution("gaphor") path = dist.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"
def test_load_save(self): """Test loading and saving models""" dist = pkg_resources.get_distribution('gaphor') path = os.path.join(dist.location, '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.assertEquals(copy, orig, 'Saved model does not match copy')
def test_load_save(self): """Test loading and saving models""" dist = pkg_resources.get_distribution("gaphor") path = os.path.join(dist.location, "test-diagrams/simple-items.gaphor") with io.open(path, "r") as ifile: storage.load(ifile, factory=self.element_factory) pf = PseudoFile() storage.save(XMLWriter(pf), factory=self.element_factory) with io.open(path, "r") as ifile: orig = ifile.read() copy = pf.data with io.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 self.assertEqual(copy, orig, "Saved model does not match copy")
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"
def save_and_load(self, filename): factory = self.element_factory f = open(filename, 'r') storage.load(f, factory=self.element_factory) f.close() self.backup_service.backup() elements = map(factory.lookup, factory.keys()) orig = StringIO() storage.save(XMLWriter(orig), factory=self.element_factory) self.backup_service.restore() restored = map(factory.lookup, factory.keys()) assert len(elements) == len(restored) assert elements != restored copy = StringIO() storage.save(XMLWriter(copy), factory=self.element_factory) orig = orig.getvalue() copy = copy.getvalue() assert len(orig) == len(copy)
def save_and_load(self, filename): factory = self.element_factory f = open(filename, 'r') storage.load(f, factory=self.element_factory) f.close() self.backup_service.backup() elements = list(map(factory.lookup, list(factory.keys()))) orig = StringIO() storage.save(XMLWriter(orig), factory=self.element_factory) self.backup_service.restore() restored = list(map(factory.lookup, list(factory.keys()))) assert len(elements) == len(restored) assert elements != restored copy = StringIO() storage.save(XMLWriter(copy), factory=self.element_factory) orig = orig.getvalue() copy = copy.getvalue() assert len(orig) == len(copy)
def save(): """Save diagram into string.""" f = StringIO() storage.save(XMLWriter(f), element_factory) data = f.getvalue() f.close() return data
def test_connection(self): """ Test connection loading of an association and two classes. (Should count for all line-like objects alike if this works). """ c1 = self.create(items.ClassItem, UML.Class) c2 = self.create(items.ClassItem, UML.Class) c2.matrix.translate(200, 200) self.diagram.canvas.update_matrix(c2) assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1, 200, 200) a = self.create(items.AssociationItem) self.connect(a, a.head, c1) head_pos = a.head.pos self.connect(a, a.tail, c2) tail_pos = a.tail.pos self.diagram.canvas.update_now() assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 10, a.tail.pos # assert a.tail.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=self.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id self.element_factory.flush() assert not list(self.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=self.element_factory) fd.close() diagrams = list(self.kindof(UML.Diagram)) self.assertEqual(1, len(diagrams)) d = diagrams[0] a = d.canvas.select(lambda e: isinstance(e, items.AssociationItem))[0] self.assertTrue(a.subject is not None) self.assertEqual(old_a_subject_id, a.subject.id) cinfo_head = a.canvas.get_connection(a.head) self.assertTrue(cinfo_head.connected is not None) cinfo_tail = a.canvas.get_connection(a.tail) self.assertTrue(cinfo_tail.connected is not None) self.assertTrue(not cinfo_head.connected is cinfo_tail.connected)
def test_save_item(self): """Save a diagranm item too. """ diagram = self.element_factory.create(UML.Diagram) diagram.create(items.CommentItem, subject=self.element_factory.create(UML.Comment)) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert '<Diagram ' in out.data assert '<Comment ' in out.data assert '<canvas>' in out.data assert ' type="CommentItem" ' in out.data, out.data
def test_connection(self): """ Test connection loading of an association and two classes. (Should count for all line-like objects alike if this works). """ c1 = self.create(items.ClassItem, UML.Class) c2 = self.create(items.ClassItem, UML.Class) c2.matrix.translate(200, 200) self.diagram.canvas.update_matrix(c2) assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1, 200, 200) a = self.create(items.AssociationItem) self.connect(a, a.head, c1) head_pos = a.head.pos self.connect(a, a.tail, c2) tail_pos = a.tail.pos self.diagram.canvas.update_now() assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 10, a.tail.pos #assert a.tail.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=self.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id self.element_factory.flush() assert not list(self.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=self.element_factory) fd.close() diagrams = list(self.kindof(UML.Diagram)) self.assertEquals(1, len(diagrams)) d = diagrams[0] a = d.canvas.select(lambda e: isinstance(e, items.AssociationItem))[0] self.assertTrue(a.subject is not None) self.assertEquals(old_a_subject_id, a.subject.id) cinfo_head = a.canvas.get_connection(a.head) self.assertTrue(cinfo_head.connected is not None) cinfo_tail = a.canvas.get_connection(a.tail) self.assertTrue(cinfo_tail.connected is not None) self.assertTrue(not cinfo_head.connected is cinfo_tail.connected)
def save(self): """Save diagram into string.""" from gaphor.storage import storage from gaphor.storage.xmlwriter import XMLWriter f = StringIO() storage.save(XMLWriter(f), factory=self.element_factory) data = f.getvalue() f.close() self.element_factory.flush() assert not list(self.element_factory.select()) assert not list(self.element_factory.lselect()) return data
def test_save_item(self): """Save a diagranm item too. """ diagram = self.element_factory.create(UML.Diagram) diagram.create(CommentItem, subject=self.element_factory.create(UML.Comment)) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert "<Diagram " in out.data assert "<Comment " in out.data assert "<canvas>" in out.data assert ' type="CommentItem"' in out.data, out.data
def test_save_uml(self): """Saving gaphor.UML model elements.""" self.element_factory.create(UML.Package) self.element_factory.create(UML.Diagram) self.element_factory.create(UML.Comment) self.element_factory.create(UML.Class) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert "<Package " in out.data assert "<Diagram " in out.data assert "<Comment " in out.data assert "<Class " in out.data
def save(self): """ Save diagram into string. """ from gaphor.storage import storage from gaphor.misc.xmlwriter import XMLWriter f = StringIO() storage.save(XMLWriter(f), factory=self.element_factory) data = f.getvalue() f.close() self.element_factory.flush() assert not list(self.element_factory.select()) assert not list(self.element_factory.lselect()) return data
def test_save_uml(self): """Saving gaphor.uml2 model elements. """ self.element_factory.create(uml2.Package) self.element_factory.create(uml2.Diagram) self.element_factory.create(uml2.Comment) self.element_factory.create(uml2.Class) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert '<Package ' in out.data assert '<Diagram ' in out.data assert '<Comment ' in out.data assert '<Class ' in out.data
def test_save_uml(self): """Saving gaphor.UML model elements. """ self.element_factory.create(UML.Package) self.element_factory.create(UML.Diagram) self.element_factory.create(UML.Comment) self.element_factory.create(UML.Class) out = PseudoFile() storage.save(XMLWriter(out), factory=self.element_factory) out.close() assert '<Package ' in out.data assert '<Diagram ' in out.data assert '<Comment ' in out.data assert '<Class ' in out.data
def test_save_and_load_of_association_with_two_connected_classes( self, case): c1 = case.create(ClassItem, UML.Class) c2 = case.create(ClassItem, UML.Class) c2.matrix.translate(200, 200) case.diagram.request_update(c2) case.diagram.update_now((c1, c2)) assert tuple(c2.matrix_i2c) == (1, 0, 0, 1, 200, 200) a = case.create(AssociationItem) case.connect(a, a.head, c1) case.connect(a, a.tail, c2) case.diagram.update_now((c1, c2, a)) assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 200, a.tail.pos assert a.tail.pos.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=case.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id case.element_factory.flush() assert not list(case.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=case.element_factory, modeling_language=case.modeling_language) fd.close() diagrams = list(case.kindof(UML.Diagram)) assert len(diagrams) == 1 d = diagrams[0] a = next(d.select(lambda e: isinstance(e, AssociationItem))) assert a.subject is not None assert old_a_subject_id == a.subject.id cinfo_head = a.diagram.connections.get_connection(a.head) assert cinfo_head.connected is not None cinfo_tail = a.diagram.connections.get_connection(a.tail) assert cinfo_tail.connected is not None assert cinfo_head.connected is not cinfo_tail.connected
def test_save_and_load_of_association_with_two_connected_classes(self): c1 = self.create(ClassItem, UML.Class) c2 = self.create(ClassItem, UML.Class) c2.matrix.translate(200, 200) self.diagram.canvas.update_matrix(c2) assert tuple(self.diagram.canvas.get_matrix_i2c(c2)) == (1, 0, 0, 1, 200, 200) a = self.create(AssociationItem) self.connect(a, a.head, c1) self.connect(a, a.tail, c2) self.diagram.canvas.update_now() assert a.head.pos.y == 0, a.head.pos assert a.tail.pos.x == 10, a.tail.pos assert a.tail.pos.y == 200, a.tail.pos assert a.subject fd = StringIO() storage.save(XMLWriter(fd), factory=self.element_factory) data = fd.getvalue() fd.close() old_a_subject_id = a.subject.id self.element_factory.flush() assert not list(self.element_factory.select()) fd = StringIO(data) storage.load(fd, factory=self.element_factory, modeling_language=self.modeling_language) fd.close() diagrams = list(self.kindof(UML.Diagram)) assert 1 == len(diagrams) d = diagrams[0] a = d.canvas.select(lambda e: isinstance(e, AssociationItem))[0] assert a.subject is not None assert old_a_subject_id == a.subject.id cinfo_head = a.canvas.get_connection(a.head) assert cinfo_head.connected is not None cinfo_tail = a.canvas.get_connection(a.tail) assert cinfo_tail.connected is not None assert cinfo_head.connected is not cinfo_tail.connected
def test_load_save(self): f = open('test-diagrams/simple-items.gaphor', 'r') storage.load(f, factory=self.element_factory) f.close() pf = PseudoFile() storage.save(XMLWriter(pf), factory=self.element_factory) f = open('test-diagrams/simple-items.gaphor', 'r') orig = f.read() f.close() copy = pf.data expr = re.compile('gaphor-version="[^"]*"') orig = expr.sub('%VER%', orig) copy = expr.sub('%VER%', copy) f = open('tmp.gaphor', 'w') f.write(copy) f.close() self.assertEquals(copy, orig)