"""Test for store model.""" from pytest import fixture from store.models.store import ( ClassificationDetail, DistributionType, Store, StoreClassificationDetail, StoreDistributionType, Substore, Territory, get_store_classifications_by_store_ids, get_store_distribution_types_by_store_ids, get_stores, get_stores_by_ids, get_substores_by_ids, ) from tests.testutils import db @fixture def test_schema() -> None: """Test schema fixture for store model tests.""" db.test_schema() @fixture def does_not_support_timed_release() -> list[Store]: """Test Store objects that do not support timed release.""" return [ Store( customer_master_master_id=1, customer_name="iTunes/Apple", supports_timed_release="N", status="active", ), Store( customer_master_master_id=2, customer_name="Napster - Legacy", supports_timed_release="N", status="active", ), Store( customer_master_master_id=3, customer_name="MediaNet", supports_timed_release="N", status="inactive", ), ] @fixture def supports_timed_release() -> list[Store]: """Test Store objects that support timed release.""" return [ Store( customer_master_master_id=117, customer_name="iMusica", supports_timed_release="Y", status="active", ), Store( customer_master_master_id=173, customer_name="iHeartRadio", supports_timed_release="Y", status="active", ), Store( customer_master_master_id=286, customer_name="Spotify", supports_timed_release="Y", status="active", ), ] @fixture def music_stores() -> list[Store]: """Test Store objects that have music classification.""" return [ Store(customer_master_master_id=286, customer_name="Spotify", status="active") ] @fixture def nr_performer_stores() -> list[Store]: """Test Store objects that have nr_performer classification.""" return [ Store( customer_master_master_id=549, customer_name="SoundExchange", status="active", ) ] @fixture def active_stores() -> list[Store]: """Test store objects that are active.""" return [ Store( customer_master_master_id=167, customer_name="Juno Records", status="active" ), Store( customer_master_master_id=206, customer_name="Touchtunes", status="active" ), ] @fixture def inactive_stores() -> list[Store]: """Test store objects that are inactive.""" return [ Store( customer_master_master_id=14, customer_name="Audio Lunchbox", status="inactive", ), Store( customer_master_master_id=137, customer_name="Groupietunes, Inc.", status="inactive", ), ] @fixture def terminated_stores() -> list[Store]: return [ Store( customer_master_master_id=2, customer_name="Napster - Legacy", status="terminated", ), Store( customer_master_master_id=5, customer_name="Musicmatch", status="terminated" ), ] @fixture def classifications() -> list[StoreClassificationDetail | ClassificationDetail]: """Test classifications.""" return [ StoreClassificationDetail(classification_detail_id=1, store_id=286), StoreClassificationDetail(classification_detail_id=2, store_id=286), StoreClassificationDetail(classification_detail_id=14, store_id=549), ClassificationDetail(id=1, classification="Music"), ClassificationDetail(id=2, classification="Physical"), ClassificationDetail(id=14, classification="NR Performer"), ] @fixture def distribution_types() -> list[StoreDistributionType | DistributionType]: """Test distribution types.""" return [ StoreDistributionType(distribution_type_id=1, customer_master_master_id=286), StoreDistributionType(distribution_type_id=2, customer_master_master_id=286), StoreDistributionType(distribution_type_id=3, customer_master_master_id=549), DistributionType(id=1, name="Full Track"), DistributionType(id=2, name="Tone"), DistributionType(id=3, name="Video Master"), ] @fixture def substores() -> list[Substore]: """Test substores""" return [ Substore(customer_id=1, customer_master_master_id=1, territory=1), Substore(customer_id=2, customer_master_master_id=1, territory=2), Substore(customer_id=3, customer_master_master_id=2, territory=1), Substore(customer_id=4, customer_master_master_id=2, territory=2), Substore(customer_id=5, customer_master_master_id=2, territory=3), ] @fixture def territories() -> list[Territory]: """Test substores""" return [ Territory(id=1, name="USA", country_code="US"), Territory(id=2, name="Canada", country_code="CA"), Territory(id=3, name="Mexico", country_code="MX"), ] def test_get_stores_no_timed_release_filter( does_not_support_timed_release: list[Store], supports_timed_release: list[Store], test_schema: None, ) -> None: """Test that if None is passed to get_stores for supports_timed_release that all results are returned. """ db.seed_models(does_not_support_timed_release) db.seed_models(supports_timed_release) stores = get_stores(supports_timed_release=None) assert len(stores) == len(does_not_support_timed_release) + len( supports_timed_release ) def test_get_stores_timed_release_filter_true( does_not_support_timed_release: list[Store], supports_timed_release: list[Store], test_schema: None, ) -> None: """Test that if True is passed to get_stores for supports_timed_release that only Stores with supports_timed_release 'Y' are returned. """ db.seed_models(does_not_support_timed_release) db.seed_models(supports_timed_release) stores = get_stores(supports_timed_release=True) assert [x["id"] for x in stores] == [ timed_release_store.customer_master_master_id for timed_release_store in supports_timed_release ] def test_get_stores_timed_release_filter_false( does_not_support_timed_release: list[Store], supports_timed_release: list[Store], test_schema: None, ) -> None: """Test that if False is passed to get_stores for supports_timed_release that only Stores with supports_timed_release 'N' are returned. """ db.seed_models(does_not_support_timed_release) db.seed_models(supports_timed_release) stores = get_stores(supports_timed_release=False) assert [x["id"] for x in stores] == [ no_timed_release_store.customer_master_master_id for no_timed_release_store in does_not_support_timed_release ] def test_get_stores_no_classification( classifications: list[StoreClassificationDetail | ClassificationDetail], nr_performer_stores: list[Store], music_stores: list[Store], test_schema: None, ) -> None: """Test that if None is passed to get_stores for classification_id that Stores of all classification are returned.""" db.seed_models(classifications) db.seed_models(nr_performer_stores) db.seed_models(music_stores) stores = get_stores(classification_id=None) assert len(stores) == len(nr_performer_stores + music_stores) def test_get_stores_music_classification( classifications: list[StoreClassificationDetail | ClassificationDetail], nr_performer_stores: list[Store], music_stores: list[Store], test_schema: None, ) -> None: """Test that if classification id for "Music" is passed that only Stores with classification for "Music" are returned.""" db.seed_models(classifications) db.seed_models(nr_performer_stores) db.seed_models(music_stores) stores = get_stores(classification_id=1) assert [x["id"] for x in stores] == [ music_store.customer_master_master_id for music_store in music_stores ] def test_get_stores_nr_performer_classification( classifications: list[StoreClassificationDetail | ClassificationDetail], nr_performer_stores: list[Store], music_stores: list[Store], test_schema: None, ) -> None: """Test that if classification id for "NR Performer" is passed that only Stores with classification for "NR Performer" are returned.""" db.seed_models(classifications) db.seed_models(nr_performer_stores) db.seed_models(music_stores) stores = get_stores(classification_id=14) assert [x["id"] for x in stores] == [ nr_performer_store.customer_master_master_id for nr_performer_store in nr_performer_stores ] def test_customer_name_sort_asc( does_not_support_timed_release: list[Store], test_schema: None ) -> None: """Test that if order_dir 'asc' is supplied that name is sorted ascending.""" db.seed_models(does_not_support_timed_release) stores = get_stores(order_by="customer_name", order_dir="asc") assert [x["name"] for x in stores] == [ "iTunes/Apple", "MediaNet", "Napster - Legacy", ] def test_customer_name_sort_desc( does_not_support_timed_release: list[Store], test_schema: None ) -> None: """Test that if order_dir 'desc' is supplied that name is sorted descending.""" db.seed_models(does_not_support_timed_release) stores = get_stores(order_by="customer_name", order_dir="desc") assert [x["name"] for x in stores] == [ "Napster - Legacy", "MediaNet", "iTunes/Apple", ] def test_get_stores_active_status( active_stores: list[Store], inactive_stores: list[Store], terminated_stores: list[Store], test_schema: None, ) -> None: """Test that if 'active' is passed in statuses that only active Stores are returned.""" db.seed_models(active_stores) db.seed_models(inactive_stores) db.seed_models(terminated_stores) stores = get_stores(statuses=["active"]) assert [x["id"] for x in stores] == [ active_store.customer_master_master_id for active_store in active_stores ] def test_get_stores_inactive_status( active_stores: list[Store], inactive_stores: list[Store], terminated_stores: list[Store], test_schema: None, ) -> None: """Test that if 'inactive' is passed in statuses that only active Stores are returned.""" db.seed_models(active_stores) db.seed_models(inactive_stores) db.seed_models(terminated_stores) stores = get_stores(statuses=["inactive"]) assert [x["id"] for x in stores] == [ inactive_store.customer_master_master_id for inactive_store in inactive_stores ] def test_get_stores_terminated_status( active_stores: list[Store], inactive_stores: list[Store], terminated_stores: list[Store], test_schema: None, ) -> None: """Test that if 'terminated' is passed in statuses that only active Stores are returned.""" db.seed_models(active_stores) db.seed_models(inactive_stores) db.seed_models(terminated_stores) stores = get_stores(statuses=["terminated"]) assert [x["id"] for x in stores] == [ terminated_store.customer_master_master_id for terminated_store in terminated_stores ] def test_get_stores_by_ids( does_not_support_timed_release: list[Store], test_schema: None ) -> None: """Test getting stores by ids.""" db.seed_models(does_not_support_timed_release) response = get_stores_by_ids([1, 2, 3]) assert response == [ { "id": 1, "name": "iTunes/Apple", "supports_timed_release": False, "status": "active", }, { "id": 2, "name": "Napster - Legacy", "supports_timed_release": False, "status": "active", }, { "id": 3, "name": "MediaNet", "supports_timed_release": False, "status": "inactive", }, ] def test_get_store_classifications_by_store_ids( classifications: list[StoreClassificationDetail | ClassificationDetail], supports_timed_release: list[Store], test_schema: None, ) -> None: """Test getting store classifications by store ids.""" db.seed_models(classifications) db.seed_models(supports_timed_release) response = get_store_classifications_by_store_ids([117, 173, 286]) assert response == [ {"id": 117, "classifications": []}, {"id": 173, "classifications": []}, {"id": 286, "classifications": [1, 2]}, ] def test_get_store_distribution_types_by_store_ids( distribution_types: list[StoreDistributionType | DistributionType], supports_timed_release: list[Store], test_schema: None, ) -> None: """Test getting store distribution types by store ids.""" db.seed_models(distribution_types) db.seed_models(supports_timed_release) response = get_store_distribution_types_by_store_ids([117, 173, 286]) assert response == [ {"id": 117, "distribution_types": []}, {"id": 173, "distribution_types": []}, {"id": 286, "distribution_types": [1, 2]}, ] def test_get_substores_by_ids( does_not_support_timed_release: list[Store], substores: list[Substore], territories: list[Territory], test_schema: None, ) -> None: """Test getting stores by ids.""" db.seed_models(does_not_support_timed_release) db.seed_models(substores) db.seed_models(territories) data = get_substores_by_ids([1, 2, 3]) assert data == [ { "store_id": 1, "territories": [ {"orchard_id": 1, "country_code": "US", "standard": 0}, {"orchard_id": 2, "country_code": "CA", "standard": 0}, ], }, { "store_id": 2, "territories": [ {"orchard_id": 1, "country_code": "US", "standard": 0}, {"orchard_id": 2, "country_code": "CA", "standard": 0}, {"orchard_id": 3, "country_code": "MX", "standard": 0}, ], }, ]