Skip to content

Commit

Permalink
Allow merging cohorts without magic collections
Browse files Browse the repository at this point in the history
  • Loading branch information
danlamanna committed Jul 5, 2023
1 parent 3373e39 commit 42db915
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions isic/ingest/services/cohort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ def cohort_merge(*, dest_cohort: Cohort, src_cohort: Cohort) -> None:
ZipUpload.objects.filter(cohort=src_cohort).update(cohort=dest_cohort)
MetadataFile.objects.filter(cohort=src_cohort).update(cohort=dest_cohort)

collection_merge_magic_collections(
dest_collection=dest_cohort.collection, src_collection=src_cohort.collection
)
if src_cohort.collection and dest_cohort.collection:
collection_merge_magic_collections(
dest_collection=dest_cohort.collection, src_collection=src_cohort.collection
)
elif src_cohort.collection:
dest_cohort.collection = src_cohort.collection
# no point in repointing the src collection to the dest collection since it's going away

src_cohort.delete()
# dest_cohort has to be saved after the delete to avoid a unique constraint violation
dest_cohort.save()
17 changes: 17 additions & 0 deletions isic/ingest/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ def test_merge_cohorts(full_cohort):
)


@pytest.mark.django_db
def test_merge_cohorts_missing_magic_collections(full_cohort):
"""Test that merging a cohort into a cohort with no magic collections works."""
dest_cohort, src_cohort = full_cohort(), full_cohort()
# coerce the copyright license to test the happy path
dest_cohort.copyright_license = src_cohort.copyright_license
dest_cohort.save()
dest_cohort.collection.delete()
dest_cohort.refresh_from_db()

total_cohort_images = set(src_cohort.collection.images.values_list("pk", flat=True))

cohort_merge(dest_cohort=dest_cohort, src_cohort=src_cohort)
dest_cohort.refresh_from_db()
assert set(dest_cohort.collection.images.values_list("pk", flat=True)) == total_cohort_images


@pytest.mark.django_db
def test_merge_cohorts_conflicting_fields(full_cohort):
cohort_a, cohort_b = full_cohort(), full_cohort()
Expand Down

0 comments on commit 42db915

Please sign in to comment.