"""Tests for product model.""" import pytest from vectororder.exceptions import ProductNotFoundError, ProductNotInContent from vectororder.models.product_model import get_product from vectororder.models.schemas import ( DistributionFeatureId, DownloadStreamRights, Product, Store, Track, TrackOfferType, TrackType, ) from tests.test_utils.db import create_product, create_track @pytest.mark.parametrize( ( "test_description", "product_data", "tracks", "store", "expected_product", "expected_is_digital_audio", "expected_has_track_with_offer_type", ), [ ( "Digital audio and track with offer type", { "distribution_format_id": 1, }, [ { "track_type": "music", "offer_type": "all", }, ], Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ), Product( product_id=123, distribution_format_id=1, not_for_distribution="N", context_type="digital", tracks=[ Track( track_id=1, track_type=TrackType.MUSIC, offer_type=TrackOfferType.ALL, distribution_rights={ DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, }, ), ], ), True, True, ), ( "Not digital", { "distribution_format_id": 70, }, [ { "track_type": "music", "offer_type": "all", }, ], Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ), Product( product_id=123, distribution_format_id=70, not_for_distribution="N", context_type="physical", tracks=[ Track( track_id=1, track_type=TrackType.MUSIC, offer_type=TrackOfferType.ALL, distribution_rights={ DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, }, ), ], ), False, True, ), ( "not audio", { "distribution_format_id": 1, }, [ { "track_type": "music", "offer_type": "all", }, { "track_type": "video", "offer_type": "all", }, ], Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ), Product( product_id=123, distribution_format_id=1, not_for_distribution="N", context_type="digital", tracks=[ Track( track_id=1, track_type=TrackType.MUSIC, offer_type=TrackOfferType.ALL, distribution_rights={ DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, }, ), Track( track_id=2, track_type=TrackType.VIDEO, offer_type=TrackOfferType.ALL, distribution_rights={ DownloadStreamRights.DOWNLOAD, DownloadStreamRights.STREAM, }, ), ], ), False, True, ), ( "no track with non-none offer type", { "distribution_format_id": 1, }, [ { "track_type": "music", "offer_type": "none", }, { "track_type": "music", "offer_type": "none", }, ], Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ), Product( product_id=123, distribution_format_id=1, not_for_distribution="N", context_type="digital", tracks=[ Track( track_id=1, track_type=TrackType.MUSIC, offer_type=TrackOfferType.NONE, distribution_rights=set(), ), Track( track_id=2, track_type=TrackType.MUSIC, offer_type=TrackOfferType.NONE, distribution_rights=set(), ), ], ), True, False, ), ], ) def test_get_product( test_description: str, product_data: dict[str, int | str], tracks: list[dict[str, str]], store: Store, expected_product: Product, expected_is_digital_audio: bool, expected_has_track_with_offer_type: bool, init_db_tables: None, ) -> None: """Test get_product.""" release_id = 123 upc = 199066946312 create_product( release_id=release_id, upc=upc, not_for_distribution="N", distribution_format_id=int(product_data["distribution_format_id"]), ) for index, track in enumerate(tracks, start=1): create_track( id_=index, release_id=release_id, **track, ) product = get_product(upc, store) assert product == expected_product assert product.is_digital_audio == expected_is_digital_audio assert product.has_track_with_offer_type == expected_has_track_with_offer_type def test_get_product_raises_not_found_when_upc_missing(init_db_tables: None) -> None: """Test get_product raises ProductNotFoundError when no row matches the UPC.""" store = Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ) with pytest.raises(ProductNotFoundError): get_product(999999999999, store) def test_get_product_raises_not_in_content_when_release_status_differs( init_db_tables: None, ) -> None: """Test get_product raises ProductNotInContent when release_status is not 'in_content'.""" upc = 199066946312 create_product(upc=upc, release_status="out_of_content") store = Store( store_id=286, distribution_feature_ids={DistributionFeatureId.A_LA_CARTE_DOWNLOAD}, ) with pytest.raises(ProductNotInContent): get_product(upc, store)