PageRenderTime 33ms CodeModel.GetById 10ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_MimeWriter.py

http://unladen-swallow.googlecode.com/
Python | 295 lines | 269 code | 18 blank | 8 comment | 5 complexity | 6d41a83be33eab13e753b9523fd3bcc6 MD5 | raw file
  1"""Test program for MimeWriter module.
  2
  3The test program was too big to comfortably fit in the MimeWriter
  4class, so it's here in its own file.
  5
  6This should generate Barry's example, modulo some quotes and newlines.
  7
  8"""
  9
 10import unittest, StringIO
 11from test.test_support import run_unittest
 12
 13import warnings
 14warnings.filterwarnings("ignore", "the MimeWriter module is deprecated.*",
 15                        DeprecationWarning)
 16
 17from MimeWriter import MimeWriter
 18
 19SELLER = '''\
 20INTERFACE Seller-1;
 21
 22TYPE Seller = OBJECT
 23    DOCUMENTATION "A simple Seller interface to test ILU"
 24    METHODS
 25            price():INTEGER,
 26    END;
 27'''
 28
 29BUYER = '''\
 30class Buyer:
 31    def __setup__(self, maxprice):
 32        self._maxprice = maxprice
 33
 34    def __main__(self, kos):
 35        """Entry point upon arrival at a new KOS."""
 36        broker = kos.broker()
 37        # B4 == Barry's Big Bass Business :-)
 38        seller = broker.lookup('Seller_1.Seller', 'B4')
 39        if seller:
 40            price = seller.price()
 41            print 'Seller wants $', price, '... '
 42            if price > self._maxprice:
 43                print 'too much!'
 44            else:
 45                print "I'll take it!"
 46        else:
 47            print 'no seller found here'
 48'''                                     # Don't ask why this comment is here
 49
 50STATE = '''\
 51# instantiate a buyer instance and put it in a magic place for the KOS
 52# to find.
 53__kp__ = Buyer()
 54__kp__.__setup__(500)
 55'''
 56
 57SIMPLE_METADATA = [
 58        ("Interpreter", "python"),
 59        ("Interpreter-Version", "1.3"),
 60        ("Owner-Name", "Barry Warsaw"),
 61        ("Owner-Rendezvous", "bwarsaw@cnri.reston.va.us"),
 62        ("Home-KSS", "kss.cnri.reston.va.us"),
 63        ("Identifier", "hdl://cnri.kss/my_first_knowbot"),
 64        ("Launch-Date", "Mon Feb 12 16:39:03 EST 1996"),
 65        ]
 66
 67COMPLEX_METADATA = [
 68        ("Metadata-Type", "complex"),
 69        ("Metadata-Key", "connection"),
 70        ("Access", "read-only"),
 71        ("Connection-Description", "Barry's Big Bass Business"),
 72        ("Connection-Id", "B4"),
 73        ("Connection-Direction", "client"),
 74        ]
 75
 76EXTERNAL_METADATA = [
 77        ("Metadata-Type", "complex"),
 78        ("Metadata-Key", "generic-interface"),
 79        ("Access", "read-only"),
 80        ("Connection-Description", "Generic Interface for All Knowbots"),
 81        ("Connection-Id", "generic-kp"),
 82        ("Connection-Direction", "client"),
 83        ]
 84
 85
 86OUTPUT = '''\
 87From: bwarsaw@cnri.reston.va.us
 88Date: Mon Feb 12 17:21:48 EST 1996
 89To: kss-submit@cnri.reston.va.us
 90MIME-Version: 1.0
 91Content-Type: multipart/knowbot;
 92    boundary="801spam999";
 93    version="0.1"
 94
 95This is a multi-part message in MIME format.
 96
 97--801spam999
 98Content-Type: multipart/knowbot-metadata;
 99    boundary="802spam999"
100
101
102--802spam999
103Content-Type: message/rfc822
104KP-Metadata-Type: simple
105KP-Access: read-only
106
107KPMD-Interpreter: python
108KPMD-Interpreter-Version: 1.3
109KPMD-Owner-Name: Barry Warsaw
110KPMD-Owner-Rendezvous: bwarsaw@cnri.reston.va.us
111KPMD-Home-KSS: kss.cnri.reston.va.us
112KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
113KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
114
115--802spam999
116Content-Type: text/isl
117KP-Metadata-Type: complex
118KP-Metadata-Key: connection
119KP-Access: read-only
120KP-Connection-Description: Barry's Big Bass Business
121KP-Connection-Id: B4
122KP-Connection-Direction: client
123
124INTERFACE Seller-1;
125
126TYPE Seller = OBJECT
127    DOCUMENTATION "A simple Seller interface to test ILU"
128    METHODS
129            price():INTEGER,
130    END;
131
132--802spam999
133Content-Type: message/external-body;
134    access-type="URL";
135    URL="hdl://cnri.kss/generic-knowbot"
136
137Content-Type: text/isl
138KP-Metadata-Type: complex
139KP-Metadata-Key: generic-interface
140KP-Access: read-only
141KP-Connection-Description: Generic Interface for All Knowbots
142KP-Connection-Id: generic-kp
143KP-Connection-Direction: client
144
145
146--802spam999--
147
148--801spam999
149Content-Type: multipart/knowbot-code;
150    boundary="803spam999"
151
152
153--803spam999
154Content-Type: text/plain
155KP-Module-Name: BuyerKP
156
157class Buyer:
158    def __setup__(self, maxprice):
159        self._maxprice = maxprice
160
161    def __main__(self, kos):
162        """Entry point upon arrival at a new KOS."""
163        broker = kos.broker()
164        # B4 == Barry's Big Bass Business :-)
165        seller = broker.lookup('Seller_1.Seller', 'B4')
166        if seller:
167            price = seller.price()
168            print 'Seller wants $', price, '... '
169            if price > self._maxprice:
170                print 'too much!'
171            else:
172                print "I'll take it!"
173        else:
174            print 'no seller found here'
175
176--803spam999--
177
178--801spam999
179Content-Type: multipart/knowbot-state;
180    boundary="804spam999"
181KP-Main-Module: main
182
183
184--804spam999
185Content-Type: text/plain
186KP-Module-Name: main
187
188# instantiate a buyer instance and put it in a magic place for the KOS
189# to find.
190__kp__ = Buyer()
191__kp__.__setup__(500)
192
193--804spam999--
194
195--801spam999--
196'''
197
198class MimewriterTest(unittest.TestCase):
199
200    def test(self):
201        buf = StringIO.StringIO()
202
203        # Toplevel headers
204
205        toplevel = MimeWriter(buf)
206        toplevel.addheader("From", "bwarsaw@cnri.reston.va.us")
207        toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
208        toplevel.addheader("To", "kss-submit@cnri.reston.va.us")
209        toplevel.addheader("MIME-Version", "1.0")
210
211        # Toplevel body parts
212
213        f = toplevel.startmultipartbody("knowbot", "801spam999",
214                                        [("version", "0.1")], prefix=0)
215        f.write("This is a multi-part message in MIME format.\n")
216
217        # First toplevel body part: metadata
218
219        md = toplevel.nextpart()
220        md.startmultipartbody("knowbot-metadata", "802spam999")
221
222        # Metadata part 1
223
224        md1 = md.nextpart()
225        md1.addheader("KP-Metadata-Type", "simple")
226        md1.addheader("KP-Access", "read-only")
227        m = MimeWriter(md1.startbody("message/rfc822"))
228        for key, value in SIMPLE_METADATA:
229            m.addheader("KPMD-" + key, value)
230        m.flushheaders()
231        del md1
232
233        # Metadata part 2
234
235        md2 = md.nextpart()
236        for key, value in COMPLEX_METADATA:
237            md2.addheader("KP-" + key, value)
238        f = md2.startbody("text/isl")
239        f.write(SELLER)
240        del md2
241
242        # Metadata part 3
243
244        md3 = md.nextpart()
245        f = md3.startbody("message/external-body",
246                          [("access-type", "URL"),
247                           ("URL", "hdl://cnri.kss/generic-knowbot")])
248        m = MimeWriter(f)
249        for key, value in EXTERNAL_METADATA:
250            md3.addheader("KP-" + key, value)
251        md3.startbody("text/isl")
252        # Phantom body doesn't need to be written
253
254        md.lastpart()
255
256        # Second toplevel body part: code
257
258        code = toplevel.nextpart()
259        code.startmultipartbody("knowbot-code", "803spam999")
260
261        # Code: buyer program source
262
263        buyer = code.nextpart()
264        buyer.addheader("KP-Module-Name", "BuyerKP")
265        f = buyer.startbody("text/plain")
266        f.write(BUYER)
267
268        code.lastpart()
269
270        # Third toplevel body part: state
271
272        state = toplevel.nextpart()
273        state.addheader("KP-Main-Module", "main")
274        state.startmultipartbody("knowbot-state", "804spam999")
275
276        # State: a bunch of assignments
277
278        st = state.nextpart()
279        st.addheader("KP-Module-Name", "main")
280        f = st.startbody("text/plain")
281        f.write(STATE)
282
283        state.lastpart()
284
285        # End toplevel body parts
286
287        toplevel.lastpart()
288
289        self.assertEqual(buf.getvalue(), OUTPUT)
290
291def test_main():
292    run_unittest(MimewriterTest)
293
294if __name__ == '__main__':
295    test_main()