• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 38. Developing New TALES Express... > Step 2: Preparing and Implementing t...

Step 2: Preparing and Implementing the Tests

Writing tests for the code in this chapter is actually quite painful because you have to simulate an entire database connection and result objects. Furthermore, you have to bring up the component architecture because the RDB connection is created by using the createObject() function, which uses factories. In tests.py, you need to add the following:

01 import unittest
02
03 from zope.interface import implements
04 from zope.component.factory import Factory
05 from zope.component.interfaces import IFactory
06 from zope.component.tests.placelesssetup import PlacelessSetup
07 from zope.tales.tests.test_expressions import Data
08 from zope.tales.engine import Engine
09
10 from zope.app.tests import ztapi
11 from zope.app.rdb.interfaces import IZopeDatabaseAdapter, IZopeConnection
12 from zope.app.rdb.tests.stubs import ConnectionStub
13 from zope.app.sqlexpr.sqlexpr import SQLExpr, ConnectionError
14
15
16 class AdapterStub(object):
17     implements(IZopeDatabaseAdapter)
18
19     def __init__(self, dsn):
20         return
21
22     def __call__(self):
23         return ConnectionStub()
24
25 class ConnectionStub(object):
26     implements(IZopeConnection)
27
28     def __init__(self):
29         self._called = {}
30
31     def cursor(self):
32         return CursorStub()
33
34 class CursorStub(object):
35
36     description = (('id', 0, 0, 0, 0, 0, 0),
37                    ('name', 0, 0, 0, 0, 0, 0),
38                    ('email', 0, 0, 0, 0, 0, 0))
39
40
41     def fetchall(self, *args, **kw):
42         return ((1, 'Stephan', 'srichter'),
43                (2, 'Foo Bar', 'foobar'))
44
45     def execute(self, operation, *args, **kw):
46         if operation != 'SELECT num FROM hitchhike':
47             raise AssertionError(operation, 'SELECT num FROM hitchhike')
48
49
50 class SQLExprTest(PlacelessSetup, unittest.TestCase):
51
52     def setUp(self):
53         super(SQLExprTest, self).setUp()
54         ztapi.provideUtility(IFactory, Factory(AdapterStub),
55                              'zope.da.Stub')
56         ztapi.provideUtility(IFactory, Factory(lambda x: None),
57                              'zope.Fake')
58         ztapi.provideUtility(IZopeDatabaseAdapter, AdapterStub(''),
59                              'test')
60
61     def test_exprUsingRDBAndDSN(self):
62         context = Data(vars = {'rdb': 'zope.da.Stub', 'dsn': 'dbi://test'})
63         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
64         result = expr(context)
65         self.assertEqual(1, result[0].id)
66         self.assertEqual('Stephan', result[0].name)
67         self.assertEqual('srichter', result[0].email)
68         self.assertEqual('Foo Bar', result[1].name)
69
70     def test_exprUsingSQLConn(self):
71         context = Data(vars = {'sql_conn': 'test'})
72         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
73         result = expr(context)
74         self.assertEqual(1, result[0].id)
75         self.assertEqual('Stephan', result[0].name)
76         self.assertEqual('srichter', result[0].email)
77         self.assertEqual('Foo Bar', result[1].name)
78
79     def test_exprUsingRDBAndDSN_InvalidFactoryId(self):
80         context = Data(vars = {'rdb': 'zope.da.Stub1', 'dsn': 'dbi://test'})
81         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
82         self.assertRaises(ConnectionError, expr, context)
83
84     def test_exprUsingRDBAndDSN_WrongFactory(self):
85         context = Data(vars = {'rdb': 'zope.Fake', 'dsn': 'dbi://test'})
86         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
87         self.assertRaises(ConnectionError, expr, context)
88
89     def test_exprUsingSQLConn_WrongId(self):
90         context = Data(vars = {'sql_conn': 'test1'})
91         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
92         self.assertRaises(ConnectionError, expr, context)
93
94     def test_noRDBSpecs(self):
95         expr = SQLExpr('name', 'SELECT num FROM hitchhike', Engine)
96         self.assertRaises(ConnectionError, expr, Data(vars={}))
97
98
99 def test_suite():
00     return unittest.TestSuite((
01         unittest.makeSuite(SQLExprTest),
02         ))
03
04 if __name__ == '__main__':
05     unittest.main(defaultTest='test_suite')


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint