[Zope3-checkins] CVS: Zope3/src/zope/app/undo/tests - __init__.py:1.1 test_undo.py:1.1 test_zodbundomanager.py:1.1

Philipp von Weitershausen philikon at philikon.de
Mon Mar 1 09:16:57 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/undo/tests
In directory cvs.zope.org:/tmp/cvs-serv25659/undo/tests

Added Files:
	__init__.py test_undo.py test_zodbundomanager.py 
Log Message:
Moved all undo-related code to zope.app.undo.


=== Added File Zope3/src/zope/app/undo/tests/__init__.py ===
# make this directory a package


=== Added File Zope3/src/zope/app/undo/tests/test_undo.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.
#
##############################################################################
"""Undo Tests

$Id: test_undo.py,v 1.1 2004/03/01 14:16:56 philikon Exp $
"""
from datetime import datetime
from unittest import TestCase, main, makeSuite

from zope.interface import implements
from zope.publisher.browser import TestRequest

from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.undo.interfaces import IUndoManager
from zope.app.undo.browser import UndoView

class TestIUndoManager:
    implements(IUndoManager)

    def __init__(self):
        dict1 = {'id': '1', 'user_name': 'monkey', 'description': 'thing1',
 'time': 'today', 'datetime': datetime(2001, 01, 01, 12, 00, 00)}
        dict2 = {'id': '2', 'user_name': 'monkey', 'description': 'thing2',
 'time': 'today', 'datetime': datetime(2001, 01, 01, 12, 00, 00)}
        dict3 = {'id': '3', 'user_name': 'bonobo', 'description': 'thing3',
 'time': 'today', 'datetime': datetime(2001, 01, 01, 12, 00, 00)}
        dict4 = {'id': '4', 'user_name': 'monkey', 'description': 'thing4',
 'time': 'today', 'datetime': datetime(2001, 01, 01, 12, 00, 00)}
        dict5 = {'id': '5', 'user_name': 'bonobo', 'description': 'thing5',
 'time': 'today', 'datetime': datetime(2001, 01, 01, 12, 00, 00)}

        self.dummy_db = [dict1, dict2, dict3, dict4, dict5]

    def getUndoInfo(self, first=0, last=-20, user_name=None):
        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(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        from zope.component import getService
        getService(None,'Utilities').provideUtility(IUndoManager,
              TestIUndoManager())

    def testGetUndoInfo(self):
        view = UndoView()
        view.context = None
        view.request = TestRequest()
        self.checkResult(view.getUndoInfo())

    def testUndoSingleTransaction(self):
        view = UndoView()
        view.context = None
        view.request = TestRequest()
        id_list = ['1']
        view.action(id_list)

        testum = TestIUndoManager()
        testum.undoTransaction(id_list)

        self.checkResult(view.getUndoInfo())

    def testUndoManyTransactions(self):
        view = UndoView()
        view.context = None
        view.request = TestRequest()
        id_list = ['1','2','3']
        view.action(id_list)

        testum = TestIUndoManager()
        testum.undoTransaction(id_list)
        
        self.checkResult(view.getUndoInfo())

    def checkResult(self, info):
        for entry in info:
            self.assertEqual(entry['datetime'], u'2001 1 1  12:00:00 ')
            self.assertEqual(entry['time'], 'today')
            self.assert_(entry['user_name'] in ('bonobo', 'monkey'))
            self.assertEqual(entry['description'], 'thing'+entry['id'])

def test_suite():
    return makeSuite(Test)

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


=== Added File Zope3/src/zope/app/undo/tests/test_zodbundomanager.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.
#
##############################################################################
"""
$Id: test_zodbundomanager.py,v 1.1 2004/03/01 14:16:56 philikon Exp $
"""

from time import time
from unittest import TestCase, main, makeSuite

from zope.testing.cleanup import CleanUp 
from zope.app.undo.undo import ZODBUndoManager

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='anthony', time=time(), description='des 3'),
    dict(id='4', user_name='jim', time=time(), description='des 4'),
    dict(id='5', user_name='anthony', time=time(), description='des 5'),
    dict(id='6', user_name='anthony', time=time(), description='des 6'),
    dict(id='7', user_name='jim', time=time(), description='des 7'),
    dict(id='8', user_name='anthony', time=time(), description='des 8'),
    dict(id='9', user_name='jim', time=time(), description='des 9'),
    dict(id='10', user_name='jim', time=time(), description='des 10'),
    ]
testdata.reverse()

class StubDB:

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

    def undoInfo(self, first=0, last=-20, specification=None):
        if last < 0:
            last = first - last + 1
        # This code ripped off from zodb.storage.base.BaseStorage.undoInfo
        if specification:
            def filter(desc, spec=specification.items()):
                for k, v in spec:
                    if desc.get(k) != v:
                        return False
                return True
        else:
            filter = None
        if not filter:
            # handle easy case first
            data = self.data[first:last]
        else:
            data = []
            for x in self.data[first:]:
                if filter(x): 
                    data.append(x)
                if len(data) >= last:
                    break
        return data

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

class Test(CleanUp, TestCase):

    def test(self):
        um = ZODBUndoManager(StubDB())

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

        txid = [d['id'] for d in um.getUndoInfo(first=0,last=-3)]
        self.assertEqual(txid, ['10','9','8','7'])
        txid = [d['id'] for d in um.getUndoInfo(first=0,last=3)]
        self.assertEqual(txid, ['10','9','8'])
        txid = [d['id'] 
                for d in um.getUndoInfo(first=0, last=3, user_name='anthony')]
        self.assertEqual(txid, ['8','6','5'])
        txid = [d['id'] for d in um.getUndoInfo(user_name='anthony')]
        self.assertEqual(txid, ['8','6','5','3'])

        um.undoTransaction(('3','4','5'))

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

        txid = [d['id'] for d in um.getUndoInfo(user_name='anthony')]
        self.assertEqual(txid, ['8','6'])


def test_suite():
    return makeSuite(Test)

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




More information about the Zope3-Checkins mailing list