import os
import pytest

from aircox.test import Interface
from aircox.controllers import playlist_import


csv_data = [
    {
        "artist": "Artist 1",
        "title": "Title 1",
        "minutes": "1",
        "seconds": "0",
        "tags": "tag1,tag12",
        "info": "info1",
    },
    {
        "artist": "Artist 2",
        "title": "Title 2",
        "minutes": "2",
        "seconds": "1",
        "tags": "tag2,tag12",
        "info": "info2",
    },
    {
        "artist": "Artist 3",
        "title": "Title 3",
        "minutes": "3",
        "seconds": "2",
        "tags": "",
        "info": "",
    },
]


@pytest.fixture
def importer(sound):
    path = os.path.join(os.path.dirname(__file__), "playlist.csv")
    return playlist_import.PlaylistImport(path, sound=sound)


class TestPlaylistImport:
    @pytest.mark.django_db
    def test_run(self, importer):
        iface = Interface(None, {"read": None, "make_playlist": None})
        importer.read = iface.read
        importer.make_playlist = iface.make_playlist
        importer.run()
        assert iface._trace("read")
        assert iface._trace("make_playlist")

    @pytest.mark.django_db
    def test_read(self, importer):
        importer.read()
        assert importer.data == csv_data

    @pytest.mark.django_db
    def test_make_playlist(self, importer, sound):
        importer.data = csv_data
        importer.make_playlist()
        track_artists = sound.track_set.all().values_list("artist", flat=True)
        csv_artists = {r["artist"] for r in csv_data}
        assert set(track_artists) == csv_artists
        # TODO: check other values
