1#!/usr/bin/env python 2# 3# Copyright 2009, Google Inc. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: 9# 10# * Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# * Redistributions in binary form must reproduce the above 13# copyright notice, this list of conditions and the following disclaimer 14# in the documentation and/or other materials provided with the 15# distribution. 16# * Neither the name of Google Inc. nor the names of its 17# contributors may be used to endorse or promote products derived from 18# this software without specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 33"""Tests for mock module.""" 34 35 36import Queue 37import threading 38import unittest 39 40import mock 41 42 43class MockConnTest(unittest.TestCase): 44 def setUp(self): 45 self._conn = mock.MockConn('ABC\r\nDEFG\r\n\r\nHIJK') 46 47 def test_readline(self): 48 self.assertEqual('ABC\r\n', self._conn.readline()) 49 self.assertEqual('DEFG\r\n', self._conn.readline()) 50 self.assertEqual('\r\n', self._conn.readline()) 51 self.assertEqual('HIJK', self._conn.readline()) 52 self.assertEqual('', self._conn.readline()) 53 54 def test_read(self): 55 self.assertEqual('ABC\r\nD', self._conn.read(6)) 56 self.assertEqual('EFG\r\n\r\nHI', self._conn.read(9)) 57 self.assertEqual('JK', self._conn.read(10)) 58 self.assertEqual('', self._conn.read(10)) 59 60 def test_read_and_readline(self): 61 self.assertEqual('ABC\r\nD', self._conn.read(6)) 62 self.assertEqual('EFG\r\n', self._conn.readline()) 63 self.assertEqual('\r\nHIJK', self._conn.read(9)) 64 self.assertEqual('', self._conn.readline()) 65 66 def test_write(self): 67 self._conn.write('Hello\r\n') 68 self._conn.write('World\r\n') 69 self.assertEqual('Hello\r\nWorld\r\n', self._conn.written_data()) 70 71 72class MockBlockingConnTest(unittest.TestCase): 73 def test_read(self): 74 class LineReader(threading.Thread): 75 def __init__(self, conn, queue): 76 threading.Thread.__init__(self) 77 self._queue = queue 78 self._conn = conn 79 self.setDaemon(True) 80 self.start() 81 def run(self): 82 while True: 83 data = self._conn.readline() 84 self._queue.put(data) 85 conn = mock.MockBlockingConn() 86 queue = Queue.Queue() 87 reader = LineReader(conn, queue) 88 self.failUnless(queue.empty()) 89 conn.put_bytes('Foo bar\r\n') 90 read = queue.get() 91 self.assertEqual('Foo bar\r\n', read) 92 93 94class MockTableTest(unittest.TestCase): 95 def test_create_from_dict(self): 96 table = mock.MockTable({'Key':'Value'}) 97 self.assertEqual('Value', table.get('KEY')) 98 self.assertEqual('Value', table['key']) 99 100 def test_create_from_list(self): 101 table = mock.MockTable([('Key', 'Value')]) 102 self.assertEqual('Value', table.get('KEY')) 103 self.assertEqual('Value', table['key']) 104 105 def test_create_from_tuple(self): 106 table = mock.MockTable((('Key', 'Value'),)) 107 self.assertEqual('Value', table.get('KEY')) 108 self.assertEqual('Value', table['key']) 109 110 def test_set_and_get(self): 111 table = mock.MockTable() 112 self.assertEqual(None, table.get('Key')) 113 table['Key'] = 'Value' 114 self.assertEqual('Value', table.get('Key')) 115 self.assertEqual('Value', table.get('key')) 116 self.assertEqual('Value', table.get('KEY')) 117 self.assertEqual('Value', table['Key']) 118 self.assertEqual('Value', table['key']) 119 self.assertEqual('Value', table['KEY']) 120 121 122if __name__ == '__main__': 123 unittest.main() 124 125 126# vi:sts=4 sw=4 et 127