|
| 1 | +""" |
| 2 | +Smoke test for Pylette library. |
| 3 | +Tests basic functionality to ensure the library works correctly after installation. |
| 4 | +
|
| 5 | +This is here because I've pushed changes twice now that broke the library... :( |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from PIL import Image |
| 15 | + |
| 16 | +import Pylette |
| 17 | + |
| 18 | + |
| 19 | +def test_library_import(): |
| 20 | + """Test that the library can be imported successfully.""" |
| 21 | + print("✓ Testing library import...") |
| 22 | + # Import should work if we get here |
| 23 | + assert hasattr(Pylette, "extract_colors"), "extract_colors function should be available" |
| 24 | + print(" Import successful") |
| 25 | + |
| 26 | + |
| 27 | +def test_basic_color_extraction(): |
| 28 | + """Test basic color extraction functionality.""" |
| 29 | + print("✓ Testing basic color extraction...") |
| 30 | + |
| 31 | + # Create a simple test image |
| 32 | + test_img = Image.new("RGB", (100, 100), color="red") |
| 33 | + palette = Pylette.extract_colors(test_img, palette_size=5) |
| 34 | + |
| 35 | + assert len(palette) <= 5, f"Palette size should not exceed 5, got {len(palette)}" |
| 36 | + assert len(palette) > 0, "Should extract at least one color" |
| 37 | + |
| 38 | + print(f" Extracted {len(palette)} colors successfully") |
| 39 | + |
| 40 | + |
| 41 | +def test_cli_functionality(): |
| 42 | + """Test CLI functionality.""" |
| 43 | + print("✓ Testing CLI functionality...") |
| 44 | + |
| 45 | + # Create a temporary test image |
| 46 | + with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp: |
| 47 | + test_img = Image.new("RGB", (50, 50), color="blue") |
| 48 | + test_img.save(tmp.name) |
| 49 | + |
| 50 | + # Test CLI command |
| 51 | + result = subprocess.run( |
| 52 | + [sys.executable, "-m", "Pylette.cmd", tmp.name, "--palette_size", "3"], capture_output=True, text=True |
| 53 | + ) |
| 54 | + |
| 55 | + if result.returncode != 0: |
| 56 | + raise RuntimeError(f"CLI test failed: {result.stderr}") |
| 57 | + |
| 58 | + # Clean up |
| 59 | + Path(tmp.name).unlink() |
| 60 | + |
| 61 | + print(" CLI test passed") |
| 62 | + |
| 63 | + |
| 64 | +def test_extraction_methods(): |
| 65 | + """Test different extraction methods.""" |
| 66 | + print("✓ Testing different extraction methods...") |
| 67 | + |
| 68 | + test_img = Image.new("RGB", (50, 50), color="green") |
| 69 | + |
| 70 | + # Test K-means extractor |
| 71 | + kmeans_palette = Pylette.extract_colors(test_img, palette_size=3, mode=Pylette.types.ExtractionMethod.KM) |
| 72 | + assert len(kmeans_palette) <= 3, "K-means should respect palette size" |
| 73 | + print(f" K-means extracted {len(kmeans_palette)} colors") |
| 74 | + |
| 75 | + # Test Median cut extractor |
| 76 | + mediancut_palette = Pylette.extract_colors(test_img, palette_size=3, mode=Pylette.types.ExtractionMethod.MC) |
| 77 | + assert len(mediancut_palette) <= 3, "Median cut should respect palette size" |
| 78 | + print(f" Median cut extracted {len(mediancut_palette)} colors") |
| 79 | + |
| 80 | + |
| 81 | +def test_json_export(): |
| 82 | + """Test JSON export functionality.""" |
| 83 | + print("✓ Testing JSON export...") |
| 84 | + |
| 85 | + test_img = Image.new("RGB", (50, 50), color="purple") |
| 86 | + palette = Pylette.extract_colors(test_img, palette_size=2) |
| 87 | + |
| 88 | + # Test JSON export |
| 89 | + with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp: |
| 90 | + palette.to_json(tmp.name) |
| 91 | + |
| 92 | + # Verify JSON file was created and is valid |
| 93 | + with open(tmp.name, "r") as f: |
| 94 | + data = json.load(f) |
| 95 | + assert "colors" in data, "JSON should contain 'colors' key" |
| 96 | + assert len(data["colors"]) <= 2, f"Should have at most 2 colors, got {len(data['colors'])}" |
| 97 | + |
| 98 | + # Clean up |
| 99 | + Path(tmp.name).unlink() |
| 100 | + |
| 101 | + print(" JSON export test passed") |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + """Run all smoke tests.""" |
| 106 | + print("🧪 Starting Pylette smoke test...") |
| 107 | + |
| 108 | + try: |
| 109 | + test_library_import() |
| 110 | + test_basic_color_extraction() |
| 111 | + test_cli_functionality() |
| 112 | + test_extraction_methods() |
| 113 | + test_json_export() |
| 114 | + |
| 115 | + print("✅ All smoke tests passed!") |
| 116 | + print("🎉 Pylette is working correctly") |
| 117 | + |
| 118 | + except Exception as e: |
| 119 | + print(f"❌ Smoke test failed: {e}") |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments