test_imports.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pytest
  2. from reflex.utils.imports import (
  3. ImportDict,
  4. ImportVar,
  5. ParsedImportDict,
  6. merge_imports,
  7. parse_imports,
  8. )
  9. @pytest.mark.parametrize(
  10. "import_var, expected_name",
  11. [
  12. (
  13. ImportVar(tag="BaseTag"),
  14. "BaseTag",
  15. ),
  16. (
  17. ImportVar(tag="BaseTag", alias="AliasTag"),
  18. "BaseTag as AliasTag",
  19. ),
  20. (
  21. ImportVar(tag="BaseTag", is_default=True),
  22. "BaseTag",
  23. ),
  24. (
  25. ImportVar(tag="BaseTag", is_default=True, alias="AliasTag"),
  26. "AliasTag",
  27. ),
  28. (
  29. ImportVar(tag="BaseTag", is_default=False),
  30. "BaseTag",
  31. ),
  32. (
  33. ImportVar(tag="BaseTag", is_default=False, alias="AliasTag"),
  34. "BaseTag as AliasTag",
  35. ),
  36. ],
  37. )
  38. def test_import_var(import_var, expected_name):
  39. """Test that the import var name is computed correctly.
  40. Args:
  41. import_var: The import var.
  42. expected_name: The expected name.
  43. """
  44. assert import_var.name == expected_name
  45. @pytest.mark.parametrize(
  46. "input_1, input_2, output",
  47. [
  48. (
  49. {"react": {"Component"}},
  50. {"react": {"Component"}, "react-dom": {"render"}},
  51. {"react": {"Component"}, "react-dom": {"render"}},
  52. ),
  53. (
  54. {"react": {"Component"}, "next/image": {"Image"}},
  55. {"react": {"Component"}, "react-dom": {"render"}},
  56. {"react": {"Component"}, "react-dom": {"render"}, "next/image": {"Image"}},
  57. ),
  58. (
  59. {"react": {"Component"}},
  60. {"": {"some/custom.css"}},
  61. {"react": {"Component"}, "": {"some/custom.css"}},
  62. ),
  63. ],
  64. )
  65. def test_merge_imports(input_1, input_2, output):
  66. """Test that imports are merged correctly.
  67. Args:
  68. input_1: The first dict to merge.
  69. input_2: The second dict to merge.
  70. output: The expected output dict after merging.
  71. """
  72. res = merge_imports(input_1, input_2)
  73. assert res.keys() == output.keys()
  74. for key in output:
  75. assert set(res[key]) == set(output[key])
  76. @pytest.mark.parametrize(
  77. "input, output",
  78. [
  79. ({}, {}),
  80. (
  81. {"react": "Component"},
  82. {"react": [ImportVar(tag="Component")]},
  83. ),
  84. (
  85. {"react": ["Component"]},
  86. {"react": [ImportVar(tag="Component")]},
  87. ),
  88. (
  89. {"react": ["Component", ImportVar(tag="useState")]},
  90. {"react": [ImportVar(tag="Component"), ImportVar(tag="useState")]},
  91. ),
  92. (
  93. {"react": ["Component"], "foo": "anotherFunction"},
  94. {
  95. "react": [ImportVar(tag="Component")],
  96. "foo": [ImportVar(tag="anotherFunction")],
  97. },
  98. ),
  99. ],
  100. )
  101. def test_parse_imports(input: ImportDict, output: ParsedImportDict):
  102. assert parse_imports(input) == output