Skip to content

Commit ec73a91

Browse files
authored
Merge pull request #14 from ForNeus57/bodzio-setup-pytest
Add python unit tests
2 parents 197949b + ef0c976 commit ec73a91

6 files changed

Lines changed: 141 additions & 7 deletions

File tree

.github/workflows/unit_tests.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Run unit tests
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- '**'
7+
8+
jobs:
9+
pytest:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v6
14+
- name: Python Setup
15+
uses: actions/setup-python@v6
16+
with:
17+
python-version: 3.13
18+
cache: 'pip'
19+
cache-dependency-path: ./setup.cfg
20+
21+
- name: Install libraries
22+
run: make python-install-development
23+
24+
- name: Run pytest
25+
run: pytest

src/python/app/io/bmp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class BMPReader(IFormatReader):
7777

7878
@override
7979
def read_format(self, file: BinaryIO) -> Image:
80-
header = BitmapFileHeader.from_bytes(data=file.read(BitmapFileHeader.HEADER_LENGTH))
80+
# header = BitmapFileHeader.from_bytes(data=file.read(BitmapFileHeader.HEADER_LENGTH))
8181

8282
dib_header_size = file.read(4)
8383
dib_header_size, = struct.unpack('I', dib_header_size)

src/python/app/operation/flip.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ def parser(cls, parser: ArgumentParser) -> None:
3434
@override
3535
def __call__(self, args: Namespace, input_image: Image) -> Image:
3636
if args.horizontal:
37-
return Image(np.flip(input_image.data, axis=1))
37+
input_image = Image(np.flip(input_image.data, axis=1))
3838

3939
if args.vertical:
40-
return Image(np.flip(input_image.data, axis=0))
40+
input_image = Image(np.flip(input_image.data, axis=0))
4141

42-
assert False, 'unreachable'
42+
if not args.horizontal and not args.vertical:
43+
assert False, 'unreachable'
44+
return input_image

src/python/app/operation/grayscale.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ def __call__(self, args: Namespace, input_image: Image) -> Image:
3030
if input_image.data.shape[-1] == 1:
3131
return input_image
3232

33-
result_image = 0.2126 * input_image.data[:, :, 0] \
34-
+ 0.7152 * input_image.data[:, :, 1] \
35-
+ 0.0722 * input_image.data[:, :, 2]
33+
result_image = \
34+
0.2126 * input_image.data[:, :, 0] \
35+
+ 0.7152 * input_image.data[:, :, 1] \
36+
+ 0.0722 * input_image.data[:, :, 2]
3637

3738
return Image(data=np.repeat(a=np.expand_dims(a=np.clip(a=result_image,
3839
a_min=0.,

tests/cpp/test_fast.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
import numpy as np
3+
4+
from app.fast import system # type: ignore
5+
from app.fast import numpy_add # type: ignore
6+
7+
8+
def test_system():
9+
assert 0 == system('ls -la')
10+
assert 0 != system('false')
11+
12+
13+
numpy_add_values = [
14+
(np.array([2, 1, 4], dtype=np.double), np.sum([2, 1, 4])),
15+
(np.array([3, 1, 2], dtype=np.double), np.sum([3, 1, 2])),
16+
(np.array([10, 24, 11], dtype=np.double), np.sum([10, 24, 11])),
17+
(np.array([99, 1, 22], dtype=np.double), np.sum([99, 1, 22]))
18+
]
19+
20+
21+
@pytest.mark.parametrize("array1,expected_result", numpy_add_values)
22+
def test_numpy_add(array1, expected_result):
23+
assert numpy_add(array1) == expected_result

tests/python/test_operations.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from argparse import Namespace
2+
import numpy as np
3+
4+
from app.image.image import Image
5+
from app.operation.bgr2rgb import BGR2RGB
6+
from app.operation.flip import Flip
7+
from app.operation.grayscale import Grayscale
8+
from app.operation.rotate90 import Rotate90
9+
10+
11+
def test_bgr2rgb():
12+
input_image = Image(np.array([
13+
[[10, 20, 30], [15, 25, 35], [16, 26, 36]],
14+
[[20, 30, 40], [21, 31, 41], [22, 32, 42]],
15+
[[30, 40, 50], [31, 41, 51], [32, 42, 52]]
16+
]))
17+
18+
expected_image = Image(np.array([
19+
[[30, 20, 10], [35, 25, 15], [36, 26, 16]],
20+
[[40, 30, 20], [41, 31, 21], [42, 32, 22]],
21+
[[50, 40, 30], [51, 41, 31], [52, 42, 32]]
22+
]))
23+
24+
output_image = BGR2RGB()(args=None, input_image=input_image)
25+
26+
assert (output_image.data == expected_image.data).all()
27+
28+
29+
def test_flip():
30+
input_image = Image(np.array([
31+
[[10, 20, 30], [15, 25, 35], [16, 26, 36]],
32+
[[20, 30, 40], [21, 31, 41], [22, 32, 42]],
33+
[[30, 40, 50], [31, 41, 51], [32, 42, 52]]
34+
]))
35+
36+
expected_image = Image(np.array([
37+
[[32, 42, 52], [31, 41, 51], [30, 40, 50]],
38+
[[22, 32, 42], [21, 31, 41], [20, 30, 40]],
39+
[[16, 26, 36], [15, 25, 35], [10, 20, 30]],
40+
]))
41+
42+
output_image = Flip()(args=Namespace(horizontal=True, vertical=True), input_image=input_image)
43+
44+
assert (expected_image.data == output_image.data).all()
45+
46+
47+
def test_grayscale():
48+
# Try for already grayscale image
49+
input_image = Image(np.array([
50+
[[5], [123], [123]],
51+
[[12], [12], [12]],
52+
[[12], [12], [12]]
53+
]))
54+
expected_output = input_image
55+
56+
output_image = Grayscale()(args=None, input_image=input_image)
57+
58+
assert (expected_output.data == output_image.data).all()
59+
60+
# Try for coloured image
61+
input_image = Image(np.array([
62+
[[15, 10, 12], [120, 33, 20]],
63+
[[30, 0, 2], [10, 45, 22]]
64+
]))
65+
66+
output_image = Grayscale()(args=None, input_image=input_image)
67+
68+
assert output_image.data.shape[-1] == 3
69+
for row in output_image.data:
70+
for pixel in row:
71+
assert np.all(pixel == pixel[0])
72+
73+
74+
def test_rotate90():
75+
input_image = Image(np.array([
76+
[[15, 10, 12], [120, 33, 20]],
77+
[[30, 0, 2], [10, 45, 22]]
78+
]))
79+
80+
for rotation in range(-5, 5):
81+
expected_output = np.rot90(input_image.data, rotation)
82+
output_image = Rotate90()(args=Namespace(rotations=rotation), input_image=input_image)
83+
assert (expected_output == output_image.data).all()

0 commit comments

Comments
 (0)