-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
606 lines (470 loc) · 20.5 KB
/
test_main.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from datetime import time
from pathlib import Path
from typing import Any, List
import pytest # type: ignore
from freezegun import freeze_time # type: ignore
import main
from main import (
Timesheet,
calc_total_flex,
fmt_mins,
handle_command,
load_timesheet,
save_timesheet,
total_flex_as_str,
)
def setup_module(module: Any) -> None:
main.cfg.datafile_dir = Path("test_files")
main.cfg.datafile_dir.mkdir(exist_ok=True)
def setup_function(func: Any) -> None:
files = main.cfg.datafile_dir.glob("*-timesheet.json")
for f in files:
f.unlink()
captured_output = Path(main.cfg.datafile_dir, "captured_output.txt")
if captured_output.exists():
captured_output.unlink()
def teardown_function(func: Any) -> None:
pass
def write_captured_output(captured_output: str) -> None:
Path(main.cfg.datafile_dir, "captured_output.txt").write_text(captured_output)
def assert_captured_out_starts_with(expected: List[str], captured: Any) -> None:
assert expected == captured.out.split("\n")[: len(expected)]
@pytest.mark.parametrize("stop_time,flex", [("16:30", 0), ("16:32", 2), ("16:27", -3)])
def test_flex(capsys, stop_time, flex) -> None:
with freeze_time("2020-09-23"): # A Wednesday
handle_command("start 08:00")
handle_command("lunch")
handle_command(f"stop {stop_time}")
ts = load_timesheet()
assert ts.today.flex_minutes == flex
captured = capsys.readouterr()
assert (
"Estimated end time for today with 30 min lunch is 16:30:00" in captured.out
)
def test_monthly_flextime() -> None:
ts = Timesheet()
ts.get_day("2020-07-09").flex_minutes = 38
ts.get_day("2020-07-10").flex_minutes = 2
ts.get_day("2020-07-11").flex_minutes = 2
assert ts.monthly_flex == 42
save_timesheet(ts)
ts = load_timesheet()
assert ts.monthly_flex == 42
# Note that no lunch was taken in this test.
def test_multiple_start_and_end(capsys) -> None:
with freeze_time("2020-09-25"): # A Friday
# Working 30 min first section
handle_command("start 08:30")
captured = capsys.readouterr()
assert (
"Estimated end time for today with 30 min lunch is 17:00:00" in captured.out
)
handle_command("stop 09:00")
ts = load_timesheet()
assert ts.today.flex_minutes == -7 * 60 - 30 # Should have -7h 30m as flex
# Working 1h 30 min more
handle_command("start 10:30")
captured = capsys.readouterr()
assert (
"Estimated end time for today with 30 min lunch is 18:30:00" in captured.out
)
handle_command("stop 12:00")
ts = load_timesheet()
assert ts.today.flex_minutes == -6 * 60 # Should have -6h as flex
# Filling up to the 8 hours
handle_command("start 13:00")
captured = capsys.readouterr()
assert (
"Estimated end time for today with 30 min lunch is 19:30:00" in captured.out
)
handle_command("stop 19:00")
ts = load_timesheet()
assert ts.today.flex_minutes == 0 # Should have 0 min as flex
# Working a few more minutes
handle_command("start 19:30")
captured = capsys.readouterr()
assert (
"Estimated end time for today with 30 min lunch is 20:00:00" in captured.out
)
handle_command("stop 19:35")
ts = load_timesheet()
assert ts.today.flex_minutes == 5 # Should have 5 min as flex
def test_workblock_that_is_already_started_cannot_be_started_again(capsys) -> None:
handle_command("start 08:00")
ts = load_timesheet()
handle_command("start 08:01")
assert ts == load_timesheet()
assert len(ts.today.work_blocks) == 1
captured = capsys.readouterr()
assert (
"Workblock already started, stop it before starting another one" in captured.out
)
def test_lunch_fails_if_day_is_not_started(capsys) -> None:
handle_command("lunch")
assert load_timesheet().today.lunch == 0
captured = capsys.readouterr()
assert "Could not find today in timesheet, did you start the day?" in captured.out
handle_command("start")
handle_command("lunch")
assert load_timesheet().today.lunch == 30
def test_running_lunch_twice_will_not_overwrite_first_lunch() -> None:
handle_command("start")
handle_command("lunch")
assert load_timesheet().today.lunch == 30
handle_command("lunch 25")
assert load_timesheet().today.lunch == 30
def test_stop_fails_if_last_workblock_is_not_started(capsys) -> None:
handle_command("stop")
assert not load_timesheet().today.last_work_block.stopped()
captured = capsys.readouterr()
assert "Could not stop workblock, is your last workblock started?" in captured.out
handle_command("start 08:05")
handle_command("stop 08:10")
assert load_timesheet().today.last_work_block.stop == time.fromisoformat("08:10:00")
assert load_timesheet().today.last_work_block.stopped()
def test_running_stop_twice_will_not_overwrite_last_stop() -> None:
handle_command("start 08:01")
handle_command("stop 08:03")
assert load_timesheet().today.last_work_block.stop == time.fromisoformat("08:03:00")
handle_command("stop 08:05")
assert load_timesheet().today.last_work_block.stop == time.fromisoformat("08:03:00")
def test_total_flextime() -> None:
ts = Timesheet()
ts.get_day("2020-07-01").flex_minutes = 2
main.cfg.datafile = "2020-07-timesheet.json"
save_timesheet(ts)
ts = Timesheet()
ts.get_day("2020-08-01").flex_minutes = 3
main.cfg.datafile = "2020-08-timesheet.json"
save_timesheet(ts)
assert calc_total_flex() == 5
def test_flextime_correct_during_weekend() -> None:
main.cfg.datafile = "2020-09-timesheet.json"
with freeze_time("2020-09-26"): # A Saturday
handle_command("start 08:00")
handle_command("stop 09:02")
assert load_timesheet().today.flex_minutes == 62
assert calc_total_flex() == 62
def test_total_flex_as_str_less_than_one_hour() -> None:
ts = Timesheet()
ts.get_day("2020-07-01").flex_minutes = 2
main.cfg.datafile = "2020-07-timesheet.json"
save_timesheet(ts)
assert total_flex_as_str() == "2min"
def test_total_flex_as_str_exactly_one_hour() -> None:
ts = Timesheet()
ts.get_day("2020-07-01").flex_minutes = 60
main.cfg.datafile = "2020-07-timesheet.json"
save_timesheet(ts)
assert total_flex_as_str() == "1h 0min"
def test_total_flex_as_str_more_than_one_hour() -> None:
ts = Timesheet()
ts.get_day("2020-07-01").flex_minutes = 63
main.cfg.datafile = "2020-07-timesheet.json"
save_timesheet(ts)
assert total_flex_as_str() == "1h 3min"
def test_start_no_arguments() -> None:
main.cfg.datafile = "2020-09-timesheet.json"
with freeze_time("2020-09-26 08:03"): # A Saturday
handle_command("start")
assert load_timesheet().today.last_work_block.start == time.fromisoformat(
"08:03:00"
)
def test_stop_no_arguments() -> None:
main.cfg.datafile = "2020-09-timesheet.json"
with freeze_time("2020-09-26 08:07"): # A Saturday
handle_command("start 08:00")
handle_command("stop")
assert load_timesheet().today.last_work_block.stop == time.fromisoformat(
"08:07:00"
)
def test_view_today(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:02")
handle_command("lunch 25")
handle_command("stop 14:21")
handle_command("start 15:01")
handle_command("stop 17:27")
capsys.readouterr()
handle_command("view") # Act
captured = capsys.readouterr()
expected = [
"2020-11-24 | worked time: 8h 20min | lunch: 25min | daily flex: 20min",
" 08:02-14:21 => 6h 19min",
" 15:01-17:27 => 2h 26min",
]
assert_captured_out_starts_with(expected, captured)
def test_view_today_with_workblock_not_ended(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:02")
capsys.readouterr()
handle_command("view") # Act
captured = capsys.readouterr()
expected = [
"2020-11-24 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
" 08:02-",
]
assert_captured_out_starts_with(expected, captured)
def test_view_today_with_a_comment(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:00")
handle_command("stop 09:00 Worked on solving the crazy hard bug.")
handle_command("start 10:00")
handle_command("stop 11:30")
capsys.readouterr()
handle_command("view") # Act
captured = capsys.readouterr()
expected = [
"2020-11-24 | worked time: 2h 30min | lunch: 0min | daily flex: -5h 30min",
" 08:00-09:00 => 1h 0min",
" Worked on solving the crazy hard bug.",
" 10:00-11:30 => 1h 30min",
]
assert_captured_out_starts_with(expected, captured)
def test_view_week(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-22"): # A Sunday the week before
handle_command("start 08:00")
handle_command("lunch")
handle_command("stop 16:30")
# The Monday is intentionally excluded
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:02")
handle_command("lunch")
handle_command("stop 16:30")
with freeze_time("2020-11-25"): # A Wednesday
handle_command("start 08:02")
handle_command("lunch 25")
handle_command("stop 14:21")
handle_command("start 15:01")
handle_command("stop 17:27")
capsys.readouterr()
handle_command("view week") # Act
captured = capsys.readouterr()
write_captured_output(captured.out)
expected = [
"2020-11-23 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"",
"2020-11-24 | worked time: 7h 58min | lunch: 30min | daily flex: -2min",
" 08:02-16:30 => 8h 28min",
"",
"2020-11-25 | worked time: 8h 20min | lunch: 25min | daily flex: 20min",
" 08:02-14:21 => 6h 19min",
" 15:01-17:27 => 2h 26min",
"---",
"Weekly flex: 18min",
]
assert_captured_out_starts_with(expected, captured)
def test_view_prev_week(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-22"): # A Sunday the week before
handle_command("start 08:00")
handle_command("lunch")
handle_command("stop 16:30")
# The Monday is intentionally excluded
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:02")
handle_command("lunch")
handle_command("stop 16:30")
with freeze_time("2020-11-25"): # A Wednesday
handle_command("start 08:02")
handle_command("lunch 25")
handle_command("stop 14:21")
handle_command("start 15:01")
handle_command("stop 17:27")
# some time passes so it is the next week
with freeze_time("2020-11-30"): # The Monday next week
capsys.readouterr()
handle_command("view prev_week") # Act
captured = capsys.readouterr()
write_captured_output(captured.out)
expected = [
"2020-11-23 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"",
"2020-11-24 | worked time: 7h 58min | lunch: 30min | daily flex: -2min",
" 08:02-16:30 => 8h 28min",
"",
"2020-11-25 | worked time: 8h 20min | lunch: 25min | daily flex: 20min",
" 08:02-14:21 => 6h 19min",
" 15:01-17:27 => 2h 26min",
"",
"2020-11-26 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"",
"2020-11-27 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"",
"2020-11-28 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"",
"2020-11-29 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
"---",
"Weekly flex: 18min",
]
assert_captured_out_starts_with(expected, captured)
def test_view_is_case_insensitive(capsys) -> None:
main.cfg.datafile = "2020-11-timesheet.json"
with freeze_time("2020-11-24"): # A Tuesday
handle_command("start 08:02")
capsys.readouterr()
handle_command("view ToDaY") # Act
captured = capsys.readouterr()
expected = [
"2020-11-24 | worked time: 0h 0min | lunch: 0min | daily flex: 0min",
" 08:02-",
]
assert_captured_out_starts_with(expected, captured)
def test_summary_month(capsys) -> None:
main.cfg.datafile = "2023-01-timesheet.json"
with freeze_time("2023-01-03"): # A Tuesday
handle_command("start 08:00")
handle_command("lunch")
handle_command("stop 16:30")
# Wednesday is intentionally excluded
with freeze_time("2023-01-05"): # A Thursday
handle_command("start 08:02")
handle_command("lunch")
handle_command("stop 16:30")
with freeze_time("2023-01-07"): # A Saturday
handle_command("timeoff 8")
handle_command("start 10:00")
handle_command("stop 11:30")
with freeze_time("2023-01-09"): # Monday the next week
handle_command("start 08:02")
handle_command("lunch 25")
handle_command("stop 14:21")
handle_command("start 15:01")
handle_command("stop 17:27")
capsys.readouterr()
handle_command("summary") # Act
captured = capsys.readouterr()
write_captured_output(captured.out)
expected = [
"┏━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━┓",
"┃ week ┃ date ┃ worked time ┃ daily flex ┃ time off ┃",
"┡━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━┩",
"│ │ 2023-01-01 │ │ │ │",
"├──────┼────────────┼─────────────┼────────────┼──────────┤",
"│ 1 │ 2023-01-02 │ │ │ │",
"│ │ 2023-01-03 │ 8h 0min │ 0min │ │",
"│ │ 2023-01-04 │ │ │ │",
"│ │ 2023-01-05 │ 7h 58min │ -2min │ │",
"│ │ 2023-01-06 │ │ │ │",
"│ │ 2023-01-07 │ 1h 30min │ 1h 30min │ 8h 0min │",
"│ │ 2023-01-08 │ │ │ │",
"├──────┼────────────┼─────────────┼────────────┼──────────┤",
"│ 2 │ 2023-01-09 │ 8h 20min │ 20min │ │",
"└──────┴────────────┴─────────────┴────────────┴──────────┘",
"",
"week 1: 17h 28min",
"week 2: 8h 20min",
"Worked 25h 48min of 24 hour(s) => monthly flex: 1h 48min",
]
assert_captured_out_starts_with(expected, captured)
def test_timeoff_half_day(capsys) -> None:
main.cfg.datafile = "2021-04-timesheet.json"
with freeze_time("2021-04-02"): # A Friday
handle_command("timeoff 4")
handle_command("start 08:00")
captured = capsys.readouterr()
write_captured_output(captured.out)
handle_command("stop 12:02")
ts = load_timesheet()
assert ts.today.time_off_minutes == 4 * 60
assert ts.today.flex_minutes == 2
assert (
"Estimated end time for today with 30 min lunch is 12:30:00" in captured.out
)
def test_timeoff_full_day() -> None:
main.cfg.datafile = "2021-04-timesheet.json"
with freeze_time("2021-04-02"): # A Friday
handle_command("timeoff 8")
ts = load_timesheet()
assert ts.today.time_off_minutes == 8 * 60
assert ts.today.flex_minutes == 0
def test_timeoff_recalcs_flex() -> None:
main.cfg.datafile = "2021-04-timesheet.json"
with freeze_time("2021-04-02"): # A Friday
handle_command("start 08:00")
handle_command("stop 12:02")
ts = load_timesheet()
assert ts.today.time_off_minutes == 0
assert ts.today.flex_minutes == -4 * 60 + 2
handle_command("timeoff 4")
ts = load_timesheet()
assert ts.today.time_off_minutes == 4 * 60
assert ts.today.flex_minutes == 2
def test_timeoff_negative_input() -> None:
main.cfg.datafile = "2021-04-timesheet.json"
with freeze_time("2021-04-02"): # A Friday
with pytest.raises(
ValueError,
match="Invalid timeoff value, must be an int between 0 and 8 inclusive.",
):
handle_command("timeoff -1")
def test_timeoff_more_than_a_workday() -> None:
main.cfg.datafile = "2021-04-timesheet.json"
with freeze_time("2021-04-02"): # A Friday
with pytest.raises(
ValueError,
match="Invalid timeoff value, must be an int between 0 and 8 inclusive.",
):
handle_command("timeoff 9")
@pytest.mark.parametrize(
"mins,expected",
[(50, "50min"), (70, "1h 10min"), (-20, "-20min"), (-70, "-1h 10min")],
)
def test_fmt_mins(mins, expected) -> None:
assert fmt_mins(mins) == expected
def test_worked_time() -> None:
handle_command("start 08:00")
handle_command("stop 09:00") # Worked 1 hour
handle_command("start 12:00")
handle_command("stop 14:00") # Worked 2 more hours
ts = load_timesheet()
assert ts.today.worked_time == 3 * 60
def test_worked_time_with_lunch() -> None:
handle_command("start 08:00")
handle_command("stop 09:00")
handle_command("lunch 25")
ts = load_timesheet()
# Worked 35 min and had 25 min lunch
assert ts.today.worked_time == 35
assert ts.today.lunch == 25
def test_worked_time_with_no_block_stopped() -> None:
handle_command("start 08:10")
handle_command("lunch")
ts = load_timesheet()
assert ts.today.worked_time == 0
def test_worked_time_with_a_block_not_stopped() -> None:
handle_command("start 08:10")
handle_command("stop 08:30") # Worked 2 mins
handle_command("start 08:50")
ts = load_timesheet()
assert ts.today.worked_time == 20
def test_comment_with_an_empty_comment() -> None:
handle_command("start 08:10")
handle_command("comment ")
ts = load_timesheet()
assert ts.today.last_work_block.comment is None
def test_comment_with_a_block_not_stopped() -> None:
handle_command("start 08:10")
handle_command("comment some comment added to this workblock")
ts = load_timesheet()
assert ts.today.last_work_block.comment == "some comment added to this workblock"
def test_comment_overwrites_previous_comment() -> None:
handle_command("start 08:10")
handle_command("comment some comment added to this workblock")
handle_command("comment new fancy comment")
ts = load_timesheet()
assert ts.today.last_work_block.comment == "new fancy comment"
def test_comment_with_workblock() -> None:
handle_command("comment some comment added to this workblock")
ts = load_timesheet()
assert ts.today.last_work_block.comment is None
def test_comment_with_no_open_workblock() -> None:
handle_command("start 08:10")
handle_command("stop 08:15")
handle_command("comment some comment added to this workblock")
ts = load_timesheet()
assert ts.today.last_work_block.comment is None