Skip to content

Commit

Permalink
Extend tests & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Acly committed Dec 7, 2023
1 parent 5aeb578 commit 8c8f8c0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.coverage
.debug
.env
.logs
Expand Down
11 changes: 0 additions & 11 deletions ai_diffusion/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ async def _handle_messages(self):
self.error = ""
else:
self.message_received.emit(msg)
# model = self._find_model(msg.job_id)
# if model is not None:
# model.handle_message(msg)
except asyncio.CancelledError:
break
except Exception as e:
Expand All @@ -131,14 +128,6 @@ async def _handle_messages(self):
except asyncio.CancelledError:
pass # shutdown

# def _report_error(self, message: str):
# self.error = message
# self.error_reported.emit(message)

# def _clear_error(self):
# self.error = None
# self.error_reported.emit("")


def apply_performance_preset(settings: Settings, device: DeviceInfo):
if settings.performance_preset is PerformancePreset.auto:
Expand Down
5 changes: 0 additions & 5 deletions ai_diffusion/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ def create_mask_from_layer(self, padding: float, is_inpaint: bool):
data: QByteArray = layer.projectionPixelData(*mask_bounds)
assert data is not None and data.size() >= mask_bounds.extent.pixel_count
mask = Mask(mask_bounds, data)

log.debug(
f"Using experimental selection mask {self.active_layer.name()}, mask bounds:"
f" {mask.bounds}, image bounds: {image_bounds}"
)
return mask, image_bounds, None

def get_image(
Expand Down
4 changes: 2 additions & 2 deletions ai_diffusion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import logging.handlers
import zipfile
from typing import Optional, TypeVar
from typing import Optional, Sequence, TypeVar

T = TypeVar("T")

Expand Down Expand Up @@ -57,7 +57,7 @@ def encode_json(obj):
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")


def get_path_dict(paths: list[str | Path]) -> dict:
def get_path_dict(paths: Sequence[str | Path]) -> dict:
"""Builds a tree like structure out of a list of paths"""

def _recurse(dic: dict, chain: tuple[str, ...] | list[str]):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ def test_clamp_bounds(input, expected):
assert result == expected


@pytest.mark.parametrize(
"input,bounds,expected",
[
(Bounds(0, 0, 1, 2), Bounds(0, 0, 2, 2), Bounds(0, 0, 1, 2)),
(Bounds(0, 0, 1, 2), Bounds(0, 0, 1, 1), Bounds(0, 0, 1, 1)),
(Bounds(2, 4, 1, 2), Bounds(1, 4, 2, 2), Bounds(2, 4, 1, 2)),
(Bounds(2, 4, 7, 9), Bounds(1, 4, 2, 2), Bounds(2, 4, 1, 2)),
(Bounds(-1, 5, 3, 3), Bounds(0, 6, 5, 2), Bounds(0, 6, 2, 2)),
],
)
def test_restrict_bounds(input: Bounds, bounds: Bounds, expected: Bounds):
result = Bounds.restrict(input, bounds)
assert result == expected


@pytest.mark.parametrize(
"input,target,expected",
[
Expand Down
40 changes: 39 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
import pytest
from ai_diffusion.util import batched
from tempfile import TemporaryDirectory
from pathlib import Path
from ai_diffusion.util import batched, get_path_dict, ZipFile


def test_batched():
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = 3
expected_output = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
assert list(batched(iterable, n)) == expected_output


def test_path_dict():
paths = [
"f1.txt",
"f2.txxt",
"d1/f1.txt",
"d2/f1.txt",
"d3/d4/d5/f1.txt",
"d1/f2.txt",
"w1\\f1.txt",
"w1\\noext",
"w1\\w2\\.noext",
]
expected = {
"f1.txt": None,
"f2.txxt": None,
"d1": {"f1.txt": None, "f2.txt": None},
"d2": {"f1.txt": None},
"d3": {"d4": {"d5": {"f1.txt": None}}},
"w1": {"f1.txt": None, "noext": None, "w2": {".noext": None}},
}
assert get_path_dict(paths) == expected


def test_long_path_zip_file():
with TemporaryDirectory() as dir:
file = Path(dir) / "test.zip"
with ZipFile(file, "w") as zip:
zip.writestr("test.txt", "test")
zip.writestr("test2.txt", "test2")
long_path = Path(dir) / ("l" + "o" * 150 + "ng") / ("l" + "o" * 150 + "ng")
with ZipFile(file, "r") as zip:
zip.extractall(long_path)
assert (long_path / "test.txt").read_text() == "test"
assert (long_path / "test2.txt").read_text() == "test2"

0 comments on commit 8c8f8c0

Please sign in to comment.