""" Tests for core.util module. """ import pytest from datetime import date from src.legacy.core.util import fold_periods from src.legacy.spotify.vendor import SpotifyPlaylists @pytest.mark.parametrize( "test_input,expected_output", ( ([], []), ([1], [[1, 1]]), ([1, 2], [[1, 2]]), ([1, 2, 3], [[1, 3]]), ([1, 2, 3, 4], [[1, 4]]), ([1, 2, 3, 6, 7], [[1, 3], [6, 7]]), ([1, 2, 3, 6, 7, 8, 9, 12], [[1, 3], [6, 9], [12, 12]]), ([3, 6, 7, 8, 9, 12, 13, 14, 15, 16, 17, 18, 19, 20], [[3, 3], [6, 9], [12, 20]]), ), ) def test_fold_periods(test_input, expected_output): """Test fold_periods() function.""" assert fold_periods([date.fromordinal(i) for i in test_input]) == [ [date.fromordinal(i[0]), date.fromordinal(i[1])] for i in expected_output ] @pytest.mark.parametrize( "search,source_isrc,result_len", ( ("mars", None, 3), ("mars", "asdf", 4), ("plain", None, 1), ("plain", "abcd", 2), ("test", None, 0), ("test", "asdf", 0), ("outside", "asdf", 1), ("outside", None, 1), ), ) def test_search_across_tracks_in_playlist(search, source_isrc, result_len): tracklist = [ {"isrc": "abcd", "name": "Pluto", "artists": ["David Mars"]}, {"isrc": "efgh", "name": "Mars", "artists": ["Stink", "Jen Popez"]}, {"isrc": "qwer", "name": "Plain Terrain", "artists": ["Kent Brockman"]}, {"isrc": "abcd", "name": "Pluto", "artists": ["David Mars"]}, {"isrc": "asdf", "name": "Outside", "artists": ["J low", "Britney Pears"]}, ] result_items = SpotifyPlaylists().search_across_tracks_in_playlist(tracklist, search, source_isrc) assert len(result_items) == result_len