"""Test common schemas.""" import datetime from analytics.constants.store import ALL_SOURCES from analytics.schemas.common import ( BaseSchema, DownloadBreakdownItemSchema, DownloadBreakdownSchema, LabelEntitySchema, SourceSchema, StreamBreakdownItemSchema, StreamBreakdownSchema, StreamDataSchema, TrackCoreMetadataSchema, ) def test_base_schema(): """Test BaseSchema.normalize.""" input_data = {"some_key": "value"} assert BaseSchema.normalize(input_data) == {} assert BaseSchema.normalize([input_data], many=True) == [{}] def test_store_schema(): """Test store schema.""" input_data = {"id": 286, "name": "Spotify"} expected_response = {"id": 286, "name": "Spotify"} assert SourceSchema().dump(input_data) == expected_response def test_store_schema_missing_name(): """Test store schema adds name field when missing.""" _id = 286 input_data = {"id": _id} expected_response = { "id": _id, "name": next(item for item in ALL_SOURCES if item["id"] == _id)["name"], } assert SourceSchema().dump(input_data) == expected_response def test_streamdata_schema(): """Test StreamDataSchema.""" input_data = {"growth_percentage": 7.6667} expected_response = {"growth_percentage": 7.6667} assert StreamDataSchema().dump(input_data) == expected_response def test_streambreakdown_schema(): """Test StreamBreakdownSchema.""" input_data = { "growth_percentage": 7.6667, "items": [ { "streams": 1, "skip_rate": 1.0, "saves": 1.0, "date": datetime.datetime(2018, 1, 1), } ], } expected_response = { "growth_percentage": 7.6667, "items": [ { "streams": 1, "skip_rate": 1.0, "saves": 1.0, "date": "2018-01-01T00:00:00Z", } ], } assert StreamBreakdownSchema().dump(input_data) == expected_response def test_downloadbreakdown_schema(): """Test DownloadBreakdownSchema.""" input_data = {"items": [{"downloads": 1, "date": datetime.datetime(2018, 1, 1)}]} expected_response = {"items": [{"downloads": 1, "date": "2018-01-01T00:00:00Z"}]} assert DownloadBreakdownSchema().dump(input_data) == expected_response def test_streambreakdownitem_schema(): """Test StreamBreakdownItemSchema.""" input_data = { "streams": 1, "skip_rate": 1.0, "saves": 1.0, "date": datetime.datetime(2018, 1, 1), } expected_response = { "streams": 1, "skip_rate": 1.0, "saves": 1.0, "date": "2018-01-01T00:00:00Z", } assert StreamBreakdownItemSchema().dump(input_data) == expected_response def test_downloadbreakdownitem_schema(): """Test DownloadBreakdownItemSchema.""" input_data = {"downloads": 1, "date": datetime.datetime(2018, 1, 1)} expected_response = {"downloads": 1, "date": "2018-01-01T00:00:00Z"} assert DownloadBreakdownItemSchema().dump(input_data) == expected_response def test_label_entity_schema(): """Test LabelEntitySchema.""" subaccount_id = 12345 input_data = { "label_id": 123, "subaccount_name": "Sub", "subaccount_id": subaccount_id, } expected_response = { "label_id": input_data["label_id"], "subaccount": input_data["subaccount_name"], "subaccount_id": input_data["subaccount_id"], } filtered_response = dict.copy(expected_response) filtered_response.pop("subaccount") assert LabelEntitySchema().dump(input_data) == expected_response assert ( LabelEntitySchema.normalize( input_data, {"label_ids": None, "subaccount_ids": [subaccount_id]} ) == filtered_response ) def test_track(): """Test TrackCoreMetadataSchema.""" inputs = [ { "isrc": "isrcvalue", "artist_name": "Bad Bunny", "image_location": "https://someserver.co.uk/here.png", "name": "Dakiti", "version": "theVersion", "artists": [ {"artist_name": "Bad Bunny", "type": "performer"}, {"artist_name": "Bad Bunny", "type": "remixer"}, ], }, { "isrc": "isrcvalue", "artist_name": "Bad Bunny", "image_location": "https://someserver.co.uk/here.png", "track_name": "Dakiti", "track_version": "theVersion", "artists": [ {"artist_name": "Bad Bunny", "type": "performer"}, {"artist_name": "Bad Bunny", "type": "remixer"}, ], }, ] expected_output = dict.copy(inputs[0]) del expected_output["name"] expected_output["track_name"] = "Dakiti" under_test = TrackCoreMetadataSchema() for item in inputs: under_test.load(item) output = under_test.dump(item) assert output == expected_output