|
| 1 | +import pytest |
| 2 | +from PIL import Image |
| 3 | +import tempfile |
| 4 | +import os |
| 5 | +from owmap.genmap import ( |
| 6 | + interpret_rgb_as_terrain, |
| 7 | + interpret_rgb_as_height, |
| 8 | + interpret_rgb_as_vegetation, |
| 9 | + generate_donut_map, |
| 10 | + generate_palette |
| 11 | +) |
| 12 | + |
| 13 | +class TestRGBInterpretation: |
| 14 | + """Test class for RGB interpretation functions. """ |
| 15 | + |
| 16 | + def test_terrain_interpretation_known_colors(self): |
| 17 | + # Test some basic terrain mappings |
| 18 | + assert interpret_rgb_as_terrain((0, 128, 0)) == "TERRAIN_LUSH" |
| 19 | + assert interpret_rgb_as_terrain((255, 200, 0)) == "TERRAIN_ARID" |
| 20 | + assert interpret_rgb_as_terrain((0, 0, 128)) == "TERRAIN_WATER" |
| 21 | + assert interpret_rgb_as_terrain((0, 255, 0)) == "TERRAIN_TEMPERATE" |
| 22 | + |
| 23 | + def test_terrain_interpretation_unknown_color(self): |
| 24 | + # Test with a color that's not in the terrain map |
| 25 | + assert interpret_rgb_as_terrain((123, 45, 67)) == "Unknown" |
| 26 | + assert interpret_rgb_as_terrain((255, 255, 255)) == "Unknown" |
| 27 | + |
| 28 | + def test_height_interpretation_known_colors(self): |
| 29 | + assert interpret_rgb_as_height((222, 222, 222)) == "HEIGHT_MOUNTAIN" |
| 30 | + assert interpret_rgb_as_height((144, 144, 144)) == "HEIGHT_HILL" |
| 31 | + assert interpret_rgb_as_height((0, 0, 255)) == "HEIGHT_OCEAN" |
| 32 | + assert interpret_rgb_as_height((255, 0, 0)) == "HEIGHT_VOLCANO" |
| 33 | + |
| 34 | + def test_height_interpretation_unknown_color(self): |
| 35 | + assert interpret_rgb_as_height((100, 200, 50)) == "None" |
| 36 | + |
| 37 | + def test_vegetation_interpretation(self): |
| 38 | + assert interpret_rgb_as_vegetation((0, 255, 0)) == "VEGETATION_TREES" |
| 39 | + assert interpret_rgb_as_vegetation((0, 128, 0)) == "VEGETATION_SCRUB" |
| 40 | + assert interpret_rgb_as_vegetation((255, 255, 255)) == "None" |
| 41 | + assert interpret_rgb_as_vegetation((50, 75, 100)) == "None" |
| 42 | + |
| 43 | + |
| 44 | +class TestImageGeneration: |
| 45 | + """Test class for image generation functions.""" |
| 46 | + |
| 47 | + def test_generate_donut_map_creates_file(self): |
| 48 | + """Test that generate_donut_map creates a PNG file.""" |
| 49 | + # Use a temporary file to avoid cluttering the filesystem |
| 50 | + with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp: |
| 51 | + temp_path = tmp.name |
| 52 | + |
| 53 | + try: |
| 54 | + # Generate the donut map |
| 55 | + generate_donut_map(temp_path, size=64) # Small size for faster test |
| 56 | + |
| 57 | + # Check that file was created and is valid |
| 58 | + assert os.path.exists(temp_path) |
| 59 | + |
| 60 | + # Verify it's a valid image by opening it |
| 61 | + img = Image.open(temp_path) |
| 62 | + assert img.size == (64, 64) |
| 63 | + assert img.mode == 'RGB' |
| 64 | + |
| 65 | + finally: |
| 66 | + # Clean up the temporary file |
| 67 | + if os.path.exists(temp_path): |
| 68 | + os.unlink(temp_path) |
| 69 | + |
| 70 | + def test_generate_palette_creates_correct_image(self): |
| 71 | + """Test that generate_palette creates the correct palette image.""" |
| 72 | + # Create a small test palette |
| 73 | + test_palette = { |
| 74 | + (255, 0, 0): "Red", # Red |
| 75 | + (0, 255, 0): "Green", # Green |
| 76 | + (0, 0, 255): "Blue" # Blue |
| 77 | + } |
| 78 | + |
| 79 | + with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp: |
| 80 | + temp_path = tmp.name |
| 81 | + |
| 82 | + try: |
| 83 | + generate_palette(test_palette, temp_path) |
| 84 | + |
| 85 | + # Verify the generated image |
| 86 | + assert os.path.exists(temp_path) |
| 87 | + img = Image.open(temp_path) |
| 88 | + |
| 89 | + # Should be 3 pixels wide (one for each color), 1 pixel tall |
| 90 | + assert img.size == (3, 1) |
| 91 | + assert img.mode == 'RGB' |
| 92 | + |
| 93 | + # Check that the colors are correct |
| 94 | + assert img.getpixel((0, 0)) == (255, 0, 0) # Red |
| 95 | + assert img.getpixel((1, 0)) == (0, 255, 0) # Green |
| 96 | + assert img.getpixel((2, 0)) == (0, 0, 255) # Blue |
| 97 | + |
| 98 | + finally: |
| 99 | + if os.path.exists(temp_path): |
| 100 | + os.unlink(temp_path) |
0 commit comments