test_default_template.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. import pytest
  13. from cookiecutter.exceptions import FailedHookException
  14. from cookiecutter.main import cookiecutter
  15. from .utils import _run_template
  16. def test_default_answer(tmpdir):
  17. cookiecutter(
  18. template="taipy/templates/default",
  19. output_dir=str(tmpdir),
  20. no_input=True,
  21. )
  22. assert os.listdir(tmpdir) == ["taipy_application"]
  23. assert (
  24. os.listdir(os.path.join(tmpdir, "taipy_application")).sort() == ["requirements.txt", "main.py", "images"].sort()
  25. )
  26. taipy_path = os.getcwd()
  27. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  28. # Assert the message when the application is run successfully is in the stdout
  29. assert "[Taipy][INFO] * Server starting on" in stdout
  30. def test_main_file_with_and_without_extension(tmpdir):
  31. cookiecutter(
  32. template="taipy/templates/default",
  33. output_dir=str(tmpdir),
  34. no_input=True,
  35. extra_context={
  36. "Application main Python file": "app.py",
  37. },
  38. )
  39. assert (
  40. os.listdir(os.path.join(tmpdir, "taipy_application")).sort() == ["requirements.txt", "app.py", "images"].sort()
  41. )
  42. cookiecutter(
  43. template="taipy/templates/default",
  44. output_dir=str(tmpdir),
  45. no_input=True,
  46. extra_context={
  47. "Application root folder name": "foo_app",
  48. "Application main Python file": "app",
  49. },
  50. )
  51. assert os.listdir(os.path.join(tmpdir, "foo_app")).sort() == ["requirements.txt", "app.py", "images"].sort()
  52. def test_with_core_service(tmpdir):
  53. cookiecutter(
  54. template="taipy/templates/default",
  55. output_dir=str(tmpdir),
  56. no_input=True,
  57. extra_context={
  58. "Does the application use scenario management or version management?": "y",
  59. "Does the application use Rest API?": "no",
  60. },
  61. )
  62. assert (
  63. os.listdir(os.path.join(tmpdir, "taipy_application")).sort()
  64. == ["requirements.txt", "main.py", "images", "configuration", "algorithms"].sort()
  65. )
  66. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  67. assert "core = Core()" in main_file.read()
  68. taipy_path = os.getcwd()
  69. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  70. # Assert the message when the application is run successfully is in the stdout
  71. assert "[Taipy][INFO] * Server starting on" in stdout
  72. assert "[Taipy][INFO] Development mode: " in stdout
  73. def test_with_rest_service(tmpdir):
  74. cookiecutter(
  75. template="taipy/templates/default",
  76. output_dir=str(tmpdir),
  77. no_input=True,
  78. extra_context={
  79. "Does the application use scenario management or version management?": "n",
  80. "Does the application use Rest API?": "yes",
  81. },
  82. )
  83. assert (
  84. os.listdir(os.path.join(tmpdir, "taipy_application")).sort() == ["requirements.txt", "main.py", "images"].sort()
  85. )
  86. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  87. assert "rest = Rest()" in main_file.read()
  88. taipy_path = os.getcwd()
  89. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  90. # Assert the message when the application is run successfully is in the stdout
  91. assert "[Taipy][INFO] * Server starting on" in stdout
  92. assert "[Taipy][INFO] Development mode: " in stdout
  93. def test_with_both_core_rest_services(tmpdir):
  94. cookiecutter(
  95. template="taipy/templates/default",
  96. output_dir=str(tmpdir),
  97. no_input=True,
  98. extra_context={
  99. "Does the application use scenario management or version management?": "n",
  100. "Does the application use Rest API?": "yes",
  101. },
  102. )
  103. assert (
  104. os.listdir(os.path.join(tmpdir, "taipy_application")).sort()
  105. == ["requirements.txt", "main.py", "images", "configuration", "algorithms"].sort()
  106. )
  107. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  108. assert "rest = Rest()" in main_file.read()
  109. assert "core = Core()" not in main_file.read()
  110. taipy_path = os.getcwd()
  111. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  112. # Assert the message when the application is run successfully is in the stdout
  113. assert "[Taipy][INFO] * Server starting on" in stdout
  114. assert "[Taipy][INFO] Development mode: " in stdout
  115. def test_multipage_gui_template(tmpdir):
  116. cookiecutter(
  117. template="taipy/templates/default",
  118. output_dir=str(tmpdir),
  119. no_input=True,
  120. extra_context={
  121. "Application root folder name": "foo_app",
  122. "Page names in multi-page application?": "name_1 name_2 name_3",
  123. },
  124. )
  125. assert (
  126. os.listdir(os.path.join(tmpdir, "foo_app")).sort() == ["requirements.txt", "main.py", "pages", "images"].sort()
  127. )
  128. assert (
  129. os.listdir(os.path.join(tmpdir, "foo_app", "pages")).sort()
  130. == ["name_1", "name_2", "name_3", "root.md", "root.py", "__init__.py"].sort()
  131. )
  132. taipy_path = os.getcwd()
  133. stdout = _run_template(taipy_path, os.path.join(tmpdir, "foo_app"), "main.py")
  134. assert "[Taipy][INFO] * Server starting on" in stdout
  135. def test_multipage_gui_template_with_invalid_page_name(tmpdir, capfd):
  136. with pytest.raises(FailedHookException):
  137. cookiecutter(
  138. template="taipy/templates/default",
  139. output_dir=str(tmpdir),
  140. no_input=True,
  141. extra_context={
  142. "Application root folder name": "foo_app",
  143. "Page names in multi-page application?": "valid_var_name 1_invalid_var_name",
  144. },
  145. )
  146. _, stderr = capfd.readouterr()
  147. assert 'Page name "1_invalid_var_name" is not a valid Python identifier' in stderr
  148. assert not os.path.exists(os.path.join(tmpdir, "foo_app"))