hi
i have a test that goes like this:
def test_cart_is_saved_after_sign_up(self, client):
client.get("/")
session = client.session
session_cart = CartSession(session)
session_cart.add(self.product1, 2)
where client
comes from pytest-django
and CartSession
is as follows:
class CartSession:
def __init__(self, session):
self.session = session
cart = self.session.get(settings.CART_SESSION_KEY)
if not cart:
cart = self.session[settings.CART_SESSION_KEY] = {}
self.cart: dict[int, dict] = cart
def save(self):
self.session.modified = True
def add(self, product, quantity=1):
if product.id not in self.cart:
self.cart[product.id] = {
"quantity": 0,
"price": product.price,
"discount": product.discount,
"sell_price": product.get_sell_price(),
}
self.cart[product.id]["quantity"] += quantity
self.save()
now when in the test i try to do this:
print(session.session_key)
print(SessionStore(session_key=session.session_key).session_key)
print(session.keys())
print(SessionStore(session_key=session.session_key).keys())
i get the following result
t3n66de197huhuttxh4qr50vh3u21uwl
t3n66de197huhuttxh4qr50vh3u21uwl
dict_keys(['cart'])
dict_keys([])
as you can see, session_key
is the same, but SessionStore
doesn’t return any value
i tried Session.objects.get(pk=session.session_key)
as well, and it returns empty as well