[Zope3-checkins] CVS: Zope3/src/zope/app/undo/tests -
test_undoview.py:1.1 test_zodbundomanager.py:1.4 test_undo.py:NONE
Philipp von Weitershausen
philikon at philikon.de
Sun Mar 21 12:20:29 EST 2004
Update of /cvs-repository/Zope3/src/zope/app/undo/tests
In directory cvs.zope.org:/tmp/cvs-serv20580/src/zope/app/undo/tests
Modified Files:
test_zodbundomanager.py
Added Files:
test_undoview.py
Removed Files:
test_undo.py
Log Message:
Implement steps 4 and 5 of
http://dev.zope.org/Zope3/SimplifyUndoModel:
- Provided new interfaces IUndo, IPrincipalUndo, IUndoManager
- Changed ZODBUndoManager to implement IUndoManager.
- Implemented a new UI for ZODBUndoManager
=== Added File Zope3/src/zope/app/undo/tests/test_undoview.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_undoview.py,v 1.1 2004/03/21 17:20:28 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.tests import ztapi
from zope.app.site.tests.placefulsetup import PlacefulSetup
from zope.app.undo.interfaces import IUndoManager
from zope.app.undo.browser import UndoView
from zope.app.security.principalregistry import principalRegistry
class TestIUndoManager:
implements(IUndoManager)
def __init__(self):
self.dummy_db = [
dict(id='1', user_name='monkey', description='thing1',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
dict(id='2', user_name='monkey', description='thing2',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
dict(id='3', user_name='bonobo', description='thing3',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
dict(id='4', user_name='monkey', description='thing4',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
dict(id='5', user_name='bonobo', description='thing5',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
dict(id='6', user_name='bonobo', description='thing6',
time='today', datetime=datetime(2001, 01, 01, 12, 00, 00)),
]
self.dummy_db.reverse()
def getTransactions(self, context=None, first=0, last=-20):
self.context = context
list = [dict for dict in self.dummy_db
if not dict.get('undone', False)]
return list[first:abs(last)]
def getPrincipalTransactions(self, principal, context=None,
first=0, last=-20):
self.principal = principal
self.context = context
list = [dict for dict in self.dummy_db
if dict['user_name'] == principal.id
and not dict.get('undone', False)]
return list[first:abs(last)]
def _emulatePublication(self, request):
self._user_name = request.user.id
def undoTransactions(self, ids):
# just remove an element for now
for db_record in self.dummy_db:
if db_record['id'] in ids:
db_record['undone'] = True
self.dummy_db.insert(0, dict(
id=str(len(self.dummy_db)+1), user_name=self._user_name,
description='undo', undo=True,
datetime=datetime(2001, 01, 01, 12, 00, 00)
))
def undoPrincipalTransactions(self, principal, ids):
self.principal = principal
self.undoTransactions(ids)
class Test(PlacefulSetup, TestCase):
def setUp(self):
super(Test, self).setUp()
self.request = TestRequest()
self.undo = TestIUndoManager()
ztapi.provideUtility(IUndoManager, self.undo)
principalRegistry.definePrincipal('monkey', 'Monkey Patch',
login='monkey')
principalRegistry.definePrincipal('bonobo', 'Bonobo Abdul-Fasil',
login='bonobo')
def testPrincipalLastTransactionIsUndo(self):
request = self.request
bonobo = principalRegistry.getPrincipal('bonobo')
request.setUser(bonobo)
view = UndoView(self.folder1, request)
self.failIf(view.principalLastTransactionIsUndo())
# now undo the last transaction
self.undo._emulatePublication(request)
self.undo.undoTransactions(['6'])
self.assert_(view.principalLastTransactionIsUndo())
def testUndoPrincipalLastTransaction(self):
request = self.request
bonobo = principalRegistry.getPrincipal('bonobo')
request.setUser(bonobo)
self.undo._emulatePublication(request)
view = UndoView(self.folder1, request)
view.undoPrincipalLastTransaction()
last_txn = self.undo.getTransactions(None, 0, 1)[0]
self.assert_(last_txn.has_key('undo'))
self.assert_(last_txn['undo'])
self.assertEqual(last_txn['user_name'], bonobo.id)
def testGetAllTransactions(self):
request = self.request
view = UndoView(self.folder1, request)
expected = self.undo.getTransactions()
self.assertEqual(view.getAllTransactions(), expected)
self.assert_(self.undo.context is self.folder1)
# test showall parameter
view.getAllTransactions(showall=True)
self.assert_(self.undo.context is None)
def testGetPrincipalTransactions(self):
request = self.request
bonobo = principalRegistry.getPrincipal('bonobo')
request.setUser(bonobo)
view = UndoView(self.folder1, request)
expected = self.undo.getPrincipalTransactions(bonobo)
self.assertEqual(view.getPrincipalTransactions(), expected)
self.assert_(self.undo.context is self.folder1)
self.assert_(self.undo.principal is bonobo)
# test showall parameter and principal
self.undo.principal = None
view.getPrincipalTransactions(showall=True)
self.assert_(self.undo.context is None)
self.assert_(self.undo.principal is bonobo)
def testUndoPrincipalTransactions(self):
request = self.request
bonobo = principalRegistry.getPrincipal('bonobo')
request.setUser(bonobo)
view = UndoView(self.folder1, request)
# Just test whether it accepts the principal. We know that
# our undo dummy above "works".
self.undo._emulatePublication(request)
view.undoPrincipalTransactions(['6'])
self.assert_(self.undo.principal is bonobo)
def test_suite():
return makeSuite(Test)
if __name__=='__main__':
main(defaultTest='test_suite')
=== Zope3/src/zope/app/undo/tests/test_zodbundomanager.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/undo/tests/test_zodbundomanager.py:1.3 Fri Mar 19 13:34:02 2004
+++ Zope3/src/zope/app/undo/tests/test_zodbundomanager.py Sun Mar 21 12:20:28 2004
@@ -17,27 +17,35 @@
from time import time
from unittest import TestCase, main, makeSuite
+from transaction import get_transaction
from zope.testing.cleanup import CleanUp
-from zope.app.undo import ZODBUndoManager
+from zope.app.tests import ztapi
+from zope.app.tests.placelesssetup import PlacelessSetup
-def dict(**kw): return kw
+from zope.app.undo import ZODBUndoManager
+from zope.app.undo.interfaces import UndoError
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='1', user_name='/ jim', time=time(), description='des 1',
+ location='/spam/1'),
+ dict(id='2', user_name='/ jim', time=time(), description='des 2',
+ location='/parrot/2'),
+ dict(id='3', user_name='/ anthony', time=time(), description='des 3',
+ location='/spam/spam/3'),
+ dict(id='4', user_name='/ jim', time=time(), description='des 4',
+ location='/spam/parrot/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='7', user_name='/ jim', time=time(), description='des 7',
+ location='/spam/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:
+class StubDB(object):
def __init__(self):
self.data = list(testdata)
@@ -69,31 +77,84 @@
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())
+class Test(PlacelessSetup, TestCase):
- self.assertEqual(list(um.getUndoInfo()), testdata)
+ def setUp(self):
+ super(Test, self).setUp()
- 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'))
+ # provide location adapter
+ from zope.app.location import LocationPhysicallyLocatable
+ from zope.app.location.interfaces import ILocation
+ from zope.app.traversing.interfaces import IPhysicallyLocatable
+ ztapi.provideAdapter(ILocation, IPhysicallyLocatable,
+ LocationPhysicallyLocatable)
+
+ # define principals
+ from zope.app.security.principalregistry import principalRegistry
+ principalRegistry.definePrincipal('jim', 'Jim Fulton', login='jim')
+ principalRegistry.definePrincipal('anthony', 'Anthony Baxter',
+ login='anthony')
+ self.undo = ZODBUndoManager(StubDB())
+ self.data = list(testdata)
- expected = [d for d in testdata if (d['id'] not in ('3','4','5'))]
- self.assertEqual(list(um.getUndoInfo()), expected)
+ def testGetTransactions(self):
+ self.assertEqual(list(self.undo.getTransactions()), self.data)
- txid = [d['id'] for d in um.getUndoInfo(user_name='anthony')]
- self.assertEqual(txid, ['8','6'])
+ def testGetPrincipalTransactions(self):
+ self.assertRaises(TypeError, self.undo.getPrincipalTransactions, None)
+ from zope.app.security.principalregistry import principalRegistry
+ jim = principalRegistry.getPrincipal('jim')
+ expected = [dict for dict in self.data if dict['user_name'] == '/ jim']
+ self.assertEqual(list(self.undo.getPrincipalTransactions(jim)),
+ expected)
+
+ def testGetTransactionsInLocation(self):
+ from zope.interface import directlyProvides
+ from zope.app.location import Location
+ from zope.app.traversing.interfaces import IContainmentRoot
+
+ root = Location()
+ spam = Location()
+ spam.__name__ = 'spam'
+ spam.__parent__ = root
+ directlyProvides(root, IContainmentRoot)
+
+ expected = [dict for dict in self.data if 'location' in dict
+ and dict['location'].startswith('/spam')]
+ self.assertEqual(list(self.undo.getTransactions(spam)), expected)
+
+ # now test this with getPrincipalTransactions()
+ from zope.app.security.principalregistry import principalRegistry
+ jim = principalRegistry.getPrincipal('jim')
+ expected = [dict for dict in expected if dict['user_name'] == '/ jim']
+ self.assertEqual(list(self.undo.getPrincipalTransactions(jim, spam)),
+ expected)
+
+ def testUndoTransactions(self):
+ ids = ('3','4','5')
+ self.undo.undoTransactions(ids)
+ expected = [d for d in testdata if (d['id'] not in ids)]
+ self.assertEqual(list(self.undo.getTransactions()), expected)
+
+ # assert that the transaction has been annotated
+ txn = get_transaction()
+ self.assert_(txn._extension.has_key('undo'))
+ self.assert_(txn._extension['undo'] is True)
+
+ def testUndoPrincipalTransactions(self):
+ self.assertRaises(TypeError, self.undo.undoPrincipalTransactions,
+ None, [])
+
+ from zope.app.security.principalregistry import principalRegistry
+ jim = principalRegistry.getPrincipal('jim')
+ self.assertRaises(UndoError, self.undo.undoPrincipalTransactions,
+ jim, ('1','2','3'))
+
+ ids = ('1', '2', '4')
+ self.undo.undoPrincipalTransactions(jim, ids)
+ expected = [d for d in testdata if (d['id'] not in ids)]
+ self.assertEqual(list(self.undo.getTransactions()), expected)
def test_suite():
return makeSuite(Test)
=== Removed File Zope3/src/zope/app/undo/tests/test_undo.py ===
More information about the Zope3-Checkins
mailing list