From 00376a62d3535f6fd636da023c286eb8a2e21430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:02:50 +0100 Subject: [PATCH] query.py coverage 100%. Increase coverage on 'query.py'. --- tests/test_query.py | 85 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/tests/test_query.py b/tests/test_query.py index 60dced9716..d09599cdf2 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1,9 +1,17 @@ import pytest from textual.app import App, ComposeResult +from textual.color import Color from textual.containers import Container -from textual.css.query import InvalidQueryFormat, NoMatches, TooManyMatches, WrongType +from textual.css.query import ( + DeclarationError, + InvalidQueryFormat, + NoMatches, + TooManyMatches, + WrongType, +) from textual.widget import Widget +from textual.widgets import Label def test_query(): @@ -230,3 +238,78 @@ def compose(self) -> ComposeResult: results = list(query.results()) assert len(results) == 5 assert not any(node.id == "root-container" for node in results) + + +async def test_query_set_styles_invalid_css_raises_error(): + app = App() + async with app.run_test(): + with pytest.raises(DeclarationError): + app.query(Widget).set_styles(css="random_rule: 1fr;") + + +async def test_query_set_styles_kwds(): + class LabelApp(App): + def compose(self): + yield Label("Some text") + + app = LabelApp() + async with app.run_test(): + # Sanity check. + assert app.query_one(Label).styles.color != Color(255, 0, 0) + app.query(Label).set_styles(color="red") + assert app.query_one(Label).styles.color == Color(255, 0, 0) + + +async def test_query_set_styles_css_and_kwds(): + class LabelApp(App): + def compose(self): + yield Label("Some text") + + app = LabelApp() + async with app.run_test(): + # Sanity checks. + lbl = app.query_one(Label) + assert lbl.styles.color != Color(255, 0, 0) + assert lbl.styles.background != Color(255, 0, 0) + + app.query(Label).set_styles(css="background: red;", color="red") + assert app.query_one(Label).styles.color == Color(255, 0, 0) + assert app.query_one(Label).styles.background == Color(255, 0, 0) + + +async def test_query_set_styles_css(): + class LabelApp(App): + def compose(self): + yield Label("Some text") + + app = LabelApp() + async with app.run_test(): + # Sanity checks. + lbl = app.query_one(Label) + assert lbl.styles.color != Color(255, 0, 0) + assert lbl.styles.background != Color(255, 0, 0) + + app.query(Label).set_styles(css="background: red; color: red;") + assert app.query_one(Label).styles.color == Color(255, 0, 0) + assert app.query_one(Label).styles.background == Color(255, 0, 0) + + +@pytest.mark.parametrize( + "args", [(False, False), (True, False), (True, True), (False, True)] +) +async def test_query_refresh(args): + refreshes = [] + + class MyWidget(Widget): + def refresh(self, *, repaint=None, layout=None): + super().refresh(repaint=repaint, layout=layout) + refreshes.append((repaint, layout)) + + class MyApp(App): + def compose(self): + yield MyWidget() + + app = MyApp() + async with app.run_test() as pilot: + app.query(MyWidget).refresh(repaint=args[0], layout=args[1]) + assert refreshes[-1] == args