def test_add_references_openmrs_schema(self): collection = OrganizationCollectionFactory( custom_validation_schema=CUSTOM_VALIDATION_SCHEMA_OPENMRS) self.assertEqual(collection.concepts.count(), 0) self.assertEqual(collection.references.count(), 0) source = OrganizationSourceFactory() concept = ConceptFactory(parent=source, sources=[source]) concept_expression = concept.uri collection.add_references([concept_expression]) self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.references.first().expression, concept.get_latest_version().uri) self.assertEqual(collection.concepts.first(), concept.get_latest_version()) concept2 = ConceptFactory(parent=source, sources=[source]) collection.add_references([concept2.uri]) self.assertEqual(collection.concepts.count(), 2) self.assertEqual(collection.references.count(), 2)
def test_delete_references(self): collection = OrganizationCollectionFactory() source = OrganizationSourceFactory() concept1 = ConceptFactory(parent=source) concept2 = ConceptFactory(parent=source) mapping = MappingFactory(from_concept=concept1, to_concept=concept2, parent=source) collection.add_references([concept1.uri, concept2.uri, mapping.uri]) self.assertEqual(collection.concepts.count(), 2) self.assertEqual(collection.mappings.count(), 1) self.assertEqual(collection.references.count(), 3) collection.delete_references([ concept2.get_latest_version().uri, mapping.get_latest_version().uri ]) self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.mappings.count(), 0) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.concepts.first().uri, concept1.get_latest_version().uri) self.assertEqual(collection.references.first().expression, concept1.get_latest_version().uri)
def test_add_references(self): collection = OrganizationCollectionFactory() self.assertEqual(collection.concepts.count(), 0) self.assertEqual(collection.references.count(), 0) self.assertEqual(collection.active_concepts, 0) source = OrganizationSourceFactory() concept = ConceptFactory(parent=source, sources=[source]) concept_expression = concept.uri collection.add_references([concept_expression]) collection.refresh_from_db() self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.references.first().expression, concept.get_latest_version().uri) self.assertEqual(collection.concepts.first(), concept.get_latest_version()) self.assertEqual(collection.active_concepts, 1) result = collection.add_references([concept_expression]) self.assertEqual( result, { concept_expression: [ 'Concept or Mapping reference name must be unique in a collection.' ] }) collection.refresh_from_db() self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.references.count(), 1) self.assertEqual(collection.active_concepts, 1)
def test_seed_children_task(self): collection = OrganizationCollectionFactory() concept = ConceptFactory() mapping = MappingFactory() concept_latest_version = concept.get_latest_version() mapping_latest_version = mapping.get_latest_version() collection.add_references([ concept_latest_version.version_url, mapping_latest_version.version_url ]) self.assertEqual(collection.references.count(), 2) self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.mappings.count(), 1) collection_v1 = OrganizationCollectionFactory( organization=collection.organization, version='v1', mnemonic=collection.mnemonic) self.assertEqual(collection_v1.references.count(), 0) self.assertEqual(collection_v1.concepts.count(), 0) self.assertEqual(collection_v1.mappings.count(), 0) seed_children('collection', collection_v1.id) # pylint: disable=no-value-for-parameter self.assertEqual(collection_v1.references.count(), 2) self.assertEqual(collection_v1.concepts.count(), 1) self.assertEqual(collection_v1.mappings.count(), 1) self.assertEqual( list(collection_v1.references.values_list('expression', flat=True)), list(collection.references.values_list('expression', flat=True)), )
def test_seed_references(self): collection1 = OrganizationCollectionFactory() collection2 = OrganizationCollectionFactory( version='v1', mnemonic=collection1.mnemonic, organization=collection1.organization) self.assertTrue(collection1.is_head) self.assertFalse(collection2.is_head) source = OrganizationSourceFactory() concept = ConceptFactory(parent=source, sources=[source]) concept_expression = concept.uri collection1.add_references([concept_expression]) self.assertEqual(collection1.references.count(), 1) self.assertEqual(collection2.references.count(), 0) collection2.seed_references() self.assertEqual(collection1.references.count(), 1) self.assertEqual(collection2.references.count(), 1) self.assertEqual(collection1.references.first().expression, collection2.references.first().expression) self.assertNotEqual(collection1.references.first().id, collection2.references.first().id)
def test_get_200(self): response = self.client.get('/mappings/', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0) mapping = MappingFactory() response = self.client.get('/mappings/', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) response = self.client.get(mapping.parent.mappings_url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) collection = OrganizationCollectionFactory() response = self.client.get(collection.mappings_url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 0) collection.add_references(expressions=[mapping.uri]) response = self.client.get(collection.mappings_url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_export_collection(self, s3_mock): # pylint: disable=too-many-locals s3_mock.url_for = Mock(return_value='https://s3-url') s3_mock.upload_file = Mock() source = OrganizationSourceFactory() concept1 = ConceptFactory(parent=source) concept2 = ConceptFactory(parent=source) mapping = MappingFactory(from_concept=concept2, to_concept=concept1, parent=source) collection = OrganizationCollectionFactory() collection.add_references([concept1.uri, concept2.uri, mapping.uri]) collection.refresh_from_db() export_collection(collection.id) # pylint: disable=no-value-for-parameter latest_temp_dir = get_latest_dir_in_path('/tmp/') zipped_file = zipfile.ZipFile(latest_temp_dir + '/export.zip') exported_data = json.loads( zipped_file.read('export.json').decode('utf-8')) self.assertEqual( exported_data, { **CollectionDetailSerializer(collection).data, 'concepts': ANY, 'mappings': ANY }) exported_concepts = exported_data['concepts'] expected_concepts = ConceptVersionDetailSerializer( [concept2.get_latest_version(), concept1.get_latest_version()], many=True).data self.assertEqual(len(exported_concepts), 2) self.assertIn(expected_concepts[0], exported_concepts) self.assertIn(expected_concepts[1], exported_concepts) exported_mappings = exported_data['mappings'] expected_mappings = MappingDetailSerializer( [mapping.get_latest_version()], many=True).data self.assertEqual(len(exported_mappings), 1) self.assertEqual(expected_mappings, exported_mappings) s3_upload_key = collection.export_path s3_mock.upload_file.assert_called_once_with(key=s3_upload_key, file_path=latest_temp_dir + '/export.zip', binary=True) s3_mock.url_for.assert_called_once_with(s3_upload_key) import shutil shutil.rmtree(latest_temp_dir)
def test_get_concepts(self): collection = OrganizationCollectionFactory() source = OrganizationSourceFactory() concept = ConceptFactory(parent=source, sources=[source]) concept_expression = concept.uri collection.add_references([concept_expression]) concepts = collection.get_concepts() self.assertEqual(concepts.count(), 1) self.assertEqual(concepts.first(), concept.get_latest_version()) self.assertEqual(collection.get_concepts(start=0, end=10).count(), 1) self.assertEqual(collection.get_concepts(start=1, end=2).count(), 0)
def test_version_delete_400(self): # sources content referred in a private collection concept = ConceptFactory(parent=self.source_v1) collection = OrganizationCollectionFactory(public_access='None') collection.add_references([concept.uri]) self.assertEqual(collection.concepts.count(), 1) self.assertEqual(collection.concepts.first(), concept.get_latest_version()) response = self.client.delete( self.source_v1.uri, HTTP_AUTHORIZATION='Token ' + self.token, format='json' ) self.assertEqual(response.status_code, 400) self.assertEqual(response.data, {'detail': [CONTENT_REFERRED_PRIVATELY.format(self.source_v1.mnemonic)]}) self.assertEqual(self.source.versions.count(), 2) self.assertTrue(self.source.versions.filter(version='v1').exists())