Ejemplo n.º 1
0
 def test_model_not_found(self):
     source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
     with self.assertRaisesRegexp(
             LookupError,
             "App 'datamigrations_facades_tests' doesn't have a '[Mm]odel[Nn]ame' model."
     ):
         SingleImportFacade.from_file_path(
             source_path, model='datamigrations_facades_tests.ModelName')
Ejemplo n.º 2
0
 def test_importation_plan_not_found(self):
     source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
     with self.assertRaisesRegexp(
             LookupError,
             "Importation plan 'planname' could not be found."):
         SingleImportFacade.from_file_path(
             source_path,
             model='datamigrations_facades_tests.Alphabet',
             plan='planname')
Ejemplo n.º 3
0
    def handle(self, *labels, **options):
        verbosity = options['verbosity']
        show_traceback = options['traceback']

        file_path = options['file']
        if not file_path:
            raise CommandError('You must give an input file.')

        kwargs = {
            'serializer':
            options['format'],
            'encoding':
            options['encoding'],
            'newline':
            options['newline'],
            'plan':
            options['plan'],
            'batch_size':
            options['batch'],
            'use_natural_primary_keys':
            options['natural'] or options['natural_primary'],
            'use_natural_foreign_keys':
            options['natural'] or options['natural_foreign'],
            'ignore_missing_foreign_keys':
            options['ignore_missing'],
        }
        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:

            SingleImportFacade.from_file_path(file_path, **kwargs)

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

        if verbosity >= 1:
            self.stdout.write('Entries were successfully imported.')
Ejemplo n.º 4
0
    def test_file(self):
        migration = ModelMigration(Alphabet)
        source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
        with open(source_path, 'r') as source_file:
            source = source_file.read()

        SingleImportFacade.from_file_path(
            source_path,
            model='datamigrations_facades_tests.Alphabet',
            serializer='csv',
        )
        result = migration.export_data(serializer=CsvSerializer)
        self.assertEqual(source.splitlines(), result.splitlines())
Ejemplo n.º 5
0
 def test_app_not_found(self):
     source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
     with self.assertRaisesRegexp(LookupError,
                                  "No installed app with label 'appname'."):
         SingleImportFacade.from_file_path(source_path,
                                           model='appname.ModelName')
Ejemplo n.º 6
0
 def test_invalid_label(self):
     source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
     with self.assertRaises(ValueError):
         SingleImportFacade.from_file_path(source_path,
                                           model='appname_ModelName')
Ejemplo n.º 7
0
 def test_no_label(self):
     source_path = os.path.join(MIGRATIONS_DIR, 'alphabet.csv')
     with self.assertRaisesRegexp(AttributeError, 'You must give a model.'):
         SingleImportFacade.from_file_path(source_path)
Ejemplo n.º 8
0
 def test_file_not_found(self):
     with self.assertRaisesRegexp(AttributeError,
                                  "File 'filename' does not exit."):
         SingleImportFacade.from_file_path('filename')
Ejemplo n.º 9
0
 def test_no_file(self):
     with self.assertRaises(TypeError):
         SingleImportFacade.from_file_path()