test_config.py 6.2 KB

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