#!/usr/bin/env python
#
# This tests checks for the functionality of short-circuiting
# transaction commands. The point is that modifying the same slot
# several times will not produce multiple ElementSlotChanges commands
# it is adequate to just remember the previous contents of the slot
# and the current contents, the intermediate values are uninteresting.
# This functionality means that many routines will work faster
# since a ElementSlotChanges for a specific <element, property> pair
# will only occur once in a transaction
#

import coral

st = coral.Project()
m = coral.lang.UML14.Model()
m.__setProject__(st)
a = []
for i in range(10):
    a.append(coral.lang.UML14.Class())

tm = coral.getTransactionManager()
tm.beginModifyCmd()
try:
    for c in a:
        c.namespace = m

finally:
    tm.endModifyCmd()

tr = tm.getCurrentTransaction()
print tr
assert tr.size() == 11


assert len(m.ownedElement) == 10
for c in a:
    assert c.namespace == m
    assert m.ownedElement.getCollection().includes(c)


tm.undo()
assert len(m.ownedElement) == 0
for c in a:
    assert c.namespace is None

tm.redo()
assert len(m.ownedElement) == 10
for c in a:
    assert c.namespace == m
    assert m.ownedElement.getCollection().includes(c)

