diff --git a/backend/src/repository/genomic_unit_collection.py b/backend/src/repository/genomic_unit_collection.py index c44e135b..478878c0 100644 --- a/backend/src/repository/genomic_unit_collection.py +++ b/backend/src/repository/genomic_unit_collection.py @@ -146,7 +146,7 @@ def add_transcript_to_genomic_unit(self, genomic_unit, transcript_id): {'$addToSet': {'transcripts': {'transcript_id': transcript_id, 'annotations': []}}}, ) - def __update_genomic_unit_by_mongo_id(self, genomic_unit_document): + def update_genomic_unit_by_mongo_id(self, genomic_unit_document): """ Takes a genomic unit and overwrites the existing object based on the object's id """ genomic_unit_id = genomic_unit_document['_id'] @@ -180,7 +180,7 @@ def annotate_genomic_unit(self, genomic_unit, genomic_annotation): genomic_unit_json = self.find_genomic_unit(genomic_unit) self.__add_to_annotations_from_document(genomic_unit_json['annotations'], dataset_name, genomic_annotation) - updated_document = self.__update_genomic_unit_by_mongo_id(genomic_unit_json) + updated_document = self.update_genomic_unit_by_mongo_id(genomic_unit_json) return updated_document @@ -197,7 +197,7 @@ def __annotate_transcript_dataset(self, genomic_unit, transcript_id: str, genomi if transcript["transcript_id"] == transcript_id: self.__add_to_annotations_from_document(transcript['annotations'], dataset_name, genomic_annotation) - return self.__update_genomic_unit_by_mongo_id(genomic_unit_document) + return self.update_genomic_unit_by_mongo_id(genomic_unit_document) def __add_to_annotations_from_document(self, list_of_annotations, dataset_name, genomic_annotation): new_dataset_entry, annotation_entry = create_annotation_entry(dataset_name, genomic_annotation) @@ -219,7 +219,7 @@ def annotate_genomic_unit_with_file(self, genomic_unit, genomic_annotation): for annotation in genomic_unit_document['annotations']: if data_set in annotation: annotation[data_set][0]['value'].append(genomic_annotation['value']) - return self.__update_genomic_unit_by_mongo_id(genomic_unit_document) + return self.update_genomic_unit_by_mongo_id(genomic_unit_document) annotation_data_set = { genomic_annotation['data_set']: [{ @@ -230,7 +230,7 @@ def annotate_genomic_unit_with_file(self, genomic_unit, genomic_annotation): } genomic_unit_document['annotations'].append(annotation_data_set) - return self.__update_genomic_unit_by_mongo_id(genomic_unit_document) + return self.update_genomic_unit_by_mongo_id(genomic_unit_document) def update_genomic_unit_file_annotation(self, genomic_unit, data_set, annotation_value, file_id_old): """ Replaces existing annotation image with new image """ @@ -245,7 +245,7 @@ def update_genomic_unit_file_annotation(self, genomic_unit, data_set, annotation annotation[data_set][0]['value'].append(annotation_value) break - self.__update_genomic_unit_by_mongo_id(genomic_unit_document) + self.update_genomic_unit_by_mongo_id(genomic_unit_document) return @@ -261,7 +261,7 @@ def remove_genomic_unit_file_annotation(self, genomic_unit, data_set, file_id): annotation[data_set][0]['value'].pop(i) break - return self.__update_genomic_unit_by_mongo_id(genomic_unit_document) + return self.update_genomic_unit_by_mongo_id(genomic_unit_document) def create_genomic_unit(self, genomic_unit): """ diff --git a/backend/tests/unit/repository/test_genomic_unit_collection.py b/backend/tests/unit/repository/test_genomic_unit_collection.py index 28b3a209..e2f65a7a 100644 --- a/backend/tests/unit/repository/test_genomic_unit_collection.py +++ b/backend/tests/unit/repository/test_genomic_unit_collection.py @@ -1,7 +1,6 @@ """ Manages the genomic unit collection. Including reading, writing, fetching various genomic units. """ from unittest.mock import Mock import copy -from pymongo import ReturnDocument import pytest from bson import ObjectId @@ -121,25 +120,6 @@ def test_add_transcript_to_genomic_unit(genomic_unit_collection): ) -def test_update_genomic_unit_annotation_by_mongo_id(genomic_unit_collection): - """ - Verifies that the update genomic unit with mongo id function makes the pymongo call with the correct params - """ - genomic_unit_document = { - "_id": ObjectId('62fbfa5f616a9799131174c8'), - "hgvs_variant": "NM_001017980.3:c.164G>T", - "transcripts": [], - "annotations": {}, - } - - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id(genomic_unit_document) - - genomic_unit_collection.collection.find_one_and_update.assert_called_once_with({ - '_id': ObjectId('62fbfa5f616a9799131174c8') - }, {'$set': genomic_unit_document}, - return_document=ReturnDocument.AFTER) - - def test_annotate_transcript_genomic_unit(genomic_unit_collection): """ Verifies that a transcript annotates a genomic unit properly """ genomic_unit = {'unit': 'NM_001017980.3:c.164G>T', 'type': GenomicUnitType.HGVS_VARIANT} @@ -160,19 +140,18 @@ def test_annotate_transcript_genomic_unit(genomic_unit_collection): } ) + expected_genomic_unit = { + "_id": ObjectId("62fbfa5f616a9799131174ca"), + "hgvs_variant": "NM_001017980.3:c.164G>T", + "transcripts": [{'transcript_id': 'NM_001363810.1', 'annotations': []}], + "annotations": {}, + } + genomic_unit_collection.annotate_genomic_unit(genomic_unit, transcript_annotation_unit) - genomic_unit_collection.collection.find_one_and_update.assert_called_once_with({ - '_id': ObjectId("62fbfa5f616a9799131174ca") - }, { - '$set': { - "_id": ObjectId("62fbfa5f616a9799131174ca"), - "hgvs_variant": "NM_001017980.3:c.164G>T", - "transcripts": [{'transcript_id': 'NM_001363810.1', 'annotations': []}], - "annotations": {}, - } - }, - return_document=ReturnDocument.AFTER) + genomic_unit_collection.collection.find_one_and_update.assert_called_once() + actual_updated_genomic_unit = genomic_unit_collection.collection.find_one_and_update.call_args_list[0][0][1]['$set'] + assert actual_updated_genomic_unit == expected_genomic_unit @pytest.mark.parametrize( @@ -231,11 +210,12 @@ def test_annotation_genomic_unit_with_file(genomic_unit_collection, get_annotati expected_genomic_unit['annotations'].append(expected_annotation_update) genomic_unit_collection.collection.find_one.return_value = genomic_unit - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id = Mock() genomic_unit_collection.annotate_genomic_unit_with_file(incoming_genomic_unit, annotation_unit) - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id.assert_called_once_with(expected_genomic_unit) + genomic_unit_collection.collection.find_one_and_update.assert_called_once() + actual_updated_genomic_unit = genomic_unit_collection.collection.find_one_and_update.call_args_list[0][0][1]['$set'] + assert actual_updated_genomic_unit == expected_genomic_unit def test_update_existing_genomic_unit_file_annotation(genomic_unit_collection, get_annotation_json): @@ -266,13 +246,13 @@ def test_update_existing_genomic_unit_file_annotation(genomic_unit_collection, g expected_genomic_unit['annotations'].append(expected_annotation_update) - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id = Mock() - genomic_unit_collection.update_genomic_unit_file_annotation( incoming_genomic_unit, data_set, annotation_unit_value, file_id_old ) - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id.assert_called_once_with(expected_genomic_unit) + genomic_unit_collection.collection.find_one_and_update.assert_called_once() + actual_updated_genomic_unit = genomic_unit_collection.collection.find_one_and_update.call_args_list[0][0][1]['$set'] + assert actual_updated_genomic_unit == expected_genomic_unit def test_remove_existing_genomic_unit_file_annotation(genomic_unit_collection, get_annotation_json): @@ -299,11 +279,11 @@ def test_remove_existing_genomic_unit_file_annotation(genomic_unit_collection, g }] }) - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id = Mock() - genomic_unit_collection.remove_genomic_unit_file_annotation(incoming_genomic_unit, data_set, file_id) - genomic_unit_collection.update_genomic_unit_annotation_by_mongo_id.assert_called_once_with(expected_genomic_unit) + genomic_unit_collection.collection.find_one_and_update.assert_called_once() + actual_updated_genomic_unit = genomic_unit_collection.collection.find_one_and_update.call_args_list[0][0][1]['$set'] + assert actual_updated_genomic_unit == expected_genomic_unit @pytest.fixture(name="prepare_test_annotate", scope="function")