def remove_invoice(invoicename): profileSession = get_profile_from_session() if profileSession.id: id_profile = profileSession.id else: logging.warning('(Invoice) Session closed: ' + profileSession.id) return 1 fdao = InvoiceDAO() if fdao.delete(fdao.where('name', invoicename)): logging.info('remove invoice %s OK', invoicename) if id_profile in CACHE_INVOICE.keys(): del CACHE_INVOICE[id_profile] return 2 else: logging.info('remove invoice %s Failed', invoicename) return 3
class InvoiceTestCase(unittest.TestCase): def setUp(self): self.fdao = InvoiceDAO(DB_PATH) self.invoice = Invoice() self.invoice.date_expiry = '01/01/2020' self.invoice.date_sent = '01/01/2020' self.invoice.days = 10 self.invoice.max_delay = '01/01/2020' self.invoice.id_client = 1 self.invoice.id_profile = 1 self.invoice.name = 'FACTURE TEST 1' self.invoice.sold = False self.invoice.project = 'TEST 1 2 3' self.invoice.day_rate = 5000 self.invoice.total = '50000' self.invoice.tax = False def test_invoice_obj(self): self.assertIsNotNone(self.fdao, msg="Impossible to instance InvoiceDAO") self.assertTrue(self.fdao.create_table(), msg="Impossible to create invoice table in db") self.assertFalse(self.fdao.exist(self.invoice), msg="Impossible to check Invoice exist in db") self.assertTrue(self.fdao.insert(self.invoice), msg="Impossible to insert Invoice in db") list_invoice = self.fdao.get(self.fdao.where('name', self.invoice.name)) self.assertIsInstance(list_invoice, list, msg="dao.get function not return a list") self.assertGreater(len(list_invoice), 0, msg="No Invoice in db") invce = list_invoice[0] self.assertIsInstance( invce, Invoice, msg="1st element of dao.get is not a Invoice obj") self.assertEqual( self.invoice.date_expiry, invce.date_expiry, msg="Invoice get, has no same attribute 'date_expiry'") self.assertEqual(self.invoice.date_sent, invce.date_sent, msg="Invoice get, has no same attribute 'date_sent'") self.assertEqual(self.invoice.days, invce.days, msg="Invoice get, has no same attribute 'days'") self.assertEqual(self.invoice.max_delay, invce.max_delay, msg="Invoice get, has no same attribute 'max_delay'") self.assertEqual(self.invoice.id_client, invce.id_client, msg="Invoice get, has no same attribute 'id_client'") self.assertEqual(self.invoice.id_profile, invce.id_profile, msg="Invoice get, has no same attribute 'id_profile'") self.assertEqual(self.invoice.name, invce.name, msg="Invoice get, has no same attribute 'name'") self.assertEqual(self.invoice.sold, invce.sold, msg="Invoice get, has no same attribute 'sold'") self.assertEqual(self.invoice.project, invce.project, msg="Invoice get, has no same attribute 'project'") self.assertEqual(self.invoice.day_rate, invce.day_rate, msg="Invoice get, has no same attribute 'day_rate'") self.assertEqual(self.invoice.total, invce.total, msg="Invoice get, has no same attribute 'total'") self.assertEqual(self.invoice.tax, invce.tax, msg="Invoice get, has no same attribute 'tax'") self.assertTrue(hasattr(invce, 'id'), msg="Invoice get, has no attribute 'id'") invce.sold = True self.assertTrue(self.fdao.update(invce)) list_invoice2 = self.fdao.get(self.fdao.where('name', invce.name)) self.assertIsInstance(list_invoice2, list, msg="dao.get function not return a list 2") self.assertGreater(len(list_invoice2), 0, msg="No Invoice 2 in db") invce2 = list_invoice2[0] self.assertEqual( invce2.date_expiry, invce.date_expiry, msg="Invoice get 2, has no same attribute 'date_expiry'") self.assertEqual( invce2.date_sent, invce.date_sent, msg="Invoice get 2, has no same attribute 'date_sent'") self.assertEqual(invce2.days, invce.days, msg="Invoice get 2, has no same attribute 'days'") self.assertEqual( invce2.max_delay, invce.max_delay, msg="Invoice get 2, has no same attribute 'max_delay'") self.assertEqual( invce2.id_client, invce.id_client, msg="Invoice get 2, has no same attribute 'id_client'") self.assertEqual( invce2.id_profile, invce.id_profile, msg="Invoice get 2, has no same attribute 'id_profile'") self.assertEqual(invce2.name, invce.name, msg="Invoice get 2, has no same attribute 'name'") self.assertEqual(invce2.sold, invce.sold, msg="Invoice get 2, has no same attribute 'sold'") self.assertEqual(invce2.project, invce.project, msg="Invoice get 2, has no same attribute 'project'") self.assertEqual(invce2.day_rate, invce.day_rate, msg="Invoice get 2, has no same attribute 'day_rate'") self.assertEqual(invce2.total, invce.total, msg="Invoice get 2, has no same attribute 'total'") self.assertEqual(invce2.tax, invce.tax, msg="Invoice get 2, has no same attribute 'tax'") self.assertTrue(self.fdao.delete(invce), msg="Impossible to delete invoice from db") self.assertFalse(self.fdao.drop(True, False), msg="Drop table invoice not wanted") self.assertFalse(self.fdao.drop(False, True), msg="Drop table invoice not wanted") self.assertTrue(self.fdao.drop(True, True), msg="Cannot drop table invoice") self.assertFalse(self.fdao.drop(True, True), msg="The table has not deleted before")