Home | History | Annotate | Download | only in test
      1 """Test program for MimeWriter module.
      2 
      3 The test program was too big to comfortably fit in the MimeWriter
      4 class, so it's here in its own file.
      5 
      6 This should generate Barry's example, modulo some quotes and newlines.
      7 
      8 """
      9 
     10 import unittest, StringIO
     11 from test.test_support import run_unittest, import_module
     12 
     13 import_module("MimeWriter", deprecated=True)
     14 from MimeWriter import MimeWriter
     15 
     16 SELLER = '''\
     17 INTERFACE Seller-1;
     18 
     19 TYPE Seller = OBJECT
     20     DOCUMENTATION "A simple Seller interface to test ILU"
     21     METHODS
     22             price():INTEGER,
     23     END;
     24 '''
     25 
     26 BUYER = '''\
     27 class 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 
     47 STATE = '''\
     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 
     54 SIMPLE_METADATA = [
     55         ("Interpreter", "python"),
     56         ("Interpreter-Version", "1.3"),
     57         ("Owner-Name", "Barry Warsaw"),
     58         ("Owner-Rendezvous", "bwarsaw (at] 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 
     64 COMPLEX_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 
     73 EXTERNAL_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 
     83 OUTPUT = '''\
     84 From: bwarsaw (at] cnri.reston.va.us
     85 Date: Mon Feb 12 17:21:48 EST 1996
     86 To: kss-submit (at] cnri.reston.va.us
     87 MIME-Version: 1.0
     88 Content-Type: multipart/knowbot;
     89     boundary="801spam999";
     90     version="0.1"
     91 
     92 This is a multi-part message in MIME format.
     93 
     94 --801spam999
     95 Content-Type: multipart/knowbot-metadata;
     96     boundary="802spam999"
     97 
     98 
     99 --802spam999
    100 Content-Type: message/rfc822
    101 KP-Metadata-Type: simple
    102 KP-Access: read-only
    103 
    104 KPMD-Interpreter: python
    105 KPMD-Interpreter-Version: 1.3
    106 KPMD-Owner-Name: Barry Warsaw
    107 KPMD-Owner-Rendezvous: bwarsaw (at] cnri.reston.va.us
    108 KPMD-Home-KSS: kss.cnri.reston.va.us
    109 KPMD-Identifier: hdl://cnri.kss/my_first_knowbot
    110 KPMD-Launch-Date: Mon Feb 12 16:39:03 EST 1996
    111 
    112 --802spam999
    113 Content-Type: text/isl
    114 KP-Metadata-Type: complex
    115 KP-Metadata-Key: connection
    116 KP-Access: read-only
    117 KP-Connection-Description: Barry's Big Bass Business
    118 KP-Connection-Id: B4
    119 KP-Connection-Direction: client
    120 
    121 INTERFACE Seller-1;
    122 
    123 TYPE Seller = OBJECT
    124     DOCUMENTATION "A simple Seller interface to test ILU"
    125     METHODS
    126             price():INTEGER,
    127     END;
    128 
    129 --802spam999
    130 Content-Type: message/external-body;
    131     access-type="URL";
    132     URL="hdl://cnri.kss/generic-knowbot"
    133 
    134 Content-Type: text/isl
    135 KP-Metadata-Type: complex
    136 KP-Metadata-Key: generic-interface
    137 KP-Access: read-only
    138 KP-Connection-Description: Generic Interface for All Knowbots
    139 KP-Connection-Id: generic-kp
    140 KP-Connection-Direction: client
    141 
    142 
    143 --802spam999--
    144 
    145 --801spam999
    146 Content-Type: multipart/knowbot-code;
    147     boundary="803spam999"
    148 
    149 
    150 --803spam999
    151 Content-Type: text/plain
    152 KP-Module-Name: BuyerKP
    153 
    154 class 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
    176 Content-Type: multipart/knowbot-state;
    177     boundary="804spam999"
    178 KP-Main-Module: main
    179 
    180 
    181 --804spam999
    182 Content-Type: text/plain
    183 KP-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 
    195 class 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 (at] cnri.reston.va.us")
    204         toplevel.addheader("Date", "Mon Feb 12 17:21:48 EST 1996")
    205         toplevel.addheader("To", "kss-submit (at] 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 
    288 def test_main():
    289     run_unittest(MimewriterTest)
    290 
    291 if __name__ == '__main__':
    292     test_main()
    293