def test_queries_performance(self, app) -> None: now = datetime.utcnow() two_months_ago = now - timedelta(days=60) book = ProductFactory(type=str(offer_type.ThingType.LIVRE_EDITION)) user = UserFactory() BookingFactory.create_batch(size=10, stock__offer__product=book, dateCreated=two_months_ago, user=user) n_queries = ( 1 # select count + 1 # select initial ids + 1 # release savepoint/COMMIT + 4 * 3 # update, release savepoint/COMMIT, select next ids ) with assert_num_queries(n_queries): handle_expired_bookings.cancel_expired_bookings(batch_size=3)
def test_pc_send_tomorrow_events_notifications_only_to_individual_bookings_users( ): """ Test that each stock that is linked to an offer that occurs tomorrow and creates a job that will send a notification to all of the stock's users with a valid (not cancelled) booking, for individual bookings only. """ tomorrow = datetime.now() + timedelta(days=1) stock_tomorrow = EventStockFactory(beginningDatetime=tomorrow, offer__name="my_offer") begin = datetime.now() + timedelta(days=7) stock_next_week = EventStockFactory(beginningDatetime=begin) bookings_tomorrow = IndividualBookingFactory.create_batch( 2, stock=stock_tomorrow, isCancelled=False) BookingFactory.create_batch(2, stock=stock_tomorrow, isCancelled=True, status=BookingStatus.CANCELLED) BookingFactory.create_batch(2, stock=stock_next_week, isCancelled=False) EducationalBookingFactory.create_batch(2, stock=stock_tomorrow, isCancelled=False) pc_send_tomorrow_events_notifications() assert len(testing.requests) == 1 assert all(data["message"]["title"] == "my_offer, c'est demain !" for data in testing.requests) user_ids = set() for data in testing.requests: for user_id in data["user_ids"]: user_ids.add(user_id) expected_user_ids = { booking.individualBooking.userId for booking in bookings_tomorrow } assert user_ids == expected_user_ids