Skip to content

Commit

Permalink
Add software tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Mar 6, 2021
1 parent 76678c9 commit 7b5b6c2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/tests.yml
@@ -0,0 +1,53 @@
name: Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:

tests:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ ubuntu-latest ] # , macos-latest, windows-latest ]
python-version: [ "3.6", "3.7", "3.8", "3.9" ]
include:
- os: ubuntu-latest
path: ~/.cache/pip
#- os: macos-latest
# path: ~/Library/Caches/pip
#- os: windows-latest
# path: ~\AppData\Local\pip\Cache

name: Python ${{ matrix.python-version }} on OS ${{ matrix.os }}
steps:

- name: Acquire sources
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: x64

- name: Apply caching of dependencies
uses: actions/cache@v2
with:
path: ${{ matrix.path }}
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py', 'requirements-test.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install program
run: |
pip install --requirement=requirements-test.txt
pip install --editable=.[service]
- name: Run tests
run: |
# Run unit- and integration-tests
pytest test
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,7 @@ in progress
- Run black and isort on the code base
- Improve dependencies
- Improve documentation
- Add software tests


2020-06-08 0.3.0
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
@@ -0,0 +1 @@
pytest>=6.1.0,<7
103 changes: 103 additions & 0 deletions test/test_core.py
@@ -0,0 +1,103 @@
import PIL
import pytest

from imagecast.core import ImageEngine


@pytest.fixture
def ie() -> ImageEngine:
ie = ImageEngine()

# Acquire image.
ie.download("https://unsplash.com/photos/WvdKljW55rM/download?force=true")

return ie


def test_read(ie: ImageEngine):

assert ie.data.startswith(
b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00"
)
assert ie.image is None

# Read image.
ie.read()
assert isinstance(ie.image, PIL.JpegImagePlugin.JpegImageFile)
assert ie.format == "JPEG"

# Test some original image attributes.
assert ie.image.getbbox() == (0, 0, 3024, 4032)
assert ie.image.getbands() == ("R", "G", "B")
assert ie.image.getextrema() == ((0, 255), (0, 255), (0, 255))
assert ie.image.getpalette() is None
assert ie.image.entropy() == 8.343561030062993


def test_grayscale(ie: ImageEngine):

ie.read()
ie.grayscale()

assert ie.image.getbbox() == (0, 0, 3024, 4032)
assert ie.image.getbands() == ("L",)
assert ie.image.getextrema() == (0, 255)
assert ie.image.getpalette() is None
assert ie.image.entropy() == 6.781713650910312


def test_monochrome(ie: ImageEngine):

ie.read()
ie.monochrome(50)

assert ie.image.getbbox() == (0, 0, 3024, 4032)
assert ie.image.getbands() == ("1",)
assert ie.image.getextrema() == (0, 255)
assert ie.image.getpalette() is None
assert ie.image.entropy() == 0.44739791215403113


def test_crop(ie: ImageEngine):

ie.read()
ie.crop((50, 50, 200, 200))

assert ie.image.getbbox() == (0, 0, 150, 150)
assert ie.image.entropy() == 7.9107901778189404


def test_resize_width(ie: ImageEngine):

ie.read()
ie.resize_width(320)

assert ie.image.getbbox() == (0, 0, 320, 426)
assert ie.image.entropy() == 7.808594077176596


def test_resize_height(ie: ImageEngine):

ie.read()
ie.resize_height(400)

assert ie.image.getbbox() == (0, 0, 300, 400)
assert ie.image.entropy() == 7.781224877582197


def test_to_buffer(ie: ImageEngine):

ie.read()
ie.crop((50, 50, 100, 100))
buffer = ie.to_buffer("png", dpi=72)

assert buffer.startswith(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")


def test_to_bytes(ie: ImageEngine):

ie.read()
ie.crop((50, 50, 100, 100))
buffer = ie.to_bytes()

assert buffer.startswith(b"\x80F:\x86L@\x96^Q\xa2m_\xacvj\xb4\x80s\xbd")

0 comments on commit 7b5b6c2

Please sign in to comment.