[Zope-CVS] SVN: book/trunk/test_ Added test files from the test
part chatpers.
Stephan Richter
srichter at cosmos.phy.tufts.edu
Mon Aug 30 20:43:22 EDT 2004
Log message for revision 27356:
Added test files from the test part chatpers.
Changed:
A book/trunk/test_sample.py
A book/trunk/test_sampledoc.py
A book/trunk/test_sampleiface.py
A book/trunk/test_zptpage.py
-=-
Added: book/trunk/test_sample.py
===================================================================
--- book/trunk/test_sample.py 2004-08-31 00:41:36 UTC (rev 27355)
+++ book/trunk/test_sample.py 2004-08-31 00:43:22 UTC (rev 27356)
@@ -0,0 +1,70 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""Code for the Zope 3 Book's Unit Tests Chapter
+
+$Id$
+"""
+import unittest
+
+class Sample(object):
+ """A trivial Sample object."""
+
+ title = None
+
+ def __init__(self):
+ """Initialize object."""
+ self._description = ''
+
+ def setDescription(self, value):
+ """Change the value of the description."""
+ assert isinstance(value, (str, unicode))
+ self._description = value
+
+ def getDescription(self):
+ """Change the value of the description."""
+ return self._description
+
+
+class SampleTest(unittest.TestCase):
+ """Test the Sample class"""
+
+ def test_title(self):
+ sample = Sample()
+ self.assertEqual(sample.title, None)
+ sample.title = 'Sample Title'
+ self.assertEqual(sample.title, 'Sample Title')
+
+ def test_getDescription(self):
+ sample = Sample()
+ self.assertEqual(sample.getDescription(), '')
+ sample._description = "Description"
+ self.assertEqual(sample.getDescription(), 'Description')
+
+ def test_setDescription(self):
+ sample = Sample()
+ self.assertEqual(sample._description, '')
+ sample.setDescription('Description')
+ self.assertEqual(sample._description, 'Description')
+ sample.setDescription(u'Description2')
+ self.assertEqual(sample._description, u'Description2')
+ self.assertRaises(AssertionError, sample.setDescription, None)
+
+
+def test_suite():
+ return unittest.TestSuite((
+ unittest.makeSuite(SampleTest),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: book/trunk/test_sampledoc.py
===================================================================
--- book/trunk/test_sampledoc.py 2004-08-31 00:41:36 UTC (rev 27355)
+++ book/trunk/test_sampledoc.py 2004-08-31 00:43:22 UTC (rev 27356)
@@ -0,0 +1,79 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""Code for the Cookbook's Doc Tests Recipe
+
+$Id: test_sample.py,v 1.1.1.1 2004/02/18 18:07:06 srichter Exp $
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+class Sample(object):
+ """A trivial Sample object.
+
+ Examples::
+
+ >>> sample = Sample()
+
+ Here you can see how the 'title' attribute works.
+
+ >>> print sample.title
+ None
+ >>> sample.title = 'Title'
+ >>> print sample.title
+ Title
+
+ The description is implemented using a accessor and mutator method
+
+ >>> sample.getDescription()
+ ''
+ >>> sample.setDescription('Hello World')
+ >>> sample.getDescription()
+ 'Hello World'
+ >>> sample.setDescription(u'Hello World')
+ >>> sample.getDescription()
+ u'Hello World'
+
+ 'setDescription()' only accepts regular and unicode strings
+
+ >>> sample.setDescription(None)
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in ?
+ File "test_sample.py", line 31, in setDescription
+ assert isinstance(value, (str, unicode))
+ AssertionError
+ """
+
+ title = None
+
+ def __init__(self):
+ """Initialize object."""
+ self._description = ''
+
+ def setDescription(self, value):
+ """Change the value of the description."""
+ assert isinstance(value, (str, unicode))
+ self._description = value
+
+ def getDescription(self):
+ """Change the value of the description."""
+ return self._description
+
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite(),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: book/trunk/test_sampleiface.py
===================================================================
--- book/trunk/test_sampleiface.py 2004-08-31 00:41:36 UTC (rev 27355)
+++ book/trunk/test_sampleiface.py 2004-08-31 00:43:22 UTC (rev 27356)
@@ -0,0 +1,137 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""Demonstration of writing unit tests for interfaces
+
+$Id: test_sampleiface.py,v 1.1.1.1 2004/02/18 18:07:00 srichter Exp $
+"""
+import unittest
+from zope.interface import implements, Interface, Attribute
+from zope.interface.verify import verifyObject
+
+class ISample(Interface):
+ """This is a Sample."""
+
+ title = Attribute('The title of the sample')
+
+ def setDescription(value):
+ """Set the description of the Sample.
+
+ Only regular and unicode values should be accepted.
+ """
+
+ def getDescription():
+ """Return the value of the description."""
+
+
+class Sample1(object):
+ """A trivial ISample implementation."""
+
+ implements(ISample)
+
+ # See ISample
+ title = None
+
+ def __init__(self):
+ """Create objects."""
+ self._description = ''
+
+ def setDescription(self, value):
+ """See ISample"""
+ assert isinstance(value, (str, unicode))
+ self._description = value
+
+ def getDescription(self):
+ """See ISample"""
+ return self._description
+
+
+class Sample2(object):
+ """A trivial ISample implementation."""
+
+ implements(ISample)
+
+ def __init__(self):
+ """Create objects."""
+ self.__desc = ''
+ self.__title = None
+
+ def getTitle(self):
+ return self.__title
+
+ def setTitle(self, value):
+ self.__title = value
+
+ def setDescription(self, value):
+ """See ISample"""
+ assert isinstance(value, (str, unicode))
+ self.__desc = value
+
+ def getDescription(self):
+ """See ISample"""
+ return self.__desc
+
+ description = property(getDescription, setDescription)
+
+ # See ISample
+ title = property(getTitle, setTitle)
+
+
+class TestISample(unittest.TestCase):
+ """Test the ISample interface"""
+
+ def makeTestObject(self):
+ """Returns an ISample instance"""
+ raise NotImplemented()
+
+ def test_verifyInterfaceImplementation(self):
+ self.assert_(verifyObject(ISample, self.makeTestObject()))
+
+ def test_title(self):
+ sample = self.makeTestObject()
+ self.assertEqual(sample.title, None)
+ sample.title = 'Sample Title'
+ self.assertEqual(sample.title, 'Sample Title')
+
+ def test_setgetDescription(self):
+ sample = self.makeTestObject()
+ self.assertEqual(sample.getDescription(), '')
+ sample.setDescription('Description')
+ self.assertEqual(sample.getDescription(), 'Description')
+ self.assertRaises(AssertionError, sample.setDescription, None)
+
+
+class TestSample1(TestISample):
+
+ def makeTestObject(self):
+ return Sample1()
+
+ # Sample1-specific tests are here
+
+
+class TestSample2(TestISample):
+
+ def makeTestObject(self):
+ return Sample2()
+
+ # Sample2-specific tests are here
+
+
+def test_suite():
+ return unittest.TestSuite((
+ unittest.makeSuite(TestSample1),
+ unittest.makeSuite(TestSample2)
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: book/trunk/test_zptpage.py
===================================================================
--- book/trunk/test_zptpage.py 2004-08-31 00:41:36 UTC (rev 27355)
+++ book/trunk/test_zptpage.py 2004-08-31 00:43:22 UTC (rev 27356)
@@ -0,0 +1,97 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""Code for the Zope 3 Book's Functional Tests Chapter
+
+$Id: test_templatedpage.py,v 1.1.1.1 2004/02/18 18:07:08 srichter Exp $
+"""
+import time
+import unittest
+from xml.sax import saxutils
+
+from transaction import get_transaction
+from zope.app.tests.functional import BrowserTestCase
+from zope.app.zptpage.zptpage import ZPTPage
+
+class TemplatedPageTests(BrowserTestCase):
+ """Funcional tests for Templated Page."""
+
+ template = u'''\
+ <html>
+ <body>
+ <h1 tal:content="modules/time/asctime" />
+ </body>
+ </html>'''
+
+ template2 = u'''\
+ <html>
+ <body>
+ <h1 tal:content="modules/time/asctime">time</h1>
+ </body>
+ </html>'''
+
+ def createPage(self):
+ root = self.getRootFolder()
+ root['zptpage'] = ZPTPage()
+ root['zptpage'].setSource(self.template, 'text/html')
+ get_transaction().commit()
+
+ def test_add(self):
+ response = self.publish(
+ "/+/zope.app.zptpage.ZPTPage=",
+ basic='mgr:mgrpw',
+ form={'add_input_name' : u'newzptpage',
+ 'field.expand.used' : u'',
+ 'field.source' : self.template,
+ 'field.evaluateInlineCode.used' : u'',
+ 'field.evaluateInlineCode' : u'on',
+ 'UPDATE_SUBMIT' : 'Add'})
+
+ self.assertEqual(response.getStatus(), 302)
+ self.assertEqual(response.getHeader('Location'),
+ 'http://localhost/@@contents.html')
+
+ zpt = self.getRootFolder()['newzptpage']
+ self.assertEqual(zpt.getSource(), self.template)
+ self.assertEqual(zpt.evaluateInlineCode, True)
+
+ def test_editCode(self):
+ self.createPage()
+ response = self.publish(
+ "/zptpage/@@edit.html",
+ basic='mgr:mgrpw',
+ form={'add_input_name' : u'zptpage',
+ 'field.expand.used' : u'',
+ 'field.source' : self.template2,
+ 'UPDATE_SUBMIT' : 'Change'})
+ self.assertEqual(response.getStatus(), 200)
+ self.assert_('>time<' in response.getBody())
+ self.checkForBrokenLinks(response.getBody(), response.getPath(),
+ 'mgr:mgrpw')
+
+ def test_index(self):
+ self.createPage()
+ t = time.asctime()
+ response = self.publish("/zptpage", basic='mgr:mgrpw')
+ self.assertEqual(response.getStatus(), 200)
+ self.assert_(response.getBody().find('<h1>'+t+'</h1>') != -1)
+ self.checkForBrokenLinks(response.getBody(), response.getPath(),
+ 'mgr:mgrpw')
+
+def test_suite():
+ return unittest.TestSuite((
+ unittest.makeSuite(TemplatedPageTests),
+ ))
+
+if __name__=='__main__':
+ unittest.main(defaultTest='test_suite')
More information about the Zope-CVS
mailing list