Skip to content

Commit

Permalink
opti fetch image sizes dict
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 4, 2024
1 parent 2a2d1a0 commit 328ac9e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
7 changes: 5 additions & 2 deletions geotribu_cli/content/header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ def run(args: argparse.Namespace) -> None:
logger.debug(f"Running {args.command} with {args}")
content_paths: list[Path] = args.content_path

# fetch image sizes dict once before processing
image_sizes = download_image_sizes()

for content_path in content_paths:
logger.info(f"Checking header of {content_path}")
check_path(
Expand All @@ -249,7 +252,7 @@ def run(args: argparse.Namespace) -> None:
# check image max size
if not check_image_size(
yaml_meta["image"],
download_image_sizes(),
image_sizes,
args.max_image_width,
args.max_image_height,
):
Expand All @@ -267,7 +270,7 @@ def run(args: argparse.Namespace) -> None:
# check image max ratio
if not check_image_ratio(
yaml_meta["image"],
download_image_sizes(),
image_sizes,
args.min_image_ratio,
args.max_image_ratio,
):
Expand Down
49 changes: 34 additions & 15 deletions tests/test_yaml_header_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestYamlHeaderCheck(unittest.TestCase):
image_sizes = download_image_sizes()

def setUp(self):
with open("tests/fixtures/content/2012-12-21_article_passe.md") as past_file:
past_content = past_file.read()
Expand Down Expand Up @@ -102,68 +104,85 @@ def test_image_extension_nok(self):
self.assertFalse(check_image_extension("https://mon.image.tiff"))

def test_image_size_ok(self):
sizes = download_image_sizes()
self.assertTrue(
check_image_size(
URL_TEST_VERTICAL_IMAGE, sizes, max_width=800, max_height=800
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=800
)
)
self.assertTrue(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE, sizes, max_width=800, max_height=800
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=800,
max_height=800,
)
)

def test_image_size_nok(self):
sizes = download_image_sizes()
self.assertFalse(
check_image_size(
URL_TEST_VERTICAL_IMAGE, sizes, max_width=380, max_height=800
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=380, max_height=800
)
)
self.assertFalse(
check_image_size(
URL_TEST_VERTICAL_IMAGE, sizes, max_width=800, max_height=799
URL_TEST_VERTICAL_IMAGE, self.image_sizes, max_width=800, max_height=799
)
)
self.assertFalse(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE, sizes, max_width=799, max_height=800
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=799,
max_height=800,
)
)
self.assertFalse(
check_image_size(
URL_TEST_HORIZONTAL_IMAGE, sizes, max_width=800, max_height=532
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
max_width=800,
max_height=532,
)
)

def test_image_ratio_ok(self):
sizes = download_image_sizes()
self.assertTrue(
check_image_ratio(
URL_TEST_VERTICAL_IMAGE, sizes, min_ratio=0.45, max_ratio=0.5
URL_TEST_VERTICAL_IMAGE, self.image_sizes, min_ratio=0.45, max_ratio=0.5
)
)
self.assertTrue(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE, sizes, min_ratio=1.45, max_ratio=1.55
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.45,
max_ratio=1.55,
)
)
self.assertTrue(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE, sizes, min_ratio=1.49, max_ratio=1.51
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.49,
max_ratio=1.51,
)
)

def test_image_ratio_nok(self):
sizes = download_image_sizes()
self.assertFalse(
check_image_ratio(
URL_TEST_VERTICAL_IMAGE, sizes, min_ratio=1.45, max_ratio=1.55
URL_TEST_VERTICAL_IMAGE,
self.image_sizes,
min_ratio=1.45,
max_ratio=1.55,
)
)
self.assertFalse(
check_image_ratio(
URL_TEST_HORIZONTAL_IMAGE, sizes, min_ratio=1.2, max_ratio=1.3
URL_TEST_HORIZONTAL_IMAGE,
self.image_sizes,
min_ratio=1.2,
max_ratio=1.3,
)
)

0 comments on commit 328ac9e

Please sign in to comment.