Exemple #1
0
    def test_post_everything_exists_201(self):
        source1 = UserSourceFactory(user=self.user)
        source2 = UserSourceFactory(user=self.user)
        concept1 = ConceptFactory(parent=source1)
        concept2 = ConceptFactory(parent=source2)

        response = self.client.post(
            source1.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept1.get_latest_version().uri,
                "to_concept_url": concept2.get_latest_version().uri,
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source1.uri)
        self.assertEqual(response.data['from_source_version'], None)
        self.assertEqual(response.data['from_concept_code'], concept1.mnemonic)
        self.assertEqual(response.data['from_concept_name'], None)
        self.assertEqual(response.data['to_source_url'], source2.uri)
        self.assertEqual(response.data['to_source_version'], None)
        self.assertEqual(response.data['to_concept_code'], concept2.mnemonic)
        self.assertEqual(response.data['to_concept_name'], None)
        mapping = source1.mappings.first()
        self.assertEqual(mapping.from_concept, concept1.get_latest_version())
        self.assertEqual(mapping.to_concept, concept2.get_latest_version())
        self.assertEqual(mapping.to_source, source2)
        self.assertEqual(mapping.from_source, source1)
Exemple #2
0
class SourceLogoViewTest(OCLAPITestCase):
    def setUp(self):
        super().setUp()
        self.user = UserProfileFactory(username='******')
        self.token = self.user.get_token()
        self.source = UserSourceFactory(mnemonic='source1', user=self.user)

    @patch('core.common.services.S3.upload_base64')
    def test_post_200(self, upload_base64_mock):
        upload_base64_mock.return_value = 'users/username/sources/source1/logo.png'
        self.assertIsNone(self.source.logo_url)
        self.assertIsNone(self.source.logo_path)

        response = self.client.post(self.source.uri + 'logo/',
                                    dict(base64='base64-data'),
                                    HTTP_AUTHORIZATION='Token ' + self.token,
                                    format='json')

        self.assertEqual(response.status_code, 200)
        expected_logo_url = 'http://oclapi2-dev.s3.amazonaws.com/users/username/sources/source1/logo.png'
        self.assertEqual(
            response.data['logo_url'].replace('https://', 'http://'),
            expected_logo_url)
        self.source.refresh_from_db()
        self.assertEqual(self.source.logo_url.replace('https://', 'http://'),
                         expected_logo_url)
        self.assertEqual(self.source.logo_path,
                         'users/username/sources/source1/logo.png')
        upload_base64_mock.assert_called_once_with(
            'base64-data', 'users/username/sources/source1/logo.png', False,
            True)
Exemple #3
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory(username='******')
     self.token = self.user.get_token()
     self.source = UserSourceFactory(mnemonic='source1', user=self.user)
     self.source_v1 = UserSourceFactory(version='v1',
                                        mnemonic='source1',
                                        user=self.user)
Exemple #4
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory(username='******')
     self.token = self.user.get_token()
     self.source = UserSourceFactory(mnemonic='source1', user=self.user)
     self.source_v1 = UserSourceFactory(version='v1',
                                        mnemonic='source1',
                                        user=self.user)
     self.v1_updated_at = self.source_v1.updated_at.strftime('%Y%m%d%H%M%S')
Exemple #5
0
    def test_post_without_to_and_from_concept_url_201(self):
        source = UserSourceFactory(user=self.user, mnemonic='source')
        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_source_url": source.uri,
                "from_source_version": "2.46",
                "from_concept_code": "32700-7",
                "from_concept_name": "Microscopic observation [Identifier] in Blood by Malaria smear",
                "to_source_url": "/orgs/WHO/sources/ICPC-2/",
                "to_source_version": "v11",
                "to_concept_code": "A73",
                "to_concept_name": "Malaria"
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source.uri)
        self.assertEqual(response.data['from_source_version'], '2.46')
        self.assertEqual(response.data['from_concept_code'], '32700-7')
        self.assertEqual(
            response.data['from_concept_name'], 'Microscopic observation [Identifier] in Blood by Malaria smear'
        )
        self.assertEqual(response.data['to_source_url'], "/orgs/WHO/sources/ICPC-2/")
        self.assertEqual(response.data['to_source_version'], 'v11')
        self.assertEqual(response.data['to_concept_code'], 'A73')
        self.assertEqual(response.data['to_concept_name'], 'Malaria')
        mapping = source.mappings.first()
        self.assertIsNone(mapping.from_concept_id)
        self.assertIsNone(mapping.to_concept_id)
        self.assertIsNone(mapping.to_source_id)

        org = OrganizationFactory(mnemonic='WHO')
        source = OrganizationSourceFactory(organization=org, mnemonic='ICPC-2')
        source.update_mappings()

        mapping.refresh_from_db()
        self.assertEqual(mapping.to_source_url, source.uri)
        self.assertEqual(mapping.to_source, source)
        self.assertIsNone(mapping.to_concept)

        concept = ConceptFactory(parent=source, mnemonic='A73', names=[LocalizedTextFactory(name='Malaria Updated')])
        concept.update_mappings()
        mapping.refresh_from_db()
        self.assertEqual(mapping.to_concept_code, 'A73')
        self.assertEqual(mapping.to_concept_name, 'Malaria')
        self.assertEqual(mapping.to_source, source)
        self.assertEqual(mapping.to_source, concept.parent)
        self.assertEqual(mapping.to_concept, concept)
Exemple #6
0
    def test_post_201(self):
        source = UserSourceFactory(user=self.user)
        concept1 = ConceptFactory(parent=source)
        concept2 = ConceptFactory(parent=source)

        response = self.client.post(
            source.mappings_url,
            dict(map_type='same as', from_concept_url=concept2.uri, to_concept_url=concept1.uri),
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['map_type'], 'same as')
        self.assertEqual(response.data['from_concept_code'], concept2.mnemonic)
        self.assertEqual(response.data['to_concept_code'], concept1.mnemonic)

        response = self.client.post(
            source.mappings_url,
            dict(map_type='same as', from_concept_url=concept2.uri, to_concept_url=concept1.uri),
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, {"__all__": ["Parent, map_type, from_concept, to_concept must be unique."]})

        response = self.client.post(
            source.mappings_url,
            dict(map_type='same as', from_concept_url=concept2.uri, to_concept_url=concept2.uri),
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.data, {'__all__': ['Cannot map concept to itself.']})
Exemple #7
0
    def test_post_with_source_versions_201(self):
        source1 = UserSourceFactory(user=self.user, mnemonic='source1')
        source1_v1 = UserSourceFactory(
            user=source1.parent, mnemonic=source1.mnemonic, version='v1.1'
        )
        concept1 = ConceptFactory(parent=source1, sources=[source1_v1])

        source2 = UserSourceFactory(user=self.user, mnemonic='source2')
        source2_v1 = UserSourceFactory(
            user=source2.parent, mnemonic=source1.mnemonic, version='v2.1'
        )
        concept2 = ConceptFactory(parent=source2, sources=[source2_v1])

        response = self.client.post(
            source1.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept1.get_latest_version().uri,
                "from_source_version": "v1.1",
                "to_concept_url": concept2.get_latest_version().uri,
                "to_source_version": "v2.1"
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source1.uri)
        self.assertEqual(response.data['from_source_version'], 'v1.1')
        self.assertEqual(response.data['from_concept_code'], concept1.mnemonic)
        self.assertEqual(response.data['from_concept_name'], None)
        self.assertEqual(response.data['to_source_url'], source2.uri)
        self.assertEqual(response.data['to_source_version'], 'v2.1')
        self.assertEqual(response.data['to_concept_code'], concept2.mnemonic)
        self.assertEqual(response.data['to_concept_name'], None)
        mapping = source1.mappings.first()
        self.assertEqual(mapping.from_concept, concept1.get_latest_version())
        self.assertEqual(mapping.to_concept, concept2.get_latest_version())
        self.assertEqual(mapping.to_source, source2)
        self.assertEqual(mapping.from_source, source1)
Exemple #8
0
    def test_post_400(self):
        source = UserSourceFactory(user=self.user)

        response = self.client.post(
            source.mappings_url,
            dict(foo='bar'),
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data,
            dict(
                map_type=[ErrorDetail(string='This field is required.', code='required')],
            )
        )
Exemple #9
0
    def test_post_to_concept_does_not_exists_201(self):
        source = UserSourceFactory(user=self.user)
        concept = ConceptFactory(parent=source)

        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept.get_latest_version().uri,
                "to_concept_url": "/orgs/WHO/sources/ICPC-2/concepts/A73/"
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source.uri)
        self.assertEqual(response.data['from_source_version'], None)
        self.assertEqual(response.data['from_concept_code'], concept.mnemonic)
        self.assertEqual(response.data['from_concept_name'], None)
        self.assertEqual(response.data['to_source_url'], '/orgs/WHO/sources/ICPC-2/')
        self.assertEqual(response.data['to_source_version'], None)
        self.assertEqual(response.data['to_concept_code'], 'A73')
        self.assertEqual(response.data['to_concept_name'], None)
Exemple #10
0
 def setUp(self):
     super().setUp()
     self.user = UserProfileFactory()
     self.token = self.user.get_token()
     self.source = UserSourceFactory(user=self.user)
     self.mapping = MappingFactory(parent=self.source)
Exemple #11
0
    def test_post_using_canonical_url_201(self):  # pylint: disable=too-many-statements
        source = UserSourceFactory(user=self.user, mnemonic='source')
        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_source_url": "http://loinc.org",
                "from_source_version": "2.46",
                "from_concept_code": "32700-7",
                "from_concept_name": "Microscopic observation [Identifier] in Blood by Malaria smear",
                "to_source_url": "http://who.int/ICPC-2",
                "to_source_version": "v11",
                "to_concept_code": "A73",
                "to_concept_name": "Malaria"
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], 'http://loinc.org')
        self.assertEqual(response.data['from_source_version'], '2.46')
        self.assertEqual(response.data['from_concept_code'], '32700-7')
        self.assertEqual(
            response.data['from_concept_name'], 'Microscopic observation [Identifier] in Blood by Malaria smear'
        )
        self.assertEqual(response.data['to_source_url'], "http://who.int/ICPC-2")
        self.assertEqual(response.data['to_source_version'], 'v11')
        self.assertEqual(response.data['to_concept_code'], 'A73')
        self.assertEqual(response.data['to_concept_name'], 'Malaria')
        mapping = source.mappings.first()
        self.assertIsNone(mapping.from_concept_id)
        self.assertIsNone(mapping.to_concept_id)
        self.assertIsNone(mapping.to_source_id)

        to_source = OrganizationSourceFactory(canonical_url="http://who.int/ICPC-2")
        to_source.update_mappings()
        mapping.refresh_from_db()

        self.assertEqual(mapping.to_source_url, to_source.canonical_url)
        self.assertEqual(mapping.to_source, to_source)
        self.assertEqual(mapping.to_source_version, 'v11')

        concept = ConceptFactory(parent=to_source, mnemonic='A73', names=[LocalizedTextFactory(name='foobar')])
        concept.update_mappings()

        mapping.refresh_from_db()
        self.assertEqual(mapping.to_concept, concept)
        self.assertTrue(mapping.to_concept_code == concept.mnemonic == 'A73')
        self.assertEqual(mapping.to_concept_name, 'Malaria')
        self.assertNotEqual(concept.display_name, 'Malaria')
        self.assertEqual(mapping.from_concept, None)
        self.assertEqual(mapping.from_source, None)

        from_source = OrganizationSourceFactory(canonical_url="http://loinc.org")
        from_source.update_mappings()
        mapping.refresh_from_db()

        self.assertTrue(mapping.from_source_url == from_source.canonical_url == "http://loinc.org")
        self.assertEqual(mapping.from_source_version, "2.46")
        self.assertEqual(mapping.from_source, from_source)

        concept = ConceptFactory(parent=from_source, mnemonic='32700-7', names=[LocalizedTextFactory(name='foobar')])
        concept.update_mappings()

        mapping.refresh_from_db()
        self.assertEqual(mapping.from_concept, concept)
        self.assertTrue(mapping.from_concept_code == concept.mnemonic == '32700-7')
        self.assertEqual(mapping.from_concept_name, 'Microscopic observation [Identifier] in Blood by Malaria smear')
        self.assertNotEqual(concept.display_name, 'Microscopic observation [Identifier] in Blood by Malaria smear')

        # retrievals with lookups
        response = self.client.get(
            mapping.uri,
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertTrue('from_source' not in response.data)
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupConcepts=false',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertTrue('from_source' not in response.data)
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupConcepts=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertIsNotNone(response.data['from_concept'])
        self.assertIsNotNone(response.data['to_concept'])
        self.assertTrue('from_source' not in response.data)
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupFromConcept=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertIsNotNone(response.data['from_concept'])
        self.assertTrue('to_concept' not in response.data)
        self.assertTrue('from_source' not in response.data)
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupToConcept=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertIsNotNone(response.data['to_concept'])
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('from_source' not in response.data)
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupSources=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertIsNotNone(response.data['from_source'])
        self.assertIsNotNone(response.data['to_source'])

        response = self.client.get(
            mapping.uri + '?lookupFromSource=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertIsNotNone(response.data['from_source'])
        self.assertTrue('to_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupToSource=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertIsNotNone(response.data['to_source'])
        self.assertTrue('from_source' not in response.data)

        response = self.client.get(
            mapping.uri + '?lookupFromSource=true&lookupToSource=true',
            HTTP_AUTHORIZATION='Token ' + self.token,
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['url'], mapping.uri)
        self.assertTrue('from_concept' not in response.data)
        self.assertTrue('to_concept' not in response.data)
        self.assertIsNotNone(response.data['from_source'])
        self.assertIsNotNone(response.data['to_source'])
Exemple #12
0
    def test_post_with_source_versions_without_to_concept_exists_201(self):
        source = UserSourceFactory(user=self.user, mnemonic='source')
        source_v1 = UserSourceFactory(
            user=source.parent, mnemonic=source.mnemonic, version='v1.1'
        )
        concept = ConceptFactory(parent=source, sources=[source_v1])

        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept.get_latest_version().uri,
                "from_source_version": "v1.1",
                "to_source_url": "/orgs/WHO/sources/ICPC-2/",
                "to_source_version": "v11",
                "to_concept_code": "A73",
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source.uri)
        self.assertEqual(response.data['from_source_version'], 'v1.1')
        self.assertEqual(response.data['from_concept_code'], concept.mnemonic)
        self.assertEqual(response.data['from_concept_name'], None)
        self.assertEqual(response.data['to_source_url'], "/orgs/WHO/sources/ICPC-2/")
        self.assertEqual(response.data['to_source_version'], 'v11')
        self.assertEqual(response.data['to_concept_code'], 'A73')
        self.assertEqual(response.data['to_concept_name'], None)
        mapping = source.mappings.first()
        self.assertEqual(mapping.from_concept, concept.get_latest_version())
        self.assertIsNone(mapping.to_concept_id)
        self.assertIsNone(mapping.to_source_id)

        concept2 = ConceptFactory(parent=source, sources=[source_v1], names=[LocalizedTextFactory()])
        self.assertIsNotNone(concept2.display_name)

        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept2.get_latest_version().uri,
                "from_source_version": "v1.1",
                "to_source_url": "/orgs/WHO/sources/ICPC-2/v11/",
                "to_concept_code": "A73",
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['from_source_url'], source.uri)
        self.assertEqual(response.data['from_source_version'], 'v1.1')
        self.assertEqual(response.data['from_concept_code'], concept2.mnemonic)
        self.assertEqual(response.data['from_concept_name'], None)
        self.assertEqual(response.data['to_source_url'], "/orgs/WHO/sources/ICPC-2/")
        self.assertEqual(response.data['to_source_version'], 'v11')
        self.assertEqual(response.data['to_concept_code'], 'A73')
        self.assertEqual(response.data['to_concept_name'], None)
        mapping = source.mappings.last()
        self.assertEqual(mapping.from_concept, concept2.get_latest_version())
        self.assertIsNone(mapping.to_concept_id)
        self.assertIsNone(mapping.to_source_id)

        response = self.client.post(
            source.mappings_url,
            {
                "map_type": "Same As",
                "from_concept_url": concept2.get_latest_version().uri,
                "from_source_version": "v1.1",
                "to_source_url": "/orgs/WHO/sources/ICPC-2/v11/",
                "to_concept_code": "A73",
            },
            HTTP_AUTHORIZATION='Token ' + self.token,
        )
        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.data, {'__all__': ['Parent, map_type, from_concept, to_source, to_concept_code must be unique.']}
        )