Skip to content

Commit 61fc8ec

Browse files
committed
refs #278 Unit Test example for ProgressAnimate
Finished up ProgressAnimate testing file along with improvments to bulletproof ProgressAnimate
1 parent 7a97927 commit 61fc8ec

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

pysimplesql/pysimplesql.py

+21
Original file line numberDiff line numberDiff line change
@@ -3905,6 +3905,24 @@ def __init__(self, title: str, config: dict = None):
39053905
f"config may only contain keys: {default_config.keys()}"
39063906
)
39073907

3908+
for k in ["bar", "red", "green", "blue"]:
3909+
if k in config:
3910+
if not all(isinstance(v, (int, float)) for v in config[k]):
3911+
raise ValueError(f"values for {k} component must all be numeric")
3912+
required_keys = {"value_start", "value_range", "period", "offset"}
3913+
if not required_keys.issubset(set(config.keys())):
3914+
raise ValueError(f"{k} must contain all of {required_keys}")
3915+
3916+
if "phrases" in config:
3917+
if type(config["phrases"]) is not list:
3918+
raise ValueError("phrases must be a list")
3919+
if not all(isinstance(v, str) for v in config["phrases"]):
3920+
raise ValueError("phrases must be a list of strings")
3921+
3922+
if "phrase_delay" in config:
3923+
if not all(isinstance(v, (int, float)) for v in config["phrase_delay"]):
3924+
raise ValueError("phrase_delay must be numeric")
3925+
39083926
self.config = {**default_config, **config}
39093927

39103928
self.title = title
@@ -3930,6 +3948,9 @@ def run(self, fn: callable, *args, **kwargs):
39303948
Runs the function in a separate co-routine, while animating the progress bar in
39313949
another.
39323950
"""
3951+
if not callable(fn):
3952+
raise ValueError("fn must be a callable")
3953+
39333954
return asyncio.run(self._dispatch(fn, *args, **kwargs))
39343955

39353956
def close(self):

tests/progressanimate_test.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from time import sleep
2-
3-
import pytest
4-
51
import pysimplesql as ss
2+
import pytest
3+
from time import sleep
64

75

86
# Simulated process
@@ -42,8 +40,29 @@ def test_config():
4240
ss.ProgressAnimate("Test", config=True)
4341
# What if the user does supply a dict, but it doesn't have the right keys?
4442
with pytest.raises(NotImplementedError):
45-
# Purposely fail by
43+
# Purposely fail by using unsupported key
4644
config = {
4745
"sound_effect": "beep",
4846
}
4947
ss.ProgressAnimate("Test", config=config)
48+
# What if supplies a correct key, but does not have required subdict keys?
49+
with pytest.raises(ValueError):
50+
# purposely omit the offset
51+
config = {
52+
"red": {"value_start": 0, "value_range": 100, "period": 2},
53+
}
54+
ss.ProgressAnimate("Test", config=config)
55+
# What if the user does supply a dict, but it doesn't have the right values?
56+
with pytest.raises(ValueError):
57+
# Purposely fail by using unsupported value
58+
config = {
59+
"red": {"value_start": True, "value_range": "A", "period": 2, "offset": 0},
60+
"phrases": [True, 0, 3.14, "This one is good though"],
61+
}
62+
ss.ProgressAnimate("Test", config=config)
63+
64+
65+
def test_run():
66+
with pytest.raises(ValueError):
67+
pa = ss.ProgressAnimate("Test")
68+
pa.run(True)

0 commit comments

Comments
 (0)