test_config.py 5.8 KB

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