/tests/tools/introspector/list_command_test.py

https://github.com/Yelp/data_pipeline · Python · 238 lines · 196 code · 27 blank · 15 comment · 3 complexity · 7c74cb07a67bc7f701b1eaff30af2ae4 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Yelp Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing,
  11. # software distributed under the License is distributed on an
  12. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. # KIND, either express or implied. See the License for the
  14. # specific language governing permissions and limitations
  15. # under the License.
  16. from __future__ import absolute_import
  17. from __future__ import unicode_literals
  18. from collections import namedtuple
  19. import mock
  20. import pytest
  21. from data_pipeline.tools.introspector.list_command.namespaces import NamespacesListCommand
  22. from data_pipeline.tools.introspector.list_command.sources import SourcesListCommand
  23. from data_pipeline.tools.introspector.list_command.topics import TopicsListCommand
  24. from tests.tools.introspector.base_test import FakeParserError
  25. from tests.tools.introspector.base_test import TestIntrospectorBase
  26. NamespacesArgs = namedtuple(
  27. "Namespace", [
  28. "sort_by",
  29. "descending_order",
  30. "active_namespaces",
  31. "verbosity"
  32. ]
  33. )
  34. SourcesArgs = namedtuple(
  35. "Namespace", [
  36. "namespace",
  37. "sort_by",
  38. "descending_order",
  39. "active_sources",
  40. "verbosity"
  41. ]
  42. )
  43. TopicsArgs = namedtuple(
  44. "Namespace", [
  45. "source_id",
  46. "source_name",
  47. "namespace",
  48. "sort_by",
  49. "descending_order",
  50. "verbosity"
  51. ]
  52. )
  53. CommandArgsPair = namedtuple("Namespace", ["command", "args"])
  54. class TestListCommand(TestIntrospectorBase):
  55. def _create_list_command(self, command):
  56. list_command = command("data_pipeline_introspector_list_topic")
  57. list_command.log = mock.Mock()
  58. list_command.log.info = mock.Mock()
  59. list_command.log.debug = mock.Mock()
  60. list_command.log.warning = mock.Mock()
  61. return list_command
  62. @pytest.fixture
  63. def topics_list_command(self):
  64. return self._create_list_command(TopicsListCommand)
  65. @pytest.fixture
  66. def sources_list_command(self):
  67. return self._create_list_command(SourcesListCommand)
  68. @pytest.fixture
  69. def namespaces_list_command(self):
  70. return self._create_list_command(NamespacesListCommand)
  71. @pytest.fixture
  72. def bad_topic_args(self, namespace_one, source_one_active):
  73. return TopicsArgs(
  74. source_id=None,
  75. source_name=source_one_active,
  76. namespace=namespace_one,
  77. sort_by="bad_field",
  78. descending_order=False,
  79. verbosity=0
  80. )
  81. @pytest.fixture
  82. def good_topic_args(self, namespace_one, source_one_active):
  83. return TopicsArgs(
  84. source_id=None,
  85. source_name=source_one_active,
  86. namespace=namespace_one,
  87. sort_by="message_count",
  88. descending_order=False,
  89. verbosity=0
  90. )
  91. @pytest.fixture
  92. def bad_source_args(self, namespace_one):
  93. return SourcesArgs(
  94. namespace=namespace_one,
  95. sort_by="bad_field",
  96. descending_order=False,
  97. active_sources=True,
  98. verbosity=0
  99. )
  100. @pytest.fixture(
  101. params=[True, False],
  102. ids=['with_active_sources', 'without_active_sources']
  103. )
  104. def good_source_args(self, request, namespace_one):
  105. return SourcesArgs(
  106. namespace=namespace_one,
  107. sort_by="name",
  108. descending_order=False,
  109. active_sources=request.param,
  110. verbosity=0
  111. )
  112. @pytest.fixture
  113. def bad_namespace_args(self):
  114. return NamespacesArgs(
  115. sort_by="bad_field",
  116. descending_order=False,
  117. active_namespaces=True,
  118. verbosity=0
  119. )
  120. @pytest.fixture(
  121. params=[True, False],
  122. ids=['with_active_namespaces', 'without_active_namespaces']
  123. )
  124. def good_namespace_args(self, request):
  125. return NamespacesArgs(
  126. sort_by="name",
  127. descending_order=False,
  128. active_namespaces=request.param,
  129. verbosity=0
  130. )
  131. def _assert_bad_fields(self, list_command, args, parser, list_type):
  132. with pytest.raises(FakeParserError) as e:
  133. list_command.run(args, parser)
  134. assert e.value.args
  135. assert "You can not sort_by by {} for list type {}".format(
  136. "bad_field", list_type
  137. ) in e.value.args[0]
  138. def _assert_good_fields(self, list_command, args, parser):
  139. list_command.run(args, parser)
  140. for field in args._fields:
  141. if field not in {"verbosity", "active_namespaces", "active_sources"}:
  142. assert getattr(list_command, field) == getattr(args, field)
  143. def test_bad_topics(
  144. self,
  145. bad_topic_args,
  146. topics_list_command,
  147. parser
  148. ):
  149. self._assert_bad_fields(
  150. topics_list_command,
  151. bad_topic_args,
  152. parser,
  153. 'topics'
  154. )
  155. def test_good_topics(
  156. self,
  157. good_topic_args,
  158. topics_list_command,
  159. parser
  160. ):
  161. self._assert_good_fields(
  162. topics_list_command,
  163. good_topic_args,
  164. parser
  165. )
  166. def test_bad_sources(
  167. self,
  168. bad_source_args,
  169. sources_list_command,
  170. parser
  171. ):
  172. self._assert_bad_fields(
  173. sources_list_command,
  174. bad_source_args,
  175. parser,
  176. 'sources'
  177. )
  178. def test_good_sources(
  179. self,
  180. good_source_args,
  181. sources_list_command,
  182. parser
  183. ):
  184. self._assert_good_fields(
  185. sources_list_command,
  186. good_source_args,
  187. parser
  188. )
  189. def test_bad_namespaces(
  190. self,
  191. bad_namespace_args,
  192. namespaces_list_command,
  193. parser
  194. ):
  195. self._assert_bad_fields(
  196. namespaces_list_command,
  197. bad_namespace_args,
  198. parser,
  199. 'namespaces'
  200. )
  201. def test_good_namespaces(
  202. self,
  203. good_namespace_args,
  204. namespaces_list_command,
  205. parser
  206. ):
  207. self._assert_good_fields(
  208. namespaces_list_command,
  209. good_namespace_args,
  210. parser
  211. )