test_utils.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import typing
  2. import pytest
  3. from pynecone import utils
  4. @pytest.mark.parametrize(
  5. "input,output",
  6. [
  7. ("", ""),
  8. ("hello", "hello"),
  9. ("Hello", "hello"),
  10. ("camelCase", "camel_case"),
  11. ("camelTwoHumps", "camel_two_humps"),
  12. ("_start_with_underscore", "_start_with_underscore"),
  13. ("__start_with_double_underscore", "__start_with_double_underscore"),
  14. ],
  15. )
  16. def test_to_snake_case(input: str, output: str):
  17. """Test converting strings to snake case.
  18. Args:
  19. input: The input string.
  20. output: The expected output string.
  21. """
  22. assert utils.to_snake_case(input) == output
  23. @pytest.mark.parametrize(
  24. "input,output",
  25. [
  26. ("", ""),
  27. ("hello", "hello"),
  28. ("Hello", "Hello"),
  29. ("snake_case", "snakeCase"),
  30. ("snake_case_two", "snakeCaseTwo"),
  31. ],
  32. )
  33. def test_to_camel_case(input: str, output: str):
  34. """Test converting strings to camel case.
  35. Args:
  36. input: The input string.
  37. output: The expected output string.
  38. """
  39. assert utils.to_camel_case(input) == output
  40. @pytest.mark.parametrize(
  41. "input,output",
  42. [
  43. ("", ""),
  44. ("hello", "Hello"),
  45. ("Hello", "Hello"),
  46. ("snake_case", "SnakeCase"),
  47. ("snake_case_two", "SnakeCaseTwo"),
  48. ],
  49. )
  50. def test_to_title_case(input: str, output: str):
  51. """Test converting strings to title case.
  52. Args:
  53. input: The input string.
  54. output: The expected output string.
  55. """
  56. assert utils.to_title_case(input) == output
  57. @pytest.mark.parametrize(
  58. "input,output",
  59. [
  60. ("{", "}"),
  61. ("(", ")"),
  62. ("[", "]"),
  63. ("<", ">"),
  64. ('"', '"'),
  65. ("'", "'"),
  66. ],
  67. )
  68. def test_get_close_char(input: str, output: str):
  69. """Test getting the close character for a given open character.
  70. Args:
  71. input: The open character.
  72. output: The expected close character.
  73. """
  74. assert utils.get_close_char(input) == output
  75. @pytest.mark.parametrize(
  76. "text,open,expected",
  77. [
  78. ("", "{", False),
  79. ("{wrap}", "{", True),
  80. ("{wrap", "{", False),
  81. ("{wrap}", "(", False),
  82. ("(wrap)", "(", True),
  83. ],
  84. )
  85. def test_is_wrapped(text: str, open: str, expected: bool):
  86. """Test checking if a string is wrapped in the given open and close characters.
  87. Args:
  88. text: The text to check.
  89. open: The open character.
  90. expected: Whether the text is wrapped.
  91. """
  92. assert utils.is_wrapped(text, open) == expected
  93. @pytest.mark.parametrize(
  94. "text,open,check_first,num,expected",
  95. [
  96. ("", "{", True, 1, "{}"),
  97. ("wrap", "{", True, 1, "{wrap}"),
  98. ("wrap", "(", True, 1, "(wrap)"),
  99. ("wrap", "(", True, 2, "((wrap))"),
  100. ("(wrap)", "(", True, 1, "(wrap)"),
  101. ("{wrap}", "{", True, 2, "{wrap}"),
  102. ("(wrap)", "{", True, 1, "{(wrap)}"),
  103. ("(wrap)", "(", False, 1, "((wrap))"),
  104. ],
  105. )
  106. def test_wrap(text: str, open: str, expected: str, check_first: bool, num: int):
  107. """Test wrapping a string.
  108. Args:
  109. text: The text to wrap.
  110. open: The open character.
  111. expected: The expected output string.
  112. check_first: Whether to check if the text is already wrapped.
  113. num: The number of times to wrap the text.
  114. """
  115. assert utils.wrap(text, open, check_first=check_first, num=num) == expected
  116. @pytest.mark.parametrize(
  117. "text,indent_level,expected",
  118. [
  119. ("", 2, ""),
  120. ("hello", 2, "hello"),
  121. ("hello\nworld", 2, " hello\n world\n"),
  122. ("hello\nworld", 4, " hello\n world\n"),
  123. (" hello\n world", 2, " hello\n world\n"),
  124. ],
  125. )
  126. def test_indent(text: str, indent_level: int, expected: str, windows_platform: bool):
  127. """Test indenting a string.
  128. Args:
  129. text: The text to indent.
  130. indent_level: The number of spaces to indent by.
  131. expected: The expected output string.
  132. windows_platform: Whether the system is windows.
  133. """
  134. assert utils.indent(text, indent_level) == (
  135. expected.replace("\n", "\r\n") if windows_platform else expected
  136. )
  137. @pytest.mark.parametrize(
  138. "condition,true_value,false_value,expected",
  139. [
  140. ("cond", "<C1>", '""', '{cond ? <C1> : ""}'),
  141. ("cond", "<C1>", "<C2>", "{cond ? <C1> : <C2>}"),
  142. ],
  143. )
  144. def test_format_cond(condition: str, true_value: str, false_value: str, expected: str):
  145. """Test formatting a cond.
  146. Args:
  147. condition: The condition to check.
  148. true_value: The value to return if the condition is true.
  149. false_value: The value to return if the condition is false.
  150. expected: The expected output string.
  151. """
  152. assert utils.format_cond(condition, true_value, false_value) == expected
  153. def test_merge_imports():
  154. """Test that imports are merged correctly."""
  155. d1 = {"react": {"Component"}}
  156. d2 = {"react": {"Component"}, "react-dom": {"render"}}
  157. d = utils.merge_imports(d1, d2)
  158. assert set(d.keys()) == {"react", "react-dom"}
  159. assert set(d["react"]) == {"Component"}
  160. assert set(d["react-dom"]) == {"render"}
  161. @pytest.mark.parametrize(
  162. "cls,expected",
  163. [
  164. (str, False),
  165. (int, False),
  166. (float, False),
  167. (bool, False),
  168. (typing.List, True),
  169. (typing.List[int], True),
  170. ],
  171. )
  172. def test_is_generic_alias(cls: type, expected: bool):
  173. """Test checking if a class is a GenericAlias.
  174. Args:
  175. cls: The class to check.
  176. expected: Whether the class is a GenericAlias.
  177. """
  178. assert utils.is_generic_alias(cls) == expected
  179. @pytest.mark.parametrize(
  180. "route,expected",
  181. [
  182. ("", "index"),
  183. ("/", "index"),
  184. ("custom-route", "custom-route"),
  185. ("custom-route/", "custom-route"),
  186. ("/custom-route", "custom-route"),
  187. ],
  188. )
  189. def test_format_route(route: str, expected: bool):
  190. """Test formatting a route.
  191. Args:
  192. route: The route to format.
  193. expected: The expected formatted route.
  194. """
  195. assert utils.format_route(route) == expected
  196. def test_setup_frontend(tmp_path, mocker):
  197. """Test checking if assets content have been
  198. copied into the .web/public folder.
  199. Args:
  200. tmp_path: root path of test case data directory
  201. mocker: mocker object to allow mocking
  202. """
  203. web_folder = tmp_path / ".web"
  204. web_public_folder = web_folder / "public"
  205. assets = tmp_path / "assets"
  206. assets.mkdir()
  207. (assets / "favicon.ico").touch()
  208. mocker.patch("pynecone.utils.install_frontend_packages")
  209. utils.setup_frontend(tmp_path)
  210. assert web_folder.exists()
  211. assert web_public_folder.exists()
  212. assert (web_public_folder / "favicon.ico").exists()
  213. @pytest.mark.parametrize(
  214. "input, output",
  215. [
  216. ("_hidden", True),
  217. ("not_hidden", False),
  218. ("__dundermethod__", False),
  219. ],
  220. )
  221. def test_is_backend_variable(input, output):
  222. assert utils.is_backend_variable(input) == output