Пример #1
0
    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.'
                ]
            })
Пример #2
0
class CollectionVersionListViewTest(OCLAPITestCase):
    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_get_200(self):
        response = self.client.get('/collections/coll/versions/?verbose=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'HEAD')

        UserCollectionFactory(mnemonic=self.collection.mnemonic,
                              user=self.user,
                              version='v1',
                              released=True)

        response = self.client.get('/collections/coll/versions/?released=true',
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['version'], 'v1')

    @patch('core.collections.views.export_collection')
    def test_post_201(self, export_collection_mock):
        export_collection_mock.delay = Mock()
        response = self.client.post('/collections/coll/versions/',
                                    dict(id='v1', description='version1'),
                                    HTTP_AUTHORIZATION='Token ' + self.token,
                                    format='json')

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['version'], 'v1')
        self.assertEqual(self.collection.versions.count(), 2)

        last_created_version = self.collection.versions.order_by(
            'created_at').last()
        self.assertEqual(last_created_version.version, 'v1')
        self.assertEqual(last_created_version.description, 'version1')
        self.assertEqual(last_created_version.concepts.count(), 1)
        self.assertEqual(last_created_version.references.count(), 1)
        self.assertEqual(last_created_version,
                         self.collection.get_latest_version())
        export_collection_mock.delay.assert_called_once_with(
            str(last_created_version.id))
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
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.']
            })
Пример #6
0
    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')
Пример #7
0
 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)
Пример #8
0
    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()))
Пример #9
0
    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)
Пример #10
0
    def get_related_mappings_with_version_information(expressions):
        related_mappings = []

        for expression in expressions:
            if is_concept(expression):
                concepts = CollectionReference.get_concept_heads_from_expression(expression)
                for concept in concepts:
                    related_mappings += concept.get_latest_unidirectional_mappings()

        return [mapping.uri for mapping in related_mappings]
Пример #11
0
    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.'
                ]
            })
Пример #12
0
    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)
Пример #13
0
    def get_related_mappings_with_version_information(self, instance,
                                                      expressions):
        related_mappings = []

        for expression in expressions:
            if is_concept(expression):
                concepts = CollectionReference.get_concept_heads_from_expression(
                    expression)
                for concept in concepts:
                    related_mappings += concept.get_unidirectional_mappings()

        return self.get_version_information_of_related_mappings(
            instance, related_mappings)
Пример #14
0
    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))
Пример #15
0
    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)
Пример #16
0
class CollectionReferencesViewTest(OCLAPITestCase):
    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_get_404(self):
        response = self.client.get('/collections/foobar/references/',
                                   format='json')

        self.assertEqual(response.status_code, 404)

    def test_get_200(self):
        response = self.client.get('/collections/coll/references/',
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['expression'],
                         self.concept.get_latest_version().uri)
        self.assertEqual(response.data[0]['reference_type'], 'concepts')

        response = self.client.get(
            '/collections/coll/references/?q={}&search_sort=desc'.format(
                self.concept.get_latest_version().uri),
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)
        self.assertEqual(response.data[0]['expression'],
                         self.concept.get_latest_version().uri)
        self.assertEqual(response.data[0]['reference_type'], 'concepts')

        response = self.client.get(
            '/collections/coll/references/?q=/concepts/&search_sort=desc',
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 1)

        response = self.client.get(
            '/collections/coll/references/?q=/mappings/&search_sort=desc',
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 0)

    def test_delete_400(self):
        response = self.client.delete('/collections/coll/references/',
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 400)

    def test_delete_204_random(self):
        response = self.client.delete('/collections/coll/references/',
                                      dict(expressions=['/foo/']),
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 1)
        self.assertEqual(self.collection.concepts.count(), 1)

    def test_delete_204_all_expressions(self):
        response = self.client.delete('/collections/coll/references/',
                                      dict(expressions='*'),
                                      HTTP_AUTHORIZATION='Token ' + self.token,
                                      format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)

    def test_delete_204_specific_expression(self):
        response = self.client.delete(
            '/collections/coll/references/',
            dict(expressions=[self.concept.get_latest_version().uri]),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')

        self.assertEqual(response.status_code, 204)
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)

        concept = ConceptFactory()
        latest_version = concept.get_latest_version()
        MappingFactory(from_concept=latest_version, parent=concept.parent)
        response = self.client.put(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(data=dict(mappings=[latest_version.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 2)
        self.assertEqual(self.collection.concepts.count(), 1)
        self.assertEqual(self.collection.mappings.count(), 1)

        response = self.client.delete(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(expressions=[latest_version.uri]),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 204)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 0)
        self.assertEqual(self.collection.concepts.count(), 0)
        self.assertEqual(self.collection.mappings.count(), 0)

    @patch('core.collections.views.add_references')
    def test_put_202_all(self, add_references_mock):
        add_references_mock.delay = Mock()

        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(concepts='*')),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 202)
        self.assertEqual(response.data, [])
        add_references_mock.delay.assert_called_once_with(
            self.user, dict(concepts='*'), self.collection,
            'http://testserver', False)

    def test_put_200_specific_expression(self):
        response = self.client.put(
            '/collections/coll/references/',
            dict(data=dict(concepts=[self.concept.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [
            dict(
                added=False,
                expression=self.concept.uri,
                message=[
                    'Concept or Mapping reference name must be unique in a collection.'
                ])
        ])

        concept2 = ConceptFactory()
        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(concepts=[concept2.uri])),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 2)
        self.assertEqual(self.collection.concepts.count(), 2)
        self.assertEqual(self.collection.active_concepts, 2)
        self.assertEqual(self.collection.active_mappings, 0)
        self.assertTrue(
            self.collection.references.filter(
                expression=concept2.get_latest_version().uri).exists())
        self.assertEqual(response.data, [
            dict(
                added=True,
                expression=concept2.get_latest_version().uri,
                message=
                'Added the latest versions of concept to the collection. Future updates will not be added'
                ' automatically.')
        ])

        mapping = MappingFactory(from_concept=concept2,
                                 to_concept=self.concept,
                                 parent=self.concept.parent)

        response = self.client.put('/collections/coll/references/',
                                   dict(data=dict(mappings=[mapping.uri])),
                                   HTTP_AUTHORIZATION='Token ' + self.token,
                                   format='json')

        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 3)
        self.assertEqual(self.collection.concepts.count(), 2)
        self.assertEqual(self.collection.mappings.count(), 1)
        self.assertEqual(self.collection.active_concepts, 2)
        self.assertEqual(self.collection.active_mappings, 1)
        self.assertTrue(
            self.collection.references.filter(
                expression=mapping.get_latest_version().uri).exists())
        self.assertEqual(response.data, [
            dict(
                added=True,
                expression=mapping.get_latest_version().uri,
                message=
                'Added the latest versions of mapping to the collection. Future updates will not be added'
                ' automatically.')
        ])

        concept3 = ConceptFactory()
        latest_version = concept3.get_latest_version()
        mapping2 = MappingFactory(from_concept=latest_version,
                                  parent=concept3.parent)

        response = self.client.put(
            '/collections/coll/references/?cascade=sourcemappings',
            dict(data=dict(concepts=[latest_version.uri])),
            HTTP_AUTHORIZATION='Token ' + self.token,
            format='json')
        self.assertEqual(response.status_code, 200)
        self.collection.refresh_from_db()
        self.assertEqual(self.collection.references.count(), 5)
        self.assertEqual(self.collection.concepts.count(), 3)
        self.assertEqual(self.collection.mappings.count(), 2)
        self.assertEqual(self.collection.active_concepts, 3)
        self.assertEqual(self.collection.active_mappings, 2)
        self.assertTrue(
            self.collection.references.filter(
                expression=mapping2.get_latest_version().uri).exists())
        self.assertTrue(
            self.collection.references.filter(
                expression=latest_version.uri).exists())