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_delete(self): coll = OrganizationCollectionFactory(mnemonic='coll1') OrganizationCollectionFactory(version='v1', is_latest_version=True, mnemonic='coll1', organization=coll.organization) user = UserProfileFactory(organizations=[coll.organization]) self.assertEqual(coll.versions.count(), 2) response = self.client.delete('/collections/coll1/v1/', HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json') self.assertEqual(response.status_code, 204) self.assertEqual(coll.versions.count(), 1) response = self.client.delete('/collections/coll1/', HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json') self.assertEqual(response.status_code, 204) self.assertEqual(coll.versions.count(), 0) self.assertFalse(Collection.objects.filter(mnemonic='coll1').exists())
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_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_get_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1') response = self.client.get( '/collections/coll1/', format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['uuid'], str(coll.id)) self.assertEqual(response.data['short_code'], 'coll1') self.assertEqual(response.data['url'], coll.uri) response = self.client.get( coll.uri, format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['uuid'], str(coll.id)) self.assertEqual(response.data['short_code'], 'coll1') response = self.client.get( coll.uri + 'summary/', format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['uuid'], str(coll.id)) self.assertEqual(response.data['active_concepts'], 0) self.assertEqual(response.data['active_mappings'], 0) self.assertEqual(response.data['versions'], 1)
def test_validate_openmrs_schema_matching_name_locale(self): ch_locale = LocalizedTextFactory(locale_preferred=False, locale='ch') concept1 = ConceptFactory(names=[ch_locale]) collection = OrganizationCollectionFactory( custom_validation_schema=CUSTOM_VALIDATION_SCHEMA_OPENMRS) collection.concepts.add(concept1) concept1_reference = CollectionReference(expression=concept1.uri) concept1_reference.save() collection.references.add(concept1_reference) en_locale1 = LocalizedTextFactory(locale='en', locale_preferred=False, name='name') en_locale2 = LocalizedTextFactory(locale='en', locale_preferred=True, name='name') concept2 = ConceptFactory(names=[en_locale1, en_locale2]) concept2_reference = CollectionReference(expression=concept2.uri) with self.assertRaises(ValidationError) as ex: collection.validate(concept2_reference) self.assertEqual( ex.exception.message_dict, { 'names': [ 'Concept fully specified name must be unique for same collection and locale.' ] })
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_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_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_get_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1') response = self.client.get('/collections/', format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) response = self.client.get('/orgs/{}/collections/?verbose=true'.format( coll.parent.mnemonic), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) concept = ConceptFactory() reference = CollectionReference(expression=concept.uri) reference.full_clean() reference.save() coll.references.add(reference) coll.concepts.set(reference.concepts) response = self.client.get( '/orgs/{}/collections/?contains={}&includeReferences=true'.format( coll.parent.mnemonic, concept.uri), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_add_references_task(self): collection = OrganizationCollectionFactory() concept1 = ConceptFactory() concept2 = ConceptFactory() mapping1 = MappingFactory(parent=concept2.parent, from_concept=concept2.get_latest_version(), to_concept=concept1.get_latest_version()) mapping2 = MappingFactory() added_references, errors = add_references( collection.created_by, dict(expressions=[ obj.get_latest_version().version_url for obj in [concept1, concept2, mapping2] ]), collection, None, True) self.assertEqual(errors, {}) self.assertEqual(len(added_references), 4) self.assertListEqual( sorted([ref.expression for ref in added_references]), sorted([ concept1.get_latest_version().version_url, concept2.get_latest_version().version_url, mapping1.get_latest_version().version_url, mapping2.get_latest_version().version_url, ]))
def test_update_order(self): org = OrganizationFactory(mnemonic='org-1') org_pin1 = Pin(organization=org, resource=OrganizationSourceFactory(organization=org)) org_pin1.save() self.assertEqual(org_pin1.order, 0) org_pin2 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin2.save() self.assertEqual(org_pin2.order, 1) org_pin3 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin3.save() self.assertEqual(org_pin3.order, 2) org_pin3.to(0) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin3.order, 0) self.assertEqual(org_pin1.order, 1) self.assertEqual(org_pin2.order, 2) org_pin3.to(2) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin1.order, 0) self.assertEqual(org_pin2.order, 1) self.assertEqual(org_pin3.order, 2) org_pin3.to(1) org_pin1.refresh_from_db() org_pin2.refresh_from_db() org_pin3.refresh_from_db() self.assertEqual(org_pin1.order, 0) self.assertEqual(org_pin3.order, 1) self.assertEqual(org_pin2.order, 2)
def test_put_401(self): coll = OrganizationCollectionFactory(mnemonic='coll1', name='Collection') self.assertEqual(coll.versions.count(), 1) response = self.client.put('/collections/coll1/', dict(name='Collection1'), format='json') self.assertEqual(response.status_code, 401)
def test_get_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1') response = self.client.get( '/collections/', format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) response = self.client.get( '/orgs/{}/collections/?verbose=true'.format(coll.parent.mnemonic), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) for attr in ['active_concepts', 'active_mappings', 'versions', 'summary']: self.assertFalse(attr in response.data[0]) concept = ConceptFactory() reference = CollectionReference(expression=concept.uri) reference.full_clean() reference.save() coll.references.add(reference) coll.concepts.set(reference.concepts) response = self.client.get( '/orgs/{}/collections/?contains={}&includeReferences=true'.format( coll.parent.mnemonic, concept.get_latest_version().uri ), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) response = self.client.get( '/orgs/{}/collections/?verbose=true&includeSummary=true'.format(coll.parent.mnemonic), format='json' ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['short_code'], 'coll1') self.assertEqual(response.data[0]['id'], 'coll1') self.assertEqual(response.data[0]['url'], coll.uri) self.assertEqual(response.data[0]['summary'], dict(versions=1, active_concepts=1, active_mappings=0))
def test_last_child_update(self): collection = OrganizationCollectionFactory() self.assertEqual(collection.last_child_update, collection.updated_at) mapping = MappingFactory() collection.mappings.add(mapping) self.assertEqual(collection.last_child_update, mapping.updated_at) concept = ConceptFactory() collection.concepts.add(concept) self.assertEqual(collection.last_child_update, concept.updated_at)
def test_default_order(self): org = OrganizationFactory(mnemonic='org-1') source = OrganizationSourceFactory(organization=org) user = UserProfileFactory(username='******') org_pin1 = Pin(organization=org, resource=source) org_pin1.save() self.assertEqual(org_pin1.order, 0) org_pin2 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin2.save() self.assertEqual(org_pin2.order, 1) org_pin2.delete() org_pin3 = Pin( organization=org, resource=OrganizationCollectionFactory(organization=org)) org_pin3.save() self.assertEqual(org_pin3.order, 1) user_pin1 = Pin(user=user, resource=source) user_pin1.save() self.assertEqual(user_pin1.order, 0) user_pin2 = Pin( user=user, resource=OrganizationCollectionFactory(organization=org)) user_pin2.save() self.assertEqual(user_pin2.order, 1) user_pin2.delete() user_pin3 = Pin( user=user, resource=OrganizationCollectionFactory(organization=org)) user_pin3.save() self.assertEqual(user_pin3.order, 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_put_405(self): coll = OrganizationCollectionFactory(mnemonic='coll1', name='Collection') user = UserProfileFactory(organizations=[coll.organization]) self.assertEqual(coll.versions.count(), 1) response = self.client.put('/collections/coll1/', dict(name='Collection1'), HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json') self.assertEqual(response.status_code, 405)
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_put_400(self): coll = OrganizationCollectionFactory(mnemonic='coll1', name='Collection') user = UserProfileFactory(organizations=[coll.organization]) self.assertEqual(coll.versions.count(), 1) response = self.client.put( coll.uri, dict(name=''), HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json' ) self.assertEqual(response.status_code, 400) self.assertEqual(response.data, dict(name=[ErrorDetail(string='This field may not be blank.', code='blank')]))
def test_put_200(self): coll = OrganizationCollectionFactory(mnemonic='coll1', name='Collection') user = UserProfileFactory(organizations=[coll.organization]) self.assertEqual(coll.versions.count(), 1) response = self.client.put(coll.uri, dict(name='Collection1'), HTTP_AUTHORIZATION='Token ' + user.get_token(), format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], 'Collection1') coll.refresh_from_db() self.assertEqual(coll.name, 'Collection1') self.assertEqual(coll.versions.count(), 1)
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())
def test_validate_reference_already_exists(self): collection = OrganizationCollectionFactory() ch_locale = LocalizedTextFactory(locale_preferred=True, locale='ch') en_locale = LocalizedTextFactory(locale_preferred=True, locale='en') concept = ConceptFactory(names=[ch_locale, en_locale]) reference = CollectionReference(expression=concept.uri) reference.save() collection.references.add(reference) self.assertEqual(collection.references.count(), 1) with self.assertRaises(ValidationError) as ex: collection.validate(reference) self.assertEqual( ex.exception.message_dict, { concept.get_latest_version().uri: [ 'Concept or Mapping reference name must be unique in a collection.' ] })
def test_get_resource(self): org = OrganizationFactory() source = OrganizationSourceFactory(organization=org) collection = OrganizationCollectionFactory(organization=org) self.assertEqual( Pin.get_resource(resource_type='Source', resource_id=source.id), source) self.assertEqual( Pin.get_resource(resource_type='collection', resource_id=collection.id), collection) self.assertEqual( Pin.get_resource(resource_type='org', resource_id=org.id), org) self.assertEqual( Pin.get_resource(resource_type='organization', resource_id=org.id), org) self.assertEqual( Pin.get_resource(resource_type='organization', resource_id=123), None) self.assertEqual( Pin.get_resource(resource_type='foobar', resource_id=123), None)
def test_org_active_inactive_should_affect_children(self): org = OrganizationFactory(is_active=True) source = OrganizationSourceFactory(organization=org, is_active=True) collection = OrganizationCollectionFactory(organization=org, is_active=True) org.is_active = False org.save() source.refresh_from_db() collection.refresh_from_db() self.assertFalse(org.is_active) self.assertFalse(source.is_active) self.assertFalse(collection.is_active) org.is_active = True org.save() source.refresh_from_db() collection.refresh_from_db() self.assertTrue(org.is_active) self.assertTrue(source.is_active) self.assertTrue(collection.is_active)
def test_user_active_inactive_should_affect_children(self): user = UserProfileFactory(is_active=True) source = OrganizationSourceFactory(user=user, is_active=True) collection = OrganizationCollectionFactory(user=user, is_active=True) user.is_active = False user.save() source.refresh_from_db() collection.refresh_from_db() self.assertFalse(user.is_active) self.assertFalse(source.is_active) self.assertFalse(collection.is_active) user.is_active = True user.save() source.refresh_from_db() collection.refresh_from_db() self.assertTrue(user.is_active) self.assertTrue(source.is_active) self.assertTrue(collection.is_active)
def test_last_concept_update(self): collection = OrganizationCollectionFactory() self.assertIsNone(collection.last_concept_update) concept = ConceptFactory() collection.concepts.add(concept) self.assertEqual(collection.last_concept_update, concept.updated_at)
def test_last_mapping_update(self): collection = OrganizationCollectionFactory() self.assertIsNone(collection.last_mapping_update) mapping = MappingFactory() collection.mappings.add(mapping) self.assertEqual(collection.last_mapping_update, mapping.updated_at)