test_yaml_parser.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Test utils.path_dict module."""
  18. from typing import Dict
  19. import pytest
  20. from ruamel.yaml import YAML
  21. from pydolphinscheduler.utils.yaml_parser import YamlParser
  22. from tests.testing.path import path_default_config_yaml
  23. yaml = YAML()
  24. expects = [
  25. {
  26. # yaml.load("no need test") is a flag about skipping it because it to different to maintainer
  27. "name": yaml.load("no need test"),
  28. "name.family": ("Smith", "SmithEdit"),
  29. "name.given": ("Alice", "AliceEdit"),
  30. "name.mark": yaml.load("no need test"),
  31. "name.mark.name_mark": yaml.load("no need test"),
  32. "name.mark.name_mark.key": ("value", "valueEdit"),
  33. },
  34. {
  35. # yaml.load("no need test") is a flag about skipping it because it to different to maintainer
  36. "java_gateway": yaml.load("no need test"),
  37. "java_gateway.address": ("127.0.0.1", "127.1.1.1"),
  38. "java_gateway.port": (25333, 25555),
  39. "java_gateway.auto_convert": (True, False),
  40. "default": yaml.load("no need test"),
  41. "default.user": yaml.load("no need test"),
  42. "default.user.name": ("userPythonGateway", "userPythonGatewayEdit"),
  43. "default.user.password": ("userPythonGateway", "userPythonGatewayEdit"),
  44. "default.user.email": (
  45. "userPythonGateway@dolphinscheduler.com",
  46. "userEdit@dolphinscheduler.com",
  47. ),
  48. "default.user.tenant": ("tenant_pydolphin", "tenant_pydolphinEdit"),
  49. "default.user.phone": (11111111111, 22222222222),
  50. "default.user.state": (1, 0),
  51. "default.workflow": yaml.load("no need test"),
  52. "default.workflow.project": ("project-pydolphin", "project-pydolphinEdit"),
  53. "default.workflow.tenant": ("tenant_pydolphin", "SmithEdit"),
  54. "default.workflow.user": ("userPythonGateway", "SmithEdit"),
  55. "default.workflow.queue": ("queuePythonGateway", "SmithEdit"),
  56. "default.workflow.worker_group": ("default", "SmithEdit"),
  57. "default.workflow.time_zone": ("Asia/Shanghai", "SmithEdit"),
  58. },
  59. ]
  60. param = [
  61. """#example
  62. name:
  63. # details
  64. family: Smith # very common
  65. given: Alice # one of the siblings
  66. mark:
  67. name_mark:
  68. key: value
  69. """
  70. ]
  71. with open(path_default_config_yaml, "r") as f:
  72. param.append(f.read())
  73. @pytest.mark.parametrize(
  74. "src, delimiter, expect",
  75. [
  76. (
  77. param[0],
  78. "|",
  79. expects[0],
  80. ),
  81. (
  82. param[1],
  83. "/",
  84. expects[1],
  85. ),
  86. ],
  87. )
  88. def test_yaml_parser_specific_delimiter(src: str, delimiter: str, expect: Dict):
  89. """Test specific delimiter for :class:`YamlParser`."""
  90. def ch_dl(key):
  91. return key.replace(".", delimiter)
  92. yaml_parser = YamlParser(src, delimiter=delimiter)
  93. assert all(
  94. [
  95. expect[key][0] == yaml_parser[ch_dl(key)]
  96. for key in expect
  97. if expect[key] != "no need test"
  98. ]
  99. )
  100. assert all(
  101. [
  102. expect[key][0] == yaml_parser.get(ch_dl(key))
  103. for key in expect
  104. if expect[key] != "no need test"
  105. ]
  106. )
  107. @pytest.mark.parametrize(
  108. "src, expect",
  109. [
  110. (
  111. param[0],
  112. expects[0],
  113. ),
  114. (
  115. param[1],
  116. expects[1],
  117. ),
  118. ],
  119. )
  120. def test_yaml_parser_contains(src: str, expect: Dict):
  121. """Test magic function :func:`YamlParser.__contain__` also with `key in obj` syntax."""
  122. yaml_parser = YamlParser(src)
  123. assert len(expect.keys()) == len(
  124. yaml_parser.dict_parser.keys()
  125. ), "Parser keys length not equal to expect keys length"
  126. assert all(
  127. [key in yaml_parser for key in expect]
  128. ), "Parser keys not equal to expect keys"
  129. @pytest.mark.parametrize(
  130. "src, expect",
  131. [
  132. (
  133. param[0],
  134. expects[0],
  135. ),
  136. (
  137. param[1],
  138. expects[1],
  139. ),
  140. ],
  141. )
  142. def test_yaml_parser_get(src: str, expect: Dict):
  143. """Test magic function :func:`YamlParser.__getitem__` also with `obj[key]` syntax."""
  144. yaml_parser = YamlParser(src)
  145. assert all(
  146. [
  147. expect[key][0] == yaml_parser[key]
  148. for key in expect
  149. if expect[key] != "no need test"
  150. ]
  151. )
  152. assert all(
  153. [
  154. expect[key][0] == yaml_parser.get(key)
  155. for key in expect
  156. if expect[key] != "no need test"
  157. ]
  158. )
  159. @pytest.mark.parametrize(
  160. "src, expect",
  161. [
  162. (
  163. param[0],
  164. expects[0],
  165. ),
  166. (
  167. param[1],
  168. expects[1],
  169. ),
  170. ],
  171. )
  172. def test_yaml_parser_set(src: str, expect: Dict):
  173. """Test magic function :func:`YamlParser.__setitem__` also with `obj[key] = val` syntax."""
  174. yaml_parser = YamlParser(src)
  175. for key in expect:
  176. assert key in yaml_parser.dict_parser.keys()
  177. if expect[key] == "no need test":
  178. continue
  179. assert expect[key][0] == yaml_parser.dict_parser[key]
  180. assert expect[key][1] != yaml_parser.dict_parser[key]
  181. yaml_parser[key] = expect[key][1]
  182. assert expect[key][0] != yaml_parser.dict_parser[key]
  183. assert expect[key][1] == yaml_parser.dict_parser[key]
  184. @pytest.mark.parametrize(
  185. "src, setter, expect",
  186. [
  187. (
  188. param[0],
  189. {"name.mark.name_mark.key": "edit"},
  190. """#example
  191. name:
  192. # details
  193. family: Smith # very common
  194. given: Alice # one of the siblings
  195. mark:
  196. name_mark:
  197. key: edit
  198. """,
  199. ),
  200. (
  201. param[0],
  202. {
  203. "name.family": "SmithEdit",
  204. "name.given": "AliceEdit",
  205. "name.mark.name_mark.key": "edit",
  206. },
  207. """#example
  208. name:
  209. # details
  210. family: SmithEdit # very common
  211. given: AliceEdit # one of the siblings
  212. mark:
  213. name_mark:
  214. key: edit
  215. """,
  216. ),
  217. ],
  218. )
  219. def test_yaml_parser_str_repr(src: str, setter: Dict, expect: str):
  220. """Test function :func:`YamlParser.to_string`."""
  221. yaml_parser = YamlParser(src)
  222. # Equal before change
  223. assert f"YamlParser({src})" == repr(yaml_parser)
  224. assert src == str(yaml_parser)
  225. for key, val in setter.items():
  226. yaml_parser[key] = val
  227. # Equal after changed
  228. assert expect == str(yaml_parser)
  229. assert f"YamlParser({expect})" == repr(yaml_parser)
  230. @pytest.mark.parametrize(
  231. "src, key, expect",
  232. [
  233. (param[1], "java_gateway.port", 25333),
  234. (param[1], "default.user.phone", 11111111111),
  235. (param[1], "default.user.state", 1),
  236. ],
  237. )
  238. def test_yaml_parser_get_int(src: str, key: str, expect: int):
  239. """Test function :func:`YamlParser.get_int`."""
  240. yaml_parser = YamlParser(src)
  241. assert expect == yaml_parser.get_int(key)
  242. @pytest.mark.parametrize(
  243. "src, key, expect",
  244. [
  245. (param[1], "java_gateway.auto_convert", True),
  246. ],
  247. )
  248. def test_yaml_parser_get_bool(src: str, key: str, expect: bool):
  249. """Test function :func:`YamlParser.get_bool`."""
  250. yaml_parser = YamlParser(src)
  251. assert expect == yaml_parser.get_bool(key)