robmarkcole / HASS-Sighthound

Beta features for Home Assistant Sighthound integration
https://www.home-assistant.io/integrations/sighthound/
MIT License
34 stars 10 forks source link

Add test #10

Open robmarkcole opened 4 years ago

robmarkcole commented 4 years ago
with patch("pillow.save_image") as mock_save:
    # call the HA code
    mock_save.assert_called_with(*args, **kwargs)
robmarkcole commented 4 years ago

Solved:

from PIL import Image
import numpy as np
from unittest.mock import patch

def get_noise_array():
    shape = (100, 100)
    img_array = np.zeros(shape)

    mean = 0
    gauss = np.random.normal(mean, 1, img_array.shape)

    # normalize image to range [0,255]
    noisy = img_array + gauss
    minv = np.amin(noisy)
    maxv = np.amax(noisy)
    noisy = (255 * (noisy - minv) / (maxv - minv)).astype(np.uint8)
    return noisy

def main():
    image = Image.fromarray(get_noise_array())
    image.save("noise.jpg")

def test_main():
    with patch("PIL.Image.Image.save") as mock_save:
        main()
        mock_save.assert_called_with("noise.jpg")