def update_venues_for_specific_provider(provider_id: int): if feature_queries.is_active(FeatureToggle.PARALLEL_SYNCHRONIZATION_OF_VENUE_PROVIDER): _update_venues_in_parallel(provider_id) return venue_providers_to_sync = get_active_venue_providers_for_specific_provider(provider_id) for venue_provider in venue_providers_to_sync: synchronize_venue_provider(venue_provider)
def update_providables(provider_name: str, venue_provider_id: str, limit: int): if (provider_name and venue_provider_id) or not (provider_name or venue_provider_id): raise ValueError("Call either with provider-name or venue-provider-id") if provider_name: synchronize_data_for_provider(provider_name, limit) if venue_provider_id: venue_provider = get_venue_provider_by_id(int(venue_provider_id)) synchronize_venue_provider(venue_provider, limit)
def fully_sync_venue(venue: Venue) -> None: logger.info("Resetting all stock quantity for full resync", extra={"venue": venue.id}) reset_stock_quantity(venue) venue_provider = VenueProvider.query.filter_by(venueId=venue.id, isActive=True).one() venue_provider.lastSyncDate = None repository.save(venue_provider) synchronize_venue_provider(venue_provider)
def test_should_log_exception_when_one_is_raised_during_provider_initialization( self, mock_build_cron_log_message, app): # Given provider_test = AllocineProviderFactory() offerer = create_offerer() venue = create_venue(offerer) venue_provider = create_venue_provider(venue, provider_test) repository.save(venue_provider) # When synchronize_venue_provider(venue_provider, 10) # Then mock_build_cron_log_message.assert_called_once_with( name=provider_test.localClass, status=ANY)
def test_should_start_synchronization_with_linked_provider( self, mock_do_update, mock_get_provider_class, app): # Given limit = 10 offerer = create_offerer() venue = create_venue(offerer) provider = create_provider(local_class="TestLocalProvider") venue_provider = create_venue_provider(venue, provider) repository.save(venue_provider) mock_get_provider_class.return_value = TestLocalProvider # When synchronize_venue_provider(venue_provider, limit) # Then mock_do_update.assert_called_once_with(fake(TestLocalProvider), limit)
def test_should_log_exception_when_one_is_raised_during_provider_initilization( self, mock_build_cron_log_message, mock_get_provider_class, app): # Given provider_test = create_provider(local_class="TestLocalProvider") offerer = create_offerer() venue = create_venue(offerer) venue_provider = create_venue_provider(venue, provider_test) repository.save(venue_provider) mock_get_provider_class.return_value = mock_init_provider # When synchronize_venue_provider(venue_provider, 10) # Then mock_build_cron_log_message.assert_called_once_with( name="mock_init_provider", status=ANY)
def test_should_init_provider_with_expected_venue_provider( self, mock_do_update, mock_get_provider_class, app): # Given limit = 10 offerer = create_offerer() venue = create_venue(offerer) provider = create_provider(local_class="TestLocalProvider") venue_provider = create_venue_provider(venue, provider) repository.save(venue_provider) mock_provider_class = MagicMock() mock_get_provider_class.return_value = mock_provider_class # When synchronize_venue_provider(venue_provider, limit) # Then mock_provider_class.assert_called_once_with(venue_provider)
def fully_sync_library(venue_id: int) -> None: stocks = Stock.query.join(Offer).filter(Offer.venueId == venue_id).filter( Offer.idAtProviders != None).all() stocks_to_update = [] for stock in stocks: stock.quantity = count_not_cancelled_bookings_quantity_by_stock_id( stock.id) stocks_to_update.append(stock) if len(stocks_to_update) >= STOCK_BATCH_UPDATE: repository.save(*stocks_to_update) stocks_to_update = [] repository.save(*stocks_to_update) venue_provider_to_sync = VenueProvider.query.filter( VenueProvider.venueId == venue_id).one() venue_provider_to_sync.lastSyncDate = None repository.save(venue_provider_to_sync) synchronize_venue_provider(venue_provider_to_sync)
def update_providables(provider_name: str, venue_provider_id: str, limit: int): start = time() logger.info( "Starting update_providables with provider_name=%s and venue_provider_id=%s", provider_name, venue_provider_id) if (provider_name and venue_provider_id) or not (provider_name or venue_provider_id): raise ValueError("Call either with provider-name or venue-provider-id") if provider_name: synchronize_data_for_provider(provider_name, limit) if venue_provider_id: venue_provider = get_venue_provider_by_id(int(venue_provider_id)) synchronize_venue_provider(venue_provider, limit) logger.info( "Finished update_providables with provider_name=%s and venue_provider_id=%s elapsed=%.2f", provider_name, venue_provider_id, time() - start, )
def test_should_init_allocine_stocks_provider_with_expected_allocine_venue_provider( self, mock_do_update, mock_get_provider_class, app): # Given offerer = create_offerer() venue = create_venue(offerer) provider = activate_provider("AllocineStocks") allocine_venue_provider = create_allocine_venue_provider(venue, provider, is_duo=True) repository.save(allocine_venue_provider) mock_provider_class = MagicMock() mock_get_provider_class.return_value = mock_provider_class # When synchronize_venue_provider(allocine_venue_provider, None) # Then mock_provider_class.assert_called_once() venue_provider_mock_arg = mock_provider_class.call_args[0][0] assert venue_provider_mock_arg == allocine_venue_provider assert venue_provider_mock_arg.isDuo
def venue_provider_job(venue_provider_id: int) -> None: venue_provider = get_venue_provider_by_id(venue_provider_id) synchronize_venue_provider(venue_provider)
def test_synchronize_venue_provider(self, app): api_url = "https://example.com/provider/api" old_provider = APIProviderFactory(id=1) provider = APIProviderFactory(id=2, apiUrl=api_url) venue_provider = VenueProviderFactory(provider=provider) venue = venue_provider.venue existing_product = offers_factories.ProductFactory( idAtProviders="4321", extraData={"prix_livre": 10}, subcategoryId=subcategories.LIVRE_PAPIER.id, ) offer_id_at_providers = f"{existing_product.idAtProviders}@{venue.siret}" existing_stock = offers_factories.StockFactory( quantity=10, offer__venue=venue, offer__product=existing_product, lastProviderId=old_provider.id, offer__idAtProviders=offer_id_at_providers, idAtProviders=offer_id_at_providers, ) bookings_factories.BookingFactory(stock=existing_stock) product_to_synchronized = offers_factories.ProductFactory( idAtProviders="1234", extraData={"prix_livre": 10}, subcategoryId=subcategories.LIVRE_PAPIER.id, ) with requests_mock.Mocker() as mock: response = { "total": 1, "stocks": [ { "ref": "1234", "available": 5 }, { "ref": "4321", "available": 12 }, ], } mock.get(f"{api_url}/{venue_provider.venueIdAtOfferProvider}", [{ "json": response }, { "json": { "stocks": [] } }]) synchronize_venue_provider(venue_provider) # Check that previously synchronized stock have been updated. assert existing_stock.offer.lastProviderId == provider.id assert existing_stock.quantity == 12 + existing_stock.dnBookedQuantity # Check that offers and stocks have been created. created_offer = Offer.query.filter_by( product=product_to_synchronized).one() assert created_offer.stocks[0].quantity == 5