1
0

test_config.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import multiprocessing
  2. import os
  3. import pytest
  4. import reflex as rx
  5. import reflex.config
  6. from reflex.constants import Endpoint
  7. def test_requires_app_name():
  8. """Test that a config requires an app_name."""
  9. with pytest.raises(ValueError):
  10. rx.Config() # type: ignore
  11. def test_set_app_name(base_config_values):
  12. """Test that the app name is set to the value passed in.
  13. Args:
  14. base_config_values: Config values.
  15. """
  16. config = rx.Config(**base_config_values)
  17. assert config.app_name == base_config_values["app_name"]
  18. @pytest.mark.parametrize(
  19. "env_var, value",
  20. [
  21. ("APP_NAME", "my_test_app"),
  22. ("FRONTEND_PORT", 3001),
  23. ("FRONTEND_PATH", "/test"),
  24. ("BACKEND_PORT", 8001),
  25. ("API_URL", "https://mybackend.com:8000"),
  26. ("DEPLOY_URL", "https://myfrontend.com"),
  27. ("BACKEND_HOST", "127.0.0.1"),
  28. ("DB_URL", "postgresql://user:pass@localhost:5432/db"),
  29. ("REDIS_URL", "redis://localhost:6379"),
  30. ("TIMEOUT", 600),
  31. ("TELEMETRY_ENABLED", False),
  32. ("TELEMETRY_ENABLED", True),
  33. ],
  34. )
  35. def test_update_from_env(base_config_values, monkeypatch, env_var, value):
  36. """Test that environment variables override config values.
  37. Args:
  38. base_config_values: Config values.
  39. monkeypatch: The pytest monkeypatch object.
  40. env_var: The environment variable name.
  41. value: The environment variable value.
  42. """
  43. monkeypatch.setenv(env_var, str(value))
  44. assert os.environ.get(env_var) == str(value)
  45. config = rx.Config(**base_config_values)
  46. assert getattr(config, env_var.lower()) == value
  47. @pytest.mark.parametrize(
  48. "kwargs, expected",
  49. [
  50. (
  51. {"app_name": "test_app", "api_url": "http://example.com"},
  52. f"{Endpoint.EVENT}",
  53. ),
  54. (
  55. {"app_name": "test_app", "api_url": "http://example.com/api"},
  56. f"/api{Endpoint.EVENT}",
  57. ),
  58. ],
  59. )
  60. def test_event_namespace(mocker, kwargs, expected):
  61. """Test the event namespace.
  62. Args:
  63. mocker: The pytest mock object.
  64. kwargs: The Config kwargs.
  65. expected: Expected namespace
  66. """
  67. conf = rx.Config(**kwargs)
  68. mocker.patch("reflex.config.get_config", return_value=conf)
  69. config = reflex.config.get_config()
  70. assert conf == config
  71. assert config.get_event_namespace() == expected
  72. DEFAULT_CONFIG = rx.Config(app_name="a")
  73. @pytest.mark.parametrize(
  74. ("config_kwargs", "env_vars", "set_persistent_vars", "exp_config_values"),
  75. [
  76. (
  77. {},
  78. {},
  79. {},
  80. {
  81. "api_url": DEFAULT_CONFIG.api_url,
  82. "backend_port": DEFAULT_CONFIG.backend_port,
  83. "deploy_url": DEFAULT_CONFIG.deploy_url,
  84. "frontend_port": DEFAULT_CONFIG.frontend_port,
  85. },
  86. ),
  87. # Ports set in config kwargs
  88. (
  89. {"backend_port": 8001, "frontend_port": 3001},
  90. {},
  91. {},
  92. {
  93. "api_url": "http://localhost:8001",
  94. "backend_port": 8001,
  95. "deploy_url": "http://localhost:3001",
  96. "frontend_port": 3001,
  97. },
  98. ),
  99. # Ports set in environment take precedence
  100. (
  101. {"backend_port": 8001, "frontend_port": 3001},
  102. {"BACKEND_PORT": 8002},
  103. {},
  104. {
  105. "api_url": "http://localhost:8002",
  106. "backend_port": 8002,
  107. "deploy_url": "http://localhost:3001",
  108. "frontend_port": 3001,
  109. },
  110. ),
  111. # Ports set on the command line take precedence
  112. (
  113. {"backend_port": 8001, "frontend_port": 3001},
  114. {"BACKEND_PORT": 8002},
  115. {"frontend_port": "3005"},
  116. {
  117. "api_url": "http://localhost:8002",
  118. "backend_port": 8002,
  119. "deploy_url": "http://localhost:3005",
  120. "frontend_port": 3005,
  121. },
  122. ),
  123. # api_url / deploy_url already set should not be overridden
  124. (
  125. {"api_url": "http://foo.bar:8900", "deploy_url": "http://foo.bar:3001"},
  126. {"BACKEND_PORT": 8002},
  127. {"frontend_port": "3005"},
  128. {
  129. "api_url": "http://foo.bar:8900",
  130. "backend_port": 8002,
  131. "deploy_url": "http://foo.bar:3001",
  132. "frontend_port": 3005,
  133. },
  134. ),
  135. ],
  136. )
  137. def test_replace_defaults(
  138. monkeypatch,
  139. config_kwargs,
  140. env_vars,
  141. set_persistent_vars,
  142. exp_config_values,
  143. ):
  144. """Test that the config replaces defaults with values from the environment.
  145. Args:
  146. monkeypatch: The pytest monkeypatch object.
  147. config_kwargs: The config kwargs.
  148. env_vars: The environment variables.
  149. set_persistent_vars: The values passed to config._set_persistent variables.
  150. exp_config_values: The expected config values.
  151. """
  152. mock_os_env = os.environ.copy()
  153. monkeypatch.setattr(reflex.config.os, "environ", mock_os_env) # type: ignore
  154. mock_os_env.update({k: str(v) for k, v in env_vars.items()})
  155. c = rx.Config(app_name="a", **config_kwargs)
  156. c._set_persistent(**set_persistent_vars)
  157. for key, value in exp_config_values.items():
  158. assert getattr(c, key) == value
  159. def reflex_dir_constant():
  160. return rx.constants.Reflex.DIR
  161. def test_reflex_dir_env_var(monkeypatch, tmp_path):
  162. """Test that the REFLEX_DIR environment variable is used to set the Reflex.DIR constant.
  163. Args:
  164. monkeypatch: The pytest monkeypatch object.
  165. tmp_path: The pytest tmp_path object.
  166. """
  167. monkeypatch.setenv("REFLEX_DIR", str(tmp_path))
  168. mp_ctx = multiprocessing.get_context(method="spawn")
  169. with mp_ctx.Pool(processes=1) as pool:
  170. assert pool.apply(reflex_dir_constant) == str(tmp_path)