2020-10-11 20:14:21 +02:00
import pytest
from gaphas import item
from gaphas . connections import Connections
from gaphas . constraint import EqualsConstraint
from gaphas . solver import Solver
@pytest.fixture
def connections ( ) :
2020-11-03 08:55:21 +01:00
return Connections ( )
def test_connections_with_custom_solver ( ) :
solver = Solver ( )
connections = Connections ( solver )
assert connections . solver is solver
2020-10-11 20:14:21 +02:00
def test_reconnect_item ( connections ) :
2020-11-01 22:43:58 +01:00
i = item . Line ( connections )
ii = item . Line ( connections )
2020-10-11 20:14:21 +02:00
cons1 = EqualsConstraint ( i . handles ( ) [ 0 ] . pos . x , i . handles ( ) [ 0 ] . pos . x )
cons2 = EqualsConstraint ( i . handles ( ) [ 0 ] . pos . y , i . handles ( ) [ 0 ] . pos . y )
connections . connect_item ( i , i . handles ( ) [ 0 ] , ii , ii . ports ( ) [ 0 ] , cons1 )
assert connections . get_connection ( i . handles ( ) [ 0 ] ) . constraint is cons1
assert cons1 in connections . solver . constraints
connections . reconnect_item ( i , i . handles ( ) [ 0 ] , constraint = cons2 )
assert connections . get_connection ( i . handles ( ) [ 0 ] ) . constraint is cons2
assert cons1 not in connections . solver . constraints
assert cons2 in connections . solver . constraints
2020-11-01 19:46:35 +01:00
def test_add_item_constraint ( connections ) :
2020-11-01 22:43:58 +01:00
i = item . Line ( connections )
2020-11-01 19:46:35 +01:00
c1 = EqualsConstraint ( i . handles ( ) [ 0 ] . pos . x , i . handles ( ) [ 0 ] . pos . x )
connections . add_constraint ( i , c1 )
cinfo = next ( connections . get_connections ( item = i ) )
assert cinfo . item is i
assert cinfo . constraint is c1
def test_remove_item_constraint ( connections ) :
2020-11-01 22:43:58 +01:00
i = item . Line ( connections )
2020-11-01 19:46:35 +01:00
c1 = EqualsConstraint ( i . handles ( ) [ 0 ] . pos . x , i . handles ( ) [ 0 ] . pos . x )
connections . add_constraint ( i , c1 )
connections . remove_constraint ( i , c1 )
assert list ( connections . get_connections ( item = i ) ) == [ ]
def test_remove_item_constraint_when_item_is_disconnected ( connections ) :
2020-11-01 22:43:58 +01:00
i = item . Line ( connections )
2020-11-01 19:46:35 +01:00
c1 = EqualsConstraint ( i . handles ( ) [ 0 ] . pos . x , i . handles ( ) [ 0 ] . pos . x )
connections . add_constraint ( i , c1 )
connections . disconnect_item ( i )
assert list ( connections . get_connections ( item = i ) ) == [ ]