From 5882146e72e13e8069ca8d14d0a07408b7f37c76 Mon Sep 17 00:00:00 2001 From: jstier Date: Thu, 12 Oct 2023 08:44:25 +0200 Subject: [PATCH] test(validators) add unit test --- sketch_map_tool/validators.py | 11 ++++++----- tests/unit/test_validators.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/sketch_map_tool/validators.py b/sketch_map_tool/validators.py index 49463074..b4e3c4e6 100644 --- a/sketch_map_tool/validators.py +++ b/sketch_map_tool/validators.py @@ -21,6 +21,7 @@ def validate_type(type_: REQUEST_TYPES): def validate_uploaded_sketchmap(file): + """Validation function for uploaded files.""" max_single_file_size = int(get_config_value("max_single_file_size")) max_pixel_per_image = int(get_config_value("max_pixel_per_image")) @@ -50,19 +51,19 @@ def validate_uuid(uuid: str): raise ValueError("The provided URL does not contain a valid UUID") from error -def validate_literature_reference(literatur_reference: LiteratureReference): - """Validate literatur reference to not include empty strings.""" - if literatur_reference.citation == "": +def validate_literature_reference(literature_reference: LiteratureReference): + """Validate literature reference to not include empty strings.""" + if literature_reference.citation == "": raise ValueError( "Literature reference JSON fields " "should not contain empty strings as values." ) - if literatur_reference.img_src == "": + if literature_reference.img_src == "": raise ValueError( "Literature reference JSON fields should " "not contain empty strings as values." ) - if literatur_reference.url == "": + if literature_reference.url == "": raise ValueError( "Literature reference JSON fields should " "not contain empty strings as values." diff --git a/tests/unit/test_validators.py b/tests/unit/test_validators.py index 5dd99374..b8a618ce 100644 --- a/tests/unit/test_validators.py +++ b/tests/unit/test_validators.py @@ -1,6 +1,11 @@ import pytest -from sketch_map_tool.validators import validate_type, validate_uuid +from sketch_map_tool.exceptions import UploadLimitsExceededError +from sketch_map_tool.validators import ( + validate_type, + validate_uploaded_sketchmap, + validate_uuid, +) @pytest.mark.parametrize( @@ -17,6 +22,18 @@ def test_validate_type_invalid(type_): validate_type(type_) +def test_validate_file_by_pixel_count(files, monkeypatch): + with pytest.raises(UploadLimitsExceededError): + monkeypatch.setenv("MAX-PIXEL-PER-IMAGE", "10") + validate_uploaded_sketchmap(files[0]) + + +def test_validate_file_by_size(files, monkeypatch): + with pytest.raises(UploadLimitsExceededError): + monkeypatch.setenv("MAX-SINGLE-FILE-SIZE", "10") + validate_uploaded_sketchmap(files[1]) + + def test_validate_uui(uuid): validate_uuid(uuid)