def test_order_remove_item_non_existing_item(self):
        items_before = ep.orders_find(self.order1['order_id']).json()['items']

        res = ep.orders_remove_item(self.order1['order_id'],
                                    self.user1['user_id'])

        items_after = ep.orders_find(self.order1['order_id']).json()['items']

        self.assertFalse(res.ok)
        self.assertEqual(items_before, items_after)
    def test_order_remove_item_existing(self):
        items_before = ep.orders_find(self.order1['order_id']).json()['items']
        res = ep.orders_remove_item(self.order1['order_id'],
                                    self.item1['item_id'])

        items_after = ep.orders_find(self.order1['order_id']).json()['items']

        test = items_before.copy()
        test.remove(self.item1['item_id'])

        self.assertTrue(res.ok)
        self.assertNotEqual(items_before, items_after)
        self.assertCountEqual(test, items_after)
    def test_order_success_paid(self):
        res = ep.orders_find(self.order1['order_id'])

        self.assertTrue(res.ok)
        paid_before = res.json()['paid']

        self.assertEqual(paid_before, 'False')

        res2 = ep.orders_checkout(self.order1['order_id'])
        res3 = ep.orders_find(self.order1['order_id'])

        self.assertTrue(res2.ok)
        self.assertTrue(res3.ok)
        paid_after = res3.json()['paid']

        self.assertEqual(paid_after, 'True')
    def test_order_delete_existing(self):
        res = ep.orders_create(self.user1['user_id'])

        self.assertTrue(res.ok)

        res2 = ep.orders_remove(res.json()['order_id'])

        self.assertTrue(res2.ok)

        res3 = ep.orders_find(res.json()['order_id'])

        self.assertFalse(res3.ok)
    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_order_find_existing(self):
        res = ep.orders_find(self.order1['order_id'])

        order = res.json()

        items = order['items']
        total_cost = 0
        for item in items:
            total_cost += ep.stock_find(item).json()['price']

        self.assertTrue(res.ok)
        self.assertEqual(order['order_id'], self.order1['order_id'])
        self.assertEqual(order['paid'], 'False')
        self.assertIn(self.item1['item_id'], order['items'])
        self.assertIn(self.item2['item_id'], order['items'])
        self.assertEqual(order['total_cost'], str(total_cost))
        self.assertEqual(order['user_id'], self.user1['user_id'])
    def test_order_checkout_insufficient_stock(self):
        items = ep.orders_find(self.order2['order_id']).json()['items']

        stock_dict = {}
        for item in items:
            stock_dict[item] = ep.stock_find(item).json()['stock']

        order_dict = {}
        for item in items:
            if item in order_dict:
                order_dict[item] += 1
            else:
                order_dict[item] = 1

        res = ep.orders_checkout(self.order2['order_id'])

        self.assertFalse(res.ok)

        for item in items:
            self.assertEqual(stock_dict[item],
                             ep.stock_find(item).json()['stock'])
    def test_order_checkout_enough_balance_stock(self):
        items = ep.orders_find(self.order1['order_id']).json()['items']

        stock_dict = {}
        for item in items:
            stock_dict[item] = ep.stock_find(item).json()['stock']

        order_dict = {}
        for item in items:
            if item in order_dict:
                order_dict[item] += 1
            else:
                order_dict[item] = 1

        res = ep.orders_checkout(self.order1['order_id'])

        self.assertTrue(res.ok)

        for item in items:
            self.assertEqual(stock_dict[item] - order_dict[item],
                             ep.stock_find(item).json()['stock'])
    def test_order_find_not_existing(self):
        res = ep.orders_find(self.user1['user_id'])

        self.assertFalse(res.ok)