个人工具
登录
查看“Zope3宝典/映射对象到文件系统”的源代码 - Ubuntu中文
页面
讨论
查看源代码
历史
搜索
导航
首页
最近更改
随机页面
页面分类
帮助
编辑
编辑指南
沙盒
新闻动态
字词处理
工具
链入页面
相关更改
特殊页面
页面信息
查看“Zope3宝典/映射对象到文件系统”的源代码
来自Ubuntu中文
←
Zope3宝典/映射对象到文件系统
跳转至:
导航
,
搜索
因为以下原因,你没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==== 22.2.3 (c) The Tests(22.2.3 (c) 测试) ==== Since even the last coding step did not provide a functional piece of code, it becomes so much more important to write some careful tests for the VirtualContentsFile component. Another requirement of the tests are that this adapter is being tested with both MessageBoard and Message instances. To realize this, we write an a base test and then realize this test for each component. So in the tests folder, create a new file called test_filerepresentation.py and add the following content:<br>因为甚至在编程的最后一步都没有提供代码的功能片断,所以这使得为 VirtualContentsFile 组件精心编写代码就变得非常重要。另一个测试的需求就是该适配器必须被留言薄和消息实例测试。认识到这点,我们编写一个基本测试,然后为每个组件实现它。所以在测试文件夹里,新建一个名为 test_filerepresentation.py 的文件,并添加以下内容: <pre><nowiki>#!python import unittest from zope.interface.verify import verifyObject from zope.app import zapi from zope.app.tests import ztapi from zope.app.tests.placelesssetup import PlacelessSetup from book.messageboard.interfaces import \ IVirtualContentsFile, IPlainText, IMessage, IMessageBoard from book.messageboard.message import \ Message, PlainText as MessagePlainText from book.messageboard.messageboard import \ MessageBoard, PlainText as MessageBoardPlainText from book.messageboard.filerepresentation import VirtualContentsFile class VirtualContentsFileTestBase(PlacelessSetup): def _makeFile(self): raise NotImplemented def _registerPlainTextAdapter(self): raise NotImplemented def setUp(self): PlacelessSetup.setUp(self) self._registerPlainTextAdapter() def testContentType(self): file = self._makeFile() self.assertEqual(file.getContentType(), 'text/plain') file.setContentType('text/html') self.assertEqual(file.getContentType(), 'text/plain') self.assertEqual(file.contentType, 'text/plain') def testData(self): file = self._makeFile() file.setData('Foobar') self.assert_(file.getData().find('Foobar') >= 0) self.assert_(file.data.find('Foobar') >= 0) file.edit('Blah', 'text/html') self.assertEqual(file.contentType, 'text/plain') self.assert_(file.data.find('Blah') >= 0) def testInterface(self): file = self._makeFile() self.failUnless(IVirtualContentsFile.providedBy(file)) self.failUnless(verifyObject(IVirtualContentsFile, file)) class MessageVirtualContentsFileTest(VirtualContentsFileTestBase, unittest.TestCase): def _makeFile(self): return VirtualContentsFile(Message()) def _registerPlainTextAdapter(self): ztapi.provideAdapter(IMessage, IPlainText, MessagePlainText) def testMessageSpecifics(self): file = self._makeFile() self.assertEqual(file.context.title, '') self.assertEqual(file.context.body, '') file.data = 'Title: Hello\n\nWorld' self.assertEqual(file.context.title, 'Hello') self.assertEqual(file.context.body, 'World') file.data = 'World 2' self.assertEqual(file.context.body, 'World 2') class MessageBoardVirtualContentsFileTest( VirtualContentsFileTestBase, unittest.TestCase): def _makeFile(self): return VirtualContentsFile(MessageBoard()) def _registerPlainTextAdapter(self): ztapi.provideAdapter(IMessageBoard, IPlainText, MessageBoardPlainText) def testMessageBoardSpecifics(self): file = self._makeFile() self.assertEqual(file.context.description, '') file.data = 'Title: Hello\n\nWorld' self.assertEqual(file.context.description, 'Title: Hello\n\nWorld') file.data = 'World 2' self.assertEqual(file.context.description, 'World 2') def test_suite(): return unittest.TestSuite(( unittest.makeSuite(MessageVirtualContentsFileTest), unittest.makeSuite(MessageBoardVirtualContentsFileTest), )) if <u>name</u> == '<u>main</u>': unittest.main(defaultTest='test_suite') </nowiki></pre> * Line 5: Since we are going to make use of adapters, we will need to bring up the component architecture using the PlacelessSetup.<br>第 5 行: 因为我们想用适配器,所以我们需要用 PlacelessSetup 提出组件架构。 * Line 7-13: Imports all the relevant interfaces and components for this test. This is always so much, since we have to register the components by hand (instead of ZCML).<br>第 7-13 行: 为本测试导入所有相关的接口和组件。这总是非常多的,因为我们必须手工注册组件(代替 ZCML) * Line 17-18: The implementation of this method should create a VirtualContentsFile adapter having the correct object as context. Since the context varies, the specific test case class has to take of its implementation.<br>第 17-18 行: 本方法的实现将创建一个有着正确对象作为上下文的 VirtualContentsFile 适配器。因为上下文的变化,具体测试案例类必须take of its implementation. * Line 20-21: Since there is a specific adapter registration required for each case (board and message), we will have to leave that up to the test case implementation as well.<br>第 20-21 行: 因为每个案例(栏和消息)都要求注册一个相应的适配器,所以我们同样也必须使之适应测试案例实施。 * Line 27-32: We need to make sure that the plain/text setting can never be overwritten.<br>第 27-32 行: 我们需要确保 plain/text 设置永远不被覆盖。 * Line 34-43: We can really just make some marginal tests here, since the storage details really depend on the IPlainText implementation. There will be stronger tests in the specific test cases for the message board and message (see below).<br>第 34-43 行: 这里我们可以真正做点最低限度的测试,因为存储的细节真正有赖于 IPlainText 实现。在为留言薄和消息进行的具体测试中还会有更多的测试(参见下面)。 * Line 45-48: Always make sure that the interface is completely implemented by the component.<br>第 45-48 行: 始终确保接口被组件完全实现。 * Line 51: This is the beginning of a concrete test case that implements the base test. Note that you should only make the concrete implementation a TestCase.<br>第 51 行: 这是实现基本测试的具体测试案例的开始。注意你应该只做 TestCase 的具体实现。 * Line 54-55: Just stick a plain, empty Message instance in the adapter.<br>第 54-55 行: 仅仅在适配器中生成一个纯文本的、空的消息实例。 * Line 60-68: Here we test that the written contents of the virtual file is correctly passed and the right properties are set.<br>第 60-68 行: 在这里我们测试虚拟文件所写内容被正确通过,同时正确的属性被设置。 * Line 71-88: Pretty much the same that was done for the Message test.<br>第 71-88 行: 同消息测试相同。 * Line 90-97: The usual test boilerplate.<br>第 90-97 行: 常用测试模板 You can now run the test and verify the functionality of the new tests.<br>你现在可以运行测试并确认新测试的功能。
返回至
Zope3宝典/映射对象到文件系统
。