[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Undo/tests - testUndo.py:1.1.2.1 testZODBUndoManager.py:1.1.2.1

Chris Humphries zopemonkey@yahoo.com
Sat, 23 Mar 2002 18:23:35 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Undo/tests
In directory cvs.zope.org:/tmp/cvs-serv31793/Undo/tests

Added Files:
      Tag: Zope-3x-branch
	testUndo.py testZODBUndoManager.py 
Log Message:

Undo: provides undo ability, like in zope 2
===========================================
undo_log.pt 	-> ZPT file for View
Undo.py		-> View
IUndoManager.py	-> Interface for undo
ZODBUndManager	-> Utility for View to do the
                   actual work. 
__init__.py	-> newline/requirement





=== Added File Zope3/lib/python/Zope/App/Undo/tests/testUndo.py ===

##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""

Revision information:
$Id: testUndo.py,v 1.1.2.1 2002/03/23 23:23:34 drauku Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup

from Zope.App.Undo.IUndoManager import IUndoManager
from Zope.App.Undo.Undo import Undo

class TestIUndoManager:
    __implements__ = IUndoManager

    def __init__(self):
        dict1 = {'id': '1', 'user_name': 'monkey', 'description': 'thing1', 'time': 'today'}
        dict2 = {'id': '2', 'user_name': 'monkey', 'description': 'thing2', 'time': 'today'}
        dict3 = {'id': '3', 'user_name': 'monkey', 'description': 'thing3', 'time': 'today'}

        self.dummy_db = [dict1, dict2, dict3]

    def getUndoInfo(self):
        return self.dummy_db

    def undoTransaction(self, id_list):
        # just remove an element for now
        temp_dict = {}
        for db_record in self.dummy_db:
            if db_record['id'] not in id_list:
                temp_dict[db_record['id']] =  db_record

        self.dummy_db = []
        for key in temp_dict.keys():
            self.dummy_db.append(temp_dict[key])
                

class Test(CleanUp, TestCase):
    def setUp(self):
        from Zope.ComponentArchitecture import provideUtility

        provideUtility(IUndoManager, TestIUndoManager()) 

        
    def testGetUndoInfo(self):
        view = Undo(None)

        self.assertEqual(view.getUndoInfo(), TestIUndoManager().getUndoInfo())
        

    def testUndoSingleTransaction(self):
        view = Undo(None)
        id_list = ['1']
        view.action(id_list)

        testum = TestIUndoManager()
        testum.undoTransaction(id_list)

        self.assertEqual(view.getUndoInfo(), testum.getUndoInfo())

    def testUndoManyTransactions(self):
        view = Undo(None)
        id_list = ['1','2','3']
        view.action(id_list)

        testum = TestIUndoManager()
        testum.undoTransaction(id_list)

        self.assertEqual(view.getUndoInfo(), testum.getUndoInfo())

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')


=== Added File Zope3/lib/python/Zope/App/Undo/tests/testZODBUndoManager.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# 
##############################################################################
"""

Revision information:
$Id: testZODBUndoManager.py,v 1.1.2.1 2002/03/23 23:23:34 drauku Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
from time import time

def dict(**kw): return kw

testdata = [
    dict(id='1', user_name='jim', time=time(), description='des 1'),
    dict(id='2', user_name='jim', time=time(), description='des 2'),
    dict(id='3', user_name='jim', time=time(), description='des 3'),
    dict(id='4', user_name='jim', time=time(), description='des 4'),
    dict(id='5', user_name='jim', time=time(), description='des 5'),
    dict(id='6', user_name='jim', time=time(), description='des 6'),
    dict(id='7', user_name='jim', time=time(), description='des 7'),
    ]
testdata.reverse()

class StubDB:

    def __init__(self):
        self.data = list(testdata)

    def undoLog(self):
        return tuple(self.data)

    def undo(self, id):
        self.data = [d for d in self.data if d['id'] != id]

class Test(CleanUp, TestCase):

    def test(self):
        from Zope.App.Undo.ZODBUndoManager import ZODBUndoManager
        um = ZODBUndoManager(StubDB())

        self.assertEqual(list(um.getUndoInfo()), testdata)

        um.undoTransaction(('3','4','5'))
        expected = testdata
        expected = [d for d in expected if (d['id'] not in ('3','4','5'))]
        
        self.assertEqual(list(um.getUndoInfo()), expected)
            
    

def test_suite():
    return TestSuite((
        makeSuite(Test),
        ))

if __name__=='__main__':
    main(defaultTest='test_suite')