def check_cell_code(code, test=False): """ The function that checks the code in a cell """ code = filter_magic_lines(code) # Ignore magics # Substitute stdout and catch with a StringIO old_stdout = sys.stdout my_stdout = StringIO() sys.stdout = my_stdout # Do the flake8 check check_code(code) # Replace the old stdout sys.stdout = old_stdout output = my_stdout.getvalue() # Remove error messages that should be ignored output = filter_flake8_output(output, codes_to_ignore) # Write the error to stderr if test is False: sys.stderr.write(output) sys.stderr.flush() return output
def check_code(self): """ Check if code follow PEP-8 guide-lines thanks to module flake8. """ if has_flake8: txt = self.get_text() r = run.check_code(txt) return r else: return
def test_mysql_cache_migration(self): out = StringIO() call_command('mysql_cache_migration', stdout=out) output = out.getvalue() # Lint it with captured_stdout() as stderr: errors = check_code(output) self.assertEqual( errors, 0, "Encountered {} errors whilst trying to lint the mysql cache " "migration.\nMigration:\n\n{}\n\nLint errors:\n\n{}" .format(errors, output, stderr.getvalue()) ) # Dynamic import and check migration_mod = imp.new_module('0001_add_cache_tables') six.exec_(output, migration_mod.__dict__) self.assertTrue(hasattr(migration_mod, 'Migration')) migration = migration_mod.Migration self.assertTrue(hasattr(migration, 'dependencies')) self.assertTrue(hasattr(migration, 'operations')) # Since they all have the same table name, there should only be one # operation self.assertEqual(len(migration.operations), 1) # Now run the migration forwards and backwards to check it works operation = migration.operations[0] self.drop_table() self.assertTableNotExists(self.table_name) state = ProjectState() new_state = state.clone() with connection.schema_editor() as editor: operation.database_forwards("django_mysql_tests", editor, state, new_state) self.assertTableExists(self.table_name) new_state = state.clone() with connection.schema_editor() as editor: operation.database_backwards("django_mysql_tests", editor, new_state, state) self.assertTableNotExists(self.table_name) self.create_table()
def test_mysql_cache_migration(self): out = StringIO() call_command('mysql_cache_migration', stdout=out) output = out.getvalue() # Lint it with captured_stdout() as stderr: errors = check_code(output) assert errors == 0, ( "Encountered {} errors whilst trying to lint the mysql cache " "migration.\nMigration:\n\n{}\n\nLint errors:\n\n{}".format( errors, output, stderr.getvalue())) # Dynamic import and check migration_mod = imp.new_module('0001_add_cache_tables') six.exec_(output, migration_mod.__dict__) assert hasattr(migration_mod, 'Migration') migration = migration_mod.Migration assert hasattr(migration, 'dependencies') assert hasattr(migration, 'operations') # Since they all have the same table name, there should only be one # operation assert len(migration.operations) == 1 # Now run the migration forwards and backwards to check it works operation = migration.operations[0] assert not self.table_exists(self.table_name) state = ProjectState() new_state = state.clone() with connection.schema_editor() as editor: operation.database_forwards("testapp", editor, state, new_state) assert self.table_exists(self.table_name) new_state = state.clone() with connection.schema_editor() as editor: operation.database_backwards("testapp", editor, new_state, state) assert not self.table_exists(self.table_name)