test_prerequisites.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from unittest.mock import mock_open
  2. import pytest
  3. from reflex import constants
  4. from reflex.config import Config
  5. from reflex.utils.prerequisites import initialize_requirements_txt, update_next_config
  6. @pytest.mark.parametrize(
  7. "template_next_config, reflex_config, expected_next_config",
  8. [
  9. (
  10. """
  11. module.exports = {
  12. basePath: "",
  13. compress: true,
  14. reactStrictMode: true,
  15. trailingSlash: true,
  16. };
  17. """,
  18. Config(
  19. app_name="test",
  20. ),
  21. """
  22. module.exports = {
  23. basePath: "",
  24. compress: true,
  25. reactStrictMode: true,
  26. trailingSlash: true,
  27. };
  28. """,
  29. ),
  30. (
  31. """
  32. module.exports = {
  33. basePath: "",
  34. compress: true,
  35. reactStrictMode: true,
  36. trailingSlash: true,
  37. };
  38. """,
  39. Config(
  40. app_name="test",
  41. next_compression=False,
  42. ),
  43. """
  44. module.exports = {
  45. basePath: "",
  46. compress: false,
  47. reactStrictMode: true,
  48. trailingSlash: true,
  49. };
  50. """,
  51. ),
  52. (
  53. """
  54. module.exports = {
  55. basePath: "",
  56. compress: true,
  57. reactStrictMode: true,
  58. trailingSlash: true,
  59. };
  60. """,
  61. Config(
  62. app_name="test",
  63. frontend_path="/test",
  64. ),
  65. """
  66. module.exports = {
  67. basePath: "/test",
  68. compress: true,
  69. reactStrictMode: true,
  70. trailingSlash: true,
  71. };
  72. """,
  73. ),
  74. (
  75. """
  76. module.exports = {
  77. basePath: "",
  78. compress: true,
  79. reactStrictMode: true,
  80. trailingSlash: true,
  81. };
  82. """,
  83. Config(
  84. app_name="test",
  85. frontend_path="/test",
  86. next_compression=False,
  87. ),
  88. """
  89. module.exports = {
  90. basePath: "/test",
  91. compress: false,
  92. reactStrictMode: true,
  93. trailingSlash: true,
  94. };
  95. """,
  96. ),
  97. ],
  98. )
  99. def test_update_next_config(template_next_config, reflex_config, expected_next_config):
  100. assert (
  101. update_next_config(template_next_config, reflex_config) == expected_next_config
  102. )
  103. def test_initialize_requirements_txt(mocker):
  104. # File exists, reflex is included, do nothing
  105. mocker.patch("os.path.exists", return_value=True)
  106. open_mock = mock_open(read_data="reflex==0.2.9")
  107. mocker.patch("builtins.open", open_mock)
  108. initialize_requirements_txt()
  109. assert open_mock.call_count == 1
  110. assert open_mock().write.call_count == 0
  111. def test_initialize_requirements_txt_missing_reflex(mocker):
  112. # File exists, reflex is not included, add reflex
  113. open_mock = mock_open(read_data="random-package=1.2.3")
  114. mocker.patch("builtins.open", open_mock)
  115. initialize_requirements_txt()
  116. # Currently open for read, then open for append
  117. assert open_mock.call_count == 2
  118. assert open_mock().write.call_count == 1
  119. assert (
  120. open_mock().write.call_args[0][0]
  121. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  122. )
  123. def test_initialize_requirements_txt_not_exist(mocker):
  124. # File does not exist, create file with reflex
  125. mocker.patch("os.path.exists", return_value=False)
  126. open_mock = mock_open()
  127. mocker.patch("builtins.open", open_mock)
  128. initialize_requirements_txt()
  129. assert open_mock.call_count == 2
  130. assert open_mock().write.call_count == 1
  131. assert (
  132. open_mock().write.call_args[0][0]
  133. == f"\n{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  134. )