예제 #1
0
 def test_model_not_found(self):
     result_path = os.path.join(self.temp_dir, 'alphabet.csv')
     with self.assertRaisesRegexp(
             LookupError,
             "App 'datamigrations_facades_tests' doesn't have a '[Mm]odel[Nn]ame' model."
     ):
         SingleExportFacade.to_file_path(
             result_path, model='datamigrations_facades_tests.ModelName')
예제 #2
0
 def test_serializer_not_found(self):
     result_path = os.path.join(self.temp_dir, 'alphabet.csv')
     with self.assertRaisesRegexp(
             LookupError,
             "Serializer 'serializername' could not be found."):
         SingleExportFacade.to_file_path(
             result_path,
             model='datamigrations_facades_tests.Alphabet',
             serializer='serializername')
예제 #3
0
    def test_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        result_path = os.path.join(self.temp_dir, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        migration.import_data(source, CsvSerializer, DirectPlan)
        SingleExportFacade.to_file_path(
            result_path,
            model='datamigrations_facades_tests.Alphabet',
            serializer='csv',
        )
        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(source.splitlines(), result.splitlines())
예제 #4
0
    def handle(self, *labels, **options):
        verbosity = options['verbosity']
        show_traceback = options['traceback']

        file_path = options['file']

        kwargs = {
            'serializer':
            options['format'],
            'encoding':
            options['encoding'],
            'newline':
            options['newline'],
            'use_natural_primary_keys':
            options['natural'] or options['natural_primary'],
            'use_natural_foreign_keys':
            options['natural'] or options['natural_foreign'],
            'use_base_manager':
            options['base_manager'],
        }
        if labels:

            if len(labels) > 1:
                raise CommandError(
                    'This command takes only one positional argument.')

            if labels[0].count('.') != 1:
                raise CommandError(
                    "Model label must be like 'appname.ModelName'.")

            kwargs['model'] = labels[0]

        if options['fields'] is not None:
            kwargs['fields'] = options['fields'].split(',')

        if options['exclude'] is not None:
            kwargs['exclude'] = options['exclude'].split(',')

        try:

            if file_path:
                SingleExportFacade.to_file_path(file_path, **kwargs)
            else:
                file = self.stdout
                if six.PY2:

                    class FileWrapper(object):
                        def __init__(self, file, encoding, errors):
                            self._file = file
                            self._encoding = encoding
                            self._errors = errors

                        def __getattr__(self, name):
                            return getattr(self._file, name)

                        def write(self, data):
                            self._file.write(
                                data.decode(self._encoding, self._errors))

                        def writelines(self, data):
                            for line in data:
                                self.write(line)

                    file = FileWrapper(file, 'utf8', 'replace')

                SingleExportFacade.to_file(file, **kwargs)

        except Exception as e:
            if show_traceback:
                raise e
            else:
                raise CommandError(str(e))

        if verbosity >= 1 and file_path:
            self.stdout.write('Objects were successfully exported.')
예제 #5
0
 def test_app_not_found(self):
     result_path = os.path.join(self.temp_dir, 'alphabet.csv')
     with self.assertRaisesRegexp(LookupError,
                                  "No installed app with label 'appname'."):
         SingleExportFacade.to_file_path(result_path,
                                         model='appname.ModelName')
예제 #6
0
 def test_invalid_label(self):
     result_path = os.path.join(self.temp_dir, 'alphabet.csv')
     with self.assertRaises(ValueError):
         SingleExportFacade.to_file_path(result_path,
                                         model='appname_ModelName')
예제 #7
0
 def test_no_label(self):
     result_path = os.path.join(self.temp_dir, 'alphabet.csv')
     with self.assertRaisesRegexp(AttributeError, 'You must give a model.'):
         SingleExportFacade.to_file_path(result_path)
예제 #8
0
 def test_no_file(self):
     with self.assertRaises(TypeError):
         SingleExportFacade.to_file_path()