test_string.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.string module."""
  18. from pydolphinscheduler.utils.string import attr2camel, snake2camel, class_name2camel
  19. import pytest
  20. @pytest.mark.parametrize(
  21. "snake, expect",
  22. [
  23. ("snake_case", "snakeCase"),
  24. ("snake_123case", "snake123Case"),
  25. ("snake_c_a_s_e", "snakeCASE"),
  26. ("snake__case", "snakeCase"),
  27. ("snake_case_case", "snakeCaseCase"),
  28. ("_snake_case", "SnakeCase"),
  29. ("__snake_case", "SnakeCase"),
  30. ("Snake_case", "SnakeCase"),
  31. ],
  32. )
  33. def test_snake2camel(snake: str, expect: str):
  34. """Test function snake2camel, this is a base function for utils.string."""
  35. assert expect == snake2camel(
  36. snake
  37. ), f"Test case {snake} do no return expect result {expect}."
  38. @pytest.mark.parametrize(
  39. "attr, expects",
  40. [
  41. # source attribute, (true expect, false expect),
  42. ("snake_case", ("snakeCase", "snakeCase")),
  43. ("snake_123case", ("snake123Case", "snake123Case")),
  44. ("snake_c_a_s_e", ("snakeCASE", "snakeCASE")),
  45. ("snake__case", ("snakeCase", "snakeCase")),
  46. ("snake_case_case", ("snakeCaseCase", "snakeCaseCase")),
  47. ("_snake_case", ("snakeCase", "SnakeCase")),
  48. ("__snake_case", ("snakeCase", "SnakeCase")),
  49. ("Snake_case", ("SnakeCase", "SnakeCase")),
  50. ],
  51. )
  52. def test_attr2camel(attr: str, expects: tuple):
  53. """Test function attr2camel."""
  54. for idx, expect in enumerate(expects):
  55. include_private = idx % 2 == 0
  56. assert expect == attr2camel(
  57. attr, include_private
  58. ), f"Test case {attr} do no return expect result {expect} when include_private is {include_private}."
  59. @pytest.mark.parametrize(
  60. "class_name, expect",
  61. [
  62. ("snake_case", "snakeCase"),
  63. ("snake_123case", "snake123Case"),
  64. ("snake_c_a_s_e", "snakeCASE"),
  65. ("snake__case", "snakeCase"),
  66. ("snake_case_case", "snakeCaseCase"),
  67. ("_snake_case", "snakeCase"),
  68. ("_Snake_case", "snakeCase"),
  69. ("__snake_case", "snakeCase"),
  70. ("__Snake_case", "snakeCase"),
  71. ("Snake_case", "snakeCase"),
  72. ],
  73. )
  74. def test_class_name2camel(class_name: str, expect: str):
  75. """Test function class_name2camel."""
  76. assert expect == class_name2camel(
  77. class_name
  78. ), f"Test case {class_name} do no return expect result {expect}."