Skip to content

Commit

Permalink
tests: redo tests with fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
loiccoyle committed Jul 5, 2024
1 parent aeacd39 commit 1eacf54
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 303 deletions.
37 changes: 37 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from pathlib import Path
import shutil
import pytest

from phomo import Pool, utils


TEST_PATH = Path("test_tiles/")


@pytest.fixture(scope="session")
def tile_dir():
tile_dir = TEST_PATH / "rainbow"
if not tile_dir.is_dir():
tile_dir.mkdir(parents=True)
channel_range = range(0, 255, 60)
utils.rainbow_of_squares(
tile_dir,
size=(50, 50),
r_range=channel_range,
g_range=channel_range,
b_range=channel_range,
)
yield tile_dir
shutil.rmtree(TEST_PATH)


@pytest.fixture(scope="session")
def pool(tile_dir: Path):
# create test pool
return Pool.from_dir(tile_dir, tile_size=(10, 10))


@pytest.fixture(scope="session")
def pool_big_tiles(tile_dir: Path):
# create test pool
return Pool.from_dir(tile_dir, tile_size=(50, 50))
118 changes: 64 additions & 54 deletions tests/unit/test_grid.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,71 @@
from unittest import TestCase

import numpy as np
import pytest
from PIL import Image

from phomo import Master
from phomo.grid import Grid


class TestMaster(TestCase):
def setUp(self):
# create a test image object
self.master_array = np.hstack(
[
np.ones((64, 72, 3), dtype="uint8") * 255,
np.zeros((64, 56, 3), dtype="uint8"),
]
)
self.master_img = Image.fromarray(self.master_array)
# create test master
self.master = Master.from_image(self.master_img)
self.grid = Grid(self.master, mosaic_shape=(64, 128), tile_shape=(16, 16))

def test_slices(self):
assert len(self.grid.slices) == (self.master_array.shape[0] // 16) * (
self.master_array.shape[1] // 16
)

def test_arrays(self):
assert len(self.grid.arrays) == (self.master_array.shape[0] // 16) * (
self.master_array.shape[1] // 16
)

def test_subdivide(self):
prev_len = len(self.grid.slices)
self.grid.subdivide(0.1)
new_len = len(self.grid.slices)
# 4 tiles get divided into 4 which adds 4*3 tiles
assert new_len == prev_len + 4 * 3

def test_origin(self):
assert self.grid.origin == (0, 0)
grid = Grid(self.master, mosaic_shape=(64, 128), tile_shape=(12, 12))
assert grid.origin == (2, 4)

def test_remove_origin(self):
grid = Grid(self.master, mosaic_shape=(64, 128), tile_shape=(12, 12))
# has an starting offset
assert grid.slices[0][0].start == 2
assert grid.slices[0][0].stop == 14
assert grid.slices[0][1].start == 4
assert grid.slices[0][1].stop == 16
new_slices = grid.remove_origin(grid.slices[0])
# no starting offset
assert new_slices[0].start == 0
assert new_slices[0].stop == 12
assert new_slices[1].start == 0
assert new_slices[1].stop == 12

def test_plot(self):
assert isinstance(self.grid.plot(), Image.Image)
@pytest.fixture
def master_array():
return np.hstack(
[
np.ones((64, 72, 3), dtype="uint8") * 255,
np.zeros((64, 56, 3), dtype="uint8"),
]
)


@pytest.fixture
def master(master_array):
return Master.from_image(Image.fromarray(master_array))


@pytest.fixture
def grid(master):
return Grid(master, mosaic_shape=(64, 128), tile_shape=(16, 16))


def test_slices(grid: Grid, master_array):
assert len(grid.slices) == (master_array.shape[0] // 16) * (
master_array.shape[1] // 16
)


def test_arrays(grid: Grid, master_array):
assert len(grid.arrays) == (master_array.shape[0] // 16) * (
master_array.shape[1] // 16
)


def test_subdivide(grid: Grid, master_array):
prev_len = len(grid.slices)
grid.subdivide(0.1)
new_len = len(grid.slices)
# 4 tiles get divided into 4 which adds 4*3 tiles
assert new_len == prev_len + 4 * 3


def test_origin(grid: Grid, master: Master):
assert grid.origin == (0, 0)
grid = Grid(master, mosaic_shape=(64, 128), tile_shape=(12, 12))
assert grid.origin == (2, 4)


def test_remove_origin(master: Master):
grid = Grid(master, mosaic_shape=(64, 128), tile_shape=(12, 12))
# has an starting offset
assert grid.slices[0][0].start == 2
assert grid.slices[0][0].stop == 14
assert grid.slices[0][1].start == 4
assert grid.slices[0][1].stop == 16
new_slices = grid.remove_origin(grid.slices[0])
# no starting offset
assert new_slices[0].start == 0
assert new_slices[0].stop == 12
assert new_slices[1].start == 0
assert new_slices[1].stop == 12


def test_plot(grid: Grid):
assert isinstance(grid.plot(), Image.Image)
91 changes: 43 additions & 48 deletions tests/unit/test_master.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
from pathlib import Path
from shutil import rmtree
from unittest import TestCase

import numpy as np
import pytest
from PIL import Image

from phomo import Master


class TestMaster(TestCase):
@classmethod
def setUpClass(cls):
cls.test_dir = Path("test_master")
if not cls.test_dir.is_dir():
cls.test_dir.mkdir()
cls.master_path = cls.test_dir / "master.png"
# create a test image object
cls.master_array = np.ones((64, 64, 3), dtype="uint8") * 255
cls.master_img = Image.fromarray(cls.master_array)
# create a test image file
cls.master_img.save(cls.master_path)
# create test master
cls.master = Master.from_file(cls.master_path)

def test_constructors(self):
master_img = Master.from_image(self.master_img)
master_file = Master.from_file(self.master_path)
assert (master_img.array == master_file.array).all()
# make sure it works for single channel modes
master = Master.from_image(self.master_img.convert("L"))
assert master.array.shape[-1] == 3

def test_img(self):
assert isinstance(self.master.img, Image.Image)

def test_pixels(self):
assert self.master.pixels.shape[-1] == 3
assert (
self.master.pixels.shape[0]
== self.master_array.shape[0] * self.master_array.shape[1]
)

# plot methods
def test_palette(self):
self.master.plot()

def test_plot(self):
self.master.plot()

@classmethod
def tearDownClass(cls):
if cls.test_dir.is_dir():
rmtree(cls.test_dir)
@pytest.fixture
def master_array():
return np.ones((64, 64, 3), dtype="uint8") * 255


@pytest.fixture
def master_img(master_array):
return Image.fromarray(master_array)


@pytest.fixture
def master_path(tmp_path, master_img):
path = tmp_path / "master.png"
master_img.save(path)
return path


@pytest.fixture
def master(master_path):
return Master.from_file(master_path)


def test_constructors(master_img: Image.Image, master_path):
master_from_img = Master.from_image(master_img)
master_from_file = Master.from_file(master_path)
assert (master_from_img.array == master_from_file.array).all()
# make sure it works for single channel modes
master = Master.from_image(master_img.convert("L"))
assert master.array.shape[-1] == 3


def test_img(master: Master):
assert isinstance(master.img, Image.Image)


def test_pixels(master: Master, master_array):
assert master.pixels.shape[-1] == 3
assert master.pixels.shape[0] == master_array.shape[0] * master_array.shape[1]


def test_plot(master: Master):
master.plot()
55 changes: 27 additions & 28 deletions tests/unit/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
from unittest import TestCase

import numpy as np

from phomo import metrics


class TestMetrics(TestCase):
# TODO: these tests are not very good...
def test_norm(self):
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.norm(a, b) == 0
def test_norm():
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.norm(a, b) == 0

a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.norm(a, b) > 0


def test_greyscale():
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.greyscale(a, b) == 0

a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.norm(a, b) > 0
a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.greyscale(a, b) > 0

def test_greyscale(self):
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.greyscale(a, b) == 0

a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.greyscale(a, b) > 0
def test_luv_approx():
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.luv_approx(a, b) == 0

def test_luv_approx(self):
a = np.ones((5, 4, 3))
b = np.ones((1, 5, 4, 3))
assert metrics.luv_approx(a, b) == 0
a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.luv_approx(a, b) > 0

a = np.ones((5, 4, 3))
b = np.zeros((1, 5, 4, 3))
assert metrics.luv_approx(a, b) > 0

def test_metrics(self):
# make sure this dictionary gets populated
assert len(metrics.METRICS) > 0
def test_metrics():
# make sure this dictionary gets populated
assert len(metrics.METRICS) > 0
Loading

0 comments on commit 1eacf54

Please sign in to comment.