def make_payment(): data = request.json try: curr_date = datetime.strptime(data['date'], "%Y-%m-%d") amount = int(data['payment_amount']) policy_id = int(data['policy_id']['id']) pa = PolicyAccounting(policy_id) except: abort(400) pa.make_payment(date_cursor=curr_date, amount=amount) return "All Good"
def test_policy_change_billing_schedule(self): self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[0].bill_date, amount=300)) self.assertEquals(len(invoices), 4) self.assertEquals(pa.return_account_balance(invoices[0].due_date), 0) self.assertEquals(pa.return_account_balance(invoices[-1].due_date), 900) # Evaluate that there are not deleted invoices in this policy yet self.assertEquals(len([invoice for invoice in invoices if invoice.deleted]), 0) # Create new invoices with a monthly schedule on the same policy pa.policy.billing_schedule = 'Monthly' pa.make_invoices() invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .order_by(Invoice.bill_date).all() # The number of invoices should now be 16 (12 + 4) self.assertEquals(len(invoices), 16) # There should be 4 "deleted" invoices in the policy self.assertEquals(len([invoice for invoice in invoices if invoice.deleted]), 4) # The pending balance should still be 900 even when new invoices were created. self.assertEquals(pa.return_account_balance(invoices[-1].due_date), 900)
def test_monthly_on_eff_date(self): self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[0].bill_date, amount=100)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[0].bill_date), 0)
def test_quarterly_on_second_installment_bill_date_with_full_payment(self): self.policy.billing_schedule = "Quarterly" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[1].bill_date, amount=600)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def test_two_pay_on_last_installment_bill_date(self): self.policy.billing_schedule = "Two-Pay" pa = PolicyAccounting(self.policy) invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .order_by(Invoice.bill_date).all() for i in range(1, 3): self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[0].bill_date, amount=600)) self.assertEquals(pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def test_two_pay_payments_and_balance(self): self.policy.billing_schedule = "Two-Pay" pa = PolicyAccounting(self.policy.id) invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .order_by(Invoice.bill_date).all() for invoice in invoices: self.assertEquals(pa.return_account_balance(date_cursor=invoice.bill_date), invoice.amount_due) self.payments.append(pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoice.bill_date, amount=invoice.amount_due)) self.assertEquals(pa.return_account_balance(date_cursor=invoice.bill_date), 0)
def test_evaluate_pending(self): self.policy.billing_schedule = "Annual" pa = PolicyAccounting(self.policy.id) # Test Evaluate cancellation pending self.assertEquals( pa.evaluate_cancellation_pending_due_to_non_pay(date(2015, 2, 2)), True) # Made payment by insured p = pa.make_payment(date_cursor=date(2015, 2, 2), amount=1200) # None return cause payment must be made by agent self.assertEquals(p, None)
def test_evaluate_cancel(self): policy = Policy('Test Policy', date(2015, 1, 1), 1200) policy.named_insured = self.test_insured.id policy.agent = self.test_agent.id self.policies.append(policy) db.session.add(policy) db.session.commit() # Get the policy from the database policy.billing_schedule = "Monthly" pa = PolicyAccounting(policy.id) # Make a payment self.payments.append(pa.make_payment(date_cursor=date(2015, 01, 01), amount=100))
def test_cancel_policy_other(self): """ Test canceling policy caused of other reason """ self.policy.billing_schedule = "Annual" pa = PolicyAccounting(self.policy.id) self.payments.append( pa.make_payment(self.policy.agent, date(2015, 1, 1), 1200)) # Cancel policy for underwriting pa.cancel_policy(desc="Underwriting") # Check wheter policy canceled with provided reason self.assertEquals(self.policy.status, "Canceled") self.assertEquals(self.policy.cancel_desc, "Underwriting")
def test_pending_payment_before_cancellation_edge(self): """ Same as above, but when an insufficient payment has been made """ pa = PolicyAccounting(self.policy.id) self.payments.append( pa.make_payment(contact_id=self.policy.named_insured, date_cursor=date(2015, 1, 15), amount=100)) # No payment made and we are past the due date, but before cancellation d = date(2015, 3, 1) self.assertTrue( pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=d))
def test_payment_contact_id_data(self): # Create and store a policy without a named insured policy = Policy('Test Policy', date(2015, 1, 1), 1200) policy.named_insured = None policy.agent = self.test_agent.id self.policies.append(policy) db.session.add(policy) db.session.commit() # Get the policy from the database policy.billing_schedule = "Annual" pa = PolicyAccounting(policy.id) # Try to create a payment without a contact id specified, no payment should be registered self.assertFalse(pa.make_payment(date_cursor=date(2015, 01, 01), amount=1200))
def test_change_billing_schedule_a_m(self): self.policy.billing_schedule = "Annual" # No invoices currently exist self.assertFalse(self.policy.invoices) # Invoices should be made when the class is initiated pa = PolicyAccounting(self.policy.id) self.assertEquals(len(self.policy.invoices), 1) self.assertEquals(self.policy.invoices[0].amount_due, self.policy.annual_premium / 1) # Make payment self.payments.append( pa.make_payment(self.policy.agent, date(2015, 1, 1), 1200)) # Change billing schedule to Monthly pa.change_billing_schedule('Monthly') # Shoulnt be able to change schedule cause paid already, invoices should still be one self.assertEquals(len(self.policy.invoices), 1)
def test_pending_payment_before_cancellation(self): """ Test the method when a payment has been made on the invoice but the account is pending cancellation and before cancellation date has been reached. """ pa = PolicyAccounting(self.policy.id) self.payments.append( pa.make_payment(contact_id=self.policy.named_insured, date_cursor=date(2015, 1, 15), amount=100)) # Second payment made and we are past the due date, but before cancellation d = date(2015, 3, 10) self.assertTrue( pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=d))
def test_good_standing_payment(self): """ Test the method when the account is in good standing and a payment has been made """ pa = PolicyAccounting(self.policy.id) self.payments.append( pa.make_payment(contact_id=self.policy.named_insured, date_cursor=date(2015, 1, 15), amount=100)) # First billing cycle should be covered by above payment, so # a date in the middle of the second cycle should be good d = date(2015, 2, 10) # print(pa.return_account_balance(d)) self.assertFalse( pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=d))
def test_quarterly_on_second_installment_bill_date_with_full_payment_after_change( self): """ Same as above, buf after change in billing schedule. """ self.policy.billing_schedule = "Monthly" pa = PolicyAccounting(self.policy.id) pa.change_billing_schedule("Quarterly") invoices = Invoice.query.filter_by(policy_id=self.policy.id)\ .filter(Invoice.deleted == False)\ .order_by(Invoice.bill_date).all() self.payments.append( pa.make_payment(contact_id=self.policy.named_insured, date_cursor=invoices[1].bill_date, amount=600)) self.assertEquals( pa.return_account_balance(date_cursor=invoices[1].bill_date), 0)
def test_change_billing_schedule_q_m_p(self): """ Test change billing schedule from quarterly to monthly when already paid first invoices """ self.policy.billing_schedule = "Quarterly" # No invoices currently exist self.assertFalse(self.policy.invoices) # Invoices should be made when the class is initiated pa = PolicyAccounting(self.policy.id) self.assertEquals(len(self.policy.invoices), 4) self.assertEquals(self.policy.invoices[1].amount_due, self.policy.annual_premium / 4) # Make payment self.payments.append( pa.make_payment(self.policy.agent, date(2015, 1, 1), 300)) # Change billing schedulte to Monthly pa.change_billing_schedule('Monthly') # Assert that total 13 invoices exist (4 old, 9 new) self.assertEquals(len(self.policy.invoices), 13)
def test_evaluate_cancellation_pending(self): policy = Policy('Test Policy', date(2015, 1, 1), 1200) policy.named_insured = self.test_insured.id policy.agent = self.test_agent.id self.policies.append(policy) db.session.add(policy) db.session.commit() # Get the policy from the database policy.billing_schedule = "Annual" pa = PolicyAccounting(policy.id) # Evaluate status on eff date self.assertFalse(pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=date(2015, 1, 1))) # Evaluate status on due date self.assertFalse(pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=date(2015, 2, 1))) # Evaluate status after due date and before cancel date self.assertTrue(pa.evaluate_cancellation_pending_due_to_non_pay(date_cursor=date(2015, 2, 2))) # Need to test this with a different billing schedule to ensure that overlapping invoices # are not causing errors. policy.billing_schedule = "Monthly" pa.make_invoices() # Make a payment self.payments.append(pa.make_payment(date_cursor=date(2015, 01, 01), amount=100))
def test_make_payment(self): pa = PolicyAccounting(self.test_policy.id) self.payments_made.append( pa.make_payment(self.test_policy.agent, datetime.now().date(), 1200)) self.assertEqual(1200, self.payments_made[0].amount_paid)
class TestCancellationPolicies(unittest.TestCase): @classmethod def setUpClass(cls): cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 1, 1), 1600) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id cls.policy.billing_schedule = "Quarterly" db.session.add(cls.policy) db.session.commit() @classmethod def tearDownClass(cls): db.session.delete(cls.test_insured) db.session.delete(cls.test_agent) db.session.delete(cls.policy) db.session.commit() def setUp(self): self.payments = [] self.pa = PolicyAccounting(self.policy.id) self.invoices = Invoice.query.filter_by( policy_id=self.policy.id).order_by(Invoice.bill_date).all() self.payments.append( self.pa.make_payment(contact_id=self.policy.named_insured, date_cursor=self.invoices[0].bill_date, amount=400)) self.pa.evaluate_cancel = MagicMock( side_effect=self.pa.evaluate_cancel) def tearDown(self): for invoice in self.policy.invoices: db.session.delete(invoice) for payment in self.payments: db.session.delete(payment) db.session.commit() def test_Given_unpaid_policy_When_cancellation_pending_before_cancel_date_Then_not_due_to_cancel( self): # evaluation date it's 10 days after due_date evaluation_date = self.invoices[1].due_date + relativedelta(days=10) result = self.pa.evaluate_cancellation_pending_due_to_non_pay( date_cursor=evaluation_date) self.pa.evaluate_cancel.assert_called_with(evaluation_date) self.assertIsNotNone(result) self.assertFalse(result) def test_Given_unpaid_policy_When_cancellation_pending_past_cancel_date_Then_due_to_cancel( self): # evaluation date it's 20 days after due_date evaluation_date = self.invoices[1].due_date + relativedelta(days=20) result = self.pa.evaluate_cancellation_pending_due_to_non_pay( date_cursor=evaluation_date) self.assertIsNotNone(result) self.assertTrue(result) def test_Given_paid_policy_after_bill_date_When_paid_before_cancel_date_Then_not_due_to_cancel( self): # the policy has been paid 8 days after due date self.payments.append( self.pa.make_payment(contact_id=self.policy.named_insured, date_cursor=self.invoices[1].due_date + relativedelta(days=8), amount=400)) evaluation_date = self.invoices[1].due_date + relativedelta(days=20) result = self.pa.evaluate_cancellation_pending_due_to_non_pay( date_cursor=evaluation_date) self.pa.evaluate_cancel.assert_called_with(evaluation_date) self.assertIsNotNone(result) self.assertFalse(result) def test_Given_policy_to_cancel_When_policy_canceled_Then_policy_data_updated( self): pa = PolicyAccounting(self.policy.id) pa.cancel_policy("underwriting") self.assertEqual(pa.policy.status, 'Canceled') self.assertEqual(pa.policy.reason, "underwriting") self.assertEqual(pa.policy.date_changed, datetime.now().date())
def test_make_payment_during_pending_cancellation_due_to_nonpay(self): pa = PolicyAccounting(self.test_policy.id) self.payments_made.append( pa.make_payment(self.test_policy.agent, date(2015, 2, 2), 1200)) self.assertEquals(len(self.payments_made), 1)
class TestCancelPolicy(unittest.TestCase): @classmethod def setUpClass(cls): cls.test_agent = Contact("Test Agent", "Agent") cls.test_insured = Contact("Test Insured", "Named Insured") db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() @classmethod def tearDownClass(cls): db.session.delete(cls.test_insured) db.session.delete(cls.test_agent) db.session.commit() def setUp(self): self.policy = Policy("Test Policy", date(2015, 1, 1), 1200) self.policy.named_insured = self.test_insured.id self.policy.agent = self.test_agent.id self.policy.billing_schedule = "Quarterly" db.session.add(self.policy) db.session.commit() self.pa = PolicyAccounting(self.policy.id) self.payments = [] def tearDown(self): for invoice in self.policy.invoices: db.session.delete(invoice) for payment in self.payments: db.session.delete(payment) db.session.delete(self.policy) db.session.commit() def test_cancel_cancelable_policy(self): self.pa.cancel_policy() self.assertEquals(self.policy.status, "Canceled") self.assertEquals(self.policy.status_change_description, None) self.assertNotEqual(self.policy.status_change_date, None) def test_cancel_cancelable_policy_with_date(self): date = datetime.now().date() self.pa.cancel_policy(date_cursor=date) self.assertEquals(self.policy.status, "Canceled") self.assertEquals(self.policy.status_change_description, None) self.assertEquals(self.policy.status_change_date, date) def test_cancel_cancelable_policy_with_description(self): description = "Test description" self.pa.cancel_policy(description=description) self.assertEquals(self.policy.status, "Canceled") self.assertEquals(self.policy.status_change_description, description) self.assertNotEqual(self.policy.status_change_date, None) def test_cancel_cancelable_policy_with_description_and_date(self): date = datetime.now().date() description = "Test description" self.pa.cancel_policy(description=description) self.assertEquals(self.policy.status, "Canceled") self.assertEquals(self.policy.status_change_description, description) self.assertEquals(self.policy.status_change_date, date) def test_cancel_uncancelable_policy(self): for invoice in self.policy.invoices: self.payments.append( self.pa.make_payment( amount=invoice.amount_due, date_cursor=invoice.bill_date ) ) self.pa.cancel_policy() self.assertEquals(self.policy.status, "Active") self.assertEquals(self.policy.status_change_description, None) self.assertEquals(self.policy.status_change_date, None)
class TestChangeBillingSchedule(unittest.TestCase): @classmethod def setUpClass(cls): cls.test_agent = Contact("Test Agent", "Agent") cls.test_insured = Contact("Test Insured", "Named Insured") db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() @classmethod def tearDownClass(cls): db.session.delete(cls.test_insured) db.session.delete(cls.test_agent) db.session.commit() def setUp(self): self.policy = Policy("Test Policy", date(2015, 1, 1), 1200) self.policy.named_insured = self.test_insured.id self.policy.agent = self.test_agent.id self.policy.billing_schedule = "Quarterly" db.session.add(self.policy) db.session.commit() self.pa = PolicyAccounting(self.policy.id) invoice = self.policy.invoices[0] self.payment = self.pa.make_payment( contact_id=self.policy.named_insured, date_cursor=invoice.bill_date, amount=invoice.amount_due, ) def tearDown(self): for invoice in self.policy.invoices: db.session.delete(invoice) db.session.delete(self.payment) db.session.delete(self.policy) db.session.commit() def test_valid_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Monthly") self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 16) self.assertEquals(self.policy.billing_schedule, "Monthly") for invoice in self.policy.invoices: if invoice not in old_invoices: self.assertEquals(invoice.amount_due, 100) self.assertEquals(invoice.policy_id, self.policy.id) self.assertEquals(invoice.deleted, False) for invoice in old_invoices: self.assertEquals(invoice.deleted, True) def test_invalid_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Invalid") self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def test_empty_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) old_invoices = self.policy.invoices self.assertEquals(len(self.policy.invoices), 4) self.pa.change_billing_schedule() self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def test_same_billing_schedule(self): self.assertEquals(self.pa.return_account_balance(), 900) self.assertEquals(len(self.policy.invoices), 4) old_invoices = self.policy.invoices self.pa.change_billing_schedule("Quarterly") self.assertEquals(self.pa.return_account_balance(), 900) self._old_data_remains_unchanged(old_invoices) def _old_data_remains_unchanged(self, old_invoices): self.assertEquals(len(self.policy.invoices), 4) self.assertEquals(self.policy.billing_schedule, "Quarterly") self.assertEquals(old_invoices, self.policy.invoices)
class TestChangingPolicy(unittest.TestCase): @classmethod def setUpClass(cls): cls.test_agent = Contact('Test Agent', 'Agent') cls.test_insured = Contact('Test Insured', 'Named Insured') db.session.add(cls.test_agent) db.session.add(cls.test_insured) db.session.commit() cls.policy = Policy('Test Policy', date(2015, 1, 1), 1600) cls.policy.named_insured = cls.test_insured.id cls.policy.agent = cls.test_agent.id db.session.add(cls.policy) db.session.commit() @classmethod def tearDownClass(cls): db.session.rollback() db.session.delete(cls.test_insured) db.session.delete(cls.test_agent) db.session.delete(cls.policy) db.session.commit() pass def setUp(self): self.payments = [] self.policy.billing_schedule = 'Quarterly' self.pa = PolicyAccounting(self.policy.id) self.pa.make_invoices = MagicMock(side_effect=self.pa.make_invoices) def tearDown(self): for invoice in self.policy.invoices: db.session.delete(invoice) for payment in self.payments: db.session.delete(payment) db.session.commit() def test_Given_policy_When_policy_is_changed_Then_old_policy_marked_deleted( self): result = self.pa.change_policy(schedule='Monthly', date_cursor=date(2015, 3, 1)) self.pa.make_invoices.assert_called_with(True, 9) invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .filter(Invoice.bill_date < date(2015, 3, 1)) \ .order_by(Invoice.bill_date).all() for invoice in invoices: self.assertTrue(invoice.deleted) def test_Given_policy_When_policy_is_changed_Then_invoices_are_added(self): already_paid_amount = 400 self.payments.append( self.pa.make_payment(contact_id=self.policy.named_insured, date_cursor=date(2015, 1, 1), amount=already_paid_amount)) expected_amount_due = (self.policy.annual_premium - already_paid_amount) / 9 result = self.pa.change_policy(schedule='Monthly', date_cursor=date(2015, 3, 1)) self.pa.make_invoices.assert_called_with(True, 9) invoices = Invoice.query.filter_by(policy_id=self.policy.id) \ .filter(Invoice.deleted != True) \ .order_by(Invoice.bill_date).all() total_invoices_for_the_policy = 9 self.assertEqual(total_invoices_for_the_policy, len(invoices)) invoice_count = 0 for invoice in filter( lambda x: x.bill_date >= date(2015, 3, 1) or x.deleted == True, invoices): invoice_count += 1 self.assertEqual(expected_amount_due, invoice.amount_due) number_of_invoices_with_new_price = 9 self.assertEqual(number_of_invoices_with_new_price, invoice_count) def test_Given_policy_When_policy_is_changed_Then_data_is_consistent(self): result = self.pa.change_policy(schedule='Monthly', date_cursor=date(2015, 3, 1)) self.pa.make_invoices.assert_called_with(True, 9) self.assertEqual(self.test_insured.id, result.named_insured) self.assertEqual(self.test_agent.id, result.agent) self.assertEqual('Monthly', self.policy.billing_schedule)