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_reference_type(self): reference = CollectionReference(expression='') self.assertFalse(reference.reference_type) reference = CollectionReference(expression=None) self.assertFalse(reference.reference_type) reference = CollectionReference( expression= '/parent/parent-mnemonic/sources/source-mnemonic/concepts/concept-mnemonic/' ) self.assertEqual(reference.reference_type, 'concepts')
def test_get_concepts(self): reference = CollectionReference() reference.expression = '/unknown/uri/' unknown_expression_concepts = reference.get_concepts() self.assertTrue(isinstance(unknown_expression_concepts, QuerySet)) self.assertFalse(unknown_expression_concepts.exists()) concept = ConceptFactory() reference.expression = concept.uri concepts = reference.get_concepts() self.assertTrue(isinstance(concepts, QuerySet)) self.assertEqual(concepts.count(), 1) self.assertEqual(concepts.first(), concept.get_latest_version()) ConceptFactory(parent=concept.parent, version='v1', mnemonic=concept.mnemonic, versioned_object=concept) reference.expression = drop_version(concept.uri) + 'versions/' concepts = reference.get_concepts().order_by('created_at') self.assertTrue(isinstance(concepts, QuerySet)) self.assertEqual(concepts.count(), 2) self.assertListEqual(list(concepts.all()), list(concept.versions.all()))
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_get_concepts(self): reference = CollectionReference() reference.expression = '/unknown/uri/' unknown_expression_concepts = reference.get_concepts() self.assertTrue(isinstance(unknown_expression_concepts, QuerySet)) self.assertFalse(unknown_expression_concepts.exists()) concept1 = ConceptFactory() reference.expression = concept1.uri concepts = reference.get_concepts() self.assertTrue(isinstance(concepts, QuerySet)) self.assertEqual(concepts.count(), 1) self.assertEqual(concepts.first(), concept1) concept2 = ConceptFactory(parent=concept1.parent, version='v1', mnemonic=concept1.mnemonic) reference.expression = drop_version(concept1.uri) + 'versions/' concepts = reference.get_concepts().order_by('created_at') self.assertTrue(isinstance(concepts, QuerySet)) self.assertEqual(concepts.count(), 2) self.assertEqual(concepts.first(), concept1) self.assertEqual(concepts.last(), concept2)
def process(self, collection_uri, expressions): self.processed += 1 self.log("Processing: {} ({}/{})".format(collection_uri, self.processed, self.total)) collection = Collection.objects.filter(uri=collection_uri).first() saved_references = [] concepts = [] mappings = [] if collection: for expression in expressions: self.log("Processing Expression: {} ".format(expression)) __is_concept = is_concept(expression) if __is_concept: model = Concept _instances = concepts else: model = Mapping _instances = mappings instance = model.objects.filter(uri=expression).first() if self.drop_version_if_version_missing and not instance: instance = model.objects.filter( uri=drop_version(expression)).first() if not instance: self.not_found_references.append(expression) continue latest_version = instance.get_latest_version() if not latest_version: latest_version = model.create_initial_version(instance) if __is_concept: latest_version.cloned_names = [ name.clone() for name in instance.names.all() ] latest_version.cloned_descriptions = [ desc.clone() for desc in instance.descriptions.all() ] latest_version.set_locales() parent = instance.parent latest_version.sources.set([parent, parent.head]) reference = CollectionReference(expression=latest_version.uri) reference.save() saved_references.append(reference) _instances.append(latest_version) self.created.append(expression) collection.references.add(*saved_references) if concepts: collection.concepts.add(*concepts) collection.batch_index(collection.concepts, ConceptDocument) if mappings: collection.mappings.add(*mappings) collection.batch_index(collection.mappings, MappingDocument) else: self.not_found.append(collection_uri)
def test_reference_as_concept_version(self): concept = ConceptFactory() expression = concept.uri reference = CollectionReference(expression=expression) reference.full_clean() self.assertEqual(len(reference.concepts), 1) self.assertTrue(isinstance(reference.concepts[0], Concept)) self.assertEqual(reference.concepts[0].id, concept.id)
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_invalid_expression(self): reference = CollectionReference(expression='') with self.assertRaises(ValidationError) as ex: reference.full_clean() self.assertEqual( ex.exception.message_dict, { 'expression': ['This field cannot be blank.'], 'detail': ['Expression specified is not valid.'] })
def setUp(self): super().setUp() self.user = UserProfileFactory() self.token = self.user.get_token() self.collection = UserCollectionFactory(mnemonic='coll', user=self.user) self.concept = ConceptFactory() self.reference = CollectionReference(expression=self.concept.uri) self.reference.full_clean() self.reference.save() self.collection.references.add(self.reference) self.collection.concepts.set(self.reference.concepts) self.assertEqual(self.collection.references.count(), 1) self.assertEqual(self.collection.concepts.count(), 1)
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 process_line(self, line): # pylint: disable=too-many-locals,too-many-statements,too-many-branches data = json.loads(line) original_data = data.copy() self.processed += 1 _id = data.pop('_id') data['internal_reference_id'] = get(_id, '$oid') for attr in [ 'active_concepts', 'active_mappings', 'last_child_update', 'last_concept_update', 'last_mapping_update', 'parent_version_id', 'previous_version_id', 'versioned_object_type_id', 'concepts', 'mappings' ]: data.pop(attr, None) data['snapshot'] = data.pop('collection_snapshot', None) data['external_id'] = data.pop('version_external_id', None) versioned_object_id = data.pop('versioned_object_id') versioned_object = self.get_collection(versioned_object_id) version = data.pop('mnemonic') created_at = data.pop('created_at') updated_at = data.pop('updated_at') created_by = data.get('created_by') updated_by = data.get('updated_by') creator = self.get_user(created_by) updater = self.get_user(updated_by) if creator: data['created_by'] = creator if updater: data['updated_by'] = updater data['created_at'] = get(created_at, '$date') data['updated_at'] = get(updated_at, '$date') data['organization_id'] = versioned_object.organization_id data['user_id'] = versioned_object.user_id data['collection_type'] = versioned_object.collection_type references = data.pop('references') or [] self.log("Processing: {} ({}/{})".format(version, self.processed, self.total)) uri = data['uri'] if Collection.objects.filter(uri=uri).exists(): self.existed.append(original_data) else: collection = Collection.objects.create( **data, version=version, mnemonic=versioned_object.mnemonic) if collection.id: self.created.append(original_data) else: self.failed.append(original_data) return saved_references = [] concepts = [] mappings = [] for ref in references: expression = ref.get('expression') __is_concept = is_concept(expression) concept = None mapping = None if __is_concept: concept = Concept.objects.filter(uri=expression).first() if concept: concepts.append(concept) else: mapping = Mapping.objects.filter(uri=expression).first() if mapping: mappings.append(mapping) if not concept and not mapping: self.add_in_not_found_expression(uri, expression) continue reference = CollectionReference(expression=expression) reference.save() saved_references.append(reference) collection.references.set(saved_references) collection.concepts.set(concepts) collection.mappings.set(mappings) collection.batch_index(collection.concepts, ConceptDocument) collection.batch_index(collection.mappings, MappingDocument)