Skip to content

Commit

Permalink
Merge pull request #3450 from Textualize/query-code-coverage
Browse files Browse the repository at this point in the history
query.py coverage 100%.
  • Loading branch information
rodrigogiraoserrao authored Oct 3, 2023
2 parents 571ea7c + 00376a6 commit 02fe3bf
Showing 1 changed file with 84 additions and 1 deletion.
85 changes: 84 additions & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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

0 comments on commit 02fe3bf

Please sign in to comment.