-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reminders.py
483 lines (409 loc) · 19.3 KB
/
test_reminders.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
import unittest
from typing import Dict
from typing import List
from typing import Tuple
from unittest.mock import call
from unittest.mock import patch
from datetime import datetime
from reminders import AmbiguousUser
from reminders import CSECT_EMAIL
from reminders import CSECT_MSG
from reminders import Format
from reminders import MissingUser
from reminders import Reminders
from reminders import ROW_HEADER
from reminders import SafeConfigParser
class TestReminders(unittest.TestCase):
def test_reminders_sheet_to_dict_errors(self):
uut = Reminders()
self.assertRaises(FileNotFoundError, lambda: uut.sheet_to_dict('foo', 'bar'))
self.assertRaises(ValueError, lambda: uut.sheet_to_dict('example/bedrock.xlsx', 'bar'))
def test_reminders_sheet_to_dict_example_action(self):
uut = Reminders()
items = uut.sheet_to_dict('example/bedrock.xlsx', 'Actions')
self.assertEqual(4, len(items))
first = items[0]
self.assertEqual('SG1', first.get('ID'))
self.assertEqual('Fred', first.get('User'))
self.assertEqual('Break big rocks into small ones', first.get('Action'))
self.assertEqual('Open', first.get('Status'))
self.assertEqual('Far in the future', first.get('Notes'))
self.assertEqual('This column is NOT included in the example', first.get('Extra'))
self.assertEqual('Actions:1', first.get('_row'))
due = first.get('Due Date')
self.assertTrue(isinstance(due, datetime))
self.assertEqual(datetime(year=2030, month=3, day=24), due)
def test_reminders_sheet_to_dict_example_user(self):
uut = Reminders()
items = uut.sheet_to_dict('example/bedrock.xlsx', 'Users')
self.assertEqual(4, len(items))
keys = set(['User', 'First', 'Email', 'Aliases', ROW_HEADER])
first = items[0]
self.assertEqual(keys, set(first.keys()))
self.assertEqual('Fred Flintstone', first.get('User'))
self.assertEqual('Fred', first.get('First'))
self.assertEqual('[email protected]', first.get('Email'))
self.assertEqual('FF', first.get('Aliases'))
self.assertEqual('Users:1', first.get('_row'))
def test_reminders_find_user(self):
uut = Reminders()
users = uut.sheet_to_dict('example/bedrock.xlsx', 'Users')
self.assertEqual(4, len(users))
fred = users[0] # Fred is the first row
wilma = users[1]
barney = users[2]
betty = users[3]
# find Fred
self.assertEqual(fred, uut.find_user(users, 'Fred'))
self.assertEqual(fred, uut.find_user(users, 'FF')) # aliases
self.assertEqual(fred, uut.find_user(users, 'ff')) # aliases, lowercase
self.assertEqual(fred, uut.find_user(users, 'FreD@slate')) # email case insensitive
self.assertEqual(fred, uut.find_user(users, 'Fred flint')) # user case insensitive
# find Wilma
self.assertEqual(wilma, uut.find_user(users, 'Wilma F'))
self.assertEqual(wilma, uut.find_user(users, 'wf')) # aliases
self.assertEqual(wilma, uut.find_user(users, 'wilma.Fl')) # email case insensitive
# find Barney
self.assertEqual(barney, uut.find_user(users, 'Barney R'))
self.assertEqual(barney, uut.find_user(users, 'BR')) # aliases
self.assertEqual(barney, uut.find_user(users, 'barney@')) # email case insensitive
# find Betty
self.assertEqual(betty, uut.find_user(users, 'Betty R'))
self.assertEqual(betty, uut.find_user(users, 'betty.rubble@')) # email case insensitive
# No such finding is NOT an error
self.assertIsNone(uut.find_user(users, 'Bambam'))
self.assertRaises(AmbiguousUser, lambda: uut.find_user(users, 'slate'))
self.assertRaises(AmbiguousUser, lambda: uut.find_user(users, 'flintstone'))
def test_reminders_config_example(self):
uut = Reminders()
uut.parse_config('example/config.ini')
self.assertEqual('bedrock.xlsx', uut.spreadsheet)
self.assertEqual('Users', uut.tab_user)
self.assertEqual('Actions', uut.tab_action)
self.assertEqual('User', uut.hdr_user)
self.assertEqual('Email', uut.hdr_email)
self.assertEqual('ID', uut.hdr_id)
self.assertEqual('Due Date', uut.hdr_due)
self.assertEqual('Status', uut.hdr_status)
self.assertEqual('smtp.office365.com', uut.mail_server)
self.assertEqual('587', uut.mail_port)
self.assertEqual(None, uut.mail_password)
self.assertEqual('[email protected]', uut.mail_from)
self.assertEqual('Do It! Now!!!', uut.mail_subject)
self.assertEqual(None, uut.mail_cc)
self.assertIn('The following actions assigned to you', uut.msg_preamble)
self.assertEqual('<p/><p>Thank you,</p><p>Mr. Slate<br/>Slate Rock and Gravel</p>', uut.msg_close)
self.assertEqual(['ID', 'Action', 'User', 'Due Date', 'Notes'], uut.msg_table_headers)
self.assertEqual({'Action': 'l', 'Notes': 'l'}, uut.msg_table_align)
self.assertEqual([], uut.check_config())
def test_reminders_config_empty(self):
uut = Reminders()
errors = uut.check_config()
self.assertIn('Missing spreadsheet tab name for actions', errors)
self.assertIn('Missing spreadsheet tab name for users', errors)
self.assertIn('Missing user/action user-id field', errors)
self.assertIn('Missing user email field', errors)
self.assertIn('Missing action identifier field', errors)
self.assertIn('Missing action due date field', errors)
self.assertIn('Missing action status field', errors)
self.assertIn('Missing mail server or port', errors)
self.assertIn('Missing mail from address', errors)
self.assertIn('Missing mail subject', errors)
self.assertIn('Missing message table headers', errors)
def test_reminders_config_cc(self):
uut = Reminders()
# start with a baseline config
config = SafeConfigParser()
config.read('example/config.ini')
config.remove_option(CSECT_EMAIL, 'cc')
uut.update_config(config)
self.assertEqual(None, uut.mail_cc)
config.set(CSECT_EMAIL, 'cc', '')
uut.update_config(config)
self.assertEqual(None, uut.mail_cc)
config.set(CSECT_EMAIL, 'cc', ', , ,')
uut.update_config(config)
self.assertEqual(None, uut.mail_cc)
mail1 = "[email protected]"
mail2 = "[email protected]"
config.set(CSECT_EMAIL, 'cc', f"{mail1}, , {mail2} ,")
uut.update_config(config)
self.assertEqual([mail1, mail2], uut.mail_cc)
def test_reminders_config_table_headers(self):
uut = Reminders()
# start with a baseline config
config = SafeConfigParser()
config.read('example/config.ini')
config.remove_option(CSECT_MSG, 'columns')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_headers)
config.set(CSECT_MSG, 'columns', '')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_headers)
config.set(CSECT_MSG, 'columns', ', , ,')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_headers)
hdr1 = "User"
hdr2 = "Due Date"
config.set(CSECT_MSG, 'columns', f" , {hdr1}, , {hdr2} ,")
uut.update_config(config)
self.assertEqual([hdr1, hdr2], uut.msg_table_headers)
def test_reminders_config_table_align(self):
uut = Reminders()
# start with a baseline config
config = SafeConfigParser()
config.read('example/config.ini')
config.remove_option(CSECT_MSG, 'align')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_align)
config.set(CSECT_MSG, 'align', '')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_align)
config.set(CSECT_MSG, 'align', ', , ,')
uut.update_config(config)
self.assertEqual(None, uut.msg_table_align)
hdr1 = "User"
hdr2 = "Due Date"
config.set(CSECT_MSG, 'align', f" , {hdr1}, , {hdr2}: l ,")
self.assertRaises(ValueError, lambda: uut.update_config(config))
config.set(CSECT_MSG, 'align', f" , {hdr1}: r, , {hdr2}: l ,")
uut.update_config(config)
self.assertEqual({hdr1: 'r', hdr2: 'l'}, uut.msg_table_align)
def findActions(self, correlated: List[Tuple], firstname: str) -> List[Dict]:
for (user, userActions) in correlated:
if user.get('First') == firstname:
return userActions
return []
def test_reminders_correlate_example(self):
uut = Reminders()
uut.parse_config('example/config.ini') # correlation needs the fields initialized
users = uut.sheet_to_dict('example/bedrock.xlsx', 'Users')
actions = uut.sheet_to_dict('example/bedrock.xlsx', 'Actions')
correlated = uut.correlate(users, actions)
self.assertEqual(len(users), len(correlated))
items = self.findActions(correlated, 'Fred')
self.assertEqual(
set(['Actions:1', 'Actions:2', 'Actions:3']),
set([_.get(ROW_HEADER) for _ in items])
)
items = self.findActions(correlated, 'Barney')
self.assertEqual(
set(['Actions:2', 'Actions:4']),
set([_.get(ROW_HEADER) for _ in items])
)
items = self.findActions(correlated, 'Betty')
self.assertEqual(
set(['Actions:4']),
set([_.get(ROW_HEADER) for _ in items])
)
items = self.findActions(correlated, 'Wilma')
self.assertEqual(
set(['Actions:3']),
set([_.get(ROW_HEADER) for _ in items])
)
# modify a user action to get a MissingUser exception
actions[2].update({'User': 'Bam Bam'})
self.assertRaises(MissingUser, lambda: uut.correlate(users, actions))
def read_text(self, filename: str) -> str:
with open(filename) as fp:
return fp.read()
def test_reminders_table(self):
uut = Reminders()
uut.parse_config('example/config.ini') # table creation needs the fields initialized
actions = uut.sheet_to_dict('example/bedrock.xlsx', 'Actions')
expected = self.read_text('resources/all_actions.html')
self.assertEqual(expected, uut._create_table(actions, Format.HTML))
expected = self.read_text('resources/all_actions.txt')
self.assertEqual(expected, uut._create_table(actions, Format.TEXT))
expected = self.read_text('resources/all_actions.json')
self.assertEqual(expected, uut._create_table(actions, Format.JSON))
expected = self.read_text('resources/all_actions.csv').replace('\n', '\r\n')
self.assertEqual(expected, uut._create_table(actions, Format.CSV))
def test_reminders_validate_users(self):
uut = Reminders()
uut.parse_config('example/config.ini') # correlation needs the fields initialized
users = uut.sheet_to_dict('example/bedrock.xlsx', 'Users')
self.assertEqual([], uut.validate_users(users))
users[0].update({uut.hdr_email: ''})
users[3].pop(uut.hdr_email, None)
errors = uut.validate_users(users)
self.assertEqual(2, len(errors))
self.assertIn('Fred Flintstone (Users:1) error(s): missing email', errors)
self.assertIn('Betty Rubble (Users:4) error(s): missing email', errors)
# change template to catch everyone
uut.msg_preamble = '{Foo}'
uut.msg_close = '{Bar}'
errors = uut.validate_users(users)
self.assertEqual(4, len(errors))
self.assertIn('Fred Flintstone (Users:1) error(s): missing email, missing email fields Bar/Foo', errors)
self.assertIn('Wilma Flintstone (Users:2) error(s): missing email fields Bar/Foo', errors)
self.assertIn('Barney Rubble (Users:3) error(s): missing email fields Bar/Foo', errors)
self.assertIn('Betty Rubble (Users:4) error(s): missing email, missing email fields Bar/Foo', errors)
def test_reminders_validate_actions(self):
uut = Reminders()
uut.parse_config('example/config.ini') # correlation needs the fields initialized
actions = uut.sheet_to_dict('example/bedrock.xlsx', 'Actions')
self.assertEqual([], uut.validate_actions(actions))
actions[0].update({uut.hdr_user: ''})
actions[3].pop(uut.hdr_user, None)
actions[3].update({uut.hdr_due: None})
errors = uut.validate_actions(actions)
self.assertEqual(2, len(errors))
self.assertIn('SG1 (Actions:1) error(s): missing assignment', errors)
self.assertIn('Rubble1 (Actions:4) error(s): missing assignment, missing due date', errors)
# change template to catch all actions
uut.msg_table_headers = ['Sna', 'Foo']
errors = uut.validate_actions(actions)
self.assertEqual(4, len(errors))
self.assertIn('SG1 (Actions:1) error(s): missing assignment, missing table fields Foo/Sna', errors)
self.assertIn('SG2 (Actions:2) error(s): missing table fields Foo/Sna', errors)
self.assertIn('FS1 (Actions:3) error(s): missing table fields Foo/Sna', errors)
self.assertIn('Rubble1 (Actions:4) error(s): missing assignment, missing due date, missing table fields Foo/Sna', errors) # noqa: E501
@patch('reminders.Reminders.print')
@patch('smtplib.SMTP.sendmail')
@patch('smtplib.SMTP.login')
@patch('smtplib.SMTP.starttls')
def test_reminders_run_example(self, mock_starttls, mock_login, mock_send, mock_print):
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-c',
'example/config.ini',
# NOTE: must specify filename, since config.ini assumes it is in same directory as config.ini
'-s',
'example/bedrock.xlsx',
]
result = uut.run(args)
self.assertEqual(0, result)
self.assertEqual(1, mock_starttls.call_count)
self.assertEqual(1, mock_login.call_count)
mock_login.assert_called_once_with(uut.mail_from, uut.mail_password)
self.assertEqual(3, mock_send.call_count)
self.assertEqual(4, mock_print.call_count)
print_calls = [
call('Sending emails about items due in the next 14 days:'),
call(' Barney Rubble: 2'),
call(' Betty Rubble: 1'),
call(' Fred Flintstone: 1'),
]
self.assertEqual(print_calls, mock_print.call_args_list)
@patch('reminders.Reminders.print')
def test_reminders_run_no_config(self, mock_print):
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-s',
'example/bedrock.xlsx',
]
result = uut.run(args)
self.assertEqual(5, result)
mock_print.assert_called_once_with('Configuration errors:\n\tMissing spreadsheet tab name for actions\n\tMissing spreadsheet tab name for users\n\tMissing user/action user-id field\n\tMissing user email field\n\tMissing action identifier field\n\tMissing action due date field\n\tMissing action status field\n\tMissing mail server or port\n\tMissing mail from address\n\tMissing mail subject\n\tMissing message table headers') # noqa: E501
@patch('reminders.Reminders.print')
def test_reminders_run_no_spreadsheet(self, mock_print):
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-c',
'example/config.ini',
]
result = uut.run(args)
self.assertEqual(1, result)
mock_print.assert_called_once_with('bedrock.xlsx is not a file!')
def test_reminders_run_print(self):
# let's output get printed
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-c',
'example/config.ini',
]
result = uut.run(args)
self.assertEqual(1, result)
@patch('reminders.Reminders.print')
@patch('smtplib.SMTP.sendmail')
@patch('smtplib.SMTP.login')
@patch('smtplib.SMTP.starttls')
def test_reminders_run_person_filter(self, mock_starttls, mock_login, mock_send, mock_print):
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-c',
'example/config.ini',
# NOTE: must specify filename, since config.ini assumes it is in same directory as config.ini
'-s',
'example/bedrock.xlsx',
'-p',
'fred',
'-d',
'30',
]
result = uut.run(args)
self.assertEqual(0, result)
self.assertEqual(1, mock_starttls.call_count)
self.assertEqual(1, mock_login.call_count)
mock_login.assert_called_once_with(uut.mail_from, uut.mail_password)
self.assertEqual(1, mock_send.call_count)
self.assertEqual(2, mock_print.call_count)
print_calls = [
call('Sending emails about items due in the next 30 days:'),
call(' Fred Flintstone: 1'),
]
self.assertEqual(print_calls, mock_print.call_args_list)
@patch('reminders.Reminders.print')
def test_reminders_run_missing_person_filter(self, mock_print):
uut = Reminders()
uut.mail_password = 'abc123' # avoid prompting
args = [
'-c',
'example/config.ini',
# NOTE: must specify filename, since config.ini assumes it is in same directory as config.ini
'-s',
'example/bedrock.xlsx',
'-p',
'Pebbles',
]
result = uut.run(args)
self.assertEqual(0, result)
mock_print.assert_called_once_with('No open user actions found in example/bedrock.xlsx for Pebbles in the next 14 days') # noqa: E501
@patch('reminders.Reminders.print')
def test_reminders_run_missing_config(self, mock_print):
uut = Reminders()
args = [
'-c',
'resources/missing_config.ini',
'-s',
'example/bedrock.xlsx',
]
result = uut.run(args)
self.assertEqual(4, result)
mock_print.assert_called_once_with('resources/missing_config.ini is not a file')
@patch('reminders.Reminders.print')
def test_reminders_run_missing_bad_users(self, mock_print):
uut = Reminders()
args = [
'-c',
'resources/bad_users.ini',
'-s',
'example/bedrock.xlsx',
]
result = uut.run(args)
self.assertEqual(2, result)
self.assertEqual(1, mock_print.call_count)
# this is a bit hacky, but don't want to print EVERYTHING...
message = mock_print.call_args.args[0]
self.assertTrue(message.startswith('Invalid users:'))
@patch('reminders.Reminders.print')
def test_reminders_run_missing_bad_actions(self, mock_print):
uut = Reminders()
args = [
'-c',
'resources/bad_actions.ini',
'-s',
'example/bedrock.xlsx',
]
result = uut.run(args)
self.assertEqual(3, result)
self.assertEqual(1, mock_print.call_count)
# this is a bit hacky, but don't want to print EVERYTHING...
message = mock_print.call_args.args[0]
self.assertTrue(message.startswith('Invalid actions:'))