test_yaml_parser.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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", "queueEdit"),
  56. "default.workflow.worker_group": ("default", "wgEdit"),
  57. "default.workflow.release_state": ("online", "offline"),
  58. "default.workflow.time_zone": ("Asia/Shanghai", "Europe/Amsterdam"),
  59. "default.workflow.warning_type": ("NONE", "SUCCESS"),
  60. },
  61. ]
  62. param = [
  63. """#example
  64. name:
  65. # details
  66. family: Smith # very common
  67. given: Alice # one of the siblings
  68. mark:
  69. name_mark:
  70. key: value
  71. """
  72. ]
  73. with open(path_default_config_yaml, "r") as f:
  74. param.append(f.read())
  75. @pytest.mark.parametrize(
  76. "src, delimiter, expect",
  77. [
  78. (
  79. param[0],
  80. "|",
  81. expects[0],
  82. ),
  83. (
  84. param[1],
  85. "/",
  86. expects[1],
  87. ),
  88. ],
  89. )
  90. def test_yaml_parser_specific_delimiter(src: str, delimiter: str, expect: Dict):
  91. """Test specific delimiter for :class:`YamlParser`."""
  92. def ch_dl(key):
  93. return key.replace(".", delimiter)
  94. yaml_parser = YamlParser(src, delimiter=delimiter)
  95. assert all(
  96. [
  97. expect[key][0] == yaml_parser[ch_dl(key)]
  98. for key in expect
  99. if expect[key] != "no need test"
  100. ]
  101. )
  102. assert all(
  103. [
  104. expect[key][0] == yaml_parser.get(ch_dl(key))
  105. for key in expect
  106. if expect[key] != "no need test"
  107. ]
  108. )
  109. @pytest.mark.parametrize(
  110. "src, expect",
  111. [
  112. (
  113. param[0],
  114. expects[0],
  115. ),
  116. (
  117. param[1],
  118. expects[1],
  119. ),
  120. ],
  121. )
  122. def test_yaml_parser_contains(src: str, expect: Dict):
  123. """Test magic function :func:`YamlParser.__contain__` also with `key in obj` syntax."""
  124. yaml_parser = YamlParser(src)
  125. assert len(expect.keys()) == len(
  126. yaml_parser.dict_parser.keys()
  127. ), "Parser keys length not equal to expect keys length"
  128. assert all(
  129. [key in yaml_parser for key in expect]
  130. ), "Parser keys not equal to expect keys"
  131. @pytest.mark.parametrize(
  132. "src, expect",
  133. [
  134. (
  135. param[0],
  136. expects[0],
  137. ),
  138. (
  139. param[1],
  140. expects[1],
  141. ),
  142. ],
  143. )
  144. def test_yaml_parser_get(src: str, expect: Dict):
  145. """Test magic function :func:`YamlParser.__getitem__` also with `obj[key]` syntax."""
  146. yaml_parser = YamlParser(src)
  147. assert all(
  148. [
  149. expect[key][0] == yaml_parser[key]
  150. for key in expect
  151. if expect[key] != "no need test"
  152. ]
  153. )
  154. assert all(
  155. [
  156. expect[key][0] == yaml_parser.get(key)
  157. for key in expect
  158. if expect[key] != "no need test"
  159. ]
  160. )
  161. @pytest.mark.parametrize(
  162. "src, expect",
  163. [
  164. (
  165. param[0],
  166. expects[0],
  167. ),
  168. (
  169. param[1],
  170. expects[1],
  171. ),
  172. ],
  173. )
  174. def test_yaml_parser_set(src: str, expect: Dict):
  175. """Test magic function :func:`YamlParser.__setitem__` also with `obj[key] = val` syntax."""
  176. yaml_parser = YamlParser(src)
  177. for key in expect:
  178. assert key in yaml_parser.dict_parser.keys()
  179. if expect[key] == "no need test":
  180. continue
  181. assert expect[key][0] == yaml_parser.dict_parser[key]
  182. assert expect[key][1] != yaml_parser.dict_parser[key]
  183. yaml_parser[key] = expect[key][1]
  184. assert expect[key][0] != yaml_parser.dict_parser[key]
  185. assert expect[key][1] == yaml_parser.dict_parser[key]
  186. @pytest.mark.parametrize(
  187. "src, setter, expect",
  188. [
  189. (
  190. param[0],
  191. {"name.mark.name_mark.key": "edit"},
  192. """#example
  193. name:
  194. # details
  195. family: Smith # very common
  196. given: Alice # one of the siblings
  197. mark:
  198. name_mark:
  199. key: edit
  200. """,
  201. ),
  202. (
  203. param[0],
  204. {
  205. "name.family": "SmithEdit",
  206. "name.given": "AliceEdit",
  207. "name.mark.name_mark.key": "edit",
  208. },
  209. """#example
  210. name:
  211. # details
  212. family: SmithEdit # very common
  213. given: AliceEdit # one of the siblings
  214. mark:
  215. name_mark:
  216. key: edit
  217. """,
  218. ),
  219. ],
  220. )
  221. def test_yaml_parser_str_repr(src: str, setter: Dict, expect: str):
  222. """Test function :func:`YamlParser.to_string`."""
  223. yaml_parser = YamlParser(src)
  224. # Equal before change
  225. assert f"YamlParser({src})" == repr(yaml_parser)
  226. assert src == str(yaml_parser)
  227. for key, val in setter.items():
  228. yaml_parser[key] = val
  229. # Equal after changed
  230. assert expect == str(yaml_parser)
  231. assert f"YamlParser({expect})" == repr(yaml_parser)