def test_before_expiry_date(self): print('Test: Remind before expiry date') db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() admin = db.get_admin() self.assertEqual(admin, {1, 2, 3}) # with no expiry date certs target_day1 = dt(2014, 8, 15) lab_users, pis = find_users_by_days(users, target_day1.date(), 'before') self.assertEqual(lab_users, []) self.assertEqual(pis[4][11], []) self.assertEqual(pis[4][13], []) self.assertEqual(pis[4][15], []) self.assertEqual(pis[5][11], []) self.assertEqual(pis[5][13], []) self.assertEqual(pis[5][15], []) self.assertEqual(pis[6][11], []) # with expiry date certs target_day2 = dt(2019, 7, 13) lab_users, pis = find_users_by_days(users, target_day2.date(), 'before') self.assertEqual(lab_users[0]['id'], 13) self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[4][11], []) self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[4][15], []) self.assertEqual(pis[5][11], []) self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[4][15], []) self.assertEqual(pis[6][11], []) target_day3 = dt(2019, 7, 14) lab_users, pis = find_users_by_days(users, target_day3.date(), 'before') self.assertEqual(lab_users[0]['id'], 11) self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}) self.assertEqual(pis[4][11], [{'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}]) self.assertEqual(pis[4][13], []) self.assertEqual(pis[4][15], []) self.assertEqual(pis[5][11][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}) self.assertEqual(pis[5][13], []) self.assertEqual(pis[4][15], []) self.assertEqual(pis[6][11][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})
def test_after_expiry_date(self): print('Test: Remind after expiry date') db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() admin = db.get_admin() self.assertEqual(admin, {1, 2, 3}) target_day1 = dt(2019, 5, 30) lab_users, pis = find_users_by_days(users, target_day1.date(), 'after') self.assertEqual(lab_users[0]['id'], 15) self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[4][11], []) self.assertEqual(pis[4][13], []) self.assertEqual(pis[4][15][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[5][11], []) self.assertEqual(pis[5][13], []) self.assertEqual(pis[5][15][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[6][11], []) target_day2 = dt(2019, 6, 30) lab_users, pis = find_users_by_days(users, target_day2.date(), 'after') self.assertEqual(lab_users[0]['id'], 15) self.assertEqual(lab_users[0]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(lab_users[0]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[4][11], []) self.assertEqual(pis[4][13], []) self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[5][11], []) self.assertEqual(pis[5][13], []) self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[6][11], []) target_day3 = dt(2019, 7, 14) lab_users, pis = find_users_by_days(users, target_day3.date(), 'after') self.assertEqual(lab_users[0]['id'], 13) self.assertEqual(lab_users[0]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(lab_users[1]['id'], 15) self.assertEqual(lab_users[1]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(lab_users[1]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[4][11], []) self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[5][11], []) self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[6][11], []) target_day4 = dt(2020, 1, 1) lab_users, pis = find_users_by_days(users, target_day4.date(), 'after') self.assertEqual(lab_users[0]['id'], 11) self.assertEqual(lab_users[0]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)}) self.assertEqual(lab_users[0]['certs'][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)}) self.assertEqual(lab_users[0]['certs'][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}) self.assertEqual(lab_users[1]['id'], 13) self.assertEqual(lab_users[1]['certs'][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(lab_users[2]['id'], 15) self.assertEqual(lab_users[2]['certs'][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(lab_users[2]['certs'][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[4][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)}) self.assertEqual(pis[4][11][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)}) self.assertEqual(pis[4][11][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}) self.assertEqual(pis[4][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[4][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[4][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[5][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)}) self.assertEqual(pis[5][11][1], {'id': 22, 'name': 'Transportation of Dangerous Goods Class 7 (Radioactivity) Receiving Course for ground', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2016, 10, 1)}) self.assertEqual(pis[5][11][2], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)}) self.assertEqual(pis[5][13][0], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 13), 'completion_date': datetime.date(2014, 7, 13)}) self.assertEqual(pis[5][15][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 6, 27), 'completion_date': datetime.date(2014, 6, 27)}) self.assertEqual(pis[5][15][1], {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 5, 29), 'completion_date': datetime.date(2014, 5, 29)}) self.assertEqual(pis[6][11][0], {'id': 16, 'name': 'Biological Safety Course', 'expiry_date': datetime.date(2019, 10, 1), 'completion_date': datetime.date(2014, 10, 1)}, {'id': 20, 'name': 'Biosafety for Permit Holders', 'expiry_date': datetime.date(2019, 7, 14), 'completion_date': datetime.date(2014, 7, 14)})
""" Send an email to administrators 2 weeks before the expiry date """ print("send email 14days before") lab_users, pis, = find_users_by_type(users, 2) if len(lab_users) > 0 and len(admin) > 0: send_email_to_admin(users, admin, lab_users, DAYS14, type) # Helper functions def contain_cert(certs, cert_id): """ Check whether user's certs contain a cert """ for cert in certs: if cert['id'] == cert_id: return True return False if __name__ == "__main__": db = CertTrackerDatabase(USER, PASSWORD, HOST, PORT, DATABASE) users = db.get_users() certs = db.get_certs() admin = db.get_admin() #send_email_30days_before(users, certs, 'before') #send_email_14days_before(users, certs, admin, 'before') db.close()