tests.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # encoding: utf-8
  2. import unittest
  3. import json
  4. import sys
  5. class FunctionalTests(unittest.TestCase):
  6. def setUp(self):
  7. from frameworkbenchmarks import main
  8. app = main({})
  9. from webtest import TestApp
  10. self.testapp = TestApp(app)
  11. self.py3k = sys.version_info >= (3, 0)
  12. def _get(self, url, content_type='application/json'):
  13. res = self.testapp.get(url, status=200)
  14. self.assertTrue('Content-Length' in res.headers)
  15. return res
  16. def _str_compat(self, obj):
  17. if self.py3k:
  18. return obj.decode('utf-8')
  19. return obj
  20. def _test_obj(self, obj):
  21. self.assertTrue('id' in obj)
  22. self.assertTrue('randomNumber' in obj)
  23. self.assertTrue(1 <= obj['randomNumber'] <= 10000)
  24. def test_json(self):
  25. """
  26. /json
  27. """
  28. res = self._get('/json')
  29. self.assertEqual(self._str_compat(res.body), """{"message": "Hello, World!"}""")
  30. def test_db(self):
  31. """
  32. /db
  33. """
  34. res = self._get('/db')
  35. obj = json.loads(self._str_compat(res.body))
  36. self._test_obj(obj)
  37. def test_queries_0(self):
  38. """
  39. /queries?queries=0
  40. """
  41. res = self._get('/queries?queries=0')
  42. self.assertEqual(len(json.loads(self._str_compat(res.body))), 1)
  43. def test_queries_999(self):
  44. """
  45. /queries?queries=999
  46. """
  47. res = self._get('/queries?queries=999')
  48. self.assertEqual(len(json.loads(self._str_compat(res.body))), 500)
  49. def test_queries_10(self):
  50. """
  51. /queries?queries=10 objects
  52. """
  53. res = self._get('/queries?queries=10')
  54. objset = json.loads(self._str_compat(res.body))
  55. for obj in objset:
  56. self._test_obj(obj)
  57. def test_fortunes(self):
  58. """
  59. /fortunes
  60. """
  61. res = self._get('/fortunes')
  62. self.assertEqual(self._str_compat(res.body).strip(), fortunes.strip())
  63. def test_updates(self):
  64. """
  65. /updates?queries=10
  66. """
  67. res = self._get('/updates?queries=10')
  68. objset = json.loads(self._str_compat(res.body))
  69. # don't bother with more...
  70. for obj in objset:
  71. self._test_obj(obj)
  72. def test_plaintext(self):
  73. """
  74. /plaintext
  75. """
  76. res = self._get('/plaintext', content_type='text/plain')
  77. self.assertEqual(self._str_compat(res.body), "Hello, World!")
  78. fortunes = """
  79. <!DOCTYPE html>
  80. <html>
  81. <head>
  82. <title>Fortunes</title>
  83. </head>
  84. <body>
  85. <table>
  86. <tr>
  87. <th>id</th>
  88. <th>message</th>
  89. </tr>
  90. <tr>
  91. <td>11</td>
  92. <td>&lt;script&gt;alert("This should not be displayed in a browser alert box.");&lt;/script&gt;</td>
  93. </tr>
  94. <tr>
  95. <td>4</td>
  96. <td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td>
  97. </tr>
  98. <tr>
  99. <td>5</td>
  100. <td>A computer program does what you tell it to do, not what you want it to do.</td>
  101. </tr>
  102. <tr>
  103. <td>2</td>
  104. <td>A computer scientist is someone who fixes things that aren't broken.</td>
  105. </tr>
  106. <tr>
  107. <td>8</td>
  108. <td>A list is only as strong as its weakest link. — Donald Knuth</td>
  109. </tr>
  110. <tr>
  111. <td>0</td>
  112. <td>Additional fortune added at request time.</td>
  113. </tr>
  114. <tr>
  115. <td>3</td>
  116. <td>After enough decimal places, nobody gives a damn.</td>
  117. </tr>
  118. <tr>
  119. <td>7</td>
  120. <td>Any program that runs right is obsolete.</td>
  121. </tr>
  122. <tr>
  123. <td>10</td>
  124. <td>Computers make very fast, very accurate mistakes.</td>
  125. </tr>
  126. <tr>
  127. <td>6</td>
  128. <td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td>
  129. </tr>
  130. <tr>
  131. <td>9</td>
  132. <td>Feature: A bug with seniority.</td>
  133. </tr>
  134. <tr>
  135. <td>1</td>
  136. <td>fortune: No such file or directory</td>
  137. </tr>
  138. <tr>
  139. <td>12</td>
  140. <td>フレームワークのベンチマーク</td>
  141. </tr>
  142. </table>
  143. </body>
  144. </html>
  145. """