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_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 test_drop_version(self): concept_head = ConceptFactory() concept_v1 = ConceptFactory(parent=concept_head.parent, version='v1', mnemonic=concept_head.mnemonic) self.assertTrue('/v1/' in concept_v1.uri) self.assertTrue('/v1/' not in drop_version(concept_v1.uri))
def get_all_related_mappings(self, expressions): all_related_mappings = [] unversioned_mappings = concept_expressions = [] for expression in expressions: if is_mapping(expression): unversioned_mappings.append(drop_version(expression)) elif is_concept(expression): concept_expressions.append(expression) for concept_expression in concept_expressions: ref = CollectionReference(expression=concept_expression) try: self.validate(ref) all_related_mappings += ref.get_related_mappings( unversioned_mappings) except: # pylint: disable=bare-except continue return all_related_mappings
def without_version(self): return drop_version(self.expression)
def add_references_in_bulk(self, expressions, user=None): # pylint: disable=too-many-locals # Fixme: Sny errors = {} collection_version = self.head new_expressions = set(expressions) new_versionless_expressions = { drop_version(expression): expression for expression in new_expressions } for reference in collection_version.references.all(): existing_versionless_expression = reference.without_version if existing_versionless_expression in new_versionless_expressions: existing_expression = new_versionless_expressions[ existing_versionless_expression] new_expressions.discard(existing_expression) errors[existing_expression] = [REFERENCE_ALREADY_EXISTS] added_references = list() for expression in new_expressions: ref = CollectionReference(expression=expression) try: ref.clean() ref.save() except Exception as ex: errors[expression] = ex.messages if hasattr(ex, 'messages') else ex continue added = False if ref.concepts: for concept in ref.concepts: if self.custom_validation_schema == CUSTOM_VALIDATION_SCHEMA_OPENMRS: try: self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute( concept, attribute='is_fully_specified', value=True, error_message= CONCEPT_FULLY_SPECIFIED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE ) self.check_concept_uniqueness_in_collection_and_locale_by_name_attribute( concept, attribute='locale_preferred', value=True, error_message= CONCEPT_PREFERRED_NAME_UNIQUE_PER_COLLECTION_AND_LOCALE ) except Exception as ex: errors[expression] = ex.messages if hasattr( ex, 'messages') else ex continue collection_version.add_concept(concept) added = True if ref.mappings: for mapping in ref.mappings: collection_version.add_mapping(mapping) added = True if added: collection_version.references.add(ref) self.references.add(ref) added_references.append(ref) if user: collection_version.updated_by = user self.updated_by = user collection_version.save() self.save() return added_references, errors