reflex/tests/units/utils/test_imports.py
Thomas Brandého 3f538865b5
reorganize all tests in a single top folder (#3981)
* lift node version restraint to allow more recent version if already installed

* add node test for latest version

* change python version

* use purple for debug logs

* update workflow

* add playwright dev dependency

* update workflow

* change test

* oops

* improve test

* update test

* fix tests

* mv units tests to a subfolder

* reorganize tests

* fix install

* update test_state

* revert node changes and only keep new tests organization

* move integration tests in tests/integration

* fix integration workflow

* fix dockerfile workflow

* fix dockerfile workflow 2

* fix shared_state
2024-09-26 01:22:52 +02:00

118 lines
3.1 KiB
Python

import pytest
from reflex.utils.imports import (
ImportDict,
ImportVar,
ParsedImportDict,
merge_imports,
parse_imports,
)
@pytest.mark.parametrize(
"import_var, expected_name",
[
(
ImportVar(tag="BaseTag"),
"BaseTag",
),
(
ImportVar(tag="BaseTag", alias="AliasTag"),
"BaseTag as AliasTag",
),
(
ImportVar(tag="BaseTag", is_default=True),
"BaseTag",
),
(
ImportVar(tag="BaseTag", is_default=True, alias="AliasTag"),
"AliasTag",
),
(
ImportVar(tag="BaseTag", is_default=False),
"BaseTag",
),
(
ImportVar(tag="BaseTag", is_default=False, alias="AliasTag"),
"BaseTag as AliasTag",
),
],
)
def test_import_var(import_var, expected_name):
"""Test that the import var name is computed correctly.
Args:
import_var: The import var.
expected_name: The expected name.
"""
assert import_var.name == expected_name
@pytest.mark.parametrize(
"input_1, input_2, output",
[
(
{"react": {"Component"}},
{"react": {"Component"}, "react-dom": {"render"}},
{"react": {ImportVar("Component")}, "react-dom": {ImportVar("render")}},
),
(
{"react": {"Component"}, "next/image": {"Image"}},
{"react": {"Component"}, "react-dom": {"render"}},
{
"react": {ImportVar("Component")},
"react-dom": {ImportVar("render")},
"next/image": {ImportVar("Image")},
},
),
(
{"react": {"Component"}},
{"": {"some/custom.css"}},
{"react": {ImportVar("Component")}, "": {ImportVar("some/custom.css")}},
),
],
)
def test_merge_imports(input_1, input_2, output):
"""Test that imports are merged correctly.
Args:
input_1: The first dict to merge.
input_2: The second dict to merge.
output: The expected output dict after merging.
"""
res = merge_imports(input_1, input_2)
assert res.keys() == output.keys()
for key in output:
assert set(res[key]) == set(output[key])
@pytest.mark.parametrize(
"input, output",
[
({}, {}),
(
{"react": "Component"},
{"react": [ImportVar(tag="Component")]},
),
(
{"react": ["Component"]},
{"react": [ImportVar(tag="Component")]},
),
(
{"react": ["Component", ImportVar(tag="useState")]},
{"react": [ImportVar(tag="Component"), ImportVar(tag="useState")]},
),
(
{"react": ["Component"], "foo": "anotherFunction"},
{
"react": [ImportVar(tag="Component")],
"foo": [ImportVar(tag="anotherFunction")],
},
),
],
)
def test_parse_imports(input: ImportDict, output: ParsedImportDict):
assert parse_imports(input) == output