test_msg.py (2791B)
1 # standard imports 2 import unittest 3 import logging 4 import json 5 from email.message import Message 6 7 # local imports 8 from piknik import ( 9 Basket, 10 Issue, 11 ) 12 from piknik.msg import IssueMessage 13 14 # test imports 15 from tests.common import TestStates 16 from tests.common import TestMsgStore 17 18 logging.basicConfig(level=logging.DEBUG) 19 logg = logging.getLogger() 20 21 22 def test_wrapper(p): 23 m = Message() 24 m.add_header('Foo', 'bar') 25 m.set_type('multipart/relative') 26 m.set_payload(p) 27 return m 28 29 30 def test_unwrapper(msg, message_callback=None, part_callback=None): 31 for v in msg.walk(): 32 if message_callback != None: 33 message_callback(v) 34 35 36 class TestMsg(unittest.TestCase): 37 38 def setUp(self): 39 self.store = TestStates() 40 self.b = Basket(self.store) 41 42 43 def test_basic(self): 44 o = Issue('foo') 45 v = IssueMessage(o) 46 47 48 def test_single_content(self): 49 o = Issue('foo') 50 v = self.b.add(o) 51 m = self.b.msg(v, 's:foo') 52 53 54 def test_multi_content(self): 55 o = Issue('foo') 56 v = self.b.add(o) 57 m = self.b.msg(v, 's:foo', 's:bar', 's:baz') 58 59 60 def test_single_file_content(self): 61 o = Issue('foo') 62 v = self.b.add(o) 63 m = self.b.msg(v, 'f:tests/one.png') 64 65 66 def test_mixed_content(self): 67 o = Issue('foo') 68 v = self.b.add(o) 69 m = self.b.msg(v, 's:bar') 70 m = self.b.msg(v, 'f:tests/one.png') 71 m = self.b.msg(v, 's:baz') 72 m = self.b.msg(v, 'f:tests/two.bin') 73 74 75 def test_wrapper(self): 76 b = Basket(self.store, message_wrapper=test_wrapper) 77 o = Issue('bar') 78 v = b.add(o) 79 m = b.msg(v, 's:foo') 80 print(m) 81 82 83 def test_render(self): 84 b = Basket(self.store, message_wrapper=test_wrapper) 85 o = Issue('bar') 86 v = b.add(o) 87 m = b.msg(v, 's:foo') 88 m = b.msg(v, 's:bar', 's:baz') 89 90 def render_envelope(msg, hdr): 91 print('eeeeeenvvv {} {}'.format(hdr, msg)) 92 93 def render_message(envelope, msg, mid): 94 print('rendeeeer {} {}'.format(mid, msg)) 95 96 m = b.get_msg(v, envelope_callback=render_envelope, message_callback=render_message) 97 98 99 def test_render_alias(self): 100 b = Basket(self.store, message_wrapper=test_wrapper) 101 o = Issue('bar', alias='xyzzy') 102 v = b.add(o) 103 m = b.msg(v, 's:foo') 104 m = b.msg(v, 's:bar', 's:baz') 105 106 def render_envelope(msg, hdr): 107 print('eeeeeenvvv {} {}'.format(hdr, msg)) 108 109 def render_message(envelope, msg, mid): 110 print('rendeeeer {} {}'.format(mid, msg)) 111 112 m = b.get_msg('xyzzy', envelope_callback=render_envelope, message_callback=render_message) 113 114 115 116 if __name__ == '__main__': 117 unittest.main()