mirror of
https://github.com/thomasasfk/sd-webui-aspect-ratio-helper.git
synced 2026-04-30 03:01:34 +00:00
Add parametrized unit test coverage
This commit is contained in:
19
.github/workflows/pytest.yml
vendored
Normal file
19
.github/workflows/pytest.yml
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
name: Run pytest
|
||||
|
||||
on: [pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install pytest
|
||||
|
||||
- name: Run pytest
|
||||
run: pytest
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
.idea
|
||||
.pytest_cache
|
||||
@@ -7,8 +7,9 @@ from modules import scripts
|
||||
from modules import shared
|
||||
from modules.shared import opts
|
||||
|
||||
_MIN_DIMENSION = 64
|
||||
_MAX_DIMENSION = 2048
|
||||
from util import _scale_dimensions_to_max_dimension
|
||||
from util import _scale_by_percentage
|
||||
|
||||
_EXTENSION_NAME = 'Aspect Ratio Helper'
|
||||
|
||||
|
||||
@@ -56,35 +57,6 @@ def on_ui_settings():
|
||||
)
|
||||
|
||||
|
||||
def _scale_by_percentage(width, height, pct):
|
||||
aspect_ratio = float(width) / float(height)
|
||||
step = (pct - 1.0)
|
||||
new_width = max(int(round(width * (1.0 + step))), 1)
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_width > _MAX_DIMENSION:
|
||||
new_width = _MAX_DIMENSION
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_height > _MAX_DIMENSION:
|
||||
new_height = _MAX_DIMENSION
|
||||
new_width = max(int(round(new_height * aspect_ratio)), 1)
|
||||
if new_width < _MIN_DIMENSION:
|
||||
new_width = _MIN_DIMENSION
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_height < _MIN_DIMENSION:
|
||||
new_height = _MIN_DIMENSION
|
||||
new_width = max(int(round(new_height * aspect_ratio)), 1)
|
||||
return new_width, new_height
|
||||
|
||||
|
||||
def _scale_dimensions_to_max_dimension(width, height, max_dim):
|
||||
if max_dim == max(width, height):
|
||||
return width, height
|
||||
aspect_ratio = float(width) / float(height)
|
||||
if width > height:
|
||||
return max_dim, max(int(round(max_dim / aspect_ratio)), 1)
|
||||
return max(int(round(max_dim * aspect_ratio)), 1), max_dim
|
||||
|
||||
|
||||
class AspectRatioStepScript(scripts.Script):
|
||||
|
||||
def title(self):
|
||||
33
scripts/util.py
Normal file
33
scripts/util.py
Normal file
@@ -0,0 +1,33 @@
|
||||
_MIN_DIMENSION = 64
|
||||
_MAX_DIMENSION = 2048
|
||||
|
||||
|
||||
def _scale_by_percentage(width, height, pct):
|
||||
aspect_ratio = float(width) / float(height)
|
||||
step = (pct - 1.0)
|
||||
new_width = max(int(round(width * (1.0 + step))), 1)
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_width > _MAX_DIMENSION:
|
||||
new_width = _MAX_DIMENSION
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_height > _MAX_DIMENSION:
|
||||
new_height = _MAX_DIMENSION
|
||||
new_width = max(int(round(new_height * aspect_ratio)), 1)
|
||||
if new_width < _MIN_DIMENSION:
|
||||
new_width = _MIN_DIMENSION
|
||||
new_height = max(int(round(new_width / aspect_ratio)), 1)
|
||||
if new_height < _MIN_DIMENSION:
|
||||
new_height = _MIN_DIMENSION
|
||||
new_width = max(int(round(new_height * aspect_ratio)), 1)
|
||||
return new_width, new_height
|
||||
|
||||
|
||||
def _scale_dimensions_to_max_dimension(width, height, max_dim):
|
||||
if not _MIN_DIMENSION < max_dim < _MAX_DIMENSION:
|
||||
raise ValueError('Invalid dimension provided.')
|
||||
if max_dim == max(width, height):
|
||||
return width, height
|
||||
aspect_ratio = float(width) / float(height)
|
||||
if width > height:
|
||||
return max_dim, max(int(round(max_dim / aspect_ratio)), 1)
|
||||
return max(int(round(max_dim * aspect_ratio)), 1), max_dim
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
86
tests/util_test.py
Normal file
86
tests/util_test.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import pytest
|
||||
|
||||
from scripts.util import _MIN_DIMENSION
|
||||
from scripts.util import _MAX_DIMENSION
|
||||
from scripts.util import _scale_by_percentage
|
||||
from scripts.util import _scale_dimensions_to_max_dimension
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"width, height, pct, expected",
|
||||
[
|
||||
pytest.param(200, 400, 0.5, (100, 200), id="50_percent_scale_down"),
|
||||
pytest.param(100, 200, 2.0, (200, 400), id="200_percent_scale_up"),
|
||||
pytest.param(100, 200, 1.1, (110, 220), id="10_percent_scale_up"),
|
||||
pytest.param(100, 200, 0.9, (90, 180), id="10_percent_scale_down"),
|
||||
pytest.param(100, 200, 0.0, (64, 128), id="scale_full_down"),
|
||||
pytest.param(
|
||||
_MIN_DIMENSION - 1,
|
||||
_MIN_DIMENSION - 1,
|
||||
0.5,
|
||||
(_MIN_DIMENSION, _MIN_DIMENSION),
|
||||
id="scale_below_min_dimension",
|
||||
),
|
||||
pytest.param(
|
||||
_MAX_DIMENSION + 1,
|
||||
_MAX_DIMENSION + 1,
|
||||
2.0,
|
||||
(_MAX_DIMENSION, _MAX_DIMENSION),
|
||||
id="scale_above_max_dimension",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_scale_by_percentage(
|
||||
width, height, pct, expected
|
||||
):
|
||||
assert _scale_by_percentage(
|
||||
width, height, pct
|
||||
) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"width, height, max_dim, expected",
|
||||
[
|
||||
pytest.param(
|
||||
100, 200, 400, (200, 400), id="scale_up_to_max_dimension_horizontally"
|
||||
),
|
||||
pytest.param(
|
||||
200, 100, 400, (400, 200), id="scale_up_to_max_dimension_vertically"
|
||||
),
|
||||
pytest.param(
|
||||
400, 64, 400, (400, 64), id="no_scale_up_needed_with_max_dimension_width"
|
||||
),
|
||||
pytest.param(
|
||||
64, 400, 400, (64, 400), id="no_scale_up_needed_with_max_dimension_height"
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_scale_dimensions_to_max_dimension(
|
||||
width, height, max_dim, expected
|
||||
):
|
||||
assert _scale_dimensions_to_max_dimension(
|
||||
width, height, max_dim
|
||||
) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"width, height, max_dim",
|
||||
[
|
||||
pytest.param(
|
||||
64,
|
||||
64,
|
||||
_MIN_DIMENSION - 1,
|
||||
id="scale_below_min_dimension",
|
||||
),
|
||||
pytest.param(
|
||||
64,
|
||||
64,
|
||||
_MAX_DIMENSION + 1,
|
||||
id="scale_above_max_dimension",
|
||||
),
|
||||
|
||||
],
|
||||
)
|
||||
def test_error_thrown_given_dim_outside_boundaries(width, height, max_dim):
|
||||
with pytest.raises(ValueError):
|
||||
_scale_dimensions_to_max_dimension(width, height, max_dim)
|
||||
Reference in New Issue
Block a user