source: main/waeup.kofa/trunk/src/waeup/kofa/tests/test_objecthistory.py @ 9091

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

ObjectHistory? method, helper function and view to modify (manipulate) student history messages.

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1## $Id: test_objecthistory.py 9012 2012-07-18 06:47:21Z 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_messages(self):
104        # we get messages as a persistent list of strings
105        hist = IObjectHistory(self.obj)
106        hist.addMessage('foo')
107        hist.addMessage('bar')
108        assert isinstance(hist.messages, PersistentList)
109        assert 'foo' in hist.messages[0]
110        assert 'bar' in hist.messages[1]
Note: See TracBrowser for help on using the repository browser.