Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query.py coverage 100%. #3450

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading