"""Unit tests for placement Marhsmallow schemas.""" import datetime import pytest from dateutil import tz from sound_recordings.schemas.common import SourceSchema from sound_recordings.schemas.placement import ( APPLE_IMAGE_SIZE, PlacementSchema, PlaylistSchema, PlaylistTracksSchema, RecentPlacementsSchema, ) class TestPlaylistsTracksSchema: """Test PlaylistTracksSchema.""" length = 50 def test_length(self): """Test length field is preserved in schema dump.""" assert PlaylistTracksSchema().dump({"length": self.length}) == { "length": self.length } payload = { "playlist_id": "pl.a7be635ddafd48c49f6c5656d826285f", "store_id": 1, "playlist_name": "British Folk Essentials", "playlist_uri": "https://itunes.apple.com/sa/playlist/pl.a7be635ddafd48c49f6c5656d826285f", # noqa "storefront": "SA", "added": datetime.datetime(2017, 11, 22, 9, 58, tzinfo=tz.tzutc()), "artwork_url": "https://is5-ssl.mzstatic.com/image/thumb/Features118/v4/3f/d9/1f/3fd91f00-c6e5-a01a-da41-1a07944df36f/source/804x804cc.jpeg", # noqa "country": "SA", "followers": 18, "track_count": 61, "position": 21, "previous_position": 18, "total_streams": 3001, "user_display_name": "Apple Music", } apple_image = "https://is3-ssl.mzstatic.com/image/thumb/Features115/v4/66/61/fb/6661fb42-d392-0d6c-39d4-fbf8b3a1b237/source/{w}x{h}cc.jpeg" # noqa class TestPlaylistSchema: """Test PlaylistSchema.""" @pytest.fixture def result(self): """Results of schema dump.""" return PlaylistSchema().dump(payload) def test_source(self, result): """Test that source is an object with result from SourceSchema.""" assert result["source"] == SourceSchema().dump({"id": payload["store_id"]}) def test_id(self, result): """Test id is value of payload playlist_id.""" assert result["id"] == payload["playlist_id"] def test_uri(self, result): """Test uri is value of payload playlist_uri.""" assert result["uri"] == payload["playlist_uri"] def test_storefront(self, result): """Test storefront is value of payload storefront.""" assert result["storefront"] == payload["storefront"] def test_name(self, result): """Test name is value of payload playlist_name.""" assert result["name"] == payload["playlist_name"] def test_image_location(self, result): """Test imageLocation is value of payload artwork_url.""" assert result["image_location"] == payload["artwork_url"] def test_user_display_name(self, result): """Test userDisplayName is value of payload user_display_name.""" assert result["user_display_name"] == payload["user_display_name"] def test_country(self, result): """Test country is value of payload country.""" assert result["country"] == payload["country"] def test_followers(self, result): """Test followers is value of payload followers.""" assert result["followers"] == payload["followers"] class TestPlaylistSchemaWithAppleImage: """Test PlaylistSoundRecordingSchema with Apple Music image url.""" @pytest.fixture def result(self): """Results of schema dump.""" return PlaylistSchema().dump({**payload, "artwork_url": apple_image}) def test_image_location_interpolates_size(self, result): """Test height and width are interpolated in image location.""" assert result["image_location"] == apple_image.replace( "{w}x{h}", "%sx%s" % (APPLE_IMAGE_SIZE, APPLE_IMAGE_SIZE) ) class TestPlacementSchema: """Test SoundRecordingPlacementSchema.""" @pytest.fixture def result(self): """Results of schema dump.""" return PlacementSchema().dump(payload) def test_position(self, result): """Test position is value of payload position.""" assert result["position"] == payload["position"] def test_previous_position(self, result): """Test previousPosition is value of payload previous_position.""" assert result["previous_position"] == payload["previous_position"] def test_streams(self, result): """Test streams is value of payload streams_last_week.""" assert result["streams"] == payload["total_streams"] def test_added(self, result): """Test added is value of iso format for payload added.""" assert result["added"] == payload["added"].strftime("%Y-%m-%dT%H:%M:%SZ") def test_playlist(self, result): """Test playlist is result of PlaylistSchema serialization.""" assert result["playlist"] == PlaylistSchema().dump(payload) class TestRecentPlacementsSchema: """Test RecentPlacementsSchema.""" recent_placements_payload = { "items": [ { "playlist_id": "pl.a7be635ddafd48c49f6c5656d826285f", "store_id": 1, "playlist_name": "British Folk Essentials", "playlist_uri": "https://itunes.apple.com/1", "storefront": "SA", "added": datetime.datetime(2019, 4, 30, 1, 48, 43), "artwork_url": "https://is5-ssl.mzstatic.com/2", "country": "SA", "followers": 18, "track_count": 110, "position": 93, "previous_position": 95, "total_streams": 3001, "isrc": "USA561066580", "user_display_name": "Apple Music", "storefronts": [ { "added": datetime.datetime(2019, 4, 30, 1, 48, 43), "country": "BR", "playlist_uri": "https://itunes.apple.com/br/playlist/pl.64", "position": 93, "previous_position": 95, "storefront": "BR", "total_streams": 0, "track_count": 110, } ], } ], "count": 100, "sources": [ {"id": 286, "name": "Spotify"}, {"id": 1, "name": "Apple Music", "error": {"code": "unreliable"}}, ], } @pytest.fixture def result(self): """Results of schema dump.""" return RecentPlacementsSchema().dump(self.recent_placements_payload) def test_position(self, result): """Test position is value of payload position.""" assert ( result["items"][0]["position"] == self.recent_placements_payload["items"][0]["position"] ) def test_previous_position(self, result): """Test previousPosition is value of payload previous_position.""" assert ( result["items"][0]["previous_position"] == self.recent_placements_payload["items"][0]["previous_position"] ) def test_streams(self, result): """Test streams is value of payload streams_last_week.""" assert ( result["items"][0]["streams"] == self.recent_placements_payload["items"][0]["total_streams"] ) def test_added(self, result): """Test added is value of iso format for payload added.""" assert result["items"][0]["added"] == self.recent_placements_payload["items"][ 0 ]["added"].strftime("%Y-%m-%dT%H:%M:%SZ") def test_playlist(self, result): """Test playlist is result of PlaylistSchema serialization.""" assert result["items"][0]["playlist"] == PlaylistSchema().dump( self.recent_placements_payload["items"][0] ) def test_isrc(self, result): """Test playlist is result of PlaylistSchema serialization.""" assert ( result["items"][0]["isrc"] == self.recent_placements_payload["items"][0]["isrc"] )