Skip to content

Commit

Permalink
query.py coverage 100%.
Browse files Browse the repository at this point in the history
Increase coverage on 'query.py'.
  • Loading branch information
rodrigogiraoserrao committed Oct 3, 2023
1 parent f4f83ee commit 00376a6
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 00376a6

Please sign in to comment.