fixed statefulness issue with os.environ in tests

This commit is contained in:
Matthias Bisping 2022-09-12 13:28:44 +02:00
parent ad9c5657fe
commit c1b9227035
2 changed files with 37 additions and 29 deletions

View File

@ -20,34 +20,42 @@ class ResponseTransformer(Transformer):
return build_image_info(data)
def get_class_specific_min_image_to_page_quotient(label):
return get_class_specific_quotient("REL_IMAGE_SIZE", label, "min", CONFIG.filters.image_to_page_quotient.min)
def get_class_specific_min_image_to_page_quotient(label, table=None):
return get_class_specific_value(
"REL_IMAGE_SIZE", label, "min", CONFIG.filters.image_to_page_quotient.min, table=table
)
def get_class_specific_max_image_to_page_quotient(label):
return get_class_specific_quotient("REL_IMAGE_SIZE", label, "max", CONFIG.filters.image_to_page_quotient.max)
def get_class_specific_max_image_to_page_quotient(label, table=None):
return get_class_specific_value(
"REL_IMAGE_SIZE", label, "max", CONFIG.filters.image_to_page_quotient.max, table=table
)
def get_class_specific_min_image_width_to_height_quotient(label):
return get_class_specific_quotient("IMAGE_FORMAT", label, "min", CONFIG.filters.image_width_to_height_quotient.min)
def get_class_specific_min_image_width_to_height_quotient(label, table=None):
return get_class_specific_value(
"IMAGE_FORMAT", label, "min", CONFIG.filters.image_width_to_height_quotient.min, table=table
)
def get_class_specific_max_image_width_to_height_quotient(label):
return get_class_specific_quotient("IMAGE_FORMAT", label, "max", CONFIG.filters.image_width_to_height_quotient.max)
def get_class_specific_max_image_width_to_height_quotient(label, table=None):
return get_class_specific_value(
"IMAGE_FORMAT", label, "max", CONFIG.filters.image_width_to_height_quotient.max, table=table
)
def get_class_specific_min_classification_confidence(label):
return get_class_specific_quotient("CONFIDENCE", label, "min", CONFIG.filters.min_confidence)
def get_class_specific_min_classification_confidence(label, table=None):
return get_class_specific_value("CONFIDENCE", label, "min", CONFIG.filters.min_confidence, table=table)
def get_class_specific_quotient(prefix, label, bound, fallback_value):
def get_class_specific_value(prefix, label, bound, fallback_value, table=None):
def fallback():
logger.warning(f"Failed to resolve {bound} {prefix.lower().replace('_', '-')} value for class '{label}'.")
return fallback_value
assert bound in ["min", "max"]
threshold_map = parse_env_var(prefix) or {}
threshold_map = parse_env_var(prefix, table=table) or {}
return threshold_map.get(label, {}).get(bound) or fallback()
@ -121,15 +129,16 @@ def build_image_info(data: dict) -> dict:
@lru_cache(maxsize=None)
def parse_env_var(prefix):
head, tail = juxt(first, compose(list, rest))(filter(prefix, os.environ))
def parse_env_var(prefix, table=None):
table = table or os.environ
head, tail = juxt(first, compose(list, rest))(filter(prefix, table))
if not head:
logger.warning(f"Found no environment variable with prefix '{prefix}'.")
elif tail:
logger.warning(f"Found multiple candidates for environment variable with prefix '{prefix}'.")
else:
try:
return parse_env_var_value(os.environ[head])
return parse_env_var_value(table[head])
except ParsingError as err:
logger.warning(err)

View File

@ -1,7 +1,7 @@
import json
import os
import pytest
from frozendict import frozendict
from image_prediction.transformer.transformers.response import (
get_class_specific_min_image_to_page_quotient,
@ -19,19 +19,18 @@ def label():
@pytest.fixture
def page_quotient_threshold_map(label):
# TODO: suboptimal, as actual environment is used
os.environ["REL_IMAGE_SIZE_MAP"] = json.dumps({label: {"min": 0.1, "max": 0.2}})
os.environ["IMAGE_FORMAT_MAP"] = json.dumps({label: {"min": 0.5, "max": 0.4}})
os.environ["CONFIDENCE"] = json.dumps({label: {"min": 0.8}})
yield
for env_var in ("REL_IMAGE_SIZE_MAP", "IMAGE_FORMAT_MAP", "CONFIDENCE"):
os.environ.pop(env_var)
return frozendict(
{
"REL_IMAGE_SIZE_MAP": json.dumps({label: {"min": 0.1, "max": 0.2}}),
"IMAGE_FORMAT_MAP": json.dumps({label: {"min": 0.5, "max": 0.4}}),
"CONFIDENCE": json.dumps({label: {"min": 0.8}}),
}
)
# FIXME: Runs correctly in isolation, but fails when other tests are run before
def test_read_environment_vars_for_thresholds(page_quotient_threshold_map, label):
assert get_class_specific_min_image_to_page_quotient(label) == 0.1
assert get_class_specific_max_image_to_page_quotient(label) == 0.2
assert get_class_specific_min_image_width_to_height_quotient(label) == 0.5
assert get_class_specific_max_image_width_to_height_quotient(label) == 0.4
assert get_class_specific_min_classification_confidence(label) == 0.8
assert get_class_specific_min_image_to_page_quotient(label, table=page_quotient_threshold_map) == 0.1
assert get_class_specific_max_image_to_page_quotient(label, table=page_quotient_threshold_map) == 0.2
assert get_class_specific_min_image_width_to_height_quotient(label, table=page_quotient_threshold_map) == 0.5
assert get_class_specific_max_image_width_to_height_quotient(label, table=page_quotient_threshold_map) == 0.4
assert get_class_specific_min_classification_confidence(label, table=page_quotient_threshold_map) == 0.8