def test_get_list_with_valid_venue_id(self, app): # given user = users_factories.ProFactory() titelive_things_provider = get_provider_by_local_class("TiteLiveThings") venue_provider = offerers_factories.VenueProviderFactory( venue__name="Librairie Titelive", provider=titelive_things_provider, lastSyncDate=datetime(2021, 8, 16) ) # test that nOffers only containe active offers offers_factories.OfferFactory( venue=venue_provider.venue, lastProviderId=venue_provider.provider.id, idAtProviders="testIdAtProviders1", isActive=True, ) offers_factories.OfferFactory( venue=venue_provider.venue, lastProviderId=venue_provider.provider.id, idAtProviders="testIdAtProviders2", isActive=False, ) # when auth_request = TestClient(app.test_client()).with_session_auth(email=user.email) response = auth_request.get("/venueProviders?venueId=" + humanize(venue_provider.venue.id)) # then assert response.status_code == 200 assert response.json["venue_providers"][0].get("nOffers") == 1 assert response.json["venue_providers"][0].get("id") == humanize(venue_provider.id) assert response.json["venue_providers"][0].get("venueId") == humanize(venue_provider.venue.id) assert response.json["venue_providers"][0].get("lastSyncDate") == "2021-08-16T00:00:00Z"
def test_should_iterate_over_2_zip_files( self, mock_get_date_from_filename, mock_get_zip_file_from_ftp, mock_get_files_to_process_from_titelive): # Given titelive_description_provider = get_provider_by_local_class( "TiteLiveThingDescriptions") mock_get_files_to_process_from_titelive.return_value = [ "Resume191012.zip", "Resume191013.zip" ] mock_get_zip_file_from_ftp.side_effect = [ MockZipFile(filename="Resume191012.zip"), MockZipFile(filename="Resume191013.zip"), ] mock_get_date_from_filename.side_effect = {"191012", "191013"} repository.save(titelive_description_provider) titelive_description_provider = TiteLiveThingDescriptions() # When with pytest.raises(StopIteration): next(titelive_description_provider) # Then mock_get_files_to_process_from_titelive.assert_called_once_with( "ResumesLivres", re.compile(r"Resume(\d{6}).zip")) assert mock_get_zip_file_from_ftp.call_count == 2 assert mock_get_date_from_filename.call_count == 3
def synchronize_stocks_job(serialized_stock_details: list[Union[dict, StockDetail]], venue_id: str) -> None: pc_provider = get_provider_by_local_class(PASS_CULTURE_STOCKS_PROVIDER_NAME) # The worker is currently paused. In the queue there are both StockDetail and dict format # TODO(viconnex): remove StockDetail formatted case when the queue is empty stock_details = [ stock_detail if isinstance(stock_detail, StockDetail) else StockDetail( products_provider_reference=stock_detail["products_provider_reference"], offers_provider_reference=stock_detail["offers_provider_reference"], stocks_provider_reference=stock_detail["stocks_provider_reference"], venue_reference=stock_detail["stocks_provider_reference"], available_quantity=stock_detail["available_quantity"], price=stock_detail["price"], ) for stock_detail in serialized_stock_details ] venue = find_venue_by_id(venue_id) operations = api.synchronize_stocks(stock_details, venue, provider_id=pc_provider.id) logger.info( "Processed stocks synchronization", extra={ "venue": venue_id, "stocks": len(stock_details), **operations, }, )
def test_update_1_thing_from_one_data_line_in_one_file( self, get_lines_from_thing_file, get_files_to_process_from_titelive_ftp, app ): # Given files_list = list() files_list.append("Quotidien30.tit") get_files_to_process_from_titelive_ftp.return_value = files_list data_line = "~".join(BASE_DATA_LINE_PARTS) get_lines_from_thing_file.return_value = iter([data_line]) titelive_things_provider = get_provider_by_local_class("TiteLiveThings") product = create_product_with_thing_subcategory( id_at_providers="9782895026310", thing_name="Toto à la playa", date_modified_at_last_provider=datetime(2001, 1, 1), last_provider_id=titelive_things_provider.id, ) activate_provider("TiteLiveThings") repository.save(product) titelive_things = TiteLiveThings() # When titelive_things.updateObjects() # Then updated_product = Product.query.first() assert updated_product.name == "nouvelles du Chili" assert updated_product.extraData.get("bookFormat") == BookFormat.BEAUX_LIVRES.value
def delete_corrupted_allocine_stocks(): new_stock_id_at_providers_format = "%#%" allocine_provider = get_provider_by_local_class("AllocineStocks") stocks_to_delete = (Stock.query.filter_by( lastProviderId=allocine_provider.id ).filter_by(isSoftDeleted=True).filter( Stock.idAtProviders.notilike(new_stock_id_at_providers_format)).all()) repository.delete(*stocks_to_delete)
def __init__(self, venue_provider=None, **options): self.venue_provider = venue_provider self.updatedObjects = 0 self.createdObjects = 0 self.checkedObjects = 0 self.erroredObjects = 0 self.createdThumbs = 0 self.updatedThumbs = 0 self.checkedThumbs = 0 self.erroredThumbs = 0 self.provider = get_provider_by_local_class(self.__class__.__name__)
def test_should_return_provider_matching_local_class(self, app): # Given existing_provider = create_provider(local_class="OpenAgenda", is_active=True, is_enable_for_pro=True) repository.save(existing_provider) # When provider = get_provider_by_local_class("OpenAgenda") # Then assert provider == existing_provider
def save_allocine_sandbox() -> None: sirene = Sirene() create_industrial_admin_users() pro = users_factories.ProFactory( firstName="Didier", lastName="Champion", publicName="Didier Champion", email="*****@*****.**", ) offerer = create_offerer( address="145, rue Chaplin", city="Paris 17", name="Le Royal - Cinéma d'essai", postal_code="75017", siren=sirene.siren, ) user_offerer = create_user_offerer( offerer=offerer, user=pro, ) venue = create_venue( offerer, address=offerer.address, booking_email="*****@*****.**", city=offerer.city, comment="Salle de cinéma", name=offerer.name + " - Salle 1", postal_code=offerer.postalCode, siret=sirene.siret, ) activate_provider("AllocineStocks") provider = get_provider_by_local_class("AllocineStocks") venue_provider = create_venue_provider(venue, provider=provider, is_active=True) repository.save(offerer, user_offerer, venue, provider, venue_provider) offer = create_offer_with_event_product( venue, event_subcategory_id=subcategories.SEANCE_CINE.id, last_provider_id=provider.id, id_at_providers="TW92aWU6MjQ4MTAy%34007977100028-VF", last_provider=provider, ) repository.save(offer)
def when_listing_all_venues_without_venue_id_argument(self, app): # given user = users_factories.ProFactory() titelive_things_provider = get_provider_by_local_class("TiteLiveThings") offerers_factories.VenueProviderFactory( venue__name="Librairie Titelive", provider=titelive_things_provider, ) # when auth_request = TestClient(app.test_client()).with_session_auth(email=user.email) response = auth_request.get("/venueProviders") # then assert response.status_code == 400
def test_should_add_price_rules_to_venue_provider(self, app): # Given offerer = create_offerer() venue = create_venue(offerer) allocine_provider = get_provider_by_local_class("AllocineStocks") allocine_provider.isActive = True allocine_venue_provider = create_allocine_venue_provider( venue, allocine_provider) allocine_venue_provider_price_rule = create_allocine_venue_provider_price_rule( allocine_venue_provider, price_rule=PriceRule.default, price=10) # When repository.save(allocine_venue_provider_price_rule) # Then assert len(allocine_venue_provider.priceRules) == 1
def install_local_providers(): # It is a lot easier in tests when all providers are active by # default (and manually inactivated if a particular test needs # that). activate_and_enable_for_pro = settings.IS_RUNNING_TESTS for class_name in pcapi.local_providers.__all__: provider_class = getattr(pcapi.local_providers, class_name) db_provider = get_provider_by_local_class(class_name) if not db_provider: provider = Provider() provider.name = provider_class.name provider.localClass = class_name provider.isActive = activate_and_enable_for_pro provider.enabledForPro = activate_and_enable_for_pro db.session.add(provider) db.session.commit()
def test_should_not_delete_stock_from_allocine_when_not_sof_deleted(app): # Given offerer = create_offerer() venue = create_venue(offerer) allocine_provider = get_provider_by_local_class("AllocineStocks") offer = create_offer_with_thing_product(venue) stock = create_stock( id_at_providers="TW92aWU6MjczNjU5%38986972800011-1", is_soft_deleted=False, last_provider_id=allocine_provider.id, offer=offer, ) repository.save(stock) # When delete_corrupted_allocine_stocks() # Then assert Stock.query.count() == 1
def test_should_raise_error_when_saving_wrong_format_price(self, app): # Given offerer = create_offerer() venue = create_venue(offerer) allocine_provider = get_provider_by_local_class("AllocineStocks") allocine_venue_provider = create_allocine_venue_provider( venue, allocine_provider) price = "wrong_price_format" venue_provider_price_rule = create_allocine_venue_provider_price_rule( allocine_venue_provider, price_rule=PriceRule.default, price=price) # When with pytest.raises(ApiErrors) as error: repository.save(venue_provider_price_rule) # Then assert error.value.errors == { "global": ["Le prix doit être un nombre décimal"] }
def test_should_raise_error_when_price_is_negative(self, app): # Given offerer = create_offerer() venue = create_venue(offerer) allocine_provider = get_provider_by_local_class("AllocineStocks") allocine_provider.isActive = True allocine_venue_provider = create_allocine_venue_provider( venue, allocine_provider) venue_provider_price_rule = create_allocine_venue_provider_price_rule( allocine_venue_provider, price_rule=PriceRule.default, price=-4) # When with pytest.raises(ApiErrors) as error: repository.save(venue_provider_price_rule) # Then assert error.value.errors["global"] == [ "Vous ne pouvez renseigner un prix négatif" ]
def test_should_call_titelive_ftp_to_get_files_list( self, mock_get_files_to_process_from_titelive, app): # Given titelive_description_provider = get_provider_by_local_class( "TiteLiveThingDescriptions") mock_get_files_to_process_from_titelive.return_value = [ "Resume191012.zip", "Resume201012.zip" ] repository.save(titelive_description_provider) # When titelive_description_provider = TiteLiveThingDescriptions() # Then mock_get_files_to_process_from_titelive.assert_called_once_with( "ResumesLivres", re.compile(r"Resume(\d{6}).zip")) titelive_description_provider.zips = [ "Resume191012.zip", "Resume201012.zip" ]
def test_should_not_delete_stock_from_allocine_with_new_id_format(app): # Given offerer = create_offerer() venue = create_venue(offerer) allocine_provider = get_provider_by_local_class("AllocineStocks") offer = create_offer_with_thing_product(venue) stock = create_stock( id_at_providers="TW92aWU6MjczNTc5%31940406700021#LOCAL/2020-01-18T14:00:00", is_soft_deleted=True, last_provider_id=allocine_provider.id, offer=offer, ) repository.save(stock) # When delete_corrupted_allocine_stocks() # Then assert Stock.query.count() == 1
def test_get_list_that_include_allocine_with_valid_venue_id(self, app): # given user = users_factories.ProFactory() allocine_stocks_provider = get_provider_by_local_class("AllocineStocks") allocine_venue_provider = offerers_factories.AllocineVenueProviderFactory( venue__name="Whatever cinema", provider=allocine_stocks_provider, ) offerers_factories.AllocineVenueProviderPriceRuleFactory( price=123.2, allocineVenueProvider=allocine_venue_provider ) # when auth_request = TestClient(app.test_client()).with_session_auth(email=user.email) response = auth_request.get("/venueProviders?venueId=" + humanize(allocine_venue_provider.venue.id)) # then assert response.status_code == 200 assert response.json["venue_providers"][0].get("id") == humanize(allocine_venue_provider.id) assert response.json["venue_providers"][0].get("venueId") == humanize(allocine_venue_provider.venue.id) assert response.json["venue_providers"][0].get("price") == 123.2
def activate_provider(provider_classname: str) -> Provider: provider = get_provider_by_local_class(provider_classname) provider.isActive = True provider.enabledForPro = True repository.save(provider) return provider
def test_should_not_return_provider_when_no_local_class_matches(self, app): # When provider = get_provider_by_local_class("NonExistingProvider") # Then assert provider is None
def synchronize_allocine_stocks() -> None: allocine_stocks_provider_id = get_provider_by_local_class( "AllocineStocks").id synchronize_venue_providers_for_provider(allocine_stocks_provider_id)