Commit 61eb8e4d authored by Renon's avatar Renon Committed by Renon

Test for _do_import with replace

parent 8d949933
......@@ -89,6 +89,7 @@ class ImportJobFactory(factory.django.DjangoModelFactory):
batch = factory.SubFactory(ImportBatchFactory)
source = factory.Faker("url")
mbid = factory.Faker("uuid4")
replace_if_duplicate = factory.Faker("boolean")
class Meta:
model = "music.ImportJob"
......
......@@ -214,7 +214,9 @@ class Command(BaseCommand):
return batch, errors
def import_file(self, path, batch, import_handler, options):
job = batch.jobs.create(source="file://" + path)
job = batch.jobs.create(
source="file://" + path, replace_if_duplicate=options["replace"]
)
if not options["in_place"]:
name = os.path.basename(path)
with open(path, "rb") as f:
......
......@@ -118,7 +118,7 @@ def test_run_import_skipping_accoustid(factories, mocker):
path = os.path.join(DATA_DIR, "test.ogg")
job = factories["music.FileImportJob"](audio_file__path=path)
tasks.import_job_run(import_job_id=job.pk, use_acoustid=False)
m.assert_called_once_with(job, False, use_acoustid=False)
m.assert_called_once_with(job, use_acoustid=False)
def test__do_import_skipping_accoustid(factories, mocker):
......@@ -130,7 +130,7 @@ def test__do_import_skipping_accoustid(factories, mocker):
path = os.path.join(DATA_DIR, "test.ogg")
job = factories["music.FileImportJob"](mbid=None, audio_file__path=path)
p = job.audio_file.path
tasks._do_import(job, replace=False, use_acoustid=False)
tasks._do_import(job, use_acoustid=False)
m.assert_called_once_with(p)
......@@ -144,10 +144,27 @@ def test__do_import_skipping_accoustid_if_no_key(factories, mocker, preferences)
path = os.path.join(DATA_DIR, "test.ogg")
job = factories["music.FileImportJob"](mbid=None, audio_file__path=path)
p = job.audio_file.path
tasks._do_import(job, replace=False, use_acoustid=False)
tasks._do_import(job, use_acoustid=False)
m.assert_called_once_with(p)
def test__do_import_replace_if_duplicate(factories, mocker):
existing_file = factories["music.TrackFile"]()
existing_track = existing_file.track
path = os.path.join(DATA_DIR, "test.ogg")
mocker.patch(
"funkwhale_api.providers.audiofile.tasks.import_track_data_from_path",
return_value=existing_track,
)
job = factories["music.FileImportJob"](
replace_if_duplicate=True, audio_file__path=path
)
tasks._do_import(job)
with pytest.raises(existing_file.__class__.DoesNotExist):
existing_file.refresh_from_db()
assert existing_file.creation_date != job.track_file.creation_date
def test_import_job_skip_if_already_exists(artists, albums, tracks, factories, mocker):
path = os.path.join(DATA_DIR, "test.ogg")
mbid = "9968a9d6-8d92-4051-8f76-674e157b6eed"
......
......@@ -6,6 +6,7 @@ from django.core.management import call_command
from django.core.management.base import CommandError
from funkwhale_api.providers.audiofile import tasks
from funkwhale_api.music.models import ImportJob
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "files")
......@@ -115,6 +116,19 @@ def test_import_with_multiple_argument(factories, mocker):
mocked_filter.assert_called_once_with([path1, path2])
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):
m = mocker.patch("funkwhale_api.music.tasks.import_job_run")
user = factories["users.User"](username="me")
......
Added replace flag during import to replace already present tracks with a new version of their track file (#222)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment