def test_watermark_text(tmp_path, png):
    """Test a text watermark."""
    image = tmp_path / "text-only.png"
    png(image)

    add_watermark(image, text="www.arresto-momentum.com")

    output = image.with_name("text-only-w.jpg")
    assert output.is_file()
def test_watermark_picture(tmp_path, picture, png):
    """Test a picture watermark."""
    image = tmp_path / "picture-only.png"
    png(image)

    add_watermark(image, picture=picture)

    output = image.with_name("picture-only-w.jpg")
    assert output.is_file()
def test_watermark_both(tmp_path, picture, png):
    """Test both text and picture watermarks."""
    image = tmp_path / "text-and-picture.png"
    png(image)

    text = "www.arresto-momentum.com"
    img = add_watermark(image, text=text, picture=picture)
    assert img is not None

    output = image.with_name("text-and-picture-w.jpg")
    assert output.is_file()

    # Calling twice should return the already processed file
    assert add_watermark(image, text=text, picture=picture)
def test_optimize(tmp_path, png):
    """Test the optimization."""
    image = tmp_path / "picture.png"
    png(image)

    watermarked = add_watermark(image, text="confidential")
    assert watermarked.is_file()

    class SourceMocked:
        @classmethod
        def from_file(cls, path):
            return cls()

        def to_file(self, path):
            Path(path).touch()

    def from_file(path):
        return SourceMocked.from_file(path)

    tinify.compression_count = 0

    with patch.object(tinify, "from_file", new=from_file):
        optimized = optimize(watermarked)

    assert optimized.is_file()

    # Already done
    assert optimize(optimized).name == optimized.name
def test_optimize_no_more_compression_counts(tmp_path, png):
    """Test zero compression count."""
    image = tmp_path / "picture.png"
    png(image)

    watermarked = add_watermark(image, text="confidential")
    assert watermarked.is_file()

    tinify.compression_count = 501
    assert optimize(watermarked) is None
def test_optimize_retry(tmp_path, png):
    """Test retries."""
    image = tmp_path / "picture.png"
    png(image)

    watermarked = add_watermark(image, text="confidential")
    assert watermarked.is_file()

    class SourceMocked:
        @classmethod
        def from_file(cls, path):
            raise tinify.ServerError("Mock'ed error")

    def from_file(path):
        return SourceMocked.from_file(path)

    tinify.compression_count = 1

    with patch.object(tinify, "from_file", new=from_file):
        assert optimize(watermarked) is None
def test_file_not_found(location):
    """Test a file that does not exist."""
    img = add_watermark(location.parent / "inexistant.png", text="foo")
    assert img is None
def test_file_not_an_image(location):
    """Test a file that is not an image."""
    img = add_watermark(location.parent / "conftest.py", text="foo")
    assert img is None