diff --git a/pysimplesql/pysimplesql.py b/pysimplesql/pysimplesql.py index 73fe814a..25281034 100644 --- a/pysimplesql/pysimplesql.py +++ b/pysimplesql/pysimplesql.py @@ -3905,6 +3905,24 @@ def __init__(self, title: str, config: dict = None): f"config may only contain keys: {default_config.keys()}" ) + for k in ["bar", "red", "green", "blue"]: + if k in config and not all(isinstance(v, (int, float)) for v in config[k]): + raise ValueError(f"values for {k} component must all be numeric") + required_keys = {"value_start", "value_range", "period", "offset"} + if k in config and not required_keys.issubset(set(config.keys())): + raise ValueError(f"{k} must contain all of {required_keys}") + + if "phrases" in config: + if type(config["phrases"]) is not list: + raise ValueError("phrases must be a list") + if not all(isinstance(v, str) for v in config["phrases"]): + raise ValueError("phrases must be a list of strings") + + if "phrase_delay" in config and not all( + isinstance(v, (int, float)) for v in config["phrase_delay"] + ): # noqa SIM102 + raise ValueError("phrase_delay must be numeric") + self.config = {**default_config, **config} self.title = title @@ -3930,6 +3948,9 @@ def run(self, fn: callable, *args, **kwargs): Runs the function in a separate co-routine, while animating the progress bar in another. """ + if not callable(fn): + raise ValueError("fn must be a callable") + return asyncio.run(self._dispatch(fn, *args, **kwargs)) def close(self): diff --git a/tests/progressanimate_test.py b/tests/progressanimate_test.py index 767fa543..fc93e9e1 100644 --- a/tests/progressanimate_test.py +++ b/tests/progressanimate_test.py @@ -42,8 +42,29 @@ def test_config(): ss.ProgressAnimate("Test", config=True) # What if the user does supply a dict, but it doesn't have the right keys? with pytest.raises(NotImplementedError): - # Purposely fail by + # Purposely fail by using unsupported key config = { "sound_effect": "beep", } ss.ProgressAnimate("Test", config=config) + # What if supplies a correct key, but does not have required subdict keys? + with pytest.raises(ValueError): + # purposely omit the offset + config = { + "red": {"value_start": 0, "value_range": 100, "period": 2}, + } + ss.ProgressAnimate("Test", config=config) + # What if the user does supply a dict, but it doesn't have the right values? + with pytest.raises(ValueError): + # Purposely fail by using unsupported value + config = { + "red": {"value_start": True, "value_range": "A", "period": 2, "offset": 0}, + "phrases": [True, 0, 3.14, "This one is good though"], + } + ss.ProgressAnimate("Test", config=config) + + +def test_run(): + with pytest.raises(ValueError): + pa = ss.ProgressAnimate("Test") + pa.run(True)