Example #1
0
 def test_invalid_label(self):
     result_path = os.path.join(self.temp_dir, 'backup')
     with self.assertRaisesRegexp(
             LookupError,
             "No installed app with label 'appname_ModelName'."):
         MultipleExportFacade.to_file_path(result_path,
                                           models=['appname_ModelName'])
Example #2
0
 def test_model_not_found(self):
     result_path = os.path.join(self.temp_dir, 'backup')
     with self.assertRaisesRegexp(
             LookupError,
             "App 'datamigrations_facades_tests' doesn't have a '[Mm]odel[Nn]ame' model."
     ):
         MultipleExportFacade.to_file_path(
             result_path, models=['datamigrations_facades_tests.ModelName'])
Example #3
0
 def test_serializer_not_found(self):
     result_path = os.path.join(self.temp_dir, 'backup')
     with self.assertRaisesRegexp(
             LookupError,
             "Serializer 'serializername' could not be found."):
         MultipleExportFacade.to_file_path(
             result_path,
             models=['datamigrations_facades_tests.Alphabet'],
             serializer='serializername')
Example #4
0
    def test_labels(self):
        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        result_path = os.path.join(self.temp_dir, 'backup')
        MultipleExportFacade.to_file_path(
            result_path,
            models=['datamigrations_facades_tests.Category'],
        )
        with open(result_path, 'r') as result_file:
            result = result_file.read()

        self.assertEqual(1, result.count('Type: MODEL;'))
        self.assertIn('Name: datamigrations_facades_tests.category;', result)
Example #5
0
    def test_file(self):
        migration = ModelMigration(Author)
        source_path = os.path.join(MIGRATIONS_DIR, 'author.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Category)
        source_path = os.path.join(MIGRATIONS_DIR, 'category.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Tag)
        source_path = os.path.join(MIGRATIONS_DIR, 'tag.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(Post)
        source_path = os.path.join(MIGRATIONS_DIR, 'post.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        migration = ModelMigration(PostTags)
        source_path = os.path.join(MIGRATIONS_DIR, 'post_tags.json')
        with open(source_path, 'r') as source_file:
            migration.import_data(source_file, JsonSerializer, DirectPlan)

        source_path = os.path.join(MIGRATIONS_DIR, 'backup')
        result_path = os.path.join(self.temp_dir, 'backup')
        MultipleExportFacade.to_file_path(
            result_path,
            serializer='json',
            use_natural_keys=True,
        )
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        with open(result_path, 'r') as result_file:
            result = result_file.read()

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

        file_path = options['file']
        directory = options['directory']
        if file_path and directory:
            raise CommandError(
                'You must give either an output file or a directory.')

        kwargs = {
            'models': labels,
            'serializer': options['format'],
            'encoding': options['encoding'],
            'newline': options['newline'],
            'use_natural_keys': options['natural'],
            'use_base_manager': options['base_manager'],
        }
        try:

            if file_path:
                MultipleExportFacade.to_file_path(file_path, **kwargs)
            elif directory:
                MultipleExportFacade.to_directory(directory, **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')

                MultipleExportFacade.to_file(file, **kwargs)

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

        if verbosity >= 1 and (file_path or directory):
            self.stdout.write('Objects were successfully exported.')
Example #7
0
 def test_no_file(self):
     with self.assertRaises(TypeError):
         MultipleExportFacade.to_file_path()