test_imports.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import pytest
  2. from reflex.utils.imports import ImportVar, merge_imports
  3. @pytest.mark.parametrize(
  4. "import_var, expected_name",
  5. [
  6. (
  7. ImportVar(tag="BaseTag"),
  8. "BaseTag",
  9. ),
  10. (
  11. ImportVar(tag="BaseTag", alias="AliasTag"),
  12. "BaseTag as AliasTag",
  13. ),
  14. (
  15. ImportVar(tag="BaseTag", is_default=True),
  16. "BaseTag",
  17. ),
  18. (
  19. ImportVar(tag="BaseTag", is_default=True, alias="AliasTag"),
  20. "AliasTag",
  21. ),
  22. (
  23. ImportVar(tag="BaseTag", is_default=False),
  24. "BaseTag",
  25. ),
  26. (
  27. ImportVar(tag="BaseTag", is_default=False, alias="AliasTag"),
  28. "BaseTag as AliasTag",
  29. ),
  30. ],
  31. )
  32. def test_import_var(import_var, expected_name):
  33. """Test that the import var name is computed correctly.
  34. Args:
  35. import_var: The import var.
  36. expected_name: The expected name.
  37. """
  38. assert import_var.name == expected_name
  39. @pytest.mark.parametrize(
  40. "input_1, input_2, output",
  41. [
  42. (
  43. {"react": {"Component"}},
  44. {"react": {"Component"}, "react-dom": {"render"}},
  45. {"react": {"Component"}, "react-dom": {"render"}},
  46. ),
  47. (
  48. {"react": {"Component"}, "next/image": {"Image"}},
  49. {"react": {"Component"}, "react-dom": {"render"}},
  50. {"react": {"Component"}, "react-dom": {"render"}, "next/image": {"Image"}},
  51. ),
  52. (
  53. {"react": {"Component"}},
  54. {"": {"some/custom.css"}},
  55. {"react": {"Component"}, "": {"some/custom.css"}},
  56. ),
  57. ],
  58. )
  59. def test_merge_imports(input_1, input_2, output):
  60. """Test that imports are merged correctly.
  61. Args:
  62. input_1: The first dict to merge.
  63. input_2: The second dict to merge.
  64. output: The expected output dict after merging.
  65. """
  66. res = merge_imports(input_1, input_2)
  67. assert set(res.keys()) == set(output.keys())
  68. for key in output:
  69. assert set(res[key]) == set(output[key])