Back to Repositories

Testing File Content Type Filtering Implementation in ComfyUI

This test suite validates the file content type filtering functionality in ComfyUI, focusing on proper categorization of media files by their extensions. It ensures accurate filtering of image, audio, and video files while handling edge cases like invalid extensions and empty inputs.

Test Coverage Overview

The test suite provides comprehensive coverage of file content type filtering functionality.

Key areas tested include:
  • Correct categorization of all supported file extensions across image, audio, and video types
  • Unique categorization verification ensuring no duplicate classifications
  • Edge case handling for invalid extensions, files without extensions, and empty file lists

Implementation Analysis

The testing approach utilizes pytest fixtures to create a controlled test environment with temporary test files. It implements systematic verification using fixture-based test data and mock directory structures.

Key patterns include:
  • Module-scoped fixtures for efficient test data reuse
  • Temporary directory management for isolated testing
  • Systematic iteration over file types and extensions

Technical Details

Testing tools and configuration:
  • pytest as the primary testing framework
  • tempfile module for temporary test directory creation
  • os module for file system operations
  • Custom fixtures for extension mapping and directory setup
  • Automated cleanup through context managers

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolation of test cases using temporary directories
  • Efficient test data management through fixtures
  • Comprehensive edge case coverage
  • Clear test case organization and naming
  • Resource cleanup through proper fixture scope management

comfyanonymous/comfyui

tests-unit/folder_paths_test/filter_by_content_types_test.py

            
import pytest
import os
import tempfile
from folder_paths import filter_files_content_types

@pytest.fixture(scope="module")
def file_extensions():
    return {
        'image': ['gif', 'heif', 'ico', 'jpeg', 'jpg', 'png', 'pnm', 'ppm', 'svg', 'tiff', 'webp', 'xbm', 'xpm'], 
        'audio': ['aif', 'aifc', 'aiff', 'au', 'flac', 'm4a', 'mp2', 'mp3', 'ogg', 'snd', 'wav'], 
        'video': ['avi', 'm2v', 'm4v', '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"]) == []