Hi,
First of all, thank you very much for your tutorial which is very good.
Just in case you would like to know, with new version of SQLAlchemy (I used 1.4.45), your tests cases for order and cart do not pass anylonger whereas it works like a charm with older version version (1.3.24 the one used in your tutorial) .
With new version of SQLAlchemy , the following errors raise:
========================================================================================= short test summary info =========================================================================================
FAILED test/cart/test_cart.py::test_add_to_cart - sqlalchemy.orm.exc.DetachedInstanceError: Instance <Product at 0x14dc9554e80> is not bound to a Session; attribute refresh operation cannot proceed (Ba...
FAILED test/orders/test_orders.py::test_order_processing - sqlalchemy.orm.exc.DetachedInstanceError: Instance <Product at 0x14dcb865df0> is not bound to a Session; attribute refresh operation cannot pr...
.....
sqlalchemy.orm.exc.DetachedInstanceError: Instance <Product at 0x14dcb865df0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3)"
The problem appears when trying to access to newly created product object attributes. For example, in file test_orders.py , function test_order_processing, line 19, in the statement params={'product_id': product_obj.id},
:
async def test_order_processing(mocker):
# mocker.patch('ecommerce.orders.tasks.send_email', return_value=True)
async with AsyncClient(app=app, base_url="http://test") as ac:
user_access_token = create_access_token({"sub": "[email protected]"})
category_obj = await category_info()
product_obj = await product_info(category_obj)
cart_response = await ac.get(f"/cart/add",
params={'product_id': product_obj.id},
headers={'Authorization': f'Bearer {user_access_token}'})
Reading the SQLAlchemy, to make the tests cases work, Session.expire_on_commit shall be set to False.
I did it in file conf_test_db.py:
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine, expire_on_commit=False)
Et voilร :) it works again :)
cheers!