/integration_tests/test_http_connection.py

https://gitlab.com/shpeely/game-stats
Python | 207 lines | 137 code | 38 blank | 32 comment | 0 complexity | 65efc03356b5537349af8f5cc3e96a20 MD5 | raw file
  1. from integration_tests.http_test import HttpBaseTest
  2. import json
  3. import pytz
  4. from datetime import datetime
  5. from unittest.mock import MagicMock
  6. from game_stats.connection import Mutation, Query
  7. class HttpTestCase(HttpBaseTest):
  8. def setUp(self):
  9. super().setUp()
  10. def tearDown(self):
  11. super().tearDown()
  12. def test_save_game_result(self):
  13. # given
  14. self.connection.handle_request = MagicMock()
  15. result = {
  16. 'bggid': '123',
  17. 'id': 'result-id',
  18. 'time': '2000-01-01T00:00:00.000Z',
  19. 'scores': [
  20. { 'player': 'player1', 'score': 10 },
  21. { 'player': 'player2', 'score': 20 },
  22. ]
  23. }
  24. # when
  25. self.app.post('/league/test-league/gameresult',
  26. data=json.dumps(result),
  27. content_type='application/json',
  28. )
  29. # then
  30. expected = {
  31. **result,
  32. 'league_id': 'test-league',
  33. 'time': datetime(2000, 1, 1, 0, 0, 0, 0, pytz.UTC),
  34. }
  35. self.connection.handle_request.assert_called_with(
  36. Mutation.save_game_result,
  37. expected
  38. )
  39. def test_update_game_result(self):
  40. # given
  41. self.connection.handle_request = MagicMock()
  42. result = {
  43. 'bggid': '123',
  44. 'time': '2000-01-01T00:00:00.000Z',
  45. 'scores': [
  46. { 'player': 'player1', 'score': 10 },
  47. { 'player': 'player2', 'score': 20 },
  48. ]
  49. }
  50. self.app.put('/league/test-league/gameresult/result-id',
  51. data=json.dumps(result),
  52. content_type='application/json',
  53. )
  54. # then
  55. expected = {
  56. **result,
  57. 'id': 'result-id',
  58. 'league_id': 'test-league',
  59. 'time': datetime(2000, 1, 1, 0, 0, 0, 0, pytz.UTC),
  60. }
  61. self.connection.handle_request.assert_called_with(
  62. Mutation.save_game_result,
  63. expected
  64. )
  65. def test_set_game_weight(self):
  66. self.connection.handle_request = MagicMock()
  67. self.app.post('/game/123/weight/3.1415')
  68. self.connection.handle_request.assert_called_with(
  69. Mutation.set_game_weight,
  70. { 'weight': 3.1415, 'game_id': '123' }
  71. )
  72. def test_get_game_weight(self):
  73. self.connection.handle_request = MagicMock()
  74. self.app.get('/game/123/weight')
  75. self.connection.handle_request.assert_called_with(
  76. Query.get_game_weight, '123'
  77. )
  78. def test_get_meta(self):
  79. self.connection.handle_request = MagicMock(return_value={})
  80. self.app.get('/league/test-league/meta')
  81. self.connection.handle_request.assert_called_with(
  82. Query.get_meta, 'test-league'
  83. )
  84. def test_set_global_meta(self):
  85. # given
  86. meta = { 'test': 123 }
  87. self.connection.handle_request = MagicMock(return_value=meta)
  88. # when
  89. self.app.post('/meta', data=json.dumps(meta), content_type='application/json')
  90. # then
  91. self.connection.handle_request.assert_called_with(
  92. Mutation.set_global_meta, meta
  93. )
  94. def test_set_global_meta_datetime(self):
  95. # given
  96. date = datetime.now(tz=pytz.timezone('UTC')).astimezone()
  97. meta = { 'date': date.isoformat() }
  98. self.connection.handle_request = MagicMock(return_value=meta)
  99. # when
  100. self.app.post('/meta', data=json.dumps(meta), content_type='application/json')
  101. # then
  102. self.connection.handle_request.assert_called_with(
  103. Mutation.set_global_meta, { 'date': date }
  104. )
  105. def test_get_global_meta(self):
  106. # given
  107. meta = { 'test': 123 }
  108. self.connection.handle_request = MagicMock(return_value=meta)
  109. # when
  110. self.app.get('/meta?key=test')
  111. # then
  112. self.connection.handle_request.assert_called_with(
  113. Query.get_global_meta, 'test'
  114. )
  115. def test_get_leagues(self):
  116. # given
  117. self.connection.handle_request = MagicMock(return_value=[])
  118. # when
  119. self.app.get('/league')
  120. # then
  121. self.connection.handle_request.assert_called_with(
  122. Query.get_leagues
  123. )
  124. def test_delete_gameresult(self):
  125. # given
  126. self.connection.handle_request = MagicMock()
  127. # when
  128. self.app.delete('/league/test-league/gameresult/test-result')
  129. # then
  130. self.connection.handle_request.assert_called_with(
  131. Mutation.delete_gameresult,
  132. { 'league_id': 'test-league', 'result_id': 'test-result' }
  133. )
  134. def test_get_gameresults(self):
  135. # given
  136. self.connection.handle_request = MagicMock(return_value=[])
  137. # when
  138. self.app.get('/league/test-league/gameresult')
  139. # then
  140. self.connection.handle_request.assert_called_with(
  141. Query.get_gameresults, 'test-league'
  142. )
  143. def test_get_game_result_stats(self):
  144. # given
  145. self.connection.handle_request = MagicMock(return_value=[])
  146. # when
  147. self.app.get('/league/test-league/gameresult/test-result/stats')
  148. # then
  149. self.connection.handle_request.assert_called_with(
  150. Query.get_game_result_stats, 'test-league', 'test-result'
  151. )
  152. def test_get_game_stats(self):
  153. # given
  154. self.connection.handle_request = MagicMock(return_value=[])
  155. # when
  156. self.app.get('/league/test-league/games/stats')
  157. # then
  158. self.connection.handle_request.assert_called_with(
  159. Query.get_game_stats, 'test-league'
  160. )
  161. def test_get_player_stats(self):
  162. # given
  163. self.connection.handle_request = MagicMock(return_value=[])
  164. # when
  165. self.app.get('/league/test-league/players/stats')
  166. # then
  167. self.connection.handle_request.assert_called_with(
  168. Query.get_player_stats, 'test-league'
  169. )