source: main/waeup.kofa/branches/uli-fix-zc-async/src/waeup/kofa/tests/test_objecthistory.py @ 17952

Last change on this file since 17952 was 9126, checked in by Henrik Bettermann, 12 years ago

Catch errors and return message removed.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1## $Id: test_objecthistory.py 9126 2012-08-30 08:11:58Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Test object history.
19"""
20import grok
21import shutil
22import tempfile
23from persistent.list import PersistentList
24from zope.interface.verify import verifyObject, verifyClass
25from zope.security.management import newInteraction, endInteraction
26from zope.security.testing import Principal, Participation
27from waeup.kofa.app import University
28from waeup.kofa.interfaces import IObjectHistory, IKofaObject
29from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer
30from waeup.kofa.objecthistory import ObjectHistory
31
32class SampleObject(grok.Model):
33    grok.implements(IKofaObject)
34    pass
35
36class ObjectHistoryTests(FunctionalTestCase):
37    # Tests for helpers like get_access_code, disable_accesscode, ...
38
39    layer = FunctionalLayer
40
41    def setUp(self):
42        super(ObjectHistoryTests, self).setUp()
43
44        # Prepopulate ZODB
45        app = University()
46        self.dc_root = tempfile.mkdtemp()
47        app['datacenter'].setStoragePath(self.dc_root)
48
49        # Prepopulate the ZODB...
50        self.getRootFolder()['app'] = app
51        self.app = self.getRootFolder()['app']
52        self.obj = SampleObject()
53
54    def tearDown(self):
55        shutil.rmtree(self.dc_root)
56        super(ObjectHistoryTests, self).tearDown()
57        endInteraction() # Just in case, one is still lingering around
58
59    def test_iface(self):
60        # ObjectHistory class and instances provide the promised ifaces
61        hist = IObjectHistory(self.obj)
62        assert verifyObject(IObjectHistory, hist)
63        assert verifyClass(IObjectHistory, ObjectHistory)
64
65    def test_adapter(self):
66        # We can get a history by adapting to IObjectHistory
67        result = IObjectHistory(self.obj)
68        assert isinstance(result, ObjectHistory)
69
70    def test_add_messages(self):
71        # We can add a message
72        hist = IObjectHistory(self.obj)
73        assert hist.messages == []
74        hist.addMessage('blah')
75        assert 'blah' in ''.join(hist.messages)
76
77    def test_add_messages_timestamp_and_user(self):
78        # Messages added get a timestamp and the current user
79        hist = IObjectHistory(self.obj)
80        hist.addMessage('blah')
81        result = ''.join(hist.messages)
82        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blah by system', result)
83
84    def test_add_messages_existing_principal(self):
85        principal = Principal('bob')
86        principal.title = 'Bob'
87        newInteraction(Participation(principal)) # set current user
88        hist = IObjectHistory(self.obj)
89        hist.addMessage('blah')
90        result = ''.join(hist.messages)
91        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blah by Bob', result)
92
93    def test_modify_messages(self):
94        principal = Principal('bob')
95        principal.title = 'Bob'
96        newInteraction(Participation(principal)) # set current user
97        hist = IObjectHistory(self.obj)
98        hist.addMessage('blah')
99        hist.modifyMessages('blah', 'blow')
100        result = ''.join(hist.messages)
101        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blow by Bob', result)
102
103    def test_remove_message(self):
104        principal = Principal('bob')
105        principal.title = 'Bob'
106        newInteraction(Participation(principal)) # set current user
107        hist = IObjectHistory(self.obj)
108        hist.addMessage('blah')
109        hist.addMessage('blow')
110        self.assertEqual(len(hist._getMessages()), 2)
111        result = ' '.join(hist.messages)
112        self.assertTrue('blah by Bob' in result)
113        self.assertTrue('blow by Bob' in result)
114        success, text = hist.removeMessage('xyz')
115        self.assertFalse(success)
116        self.assertEqual(text, 'Not a number')
117        success, text = hist.removeMessage(100)
118        self.assertFalse(success)
119        self.assertEqual(text, 'Number out of range')
120        success, text = hist.removeMessage(1)
121        self.assertTrue(success)
122        self.assertMatches('<YYYY-MM-DD hh:mm:ss> UTC - blow by Bob', text)
123        self.assertEqual(len(hist.messages), 1)
124        result = ' '.join(hist.messages)
125        self.assertFalse('blow by Bob' in result)
126
127    def test_messages(self):
128        # we get messages as a persistent list of strings
129        hist = IObjectHistory(self.obj)
130        hist.addMessage('foo')
131        hist.addMessage('bar')
132        assert isinstance(hist.messages, PersistentList)
133        assert 'foo' in hist.messages[0]
134        assert 'bar' in hist.messages[1]
Note: See TracBrowser for help on using the repository browser.