Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: modify file bowser unit test to make it easier to understand #188

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions tests/unit/file_browser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,31 +197,26 @@ def directory_filter(path: Path) -> bool:
return path.is_dir() and not path.name.startswith("_")

BASE_PATH = HERE.parent.parent
directory = solara.reactive(BASE_PATH)

@solara.component
def Page():
def protect():
def check_base_path(value):
if not str(value).startswith(str(BASE_PATH)):
directory.value = BASE_PATH
def set_directory(path: Path) -> None:
directory.value = path if str(path).startswith(str(BASE_PATH)) else BASE_PATH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
directory.value = path if str(path).startswith(str(BASE_PATH)) else BASE_PATH
directory.value = path if str(path).startswith(str(BASE_PATH)) else BASE_PATH

Just to make you aware that this will always assign and will always need to do a comparison. Not that it matters because it is a test, but this might be slower in cases where compare is slow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I never thought about that. But it would also hard to imagine a path comparison could be too expensive. Do you have an example in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just wanted to make you aware that the behaviour has changed. Happy to merge this as is!


return directory.subscribe(check_base_path)

solara.use_effect(protect, [])
directory = solara.use_reactive(BASE_PATH, set_directory)
solara.FileBrowser(directory, filter=directory_filter)

top, rc = solara.render(Page(), handle_error=False)
list = rc.find(solara.components.file_browser.FileListWidget).widget
_, rc = solara.render(Page(), handle_error=False)
file_list = rc.find(solara.components.file_browser.FileListWidget).widget
mock = unittest.mock.MagicMock()
list.observe(mock, "files")
items = list.files
file_list.observe(mock, "files")
items = file_list.files
names = {k["name"] for k in items}
assert names == {"unit", "ui", "integration", ".."}
list.test_click("..")
file_list.test_click("..")
assert mock.call_count == 0
list.test_click("integration")
items = list.files
file_list.test_click("integration")
items = file_list.files
names = {k["name"] for k in items}
assert names != {"unit", "ui", "integration", ".."}
assert mock.call_count == 1