test_assets.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import shutil
  2. from pathlib import Path
  3. from typing import Generator
  4. import pytest
  5. import reflex as rx
  6. import reflex.constants as constants
  7. def test_shared_asset() -> None:
  8. """Test shared assets."""
  9. # The asset function copies a file to the app's external assets directory.
  10. asset = rx.asset(path="custom_script.js", shared=True, subfolder="subfolder")
  11. assert asset == "/external/test_assets/subfolder/custom_script.js"
  12. result_file = Path(
  13. Path.cwd(), "assets/external/test_assets/subfolder/custom_script.js"
  14. )
  15. assert result_file.exists()
  16. # Running a second time should not raise an error.
  17. asset = rx.asset(path="custom_script.js", shared=True, subfolder="subfolder")
  18. # Test the asset function without a subfolder.
  19. asset = rx.asset(path="custom_script.js", shared=True)
  20. assert asset == "/external/test_assets/custom_script.js"
  21. result_file = Path(Path.cwd(), "assets/external/test_assets/custom_script.js")
  22. assert result_file.exists()
  23. # clean up
  24. shutil.rmtree(Path.cwd() / "assets/external")
  25. with pytest.raises(FileNotFoundError):
  26. asset = rx.asset("non_existent_file.js")
  27. # Nothing is done to assets when file does not exist.
  28. assert not Path(Path.cwd() / "assets/external").exists()
  29. def test_deprecated_x_asset(capsys) -> None:
  30. """Test that the deprecated asset function raises a warning.
  31. Args:
  32. capsys: Pytest fixture that captures stdout and stderr.
  33. """
  34. assert rx.asset("custom_script.js", shared=True) == rx._x.asset("custom_script.js")
  35. assert (
  36. "DeprecationWarning: rx._x.asset has been deprecated in version 0.6.6"
  37. in capsys.readouterr().out
  38. )
  39. @pytest.mark.parametrize(
  40. "path,shared",
  41. [
  42. pytest.param("non_existing_file", True),
  43. pytest.param("non_existing_file", False),
  44. ],
  45. )
  46. def test_invalid_assets(path: str, shared: bool) -> None:
  47. """Test that asset raises an error when the file does not exist.
  48. Args:
  49. path: The path to the asset.
  50. shared: Whether the asset should be shared.
  51. """
  52. with pytest.raises(FileNotFoundError):
  53. _ = rx.asset(path, shared=shared)
  54. @pytest.fixture
  55. def custom_script_in_asset_dir() -> Generator[Path, None, None]:
  56. """Create a custom_script.js file in the app's assets directory.
  57. Yields:
  58. The path to the custom_script.js file.
  59. """
  60. asset_dir = Path.cwd() / constants.Dirs.APP_ASSETS
  61. asset_dir.mkdir(exist_ok=True)
  62. path = asset_dir / "custom_script.js"
  63. path.touch()
  64. yield path
  65. path.unlink()
  66. def test_local_asset(custom_script_in_asset_dir: Path) -> None:
  67. """Test that no error is raised if shared is set and both files exist.
  68. Args:
  69. custom_script_in_asset_dir: Fixture that creates a custom_script.js file in the app's assets directory.
  70. """
  71. asset = rx.asset("custom_script.js", shared=False)
  72. assert asset == "/custom_script.js"