Exemple #1
0
    def get_mappings(self, obj):
        if self.include_direct_mappings:
            return MappingDetailSerializer(obj.get_unidirectional_mappings(), many=True).data
        if self.include_indirect_mappings:
            return MappingDetailSerializer(obj.get_bidirectional_mappings(), many=True).data

        return []
Exemple #2
0
    def get_mappings(self, obj):
        from core.mappings.serializers import MappingDetailSerializer
        context = get(self, 'context')
        if self.include_direct_mappings:
            return MappingDetailSerializer(obj.get_unidirectional_mappings(), many=True, context=context).data
        if self.include_indirect_mappings:
            return MappingDetailSerializer(obj.get_bidirectional_mappings(), many=True, context=context).data

        return []
Exemple #3
0
    def test_export_source(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)

        source_v1 = OrganizationSourceFactory(mnemonic=source.mnemonic,
                                              organization=source.organization,
                                              version='v1')
        concept1.sources.add(source_v1)
        concept2.sources.add(source_v1)
        mapping.sources.add(source_v1)

        export_source(source_v1.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, {
                **SourceVersionExportSerializer(source_v1).data, 'concepts':
                ANY,
                'mappings': ANY
            })

        exported_concepts = exported_data['concepts']
        expected_concepts = ConceptVersionDetailSerializer(
            [concept2, concept1], 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], many=True).data

        self.assertEqual(len(exported_mappings), 1)
        self.assertEqual(expected_mappings, exported_mappings)

        s3_upload_key = source_v1.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)
Exemple #4
0
 def get_detail_serializer(obj, data=None, files=None, partial=False):
     return MappingDetailSerializer(obj, data, files, partial)
    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, {
                **CollectionVersionExportSerializer(collection).data, 'concepts':
                ANY,
                'mappings': ANY,
                'references': 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)

        exported_references = exported_data['references']
        expected_references = CollectionReferenceSerializer(
            collection.references.all(), many=True).data

        self.assertEqual(len(exported_references), 3)
        self.assertIn(exported_references[0], expected_references)
        self.assertIn(exported_references[1], expected_references)
        self.assertIn(exported_references[2], expected_references)

        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,
            metadata={'ContentType': 'application/zip'},
            headers={'content-type': 'application/zip'})
        s3_mock.url_for.assert_called_once_with(s3_upload_key)

        import shutil
        shutil.rmtree(latest_temp_dir)