Skip to content
Snippets Groups Projects
test_import_audio_file.py 6.99 KiB
Newer Older
  • Learn to ignore specific revisions
  • import datetime
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    import pytest
    
    from django.core.management import call_command
    from django.core.management.base import CommandError
    
    
    from funkwhale_api.providers.audiofile import tasks
    
    R En's avatar
    R En committed
    from funkwhale_api.music.models import ImportJob
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")
    
    def test_can_create_track_from_file_metadata_no_mbid(db, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "artist": ["Test artist"],
            "album": ["Test album"],
            "title": ["Test track"],
            "TRACKNUMBER": ["4"],
            "date": ["2012-08-15"],
    
        mocker.patch("mutagen.File", return_value=metadata)
        mocker.patch(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "funkwhale_api.music.metadata.Metadata.get_file_type", return_value="OggVorbis"
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        track = tasks.import_track_data_from_path(os.path.join(DATA_DIR, "dummy_file.ogg"))
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert track.title == metadata["title"][0]
    
        assert track.position == 4
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert track.album.title == metadata["album"][0]
    
        assert track.album.release_date == datetime.date(2012, 8, 15)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert track.artist.name == metadata["artist"][0]
    
        assert track.artist.mbid is None
    
    
    def test_can_create_track_from_file_metadata_mbid(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        album = factories["music.Album"]()
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "funkwhale_api.music.models.Album.get_or_create_from_api",
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "release": {
                "id": album.mbid,
                "medium-list": [
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                        "track-list": [
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                                "id": "03baca8b-855a-3c05-8f3d-d3235287d84d",
                                "position": "4",
                                "number": "4",
                                "recording": {
                                    "id": "2109e376-132b-40ad-b993-2bb6812e19d4",
                                    "title": "Teen Age Riot",
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                        "track-count": 1,
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        mocker.patch("funkwhale_api.musicbrainz.api.releases.get", return_value=album_data)
        track_data = album_data["release"]["medium-list"][0]["track-list"][0]
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "musicbrainz_albumid": [album.mbid],
            "musicbrainz_trackid": [track_data["recording"]["id"]],
    
        mocker.patch("mutagen.File", return_value=metadata)
        mocker.patch(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "funkwhale_api.music.metadata.Metadata.get_file_type", return_value="OggVorbis"
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        track = tasks.import_track_data_from_path(os.path.join(DATA_DIR, "dummy_file.ogg"))
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert track.title == track_data["recording"]["title"]
        assert track.mbid == track_data["recording"]["id"]
    
        assert track.position == 4
        assert track.album == album
        assert track.artist == album.artist
    
    
    
    def test_management_command_requires_a_valid_username(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        path = os.path.join(DATA_DIR, "dummy_file.ogg")
    
        factories["users.User"](username="me")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        mocker.patch(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "funkwhale_api.providers.audiofile.management.commands.import_files.Command.do_import",  # noqa
            return_value=(mocker.MagicMock(), []),
        )
    
        with pytest.raises(CommandError):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            call_command("import_files", path, username="not_me", interactive=False)
        call_command("import_files", path, username="me", interactive=False)
    
    def test_in_place_import_only_from_music_dir(factories, settings):
    
        factories["users.User"](username="me")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        settings.MUSIC_DIRECTORY_PATH = "/nope"
        path = os.path.join(DATA_DIR, "dummy_file.ogg")
    
        with pytest.raises(CommandError):
            call_command(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                "import_files", path, in_place=True, username="me", interactive=False
    
    def test_import_with_multiple_argument(factories, mocker):
        factories["users.User"](username="me")
        path1 = os.path.join(DATA_DIR, "dummy_file.ogg")
        path2 = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
        mocked_filter = mocker.patch(
            "funkwhale_api.providers.audiofile.management.commands.import_files.Command.filter_matching",
            return_value=({"new": [], "skipped": []}),
        )
        call_command("import_files", path1, path2, username="me", interactive=False)
        mocked_filter.assert_called_once_with([path1, path2])
    
    
    
    R En's avatar
    R En committed
    def test_import_with_replace_flag(factories, mocker):
        factories["users.User"](username="me")
        path = os.path.join(DATA_DIR, "dummy_file.ogg")
        mocked_job_run = mocker.patch("funkwhale_api.music.tasks.import_job_run")
        call_command("import_files", path, username="me", replace=True, interactive=False)
        created_job = ImportJob.objects.latest("id")
    
        assert created_job.replace_if_duplicate is True
        mocked_job_run.assert_called_once_with(
            import_job_id=created_job.id, use_acoustid=False
        )
    
    
    
    def test_import_files_creates_a_batch_and_job(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
        user = factories["users.User"](username="me")
        path = os.path.join(DATA_DIR, "dummy_file.ogg")
        call_command("import_files", path, username="me", async=False, interactive=False)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        batch = user.imports.latest("id")
        assert batch.source == "shell"
    
        assert batch.jobs.count() == 1
    
        job = batch.jobs.first()
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert job.status == "pending"
        with open(path, "rb") as f:
    
            assert job.audio_file.read() == f.read()
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        assert job.source == "file://" + path
        m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
    
    def test_import_files_skip_if_path_already_imported(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        user = factories["users.User"](username="me")
        path = os.path.join(DATA_DIR, "dummy_file.ogg")
    
        factories["music.TrackFile"](source="file://{}".format(path))
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        call_command("import_files", path, username="me", async=False, interactive=False)
    
    def test_import_files_works_with_utf8_file_name(factories, mocker):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
        user = factories["users.User"](username="me")
        path = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
        call_command("import_files", path, username="me", async=False, interactive=False)
        batch = user.imports.latest("id")
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
    
    def test_import_files_in_place(factories, mocker, settings):
        settings.MUSIC_DIRECTORY_PATH = DATA_DIR
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
        user = factories["users.User"](username="me")
        path = os.path.join(DATA_DIR, "utf8-éà◌.ogg")
    
        call_command(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "import_files",
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            username="me",
    
            async=False,
            in_place=True,
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            interactive=False,
        )
        batch = user.imports.latest("id")
    
        job = batch.jobs.first()
        assert bool(job.audio_file) is False
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        m.assert_called_once_with(import_job_id=job.pk, use_acoustid=False)
    
    def test_storage_rename_utf_8_files(factories):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        tf = factories["music.TrackFile"](audio_file__filename="été.ogg")
        assert tf.audio_file.name.endswith("ete.ogg")