annotate wibble/tests.py @ 13:21eb67081c43

Added docstrings
author Ben Croston <ben@croston.org>
date Mon, 05 Sep 2011 12:42:05 +0100
parents 4ac14cfadc15
children 45c1d78559e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
1 #!/usr/bin/env python
10
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
2
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
3 # Copyright (c) 2011 Ben Croston
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
4 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
6 # this software and associated documentation files (the "Software"), to deal in
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
7 # the Software without restriction, including without limitation the rights to
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
8 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
9 # of the Software, and to permit persons to whom the Software is furnished to do
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
10 # so, subject to the following conditions:
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
11 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
12 # The above copyright notice and this permission notice shall be included in all
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
13 # copies or substantial portions of the Software.
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
14 #
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
21 # SOFTWARE.
58e764e39492 Added licence to tests.py
Ben Croston <ben@croston.org>
parents: 9
diff changeset
22
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
23 import unittest
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
24 import hashlib
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
25 from threading import Thread
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
26 import time
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
27 from wsgiref import simple_server
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
28 import platform
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
29
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
30 ##### server vvv #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
31 class api(object):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
32 def mymethod(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
33 return 'wibbler woz ere'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
34
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
35 def echo(self, mystring):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
36 return 'ECHO: ' + mystring
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
37
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
38 def raiseexception(self):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
39 dividebyzeroerror = 1/0
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
40
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
41 def returnnothing(self):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
42 pass
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
43
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
44 def myauth(username, password, useragent=None):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
45 return username == 'testuser' and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
46 hashlib.md5('s3cr3t').hexdigest() == password and \
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
47 useragent == 'wibble_unittest'
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
48
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
49 def make_server():
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
50 from server import JsonRpcApp
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
51 class myhandler(simple_server.WSGIRequestHandler):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
52 def log_request(self, *a, **b):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
53 pass # do not output log messages
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
54 application = JsonRpcApp(api(), auth=myauth)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
55 return simple_server.make_server('localhost', 1337, application, handler_class=myhandler)
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
56 ##### server ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
57
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
58 ##### client vvv #####
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
59 class AuthTest(unittest.TestCase):
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
60 def runTest(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
61 from client import ServerProxy, UnauthorisedException
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
62 self.client = ServerProxy('http://localhost:1337/',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
63 username='testuser',
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
64 password='s3cr3t',
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
65 user_agent='InternetExploiter')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
66 with self.assertRaises(UnauthorisedException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
67 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
68
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
69 self.client = ServerProxy('http://localhost:1337/',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
70 username='testuser',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
71 password='wrongpassword',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
72 user_agent='wibble_unittest')
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
73 with self.assertRaises(UnauthorisedException):
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
74 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
75
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
76 self.client = ServerProxy('http://localhost:1337/',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
77 username='wronguser',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
78 password='s3cr3t',
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
79 user_agent='wibble_unittest')
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
80 with self.assertRaises(UnauthorisedException):
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
81 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
82
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
83 class WibbleTests(unittest.TestCase):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
84 def setUp(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
85 from client import ServerProxy
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
86 self.client = ServerProxy('http://localhost:1337/',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
87 username='testuser',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
88 password='s3cr3t',
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
89 user_agent='wibble_unittest')
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
90
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
91 class IgnoreClassNameTest(WibbleTests):
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
92 def runTest(self):
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
93 self.assertEqual(self.client.api.mymethod(),self.client.mymethod())
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
94
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
95 class ExceptionTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
96 def runTest(self):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
97 with self.assertRaises(Exception):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
98 self.client.raiseexception()
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
99
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
100 class BadRequestTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
101 def runTest(self):
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
102 from client import BadRequestException
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
103 with self.assertRaises(BadRequestException):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
104 self.client.FunctionDoesNotExist()
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
105
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
106 class EchoTest(WibbleTests):
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
107 def runTest(self):
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
108 if platform.python_version().startswith('3'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
109 POUND = '\u00A3'
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
110 else:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
111 POUND = unicode('\u00A3')
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
112 self.assertEqual(self.client.echo(POUND), 'ECHO: ' + POUND)
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
113 self.assertEqual(self.client.echo('hello mum!'), 'ECHO: hello mum!')
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
114
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
115 class ReturnNothing(WibbleTests):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
116 def runTest(self):
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
117 self.assertEqual(self.client.returnnothing(), None)
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
118 ##### client ^^^ #####
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
119
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
120 finished = False
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
121 def suite():
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
122 if platform.python_version().startswith('2'):
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
123 # create server
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
124 def test_wrapper():
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
125 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
126 while not finished:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
127 server.handle_request()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
128 thread = Thread(target=test_wrapper)
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
129 thread.start()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
130 time.sleep(0.1) # wait for server thread to start
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
131
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
132 # tests are as client
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
133 suite = unittest.TestSuite()
12
4ac14cfadc15 Improved AuthTest
Ben Croston <ben@croston.org>
parents: 11
diff changeset
134 suite.addTest(AuthTest())
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
135 suite.addTest(IgnoreClassNameTest())
7
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
136 suite.addTest(ExceptionTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
137 suite.addTest(BadRequestTest())
3a6f3193cc7d Created new tests
Ben Croston <ben@croston.org>
parents: 5
diff changeset
138 suite.addTest(EchoTest())
11
dbd55e6af4f6 Added test
Ben Croston <ben@croston.org>
parents: 10
diff changeset
139 suite.addTest(ReturnNothing())
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
140 return suite
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
141
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
142 if __name__ == '__main__':
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
143 import sys
9
23743b0f67ab Moved client.ServerProxy into client.__init__
Ben Croston <ben@croston.org>
parents: 8
diff changeset
144 if platform.python_version().startswith('2') and 'serve' in sys.argv:
8
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
145 print 'Listening on port 1337 (Ctrl-C qo quit)...'
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
146 server = make_server()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
147 try:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
148 server.serve_forever()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
149 except KeyboardInterrupt:
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
150 sys.exit()
685479d1f0a7 Tests available for both py2 and py3
Ben Croston <ben@croston.org>
parents: 7
diff changeset
151
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
152 unittest.TextTestRunner(verbosity=2).run(suite())
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
153 finished = True
4
ad5a8748afcf Add test framework
Ben Croston <ben@croston.org>
parents:
diff changeset
154
5
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
155 # make a dummy request to get server thread out of loop
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
156 try:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
157 import urllib
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
158 urllib.urlopen('http://localhost:1337/')
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
159 except:
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
160 pass
f5e3ba8cfcd0 Improved test framework
Ben Croston <ben@croston.org>
parents: 4
diff changeset
161