| 1 |
""" |
|---|
| 2 |
Basic test case for Gaphor tests. |
|---|
| 3 |
|
|---|
| 4 |
Everything is about services so the TestCase can define it's required |
|---|
| 5 |
services and start off. |
|---|
| 6 |
""" |
|---|
| 7 |
|
|---|
| 8 |
import unittest |
|---|
| 9 |
from cStringIO import StringIO |
|---|
| 10 |
|
|---|
| 11 |
from gaphor import UML |
|---|
| 12 |
from gaphor import storage |
|---|
| 13 |
from gaphor.application import Application |
|---|
| 14 |
from gaphor.misc.xmlwriter import XMLWriter |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
log.set_log_level(log.WARNING) |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class TestCase(unittest.TestCase): |
|---|
| 21 |
|
|---|
| 22 |
services = ['element_factory'] |
|---|
| 23 |
|
|---|
| 24 |
def setUp(self): |
|---|
| 25 |
Application.init(services=self.services) |
|---|
| 26 |
self.element_factory = Application.get_service('element_factory') |
|---|
| 27 |
assert len(list(self.element_factory.select())) == 0, list(self.element_factory.select()) |
|---|
| 28 |
self.diagram = self.element_factory.create(UML.Diagram) |
|---|
| 29 |
assert len(list(self.element_factory.select())) == 1, list(self.element_factory.select()) |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
def tearDown(self): |
|---|
| 33 |
Application.shutdown() |
|---|
| 34 |
self.element_factory.flush() |
|---|
| 35 |
self.element_factory.shutdown() |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
def get_service(self, name): |
|---|
| 39 |
return Application.get_service(name) |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
def create(self, item_cls, subject_cls=None): |
|---|
| 43 |
""" |
|---|
| 44 |
Create an item with specified subject. |
|---|
| 45 |
""" |
|---|
| 46 |
if subject_cls is None: |
|---|
| 47 |
subject = None |
|---|
| 48 |
else: |
|---|
| 49 |
subject = self.element_factory.create(subject_cls) |
|---|
| 50 |
item = self.diagram.create(item_cls, subject=subject) |
|---|
| 51 |
self.diagram.canvas.update() |
|---|
| 52 |
return item |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
def save(self): |
|---|
| 56 |
""" |
|---|
| 57 |
Save diagram into string. |
|---|
| 58 |
""" |
|---|
| 59 |
f = StringIO() |
|---|
| 60 |
storage.save(XMLWriter(f), factory=self.element_factory) |
|---|
| 61 |
data = f.getvalue() |
|---|
| 62 |
f.close() |
|---|
| 63 |
|
|---|
| 64 |
self.element_factory.flush() |
|---|
| 65 |
assert not list(self.element_factory.select()) |
|---|
| 66 |
assert not list(self.element_factory.lselect()) |
|---|
| 67 |
return data |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
def load(self, data): |
|---|
| 71 |
""" |
|---|
| 72 |
Load data from specified string. Update ``TestCase.diagram`` |
|---|
| 73 |
attribute to hold new loaded diagram. |
|---|
| 74 |
""" |
|---|
| 75 |
f = StringIO(data) |
|---|
| 76 |
storage.load(f, factory=self.element_factory) |
|---|
| 77 |
f.close() |
|---|
| 78 |
|
|---|
| 79 |
self.diagram = self.element_factory.lselect(lambda e: e.isKindOf(UML.Diagram))[0] |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
main = unittest.main |
|---|
| 83 |
|
|---|
| 84 |
|
|---|