Skip to content

Commit

Permalink
(PC-33424)[API] refactor: add stock and mediation to headline offer f…
Browse files Browse the repository at this point in the history
…actory
  • Loading branch information
ogeber-pass committed Jan 13, 2025
1 parent 38eba16 commit f96d0e9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
18 changes: 17 additions & 1 deletion api/src/pcapi/core/offers/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pcapi.core.categories import subcategories_v2 as subcategories
from pcapi.core.factories import BaseFactory
import pcapi.core.offerers.factories as offerers_factories
from pcapi.core.offerers.schemas import VenueTypeCode
import pcapi.core.offers.models as offers_models
from pcapi.core.providers.constants import TITELIVE_MUSIC_GENRES_BY_GTL_ID
from pcapi.core.providers.titelive_gtl import GTLS
Expand Down Expand Up @@ -263,10 +264,25 @@ class HeadlineOfferFactory(BaseFactory):
class Meta:
model = models.HeadlineOffer

offer = factory.SubFactory(OfferFactory)
offer = factory.SubFactory(
OfferFactory,
isActive=True,
venue=factory.SubFactory(offerers_factories.VenueFactory, venueTypeCode=VenueTypeCode.LIBRARY),
)
venue = factory.SelfAttribute("offer.venue")
timespan = (datetime.datetime.utcnow(),)

@factory.post_generation
def stockAndMediationGeneration(
self,
create: bool,
_extracted: typing.Any,
**_kwargs: typing.Any,
) -> None:
if create:
StockFactory(offer=self.offer)
MediationFactory(offer=self.offer)


class PriceCategoryLabelFactory(BaseFactory):
class Meta:
Expand Down
5 changes: 1 addition & 4 deletions api/tests/core/offerers/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ def test_get_offerer_addresses(self):
class GetOffererHeadlineOfferTest:
def test_return_headline_offer(self):
offer = offers_factories.OfferFactory()
offers_factories.StockFactory(offer=offer)
offers_factories.HeadlineOfferFactory(offer=offer, venue=offer.venue)
offers_factories.HeadlineOfferFactory(offer=offer)

headline_offer = repository.get_offerer_headline_offer(offer.venue.managingOffererId)

Expand All @@ -288,9 +287,7 @@ def test_shoud_not_return_several_headline_offer(self):
venue = offerers_factories.VenueFactory(managingOfferer=offerer)
other_venue = offerers_factories.VenueFactory(managingOfferer=offerer)
offer = offers_factories.OfferFactory(venue=venue)
offers_factories.StockFactory(offer=offer)
other_offer = offers_factories.OfferFactory(venue=other_venue)
offers_factories.StockFactory(offer=other_offer)
offers_factories.HeadlineOfferFactory(offer=offer, venue=venue)
offers_factories.HeadlineOfferFactory(offer=other_offer, venue=other_venue)

Expand Down
62 changes: 33 additions & 29 deletions api/tests/core/offers/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,9 +2152,7 @@ def test_create_offer_headline_when_another_is_still_active_should_fail(self):
assert error.value.errors["headlineOffer"] == ["This offer is already an active headline offer"]

def test_remove_headline_offer(self):
venue = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer = factories.OfferFactory(isActive=True, venue=venue)
factories.StockFactory(offer=offer)
offer = factories.OfferFactory(isActive=True)
headline_offer = factories.HeadlineOfferFactory(offer=offer)

api.remove_headline_offer(headline_offer)
Expand Down Expand Up @@ -2249,30 +2247,17 @@ def test_headline_offer_on_rejected_offer_is_inactive(self):
assert not offer.is_headline_offer

def test_set_upper_timespan_of_inactive_headline_offers(self):
venue_1 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer_1 = factories.OfferFactory(isActive=True, venue=venue_1)
factories.StockFactory(offer=offer_1)
factories.MediationFactory(offer=offer_1)
venue_2 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
stock = factories.StockFactory(quantity=1)
offer_2 = factories.OfferFactory(isActive=True, venue=venue_2, stocks=[stock])
factories.MediationFactory(offer=offer_2)
venue_3 = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer_3 = factories.OfferFactory(isActive=True, venue=venue_3)
factories.StockFactory(offer=offer_3)
factories.MediationFactory(offer=offer_3)

headline_offer_1 = factories.HeadlineOfferFactory(offer=offer_1)
headline_offer_2 = factories.HeadlineOfferFactory(offer=offer_2)
headline_offer_3 = factories.HeadlineOfferFactory(offer=offer_3)
headline_offer_1 = factories.HeadlineOfferFactory()
headline_offer_2 = factories.HeadlineOfferFactory()
headline_offer_3 = factories.HeadlineOfferFactory()

assert headline_offer_1.isActive
assert headline_offer_1.timespan.upper is None
assert headline_offer_2.isActive
assert headline_offer_2.timespan.upper is None

offer_1.validation = models.OfferValidationStatus.REJECTED
stock.quantity = 0
headline_offer_1.offer.validation = models.OfferValidationStatus.REJECTED
headline_offer_2.offer.stocks[0].quantity = 0

api.set_upper_timespan_of_inactive_headline_offers()
assert not headline_offer_1.isActive
Expand All @@ -2283,23 +2268,42 @@ def test_set_upper_timespan_of_inactive_headline_offers(self):
assert headline_offer_3.isActive
assert headline_offer_3.timespan.upper is None

def test_do_not_deactivate_headline_offer_with_product_mediation(self):
product_without_mediation = factories.ProductFactory()
product_with_mediation = factories.ProductFactory()
factories.ProductMediationFactory(product=product_with_mediation)
offer_with_product_mediation = factories.OfferFactory(product=product_with_mediation)
offer_without_product_mediation = factories.OfferFactory(product=product_without_mediation)
headline_offer_with_product_mediation = factories.HeadlineOfferFactory(offer=offer_with_product_mediation)
headline_offer_without_product_mediation = factories.HeadlineOfferFactory(offer=offer_without_product_mediation)

db.session.delete(headline_offer_with_product_mediation.offer.mediations[0])
db.session.delete(headline_offer_without_product_mediation.offer.mediations[0])
db.session.commit()

assert headline_offer_with_product_mediation.offer.images
assert not headline_offer_without_product_mediation.offer.images

api.set_upper_timespan_of_inactive_headline_offers()
assert headline_offer_with_product_mediation.isActive
assert headline_offer_with_product_mediation.timespan.upper is None

assert not headline_offer_without_product_mediation.isActive
assert not headline_offer_without_product_mediation.timespan.upper is None

def test_do_not_update_upper_timespan_of_already_inactive_headline_offers(self):
venue = offerers_factories.VenueFactory(venueTypeCode=VenueTypeCode.LIBRARY)
offer = factories.OfferFactory(isActive=True, venue=venue)
factories.StockFactory(offer=offer)
factories.MediationFactory(offer=offer)
creation_time = datetime.utcnow() - timedelta(days=20)
finished_timespan = (creation_time, creation_time + timedelta(days=10))
old_headline_offer = factories.HeadlineOfferFactory(offer=offer, timespan=finished_timespan)
old_headline_offer = factories.HeadlineOfferFactory(timespan=finished_timespan)

api.set_upper_timespan_of_inactive_headline_offers()
assert old_headline_offer.timespan.lower.date() == creation_time.date()
assert old_headline_offer.timespan.upper.date() == (creation_time + timedelta(days=10)).date()

def test_set_upper_timespan_of_inactive_headline_offers_without_image(self):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)
headline_offer = factories.HeadlineOfferFactory()
db.session.delete(headline_offer.offer.mediations[0])

headline_offer = factories.HeadlineOfferFactory(offer=offer)
assert headline_offer.isActive

api.set_upper_timespan_of_inactive_headline_offers()
Expand Down
13 changes: 3 additions & 10 deletions api/tests/core/offers/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,21 +706,15 @@ class HeadlineOfferTest:
next_month = today + datetime.timedelta(days=30)

def test_headline_offer_is_active(self):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)
headline_offer = factories.HeadlineOfferFactory(offer=offer, timespan=(self.today, None))
headline_offer = factories.HeadlineOfferFactory(timespan=(self.today, None))
assert headline_offer.isActive

def test_headline_offer_with_ending_time_in_the_future_is_active(self):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)
headline_offer = factories.HeadlineOfferFactory(offer=offer, timespan=(self.today, self.day_after_tomorrow))
headline_offer = factories.HeadlineOfferFactory(timespan=(self.today, self.day_after_tomorrow))
assert headline_offer.isActive

def test_headline_offer_is_not_active(self):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)
headline_offer = factories.HeadlineOfferFactory(offer=offer, timespan=(self.today, self.day_after_tomorrow))
headline_offer = factories.HeadlineOfferFactory(timespan=(self.today, self.day_after_tomorrow))
with time_machine.travel(self.next_month):
assert not headline_offer.isActive

Expand All @@ -735,7 +729,6 @@ def test_headline_offer_is_not_active(self):
)
def test_unicity_headline_offer(self, timespan, overlaping_timespan):
offer = factories.OfferFactory(isActive=True)
factories.StockFactory(offer=offer)
factories.HeadlineOfferFactory(offer=offer, timespan=timespan)
with pytest.raises(exc.IntegrityError):
factories.HeadlineOfferFactory(offer=offer, timespan=overlaping_timespan)
Expand Down
6 changes: 4 additions & 2 deletions api/tests/routes/native/v1/offerers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def test_get_offerer_headline_offer_success(self, client):
offerer = user_offerer.offerer
venue = offerers_factories.VenueFactory(managingOfferer=offerer)
offer = offers_factories.OfferFactory(venue=venue)
offers_factories.StockFactory(offer=offer)
offers_factories.HeadlineOfferFactory(offer=offer, venue=venue)

client = client.with_session_auth(email=pro.email)
Expand All @@ -222,7 +221,10 @@ def test_get_offerer_headline_offer_success(self, client):
assert response.json == {
"name": offer.name,
"id": offer.id,
"image": offer.image,
"image": {
"credit": offer.image.credit,
"url": offer.image.url,
},
}

def test_get_offerer_headline_offer_not_found(self, client):
Expand Down
5 changes: 4 additions & 1 deletion api/tests/routes/pro/get_offerer_headline_offer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def test_get_offerer_headline_offer_success(self, client):
assert response.json == {
"name": offer.name,
"id": offer.id,
"image": offer.image,
"image": {
"credit": offer.image.credit,
"url": offer.image.url,
},
}

def test_get_offerer_headline_offer_with_product_mediations(self, client):
Expand Down

0 comments on commit f96d0e9

Please sign in to comment.