#!/usr/bin/env python
# This test is written to test that a deleted association isn't updated
# and cause errors like in CRL-228

import os
from coral.modeler import modeler
import coral
import time

DI = coral.lang.XMIDI20
UML14 = coral.lang.UML14

m=modeler.ModelerWindow()
project=m.openProject("2classes_with_association.xmi")
m.main(loop=0)

# Find the model and the diagram in the Project
model=None
diagram=None
for e in project.contents():
    if e.oclIsKindOf(UML14.Model):
        model=e
    if e.oclIsKindOf(DI.Diagram):
        diagram=e

assert model
assert diagram

# Find the ONLY association between the classes
association=None
for e in model.__getAllParts__():
    if e.oclIsKindOf(UML14.Association):
        association=e
        break

assert association

# Delete the association
coral.getTransactionManager().beginModifyCmd()
try:
    association.__resetAttrs__()
finally:
    coral.getTransactionManager().endModifyCmd()

# Check that both graphconnectors deleted their references
for e in diagram.contained:
    assert len(e.anchorage[0].graphEdge)==0

# Check that the graphEdge is deleted too
for ge in diagram.contained:
    assert not ge.oclIsKindOf(DI.GraphEdge)
    

