Skip to content

Commit

Permalink
(BSR)[API] fix: number of query when booking an offer (flaky test_cre…
Browse files Browse the repository at this point in the history
…ate_booking)
  • Loading branch information
prouzet-pass authored and prouzet committed Aug 9, 2024
1 parent 86b8984 commit 3fe544a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 7 additions & 1 deletion api/src/pcapi/core/offers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,13 @@ def get_and_lock_stock(stock_id: int) -> models.Stock:
# This is required to prevent bugs due to concurrent access
# Also call `populate_existing()` to make sure we don't use something
# older from the SQLAlchemy's session.
stock = models.Stock.query.filter_by(id=stock_id).populate_existing().with_for_update().one_or_none()
stock = (
models.Stock.query.filter_by(id=stock_id)
.populate_existing()
.with_for_update()
.options(sa.orm.joinedload(models.Stock.offer, innerjoin=True))
.one_or_none()
)
if not stock:
raise exceptions.StockDoesNotExist()
return stock
Expand Down
13 changes: 7 additions & 6 deletions api/tests/core/bookings/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,26 @@ class BookOfferTest:
def test_create_booking(self, mocked_async_index_offer_ids, app):
beneficiary = users_factories.BeneficiaryGrant18Factory()
stock = offers_factories.StockFactory(price=10, dnBookedQuantity=5, offer__bookingEmail="[email protected]")
stock_id = stock.id

# There is a different email for the first venue booking
bookings_factories.BookingFactory(stock=stock)

# 2 - SELECT the stock (twice ??)
# 1 - SELECT the stock
# 1 - SELECT the offer
# 1 - SELECT the booking
# 1 - SELECT the stock FOR UPDATE (joined with offer)
# 2 - SELECT the user + SELECT FOR UPDATE
# 1 - SELECT COUNT reservations
# 1 - SELECT the venue
# 1 - SELECT offerer address
# 1 - SELECT offerer
# 1 - SELECT user's deposit
# 1 - SELECT the bookings not cancelled
# 1 - SELECT EXISTS on the booking's token
# 1 - UPDATE dnBookedQuantity
# 1 - INSERT the new booking
# 1 - SELECT the user
# 8 - SELECT the stock, booking, external_booking, offer, stock, venue, offerer, provider
# 7 - SELECT the stock, booking, external_booking, stock, venue, offerer, provider
# 1 - SELECT venue with bank activation & bank account
# 1 - SELECT activation code
# 1 - SELECT criterion
Expand All @@ -182,9 +184,8 @@ def test_create_booking(self, mocked_async_index_offer_ids, app):
# 1 - SELECT from offer that I don't get
# 1 - SELECT bookings for the venue ???
# 1 - SELECT feature
# 1 - one query I missed somewhere
with assert_num_queries(37):
booking = api.book_offer(beneficiary=beneficiary, stock_id=stock.id, quantity=1)
with assert_num_queries(35):
booking = api.book_offer(beneficiary=beneficiary, stock_id=stock_id, quantity=1)

# One request should have been sent to Batch to trigger the event
# HAS_BOOKED_OFFER, and another one with the user's
Expand Down
4 changes: 2 additions & 2 deletions api/tests/routes/native/v1/bookings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ def test_cancel_booking(self, client):
booking = booking_factories.BookingFactory(user=user)

client = client.with_token(self.identifier)
with assert_num_queries(29):
with assert_num_queries(27):
response = client.post(f"/native/v1/bookings/{booking.id}/cancel")

assert response.status_code == 204
Expand All @@ -972,7 +972,7 @@ def test_cancel_booking_trigger_recredit_event(self, client):
booking = booking_factories.BookingFactory(user=user)

client = client.with_token(self.identifier)
with assert_num_queries(29):
with assert_num_queries(27):
response = client.post(f"/native/v1/bookings/{booking.id}/cancel")

assert response.status_code == 204
Expand Down

0 comments on commit 3fe544a

Please sign in to comment.