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