def test_update_wallet_post_delete_card(self, mocked_handler): post_delete.connect(mocked_handler, sender=CreditCard, dispatch_uid='test_handler') card = CreditCardFactory(wallet=self.wallet) card.delete() self.assertTrue(mocked_handler.called) self.assertEquals(mocked_handler.call_count, 1)
def test_new_purchase_update_wallet(self, mocked_payment): credit_card = CreditCardFactory(wallet=self.wallet) self.assertEqual(self.wallet.credit_available, Decimal('500.00')) mock_purchase = Mock(value=Decimal('20.00'), wallet=self.wallet) new_purchase_update_wallet(mock_purchase) self.assertEqual(self.wallet.credit_available, Decimal('480.00')) credit_card.refresh_from_db() self.assertEqual(credit_card.available, self.wallet.credit_available)
def test_obtain_bill_next_month(self): user = UserFactory() wallet = WalletFactory(user=user) credit_card = CreditCardFactory(wallet=wallet) with freeze_time('2018-10-10'): payment_value = Decimal('200.00') payment = Payment( purchase=None, credit_card=credit_card, value=payment_value ) payment_two = Payment( purchase=None, credit_card=credit_card, value=payment_value ) generate_or_obtain_bill(payment) generate_or_obtain_bill(payment_two) self.assertEqual(Bill.objects.count(), 1) generated_bill = Bill.objects.get(id=1) self.assertEqual(generated_bill.value, payment_value * 2) self.assertEqual(generated_bill.expires_at.month, 11)
def setUp(self): self.user = UserFactory() self.wallet = WalletFactory(user=self.user) self.card_one = CreditCardFactory(wallet=self.wallet, number='4916293874816172') self.card_two = CreditCardFactory(wallet=self.wallet, number='4349469061007652') self.bill_one = BillFactory(id=1, credit_card=self.card_one, expires_at=date(2018, 10, 8)) self.bill_two = BillFactory(id=2, credit_card=self.card_two, expires_at=date(2018, 10, 9)) self.bill_three = BillFactory(id=3, credit_card=self.card_two, expires_at=date(2018, 10, 10))
def test_user_creditcard_list(self, logger_mock): wallet = WalletFactory(user=self.user) CreditCardFactory( wallet=wallet, number='4729333912967715', ) CreditCardFactory( wallet=wallet, number='4729333922967715', ) self.client.force_authenticate(self.user) response = self.client.get( reverse('wallet-creditcards-list', args=[wallet.id])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json()['count'], 2) logger_mock.assert_called()
def test_card_delete_update_wallet(self): credit_card = CreditCardFactory(wallet=self.wallet, number='1111111111111111', monthly_billing_day=10, limit=Decimal('50.00')) self.assertEqual(self.wallet.credit_available, Decimal('50.00')) card_delete_update_wallet(credit_card) self.assertEqual(self.wallet.credit_available, Decimal('0.00'))
def test_update_credit_post_bill_paid(self, mocked_handler): update_credit.connect(mocked_handler, sender=Bill, dispatch_uid='test_handler') bill = BillFactory(credit_card=CreditCardFactory(wallet=self.wallet)) pay_bill(bill, Decimal('100.00')) self.assertTrue(mocked_handler.called) self.assertEquals(mocked_handler.call_count, 1)
def test_update_bill_post_payment(self, mocked_handler): post_save.connect(mocked_handler, sender=CreditCard, dispatch_uid='test_handler') CreditCardFactory(wallet=self.wallet) self.assertTrue(mocked_handler.called) self.assertEquals(mocked_handler.call_count, 2)
def test_bill_paid_update_credit_card(self): credit_card = CreditCardFactory(wallet=self.wallet, number='1111111111111111', monthly_billing_day=10, limit=Decimal('50.00')) bill = BillFactory(credit_card=credit_card) self.assertEqual(credit_card.available, Decimal('50.00')) bill_paid_update_credit_card(bill, Decimal('100.00')) self.assertEqual(credit_card.available, Decimal('150.00'))
def test_purchase_create(self, logger_mock): CreditCardFactory(wallet=self.wallet) self.client.force_authenticate(self.user) response = self.client.post(reverse('purchases-list'), { 'wallet': self.wallet.id, 'value': '100.00' }, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Purchase.objects.count(), 1) logger_mock.assert_called()
def test_user_creditcard_delete(self, logger_mock): wallet = WalletFactory(user=self.user) credit_card = CreditCardFactory( wallet=wallet, number='4729333912967715', ) self.client.force_authenticate(self.user) response = self.client.delete( reverse('wallet-creditcards-detail', args=[wallet.id, credit_card.id])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) logger_mock.assert_called()
def test_purchase_invalid_value(self, logger_mock): CreditCardFactory(wallet=self.wallet) self.client.force_authenticate(self.user) response = self.client.post(reverse('purchases-list'), { 'wallet': self.wallet.id, 'value': '1999.00' }, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn("Value exceeds your credit available.", str(response.json())) logger_mock.assert_called()
def test_set_payment_records_split_between_cards(self, mocked_payment): credit_card_one = CreditCardFactory(wallet=self.wallet, number='1111111111111111', monthly_billing_day=10, limit=Decimal('50.00')) credit_card_two = CreditCardFactory(wallet=self.wallet, number='1111111111111110', monthly_billing_day=10, limit=Decimal('60.00')) mock_purchase = Mock(value=Decimal('100.00'), wallet=self.wallet) self.assertEqual(credit_card_one.available, credit_card_one.limit) self.assertEqual(credit_card_two.available, credit_card_two.limit) set_payment_records(self.wallet.credit_cards.all(), mock_purchase) credit_card_one.refresh_from_db() credit_card_two.refresh_from_db() self.assertEqual(credit_card_one.available, Decimal('0')) self.assertEqual(credit_card_two.available, Decimal('10.0'))
def setUp(self): self.user = UserFactory() self.wallet = WalletFactory(user=self.user) self.credit_card = CreditCardFactory(wallet=self.wallet, number='4729333912967716') self.bill = BillFactory(credit_card=self.credit_card)