File: test_notifications.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (78 lines) | stat: -rw-r--r-- 2,364 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import annotations

from time import sleep

from textual.notifications import Notification, Notifications


def test_empty_to_start_with() -> None:
    """We should have no notifications if we've not raised any."""
    assert len(Notifications()) == 0


def test_many_notifications() -> None:
    """Adding lots of long-timeout notifications should result in them being in the list."""
    tester = Notifications()
    for _ in range(100):
        tester.add(Notification("test", timeout=60))
    assert len(tester) == 100


def test_timeout() -> None:
    """Notifications should timeout from the list."""
    tester = Notifications()
    for n in range(100):
        tester.add(Notification("test", timeout=(0.5 if bool(n % 2) else 60)))
    assert len(tester) == 100
    sleep(0.6)
    assert len(tester) == 50


def test_in() -> None:
    """It should be possible to test if a notification is in a collection."""
    tester = Notifications()
    within = Notification("within", timeout=120)
    outwith = Notification("outwith", timeout=120)
    tester.add(within)
    assert within in tester
    assert outwith not in tester


def test_remove_notification() -> None:
    """It should be possible to remove a notification."""
    tester = Notifications()
    first = Notification("first", timeout=120)
    second = Notification("second", timeout=120)
    third = Notification("third", timeout=120)
    tester.add(first)
    tester.add(second)
    tester.add(third)
    assert list(tester) == [first, second, third]
    del tester[second]
    assert list(tester) == [first, third]
    del tester[first]
    assert list(tester) == [third]
    del tester[third]
    assert list(tester) == []


def test_remove_notification_multiple_times() -> None:
    """It should be possible to remove the same notification more than once without an error."""
    tester = Notifications()
    alert = Notification("delete me")
    tester.add(alert)
    assert list(tester) == [alert]
    del tester[alert]
    assert list(tester) == []
    del tester[alert]
    assert list(tester) == []


def test_clear() -> None:
    """It should be possible to clear all notifications."""
    tester = Notifications()
    for _ in range(100):
        tester.add(Notification("test", timeout=120))
    assert len(tester) == 100
    tester.clear()
    assert len(tester) == 0