PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/small/rpc/test_response.py

https://gitlab.com/durandj/mymcadmin
Python | 480 lines | 430 code | 7 blank | 43 comment | 3 complexity | 7a779efad0d72136c31a0cd3fb2b6b88 MD5 | raw file
  1. """
  2. Tests for the JSON RPC response class
  3. """
  4. import json
  5. import unittest
  6. import nose
  7. from mymcadmin.rpc import response
  8. class TestJsonRpcResponse(unittest.TestCase):
  9. """
  10. Tests for the JSON RPC response class
  11. """
  12. # pylint: disable=no-self-use
  13. @nose.tools.raises(ValueError)
  14. def test_constructor_missing(self):
  15. """
  16. Tests that we require certain fields
  17. """
  18. response.JsonRpcResponse()
  19. # pylint: enable=no-self-use
  20. # pylint: disable=no-self-use
  21. @nose.tools.raises(ValueError)
  22. def test_constructor_invalid(self):
  23. """
  24. Tests that we don't allow error and result to be set
  25. """
  26. response.JsonRpcResponse(error = {}, result = {})
  27. # pylint: enable=no-self-use
  28. def test_get_result(self):
  29. """
  30. Tests that we can get the result property
  31. """
  32. resp = response.JsonRpcResponse(result = {'test': 'value'})
  33. self.assertDictEqual(
  34. {'test': 'value'},
  35. resp.result,
  36. 'Response result did not match',
  37. )
  38. def test_set_result(self):
  39. """
  40. Tests that we can set the result property
  41. """
  42. resp = response.JsonRpcResponse(result = {'test': 'value'})
  43. resp.result = {'new': 'value'}
  44. self.assertDictEqual(
  45. {'new': 'value'},
  46. resp.result,
  47. 'Result was not updated',
  48. )
  49. # pylint: disable=no-self-use
  50. @nose.tools.raises(ValueError)
  51. def test_set_result_error(self):
  52. """
  53. Tests that we can't set the result and error properties
  54. """
  55. resp = response.JsonRpcResponse(error = {'test': 'value'})
  56. resp.result = {'new': 'value'}
  57. # pylint: enable=no-self-use
  58. def test_get_error(self):
  59. """
  60. Tests that we can get the error property
  61. """
  62. resp = response.JsonRpcResponse(error = {'test': 'value'})
  63. self.assertEqual(
  64. {'test': 'value'},
  65. resp.error,
  66. 'Response error did not match',
  67. )
  68. def test_set_error(self):
  69. """
  70. Tests that we can set the error property
  71. """
  72. resp = response.JsonRpcResponse(error = {'test': 'value'})
  73. resp.error = {'new': 'value'}
  74. self.assertEqual(
  75. {'new': 'value'},
  76. resp.error,
  77. 'Response error did not match',
  78. )
  79. # pylint: disable=no-self-use
  80. @nose.tools.raises(ValueError)
  81. def test_set_error_error(self):
  82. """
  83. Tests that we can't set the error and result properties
  84. """
  85. resp = response.JsonRpcResponse(result = {'test': 'value'})
  86. resp.error = {'new': 'value'}
  87. # pylint: enable=no-self-use
  88. def test_get_response_id(self):
  89. """
  90. Tests that we can get the response_id property
  91. """
  92. resp = response.JsonRpcResponse(
  93. response_id = 1,
  94. result = {'test': 'value'},
  95. )
  96. self.assertEqual(
  97. 1,
  98. resp.response_id,
  99. 'Response ID did not match',
  100. )
  101. def test_set_response_id(self):
  102. """
  103. Tests that we can set the response_id property
  104. """
  105. resp = response.JsonRpcResponse(result = {'test': 'value'})
  106. resp.response_id = 2
  107. self.assertEqual(
  108. 2,
  109. resp.response_id,
  110. 'Response ID was not updated',
  111. )
  112. def test_get_data_result(self):
  113. """
  114. Tests that we can get the data property
  115. """
  116. resp = response.JsonRpcResponse(
  117. result = {'test': 'value'},
  118. response_id = 9000,
  119. )
  120. self.assertDictEqual(
  121. {
  122. 'jsonrpc': '2.0',
  123. 'result': {'test': 'value'},
  124. 'id': 9000,
  125. },
  126. resp.data,
  127. 'Response data did not match',
  128. )
  129. resp = response.JsonRpcResponse(
  130. result = {},
  131. response_id = 9001,
  132. )
  133. self.assertDictEqual(
  134. {
  135. 'jsonrpc': '2.0',
  136. 'result': {},
  137. 'id': 9001,
  138. },
  139. resp.data,
  140. 'Response data did not match',
  141. )
  142. resp = response.JsonRpcResponse(
  143. result = [],
  144. response_id = 9002,
  145. )
  146. self.assertDictEqual(
  147. {
  148. 'jsonrpc': '2.0',
  149. 'result': [],
  150. 'id': 9002,
  151. },
  152. resp.data,
  153. 'Response data did not match',
  154. )
  155. def test_get_data_result_no_id(self):
  156. """
  157. Tests that we can get data with no response ID
  158. """
  159. resp = response.JsonRpcResponse(
  160. result = {'test': 'value'},
  161. )
  162. self.assertDictEqual(
  163. {
  164. 'jsonrpc': '2.0',
  165. 'result': {'test': 'value'},
  166. },
  167. resp.data,
  168. 'Response data did not match',
  169. )
  170. def test_get_data_error(self):
  171. """
  172. Tests that we get the data property with an error
  173. """
  174. resp = response.JsonRpcResponse(
  175. error = {'test': 'value'},
  176. response_id = 9001,
  177. )
  178. self.assertDictEqual(
  179. {
  180. 'jsonrpc': '2.0',
  181. 'error': {'test': 'value'},
  182. 'id': 9001,
  183. },
  184. resp.data,
  185. 'Response data did not match',
  186. )
  187. def test_get_data_error_no_id(self):
  188. """
  189. Tests that we get the error response with no response ID
  190. """
  191. resp = response.JsonRpcResponse(
  192. error = {'test': 'value'},
  193. )
  194. self.assertDictEqual(
  195. {
  196. 'jsonrpc': '2.0',
  197. 'error': {'test': 'value'},
  198. },
  199. resp.data,
  200. 'Response data did not match',
  201. )
  202. # pylint: disable=no-self-use
  203. @nose.tools.raises(AttributeError)
  204. def test_set_data(self):
  205. """
  206. Tests that we can't set the data property
  207. """
  208. resp = response.JsonRpcResponse(
  209. result = {'test': 'value'},
  210. )
  211. resp.data = {'new': 'value'}
  212. # pylint: enable=no-self-use
  213. def test_from_json(self):
  214. """
  215. Tests that we can get a request from a JSON string
  216. """
  217. original_resp = {
  218. 'jsonrpc': '2.0',
  219. 'result': {'test': 'value'},
  220. 'id': 8080,
  221. }
  222. resp = response.JsonRpcResponse.from_json(json.dumps(original_resp))
  223. self.assertDictEqual(
  224. original_resp,
  225. resp.data,
  226. 'Deserialized JSON RPC response did not match',
  227. )
  228. # pylint: disable=no-self-use
  229. @nose.tools.raises(ValueError)
  230. def test_from_json_missing_version(self):
  231. """
  232. Tests that we raise the correct error when the version is missing
  233. """
  234. original_resp = {
  235. 'result': '-1.0',
  236. }
  237. response.JsonRpcResponse.from_json(json.dumps(original_resp))
  238. # pylint: enable=no-self-use
  239. # pylint: disable=no-self-use
  240. @nose.tools.raises(ValueError)
  241. def test_from_json_invalid_version(self):
  242. """
  243. Tests that we raise the correct error when the version isn't correct
  244. """
  245. original_resp = {
  246. 'jsonrpc': '-1.0',
  247. 'result': {'test': 'value'},
  248. }
  249. response.JsonRpcResponse.from_json(json.dumps(original_resp))
  250. # pylint: enable=no-self-use
  251. # pylint: disable=no-self-use
  252. @nose.tools.raises(ValueError)
  253. def test_from_json_list(self):
  254. """
  255. Tests that we raise the correct error for an empty array
  256. """
  257. response.JsonRpcResponse.from_json('[]')
  258. # pylint: enable=no-self-use
  259. # pylint: disable=no-self-use
  260. @nose.tools.raises(ValueError)
  261. def test_from_json_extra_field(self):
  262. """
  263. Tests that we raise an error when there's extra fields in the object
  264. """
  265. original_resp = {
  266. 'jsonrpc': '2.0',
  267. 'result': {'test': 'value'},
  268. 'extra': 'value',
  269. }
  270. response.JsonRpcResponse.from_json(json.dumps(original_resp))
  271. # pylint: enable=no-self-use
  272. class TestJsonRpcBatchResponse(unittest.TestCase):
  273. """
  274. Tests for the JsonRpcBatchResponse class
  275. """
  276. def test_get_data(self):
  277. """
  278. Tests that we can get the data property
  279. """
  280. original_resp = [
  281. response.JsonRpcResponse(
  282. response_id = 9001, result = {'test': 'value'}
  283. ),
  284. response.JsonRpcResponse(
  285. response_id = 9002, error = 'Bad things!',
  286. ),
  287. response.JsonRpcResponse(
  288. result = {'more': 'values'},
  289. ),
  290. ]
  291. resp = response.JsonRpcBatchResponse(original_resp)
  292. original_resp = [r.data for r in original_resp]
  293. self.assertListEqual(
  294. original_resp,
  295. resp.data,
  296. 'Batch response data did not match',
  297. )
  298. # pylint: disable=no-self-use
  299. @nose.tools.raises(AttributeError)
  300. def test_set_data(self):
  301. """
  302. Tests that we can't set the data property
  303. """
  304. original_resp = [
  305. response.JsonRpcResponse(
  306. response_id = 9001, result = {'test': 'value'}
  307. ),
  308. response.JsonRpcResponse(
  309. response_id = 9002, error = 'Bad things!',
  310. ),
  311. response.JsonRpcResponse(
  312. result = {'more': 'values'},
  313. ),
  314. ]
  315. resp = response.JsonRpcBatchResponse(original_resp)
  316. resp.data = []
  317. # pylint: enable=no-self-use
  318. def test_get_json(self):
  319. """
  320. Test that we can get the JSON object
  321. """
  322. original_resp = [
  323. response.JsonRpcResponse(
  324. response_id = 9001, result = {'test': 'value'}
  325. ),
  326. response.JsonRpcResponse(
  327. response_id = 9002, error = 'Bad things!',
  328. ),
  329. response.JsonRpcResponse(
  330. result = {'more': 'values'},
  331. ),
  332. ]
  333. resp = response.JsonRpcBatchResponse(original_resp)
  334. original_resp = [r.data for r in original_resp]
  335. self.assertListEqual(
  336. original_resp,
  337. json.loads(resp.json),
  338. 'Serialized JSON string did not match',
  339. )
  340. # pylint: disable=no-self-use
  341. @nose.tools.raises(AttributeError)
  342. def test_set_json(self):
  343. """
  344. Tests that we can't set the JSON property
  345. """
  346. original_resp = [
  347. response.JsonRpcResponse(
  348. response_id = 9001, result = {'test': 'value'}
  349. ),
  350. response.JsonRpcResponse(
  351. response_id = 9002, error = 'Bad things!',
  352. ),
  353. response.JsonRpcResponse(
  354. result = {'more': 'values'},
  355. ),
  356. ]
  357. resp = response.JsonRpcBatchResponse(original_resp)
  358. resp.json = 'bad!'
  359. # pylint: enable=no-self-use
  360. def test_iter(self):
  361. """
  362. Tests that we can iterate over the requests
  363. """
  364. original_resp = [
  365. response.JsonRpcResponse(
  366. response_id = 9001, result = {'test': 'value'}
  367. ),
  368. response.JsonRpcResponse(
  369. response_id = 9002, error = 'Bad things!',
  370. ),
  371. response.JsonRpcResponse(
  372. result = {'more': 'values'},
  373. ),
  374. ]
  375. resps = response.JsonRpcBatchResponse(original_resp)
  376. original_resp = [r.data for r in original_resp]
  377. count = 0
  378. for resp in resps:
  379. self.assertDictEqual(
  380. original_resp[count],
  381. resp.data,
  382. 'Response data did not match',
  383. )
  384. count += 1
  385. if __name__ == '__main__':
  386. unittest.main()