-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add content-type filter method to folder_paths (#4054)
* Add content-type filter method to folder_paths * Add unit tests * Hardcode webp content-type * Annotate content_types as Literal["image", "video", "audio"]
- Loading branch information
1 parent
36c83cd
commit e760bf5
Showing
4 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
52 changes: 52 additions & 0 deletions
52
tests-unit/folder_paths_test/filter_by_content_types_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
import os | ||
import tempfile | ||
from folder_paths import filter_files_content_types | ||
|
||
@pytest.fixture(scope="module") | ||
def file_extensions(): | ||
return { | ||
'image': ['bmp', 'cdr', 'gif', 'heif', 'ico', 'jpeg', 'jpg', 'pcx', 'png', 'pnm', 'ppm', 'psd', 'sgi', 'svg', 'tiff', 'webp', 'xbm', 'xcf', 'xpm'], | ||
'audio': ['aif', 'aifc', 'aiff', 'au', 'awb', 'flac', 'm4a', 'mp2', 'mp3', 'ogg', 'sd2', 'smp', 'snd', 'wav'], | ||
'video': ['avi', 'flv', 'm2v', 'm4v', 'mj2', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ogv', 'qt', 'webm', 'wmv'] | ||
} | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def mock_dir(file_extensions): | ||
with tempfile.TemporaryDirectory() as directory: | ||
for content_type, extensions in file_extensions.items(): | ||
for extension in extensions: | ||
with open(f"{directory}/sample_{content_type}.{extension}", "w") as f: | ||
f.write(f"Sample {content_type} file in {extension} format") | ||
yield directory | ||
|
||
|
||
def test_categorizes_all_correctly(mock_dir, file_extensions): | ||
files = os.listdir(mock_dir) | ||
for content_type, extensions in file_extensions.items(): | ||
filtered_files = filter_files_content_types(files, [content_type]) | ||
for extension in extensions: | ||
assert f"sample_{content_type}.{extension}" in filtered_files | ||
|
||
|
||
def test_categorizes_all_uniquely(mock_dir, file_extensions): | ||
files = os.listdir(mock_dir) | ||
for content_type, extensions in file_extensions.items(): | ||
filtered_files = filter_files_content_types(files, [content_type]) | ||
assert len(filtered_files) == len(extensions) | ||
|
||
|
||
def test_handles_bad_extensions(): | ||
files = ["file1.txt", "file2.py", "file3.example", "file4.pdf", "file5.ini", "file6.doc", "file7.md"] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] | ||
|
||
|
||
def test_handles_no_extension(): | ||
files = ["file1", "file2", "file3", "file4", "file5", "file6", "file7"] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] | ||
|
||
|
||
def test_handles_no_files(): | ||
files = [] | ||
assert filter_files_content_types(files, ["image", "audio", "video"]) == [] |