def test_order_checkout_insufficient_balance(self): total_cost = float( ep.orders_find(self.order2['order_id']).json()['total_cost']) old_balance = float( ep.users_find(self.user2['user_id']).json()['credit']) res = ep.orders_checkout(self.order2['order_id']) new_balance = float( ep.users_find(self.user2['user_id']).json()['credit']) self.assertFalse(res.ok) self.assertLessEqual(old_balance, total_cost) self.assertEqual(old_balance, new_balance)
def test_order_checkout_enough_balance_payment(self): total_cost = float( ep.orders_find(self.order1['order_id']).json()['total_cost']) old_balance = float( ep.users_find(self.user1['user_id']).json()['credit']) res = ep.orders_checkout(self.order1['order_id']) new_balance = float( ep.users_find(self.user1['user_id']).json()['credit']) self.assertTrue( res.ok, f"total_cost: {total_cost}, old_balance: {old_balance}, new_balance: {new_balance}" ) self.assertGreaterEqual(old_balance, total_cost) self.assertEqual(new_balance, old_balance - total_cost)
def test_payment_insufficient_funds(self): status_before = ep.payment_status(self.order2['order_id']) self.assertFalse(status_before.ok) balance_before = ep.users_find(self.user1['user_id']).json()['credit'] res = ep.payment_pay(self.user2['user_id'], self.order2['order_id'], self.price) status_after = ep.payment_status(self.order1['order_id']) balance_after = ep.users_find(self.user1['user_id']).json()['credit'] self.assertFalse(res.ok) self.assertFalse(status_after.ok) self.assertEqual(balance_before, balance_after)
def test_insufficient_stock2(self): ep.stock_add(self.item_id, 0) ep.stock_add(self.item_id2, 10) ep.users_credit_add(self.user_id, 500) ep.orders_add_item(self.order_id, self.item_id) ep.orders_add_item(self.order_id, self.item_id2) response = ep.orders_checkout(self.order_id) item1_stock = int(ep.stock_find(self.item_id).json()['stock']) item2_stock = int(ep.stock_find(self.item_id2).json()['stock']) user_credit = Decimal(ep.users_find(self.user_id).json()['credit']) self.assertFalse(response.ok) self.assertEqual(item1_stock, 0) self.assertEqual(item2_stock, 10) self.assertEqual(user_credit, Decimal(500))
def test_happy_flow(self): ep.stock_add(self.item_id, 10) ep.stock_add(self.item_id2, 10) ep.users_credit_add(self.user_id, 500) ep.orders_add_item(self.order_id, self.item_id) ep.orders_add_item(self.order_id, self.item_id2) response = ep.orders_checkout(self.order_id) item1_stock = int(ep.stock_find(self.item_id).json()['stock']) item2_stock = int(ep.stock_find(self.item_id2).json()['stock']) user_credit = Decimal(ep.users_find(self.user_id).json()['credit']) self.assertTrue(response.ok) self.assertEqual(item1_stock, 9) self.assertEqual(item2_stock, 9) self.assertEqual(user_credit, Decimal(493.5))