From 25dafa9cdbf8232cfb78502ddc5d5c4d85405872 Mon Sep 17 00:00:00 2001 From: cnormant-pass Date: Mon, 27 Jan 2025 11:28:25 +0100 Subject: [PATCH] (BSR)[API] test: misc pylint errors in tests --- api/tests/conftest.py | 2 +- api/tests/core/achievements/test_api.py | 2 +- api/tests/core/bookings/test_api.py | 2 +- api/tests/core/bookings/test_utils.py | 2 +- .../external/pro_user_automations_test.py | 2 +- .../core/external_bookings/test_decorator.py | 23 +++++++++++-------- api/tests/core/offerers/test_api.py | 2 +- api/tests/core/offers/test_validation.py | 8 +++++-- api/tests/routes/backoffice/conftest.py | 3 ++- api/tests/routes/native/v1/bookings_test.py | 4 ++-- .../routes/pro/get_offerer_addresses_test.py | 5 +++- api/tests/routes/pro/get_statistics_test.py | 2 +- .../routes/pro/patch_draft_offer_test.py | 4 ++-- .../endpoints/adage_mock/test_bookings.py | 5 +++- .../v1/get_search_addresses_test.py | 2 +- .../individual_offers/v1/post_address_test.py | 2 +- 16 files changed, 43 insertions(+), 27 deletions(-) diff --git a/api/tests/conftest.py b/api/tests/conftest.py index dc1747a5c45..e1921ec9475 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -850,7 +850,7 @@ def db_session(_engine, _transaction, mocker, request): # No need for the fixture, `clean_database` will do the job if "clean_database" in request.fixturenames: - return + return None _, _, _session = _transaction diff --git a/api/tests/core/achievements/test_api.py b/api/tests/core/achievements/test_api.py index 38958c24eca..d6d654f5ac8 100644 --- a/api/tests/core/achievements/test_api.py +++ b/api/tests/core/achievements/test_api.py @@ -3,7 +3,7 @@ from pcapi.core.achievements import api as achievements_api from pcapi.core.achievements import models as achievements_models from pcapi.core.bookings import factories as bookings_factories -from pcapi.core.categories import subcategories_v2 as subcategories_v2 +from pcapi.core.categories import subcategories_v2 from pcapi.core.users import factories as users_factories diff --git a/api/tests/core/bookings/test_api.py b/api/tests/core/bookings/test_api.py index 1e0e3960e57..8ee106cd46a 100644 --- a/api/tests/core/bookings/test_api.py +++ b/api/tests/core/bookings/test_api.py @@ -206,7 +206,7 @@ def test_if_it_is_first_venue_booking_to_send_specific_email(self): assert email_data1["template"] == dataclasses.asdict( TransactionalEmail.FIRST_VENUE_BOOKING_TO_PRO.value ) # to offerer - email_data1["params"]["OFFER_ADDRESS"] == stock.offer.fullAddress + assert email_data1["params"]["OFFER_ADDRESS"] == stock.offer.fullAddress email_data2 = mails_testing.outbox[1] assert email_data2["template"] == dataclasses.asdict( TransactionalEmail.BOOKING_CONFIRMATION_BY_BENEFICIARY.value diff --git a/api/tests/core/bookings/test_utils.py b/api/tests/core/bookings/test_utils.py index 0c50d2e1c5c..b704f812bd9 100644 --- a/api/tests/core/bookings/test_utils.py +++ b/api/tests/core/bookings/test_utils.py @@ -5,7 +5,7 @@ import pytz import time_machine -import pcapi.core.bookings.utils as utils +from pcapi.core.bookings import utils from pcapi.core.categories import subcategories_v2 as subcategories diff --git a/api/tests/core/external/pro_user_automations_test.py b/api/tests/core/external/pro_user_automations_test.py index d92e6226fdc..e3a10b52aa0 100644 --- a/api/tests/core/external/pro_user_automations_test.py +++ b/api/tests/core/external/pro_user_automations_test.py @@ -56,7 +56,7 @@ def test_automation(self): mock_run_query.return_value = mocked_bq_rows() with patch(self.MOCK_IMPORT_CONTACT_PATH) as mock_import_contacts: - assert type(self).func() + assert type(self).func() # pylint: disable=no-value-for-parameter # I don't get this code expected_params = build_expected_called_params(self.id) mock_import_contacts.assert_called_once_with(expected_params) diff --git a/api/tests/core/external_bookings/test_decorator.py b/api/tests/core/external_bookings/test_decorator.py index 565a8cc9d30..4ac7f585143 100644 --- a/api/tests/core/external_bookings/test_decorator.py +++ b/api/tests/core/external_bookings/test_decorator.py @@ -2,8 +2,6 @@ from unittest.mock import Mock import pytest -from requests.exceptions import ReadTimeout -from requests.exceptions import Timeout from pcapi.core.bookings import models as bookings_models from pcapi.core.external_bookings import models as external_bookings_models @@ -11,11 +9,12 @@ from pcapi.core.external_bookings.decorators import catch_cinema_provider_request_timeout from pcapi.core.external_bookings.exceptions import ExternalBookingTimeoutException from pcapi.core.users import models as users_models +from pcapi.utils.requests import requests class FakeExternalBookingClientAPI(external_bookings_models.ExternalBookingsClientAPI): def __init__(self, cinema_id: str, connector) -> None: - self.cinema_id = cinema_id + super().__init__(cinema_id) self.connector = connector @catch_cinema_provider_request_timeout @@ -26,6 +25,12 @@ def get_film_showtimes_stocks(self, film_id): def book_ticket(self, show_id, booking, beneficiary): return self.connector.make_request(show_id, booking, beneficiary) + def cancel_booking(self, barcodes): + pass + + def get_shows_remaining_places(self, shows_id): + return {} + class FakeClass: @catch_cinema_provider_request_timeout @@ -52,14 +57,15 @@ def test_should_raise_understandable_error_if_the_decorated_func_is_called_with_ client = FakeExternalBookingClientAPI(cinema_id=1, connector=Mock()) with pytest.raises(TypeError) as exception: - client.get_film_showtimes_stocks(12, show_id=1) + # We ignore the pylint error because we test incorrect args here + client.get_film_showtimes_stocks(12, show_id=1) # pylint: disable=unexpected-keyword-arg assert ( str(exception.value) == "TypeError: get_film_showtimes_stocks() got an unexpected keyword argument 'show_id'" ) with pytest.raises(TypeError) as exception: - client.get_film_showtimes_stocks(12, 13, 24) + client.get_film_showtimes_stocks(12, 13, 24) # pylint: disable=too-many-function-args assert ( str(exception.value) == "TypeError: get_film_showtimes_stocks() takes 1 positional arguments but 3 were given" @@ -71,7 +77,7 @@ def test_should_raise_understandable_error_if_the_decorated_func_is_called_with_ ( [12345], {"booking": bookings_models.Booking(id=1), "beneficiary": users_models.User(id=3)}, - ReadTimeout, + requests.exceptions.ReadTimeout, Mock(url="https://provider.com/route/un/peu/instable", method="POST"), { "cinema_id": 789, @@ -88,7 +94,7 @@ def test_should_raise_understandable_error_if_the_decorated_func_is_called_with_ ( [678, bookings_models.Booking(id=1), users_models.User(id=3)], {}, - Timeout, + requests.exceptions.Timeout, Mock(url="https://provider.com/route/qui/timeout", method="GET"), { "cinema_id": 789, @@ -109,13 +115,12 @@ def test_should_raise_understandable_error_if_the_decorated_func_is_called_with_ "booking": bookings_models.Booking(id=4567), "beneficiary": users_models.User(id=12345767), }, - ReadTimeout, + requests.exceptions.ReadTimeout, Mock(url="https://provider.com/oh/zut/encore/un/timeout", method="PUT"), { "cinema_id": 789, "method": "book_ticket", "client": "FakeExternalBookingClientAPI", - "cinema_id": 789, "method_params": { "show_id": "562", "booking": "", diff --git a/api/tests/core/offerers/test_api.py b/api/tests/core/offerers/test_api.py index f0a8e7f423f..42a8499b086 100644 --- a/api/tests/core/offerers/test_api.py +++ b/api/tests/core/offerers/test_api.py @@ -1899,7 +1899,7 @@ def test_get_offerer_stats_dashboard_url(): class GetOffererTotalRevenueTest: def _create_data(self): - today = datetime.datetime.now(datetime.timezone.utc) + today = datetime.datetime.now(datetime.timezone.utc) # pylint: disable=datetime-now offerer = offerers_factories.OffererFactory() bookings_factories.BookingFactory(stock__offer__venue__managingOfferer=offerer, stock__price=10) bookings_factories.UsedBookingFactory( diff --git a/api/tests/core/offers/test_validation.py b/api/tests/core/offers/test_validation.py index ddfc16d7c50..b73889871e9 100644 --- a/api/tests/core/offers/test_validation.py +++ b/api/tests/core/offers/test_validation.py @@ -847,8 +847,12 @@ def test_check_booking_limit_datetime_priorisation_order(self, time_zone_expecte beginning_date = datetime.datetime(2024, 7, 19, 8, tzinfo=datetime.timezone.utc) booking_limit_date = beginning_date - datetime.timedelta(hours=1) - beginning, booking_limit_datetime = validation.check_booking_limit_datetime( - stock, beginning=beginning_date, booking_limit_datetime=booking_limit_date + # It's ok to ignore the tuple unpacking warning here because we are testing the value of beginning + # and it should fails if check_booking_limit_datetime returns an empty list + beginning, booking_limit_datetime = ( # pylint: disable=unbalanced-tuple-unpacking + validation.check_booking_limit_datetime( + stock, beginning=beginning_date, booking_limit_datetime=booking_limit_date + ) ) assert beginning.tzinfo == booking_limit_datetime.tzinfo == time_zone_expected diff --git a/api/tests/routes/backoffice/conftest.py b/api/tests/routes/backoffice/conftest.py index a360f29d1d4..10407cfcacb 100644 --- a/api/tests/routes/backoffice/conftest.py +++ b/api/tests/routes/backoffice/conftest.py @@ -537,7 +537,8 @@ def collective_offerer_booking_fixture(venue_with_educational_status): @pytest.fixture(name="today", scope="module") def today_fixture(): - return datetime.datetime.now(datetime.timezone.utc) + # Disable datetime-now custom rule, here we use the timezone aware datetime at UTC + return datetime.datetime.now(datetime.timezone.utc) # pylint: disable=datetime-now @pytest.fixture(name="collective_venue_booking") diff --git a/api/tests/routes/native/v1/bookings_test.py b/api/tests/routes/native/v1/bookings_test.py index 6c76065e665..a750cbc7160 100644 --- a/api/tests/routes/native/v1/bookings_test.py +++ b/api/tests/routes/native/v1/bookings_test.py @@ -286,7 +286,7 @@ def test_bookings_with_external_event_booking_infos(self, client, requests_mock) == hmac.new(provider.hmacKey.encode(), json.dumps(json_data).encode(), hashlib.sha256).hexdigest() ) - now = datetime.now() + now = datetime.utcnow() json_confirmation_date = datetime.fromisoformat(json_data.pop("booking_confirmation_date")) json_creation_date = datetime.fromisoformat(json_data.pop("booking_creation_date")) @@ -362,7 +362,7 @@ def test_bookings_with_external_event_booking_and_remaining_quantity_unlimited(s assert response.status_code == 200 json_data = json.loads(requests_mock.last_request.json()) - now = datetime.now() + now = datetime.utcnow() json_confirmation_date = datetime.fromisoformat(json_data.pop("booking_confirmation_date")) json_creation_date = datetime.fromisoformat(json_data.pop("booking_creation_date")) diff --git a/api/tests/routes/pro/get_offerer_addresses_test.py b/api/tests/routes/pro/get_offerer_addresses_test.py index 820db8c3176..d3c0270e7c0 100644 --- a/api/tests/routes/pro/get_offerer_addresses_test.py +++ b/api/tests/routes/pro/get_offerer_addresses_test.py @@ -169,12 +169,15 @@ def test_get_offerer_addresses_is_editable(self, client, linked_to_venue): if linked_to_venue: venue = offerers_factories.VenueFactory(managingOfferer=offerer, offererAddress=offerer_address_1) + venue_public_name = venue.common_name + else: + venue_public_name = None + client = client.with_session_auth(email=pro.email) offerer_id = offerer.id offerer_address_1_id = offerer_address_1.id offerer_address_2_id = offerer_address_2.id offerer_address_3_id = offerer_address_3.id - venue_public_name = venue.common_name if linked_to_venue else None with assert_num_queries(self.num_queries): response = client.get(f"/offerers/{offerer_id}/offerer_addresses") assert response.status_code == 200 diff --git a/api/tests/routes/pro/get_statistics_test.py b/api/tests/routes/pro/get_statistics_test.py index c97fb0b3c8b..d361acb9a6e 100644 --- a/api/tests/routes/pro/get_statistics_test.py +++ b/api/tests/routes/pro/get_statistics_test.py @@ -217,7 +217,7 @@ def test_get_statistics_with_no_venue_id_should_fail(self, client): test_client = client.with_session_auth(email=user.email) num_queries = testing.AUTHENTICATION_QUERIES with testing.assert_num_queries(num_queries): - response = test_client.get(f"/get-statistics/") + response = test_client.get("/get-statistics/") assert response.status_code == 422 assert response.json["global"] == ["Vous devez préciser au moins un ID de partenaire culturel"] diff --git a/api/tests/routes/pro/patch_draft_offer_test.py b/api/tests/routes/pro/patch_draft_offer_test.py index 27d75484823..76200addf26 100644 --- a/api/tests/routes/pro/patch_draft_offer_test.py +++ b/api/tests/routes/pro/patch_draft_offer_test.py @@ -396,7 +396,7 @@ def test_first_step_funnel_creation_shouldnt_create_offer_offerer_address_ff_off }, } user_email = user_offerer.user.email - response = client.with_session_auth(user_email).post(f"/offers/draft", json=data) + response = client.with_session_auth(user_email).post("/offers/draft", json=data) assert response.status_code == 201 draft_offer = Offer.query.one() @@ -546,7 +546,7 @@ def test_first_step_funnel_creation_shouldnt_create_offer_offerer_address_ff_on( }, } user_email = user_offerer.user.email - response = client.with_session_auth(user_email).post(f"/offers/draft", json=data) + response = client.with_session_auth(user_email).post("/offers/draft", json=data) assert response.status_code == 201 draft_offer = Offer.query.one() diff --git a/api/tests/routes/public/collective/endpoints/adage_mock/test_bookings.py b/api/tests/routes/public/collective/endpoints/adage_mock/test_bookings.py index eda7da7d62f..45802d002d7 100644 --- a/api/tests/routes/public/collective/endpoints/adage_mock/test_bookings.py +++ b/api/tests/routes/public/collective/endpoints/adage_mock/test_bookings.py @@ -695,7 +695,10 @@ def test_cannot_book_offer_if_no_educational_year_matches_start(self, client): # deposit creates an educational year collectiveOffer__institution=deposit.educationalInstitution, # start will not match the educational year - startDatetime=datetime.now(timezone.utc) + timedelta(weeks=512), + startDatetime=datetime.now( # pylint: disable=datetime-now # ok because we use timezone aware datetime + timezone.utc + ) + + timedelta(weeks=512), ).collectiveOffer self.assert_request_has_expected_result( diff --git a/api/tests/routes/public/individual_offers/v1/get_search_addresses_test.py b/api/tests/routes/public/individual_offers/v1/get_search_addresses_test.py index a1330e5a05f..206e6e8f5a6 100644 --- a/api/tests/routes/public/individual_offers/v1/get_search_addresses_test.py +++ b/api/tests/routes/public/individual_offers/v1/get_search_addresses_test.py @@ -202,5 +202,5 @@ def test_should_raise_400_because_municipality_not_found_on_BAN_API( assert result.status_code == 400 assert result.json == { - "__root__": [f"No municipality found for `city=Parisse (comme disent les Anglais)` and `postalCode=75017`"] + "__root__": ["No municipality found for `city=Parisse (comme disent les Anglais)` and `postalCode=75017`"] } diff --git a/api/tests/routes/public/individual_offers/v1/post_address_test.py b/api/tests/routes/public/individual_offers/v1/post_address_test.py index 3a855ca4a18..7493bf964c0 100644 --- a/api/tests/routes/public/individual_offers/v1/post_address_test.py +++ b/api/tests/routes/public/individual_offers/v1/post_address_test.py @@ -208,7 +208,7 @@ def test_should_raise_400_because_municipality_not_found_on_BAN_API( assert result.status_code == 400 assert result.json == { - "__root__": [f"No municipality found for `city=Parisse (comme disent les Anglais)` and `postalCode=75017`"] + "__root__": ["No municipality found for `city=Parisse (comme disent les Anglais)` and `postalCode=75017`"] } @patch("pcapi.connectors.api_adresse.get_municipality_centroid")