2020-01-26 21:32:04 +03:00
import pytest
2018-09-21 20:46:40 +03:00
from gaphor import UML
2020-01-26 21:32:04 +03:00
from gaphor . application import Application
2020-08-18 17:59:20 +03:00
from gaphor . core import Transaction
2020-12-22 12:50:48 +03:00
from gaphor . diagram . tests . fixtures import connect
2020-04-12 21:24:39 +03:00
from gaphor . UML . classes import AssociationItem , ClassItem
2020-01-26 21:32:04 +03:00
@pytest.fixture
def application ( ) :
app = Application ( )
yield app
app . shutdown ( )
@pytest.fixture
def session ( application ) :
return application . new_session ( )
2020-08-18 17:59:20 +03:00
@pytest.fixture
def event_manager ( session ) :
return session . get_service ( " event_manager " )
2020-01-26 21:32:04 +03:00
@pytest.fixture
def element_factory ( session ) :
return session . get_service ( " element_factory " )
@pytest.fixture
def undo_manager ( session ) :
return session . get_service ( " undo_manager " )
2020-08-18 17:59:20 +03:00
def test_class_association_undo_redo ( event_manager , element_factory , undo_manager ) :
2020-01-26 21:32:04 +03:00
diagram = element_factory . create ( UML . Diagram )
2010-02-10 10:25:22 +03:00
2020-12-22 12:50:48 +03:00
assert 0 == len ( diagram . connections . solver . constraints )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
ci1 = diagram . create ( ClassItem , subject = element_factory . create ( UML . Class ) )
2020-12-22 12:50:48 +03:00
assert 6 == len ( diagram . connections . solver . constraints )
2018-11-24 00:44:52 +03:00
2020-01-26 21:32:04 +03:00
ci2 = diagram . create ( ClassItem , subject = element_factory . create ( UML . Class ) )
2020-12-22 12:50:48 +03:00
assert 12 == len ( diagram . connections . solver . constraints )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
a = diagram . create ( AssociationItem )
2018-11-23 01:14:07 +03:00
2020-01-26 21:32:04 +03:00
connect ( a , a . head , ci1 )
connect ( a , a . tail , ci2 )
2010-02-10 10:25:22 +03:00
2020-10-28 12:53:54 +03:00
# Diagram, Association, 2x Class, Property, LiteralSpecification
assert 6 == len ( element_factory . lselect ( ) )
2020-12-22 12:50:48 +03:00
assert 14 == len ( diagram . connections . solver . constraints )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
undo_manager . clear_undo_stack ( )
assert not undo_manager . can_undo ( )
2010-02-10 10:25:22 +03:00
2020-08-18 17:59:20 +03:00
with Transaction ( event_manager ) :
ci2 . unlink ( )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
assert undo_manager . can_undo ( )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
def get_connected ( handle ) :
2020-09-07 22:28:20 +03:00
""" Get item connected to line via handle. """
2020-12-22 12:50:48 +03:00
cinfo = diagram . connections . get_connection ( handle )
2020-01-26 21:32:04 +03:00
if cinfo :
return cinfo . connected
return None
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
assert ci1 == get_connected ( a . head )
assert None is get_connected ( a . tail )
2010-02-10 10:25:22 +03:00
2020-01-26 21:32:04 +03:00
for i in range ( 3 ) :
2020-12-22 12:50:48 +03:00
assert 7 == len ( diagram . connections . solver . constraints )
2010-02-11 08:42:27 +03:00
2020-01-26 21:32:04 +03:00
undo_manager . undo_transaction ( )
2010-02-11 08:42:27 +03:00
2020-12-22 12:50:48 +03:00
assert 14 == len ( diagram . connections . solver . constraints )
2010-02-11 08:42:27 +03:00
2020-01-26 21:32:04 +03:00
assert ci1 == get_connected ( a . head )
assert ci2 == get_connected ( a . tail )
2010-02-11 08:42:27 +03:00
2020-01-26 21:32:04 +03:00
undo_manager . redo_transaction ( )
2020-08-18 17:59:20 +03:00
def test_diagram_item_should_not_end_up_in_element_factory (
event_manager , element_factory , undo_manager
) :
diagram = element_factory . create ( UML . Diagram )
with Transaction ( event_manager ) :
cls = diagram . create ( ClassItem , subject = element_factory . create ( UML . Class ) )
undo_manager . undo_transaction ( )
undo_manager . redo_transaction ( )
assert cls not in element_factory . lselect ( ) , element_factory . lselect ( )
def test_deleted_diagram_item_should_not_end_up_in_element_factory (
event_manager , element_factory , undo_manager
) :
diagram = element_factory . create ( UML . Diagram )
cls = diagram . create ( ClassItem , subject = element_factory . create ( UML . Class ) )
with Transaction ( event_manager ) :
cls . unlink ( )
undo_manager . undo_transaction ( )
assert cls not in element_factory . lselect ( ) , element_factory . lselect ( )
undo_manager . redo_transaction ( )
assert cls not in element_factory . lselect ( ) , element_factory . lselect ( )