def test_badinput(): dsT = dataset() try: ds = dataset(historySize=0) except ValueError as ex: assert ( str(ex) == 'Requested history size "0" is too small. It must be at least one.' ) try: ds = dataset(dataset={1: 2}) except ValueError as ex: assert ( str(ex) == "All datasets within a database must use strings as names. This dataset has a database named 1" ) try: ds = dataset(dataset={"eval": {"a": 1}}) except NameError as ex: assert (str(ex) == "eval is a reserved name and cannot be used witin a dataset") ds = dataset(dataset={"db": {"a": 1}}) try: ds.add("db", {"b": 2}) except NameError as ex: assert str(ex) == "db already exists in dataset"
def test_text_widget(): """Test text widget render including render after variable change.""" path = (Path(__file__).parent / "reference/images/text_artist_sting_60x8.png") img = Image.open(path).convert("1") db = {"artist": "Sting"} ds = dataset({"db": db}) w = text(dvalue="f\"Artist {db['artist']}\"", dataset=ds, size=(60, 8), mode="1") renderImage = w.render()[0] bbox = ImageChops.difference(img, renderImage).getbbox() assert not bbox, "Sting image did not match" path = (Path(__file__).parent / "reference/images/text_artist_new_republic_60x8.png") img = Image.open(path).convert("1") db["artist"] = "New Republic" ds.update("db", db) renderImage = w.render()[0] bbox = ImageChops.difference(img, renderImage).getbbox() assert not bbox, "New Republic image did not match"
def test_eval_errors(): ds = dataset() ds.add("db", {"val": "a"}) e = evaluator(ds) # Type Error e.compile("db['val']+1 == 2", name="TypeTest") try: e.eval("TypeTest") except TypeError as ex: # Need variant of error message because 3.6 not the same as >=3.7 assert ( str(ex) == "Type Error: can only concatenate str (not \"int\") to str while trying to evalute db['val']+1 == 2" or str(ex) == "Type Error: must be str, not int while trying to evalute db['val']+1 == 2" ) # KeyError e.compile("db['value'] == 2", name="KeyTest") try: e.eval("KeyTest") except KeyError as ex: assert (str(ex).strip('"') == "KeyError: 'value' while trying to evaluate db['value'] == 2")
def test_get(): ds = dataset() ds.add("db", {"val": "a"}) assert ds.get("db")["val"] == "a", "Get returned unexpected value" assert ds.get("bad", {"val": "a"}) == { "val": "a" }, "Get returned unexpected default value"
def test_len_interface(): ds = dataset() ds.add("db", {"title": "Synchronicity"}) ds.add("sys", {"temp": 90}) assert ( len(ds) == 2 ), f"Dataset should contain two values but instead contained {len(ds)}"
def test_ischanged(): ds = dataset() ds.add("db", {"title": "a"}) e = evaluator(ds) e.compile("changed(db['title'])", name="Test") assert not e.eval("Test"), "Nothing has changed yet" ds.update("db", {"title": "b"}) assert e.eval("Test"), "Title has changed but change was not detected"
def test_dsEval(updates, condition): ds = dataset(historySize=5) for u in updates: ds.update(u[0], u[1]) e = evaluator(ds) e.compile(condition) ans = e.eval(condition) assert ans, f"{condition} failed for {updates}"
def test_validate(): ds = dataset() ds.registerValidation( "db", "test", type=int, onUpdate="_VAL_*10", validate="0 <= _VAL_ < 100", default=0, sample=1, ) ds.update("db", {"test": 3}) assert (ds.db["test"] == 30 ), f"onUpdate Test. Expected value 30. It was {ds.db['test']}." ds.update("db", {"test": "abc"}) assert (ds.db["test"] == 0 ), f"type Test. Expected value 0. It was {ds.db['test']}." ds.update("db", {"test": 101}) assert (ds.db["test"] == 0 ), f"validate Test. Expected value 0. It was {ds.db['test']}." ds.update("db", {"test": 10}) assert ( ds.db["test"] == 100 ), f"Show validate happens before onUpdate Test. Expected value 100. It was {ds.db['test']}." ds.registerValidation("db", "test", onUpdate=["_VAL_*10", "_VAL_//2"]) ds.update("db", {"test": 10}) assert ( ds.db["test"] == 50 ), f"Multi-line onUpdate test. Expected value 50. It was {ds.db['test']}." ds.registerValidation("db", "test", validate=["_VAL_ >= 0", "_VAL_ < 100"]) ds.update("db", {"test": 101}) assert ( ds.db["test"] == 0 ), f"Multi-line validate test. Expected value 0. It was {ds.db['test']}." ds._debug = True try: ds.update("db", {"test": 101}) except ValidationError as ex: assert (str(ex) == "db[test]: 101 failed validation: _VAL_ < 100" ), "Expected Validation Error" ds.registerValidation("db", onUpdate="{k.upper(): v for k, v in _VAL_.items()}") ds.update("db", {"test": 1}) assert (ds.db.get("TEST") == 1 ), "DB level onUpdate test. Expected value was 1"
def makeSetup(): db = {"artist": "Sting", "title": "Desert Rose"} system = {"state": "play", "temp": 40} ds = dataset({"db": db, "sys": system}) artist = text(value="f\"Artist {db['artist']}\"", dataset=ds) title = text(value="f\"Title {db['title']}\"", dataset=ds) alert = text(value="'ALERT -- HOT'") time = text("'12:32p'") cArt = canvas( size=(80, 16), duration=2, activeWhen="sys['state'] == 'play'", dataset=ds, name="Artist", ) cArt.append(artist) cTitle = canvas( size=(80, 16), duration=2, activeWhen="sys['state'] == 'play'", dataset=ds, name="Title", ) cTitle.append(title) cAlert = canvas( size=(80, 16), duration=5, minDuration=2, activeWhen="sys['temp'] >= 100", dataset=ds, name="Alert", ) cAlert.append(alert) cTime = canvas( size=(80, 16), duration=10, activeWhen="sys['state'] == 'stop'", dataset=ds, name="Time", ) cTime.append(time, placement="mm") seq = sequence(dataset=ds) seq.append(cArt) seq.append(cTitle) seq.append(cAlert) seq.append(cTime) return (ds, seq)
def make_dataset(): ds = dataset() ds.add( "db", { "artist": "David Bowie", "album": "Blackstar", "title": "Sue (Or in a Season of Crime)", "elapsed": 2.34, "length": 93.2, "plPos": 2, "plLen": 7, "state": "play", }, ) ds.add("sys", {"temp": 53.5, "time": time.time()}) return ds
def test_request_size(): s = "abc" w = text(f"{s}", size=(10, 8)) img = Image.new(w.image.mode, (10, 8), w._background) drw = ImageDraw.Draw(img) drw.text((0, 0), s, font=w.font, fill=w._foreground) assert ci(w.image, img), f"Image should only contain 'ab'" db = {"value": s} ds = dataset() ds.add("db", db) w = text(dvalue="db['value']", dataset=ds, size=(10, 8)) db["value"] = s[0] ds.update("db", db) w.render() img = Image.new(w.image.mode, (10, 8), w._background) drw = ImageDraw.Draw(img) drw.text((0, 0), s[0], font=w.font, fill=w._foreground) assert ci(w.image, img), f"Image should only contain 'a'"
def makeSetup(): db = { "artist": "Sting", "title": "Desert Rose", "album": "Ten Summoner's Tales", } system = {"state": "play", "temp": 40} ds = dataset({"db": db, "sys": system}) # Widgets artist = text(dvalue="f\"Artist {db['artist']}\"", dataset=ds) title = text(dvalue="f\"Title {db['title']}\"", dataset=ds) album = text(dvalue="f\"Album {db['album']}\"", dataset=ds) alert = text(dvalue="'ALERT -- HOT'") rectAlert = rectangle( (0, 0, alert.size[0] + 3, alert.size[1] + 3), outline="white", fill="black", ) time = text("12:32p") # Canvases cAT = canvas(size=(80, 16), duration=2) cAT.append(artist) cAT.append(title, placement=(0, 8)) cAA = canvas( size=(80, 16), duration=2, activeWhen="len(db['album']) > 0", dataset=ds, ) cAA.append(artist) cAA.append(album, placement=(0, 8)) seqPlay = sequence(size=(80, 16), activeWhen="sys['state'] == 'play'", dataset=ds) seqPlay.append(cAT) seqPlay.append(cAA) cStop = canvas(size=(80, 16), activeWhen="sys['state'] == 'stop'", dataset=ds) cStop.append(time, placement="mm") cAlert = canvas( size=(64, 12), duration=5, minDuration=2, coolingPeriod=10, activeWhen="sys['temp'] >= 100", dataset=ds, ) cAlert.append(alert, placement="mm") cAlert.append(rectAlert, placement="mm") wins = canvas(size=(80, 16), dataset=ds) wins.append(seqPlay) wins.append(cStop) wins.append(cAlert, placement="mm", z=canvas.ZVHIGH) return (ds, wins, seqPlay, cStop, cAlert, cAT, cAA)