Repository navigation
Expand file tree
/
Copy pathtest_ui.py
More file actions
194 lines (148 loc) · 5.48 KB
/
Copy pathtest_ui.py
File metadata and controls
194 lines (148 loc) · 5.48 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from __future__ import annotations
from datetime import datetime
from typing import Callable
from unittest import mock
import py
import pytest
import pytz
from freezegun import freeze_time
from urwid import ExitMainLoop
from todoman.formatters import DefaultFormatter
from todoman.formatters import Formatter
from todoman.interactive import TodoEditor
from todoman.model import Database
def test_todo_editor_priority(
default_database: Database,
todo_factory: Callable,
default_formatter: DefaultFormatter,
) -> None:
todo = todo_factory(priority=1)
lists = list(default_database.lists())
editor = TodoEditor(todo, lists, default_formatter)
assert editor._priority.label == "high"
editor._priority.keypress((10,), "right")
with pytest.raises(ExitMainLoop): # Look at editor._msg_text if this fails
editor._keypress("ctrl s")
assert todo.priority == 0
def test_todo_editor_list(
default_database: Database,
todo_factory: Callable,
default_formatter: Formatter,
tmpdir: py.path.local,
) -> None:
tmpdir.mkdir("another_list")
default_database.paths = [
str(tmpdir.join("default")),
str(tmpdir.join("another_list")),
]
default_database.update_cache()
todo = todo_factory()
lists = list(default_database.lists())
editor = TodoEditor(todo, lists, default_formatter)
default_list = next(filter(lambda x: x.label == "default", editor.list_selector))
another_list = next(
filter(lambda x: x.label == "another_list", editor.list_selector)
)
assert editor.current_list == todo.list
assert default_list.label == todo.list.name
another_list.set_state(True)
editor._save_inner()
assert editor.current_list == todo.list
assert another_list.label == todo.list.name
def test_todo_editor_summary(
default_database: Database,
todo_factory: Callable,
default_formatter: Formatter,
) -> None:
todo = todo_factory()
lists = list(default_database.lists())
editor = TodoEditor(todo, lists, default_formatter)
assert editor._summary.edit_text == "YARR!"
editor._summary.edit_text = "Goodbye"
with pytest.raises(ExitMainLoop): # Look at editor._msg_text if this fails
editor._keypress("ctrl s")
assert todo.summary == "Goodbye"
@freeze_time("2017-03-04 14:00:00", tz_offset=4)
def test_todo_editor_due(
default_database: Database,
todo_factory: Callable,
default_formatter: DefaultFormatter,
) -> None:
tz = pytz.timezone("CET")
todo = todo_factory(due=datetime(2017, 3, 4, 14))
lists = list(default_database.lists())
default_formatter.tz = tz
editor = TodoEditor(todo, lists, default_formatter)
assert editor._due.edit_text == "2017-03-04 14:00"
editor._due.edit_text = "2017-03-10 12:00"
with pytest.raises(ExitMainLoop): # Look at editor._msg_text if this fails
editor._keypress("ctrl s")
assert todo.due == datetime(2017, 3, 10, 12, tzinfo=tz)
def test_toggle_help(
default_database: Database,
default_formatter: Formatter,
todo_factory: Callable,
) -> None:
todo = todo_factory()
lists = list(default_database.lists())
editor = TodoEditor(todo, lists, default_formatter)
editor._loop = mock.MagicMock() # type: ignore[assignment]
assert editor._help_text not in editor.left_column.body.contents
editor._keypress("f1")
# Help text is made visible
assert editor._help_text in editor.left_column.body.contents
assert editor._loop
# Called event_loop.draw_screen
assert editor._loop.draw_screen.call_count == 1
assert editor._loop.draw_screen.call_args == mock.call()
editor._keypress("f1")
# Help text is made visible
assert editor._help_text not in editor.left_column.body.contents
# Called event_loop.draw_screen
assert editor._loop.draw_screen.call_count == 2
assert editor._loop.draw_screen.call_args == mock.call()
def test_show_save_errors(
default_database: Database,
default_formatter: Formatter,
todo_factory: Callable,
) -> None:
todo = todo_factory()
lists = list(default_database.lists())
editor = TodoEditor(todo, lists, default_formatter)
# editor._loop = mock.MagicMock()
editor._due.set_edit_text("not a date")
editor._keypress("ctrl s")
assert (
editor.left_column.body.contents[2].get_text()[0]
== "Time description not recognized: not a date"
)
@pytest.mark.parametrize("completed", [True, False])
@pytest.mark.parametrize("check", [True, False])
def test_save_completed(
check: bool,
completed: bool,
default_formatter: Formatter,
todo_factory: Callable,
) -> None:
todo = todo_factory()
if completed:
todo.complete()
editor = TodoEditor(todo, [todo.list], default_formatter)
editor._completed.state = check
with pytest.raises(ExitMainLoop):
editor._keypress("ctrl s")
assert todo.is_completed is check
@pytest.mark.skip("See: https://github.com/pimutils/todoman/issues/537")
def test_ctrl_c_clears(default_formatter: Formatter, todo_factory: Callable) -> None:
todo = todo_factory()
editor = TodoEditor(todo, [todo.list], default_formatter)
# Simulate that ctrl+c gets pressed, since we can't *really* do that
# trivially inside unit tests.
with (
mock.patch("urwid.main_loop.MainLoop.run", side_effect=KeyboardInterrupt),
mock.patch(
"urwid.main_loop.MainLoop.stop",
) as mocked_stop,
):
editor.edit()
assert mocked_stop.call_count == 1