def test_sigint_handler(self): """SIGINT is ignored in Python and passed to psql to abort quries.""" def _mock_subprocess_run(*args, **kwargs): handler = signal.getsignal(signal.SIGINT) self.assertEqual(handler, signal.SIG_IGN) sigint_handler = signal.getsignal(signal.SIGINT) # The default handler isn't SIG_IGN. self.assertNotEqual(sigint_handler, signal.SIG_IGN) with mock.patch('subprocess.run', new=_mock_subprocess_run): DatabaseClient.runshell_db({}) # dbshell restores the original handler. self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))
def test_sigint_handler(self): """SIGINT is ignored in Python and passed to psql to abort quries.""" def _mock_subprocess_call(*args): handler = signal.getsignal(signal.SIGINT) self.assertEqual(handler, signal.SIG_IGN) sigint_handler = signal.getsignal(signal.SIGINT) # The default handler isn't SIG_IGN. self.assertNotEqual(sigint_handler, signal.SIG_IGN) with mock.patch('subprocess.check_call', new=_mock_subprocess_call): DatabaseClient.runshell_db({}) # dbshell restores the original handler. self.assertEqual(sigint_handler, signal.getsignal(signal.SIGINT))
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.run(). It returns a 2-tuple with: - The command line list - The the value of the PGPASSWORD environment variable, or None. """ def _mock_subprocess_run(*args, env=os.environ, **kwargs): self.subprocess_args = list(*args) self.pgpassword = env.get('PGPASSWORD') return subprocess.CompletedProcess(self.subprocess_args, 0) with mock.patch('subprocess.run', new=_mock_subprocess_run): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pgpassword
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.run(). It returns a 2-tuple with: - The command line list - The dictionary of PG* environment variables, or {}. """ def _mock_subprocess_run(*args, env=os.environ, **kwargs): self.subprocess_args = list(*args) # PostgreSQL environment variables. self.pg_env = {key: env[key] for key in env if key.startswith('PG')} return subprocess.CompletedProcess(self.subprocess_args, 0) with mock.patch('subprocess.run', new=_mock_subprocess_run): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pg_env
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.run(). It returns a 2-tuple with: - The command line list - The the value of the PGPASSWORD environment variable, or None. """ def _mock_subprocess_run(*args, env=os.environ, **kwargs): self.subprocess_args = list(*args) self.pgpassword = env.get('PGPASSWORD') return subprocess.CompletedProcess(self.subprocess_args, 0) with mock.patch('subprocess.run', new=_mock_subprocess_run): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pgpassword
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.run(). It returns a 2-tuple with: - The command line list - The dictionary of PG* environment variables, or {}. """ def _mock_subprocess_run(*args, env=os.environ, **kwargs): self.subprocess_args = list(*args) # PostgreSQL environment variables. self.pg_env = {key: env[key] for key in env if key.startswith('PG')} return subprocess.CompletedProcess(self.subprocess_args, 0) with mock.patch('subprocess.run', new=_mock_subprocess_run): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pg_env
def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) self.validation = BaseDatabaseValidation(self)
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.call. It returns a 2-tuple with: - The command line list - The content of the file pointed by environment PGPASSFILE, or None. """ def _mock_subprocess_call(*args): self.subprocess_args = list(*args) if 'PGPASSFILE' in os.environ: with open(os.environ['PGPASSFILE']) as f: self.pgpass = f.read().strip() # ignore line endings else: self.pgpass = None return 0 self.subprocess_args = None self.pgpass = None with mock.patch('subprocess.call', new=_mock_subprocess_call): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pgpass
def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) self.features = DatabaseFeatures() autocommit = self.settings_dict["OPTIONS"].get('autocommit', False) self.features.uses_autocommit = autocommit self._set_isolation_level(int(not autocommit)) self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) self.validation = BaseDatabaseValidation(self)
def _run_it(self, dbinfo): """ That function invokes the runshell command, while mocking subprocess.call. It returns a 2-tuple with: - The command line list - The content of the file pointed by environment PGPASSFILE, or None. """ def _mock_subprocess_call(*args): self.subprocess_args = list(*args) if 'PGPASSFILE' in os.environ: with open(os.environ['PGPASSFILE'], 'r') as f: self.pgpass = f.read().strip() # ignore line endings else: self.pgpass = None return 0 self.subprocess_args = None self.pgpass = None with mock.patch('subprocess.call', new=_mock_subprocess_call): DatabaseClient.runshell_db(dbinfo) return self.subprocess_args, self.pgpass
def test_fail_exec(self): # The psql PostgreSQL cient does not fail on invalid SQL queries. Make login fail instead. client = DatabaseClient(connection) with mock.patch( 'django.db.backends.postgresql.base.DatabaseWrapper.get_connection_params' ) as mock_params: mock_params.return_value = {} self.assertRaises(CalledProcessError, client.runshell, input=b'SELECT * FROM user_tables;', stdout=PIPE, stderr=PIPE) mock_params.assert_called_once()
def __init__(self, *args, **kwargs): super(DatabaseWrapper, self).__init__(*args, **kwargs) import warnings warnings.warn( 'The "postgresql" backend has been deprecated. Use "postgresql_psycopg2" instead.', DeprecationWarning) self.features = DatabaseFeatures(self) self.ops = DatabaseOperations(self) self.client = DatabaseClient(self) self.creation = DatabaseCreation(self) self.introspection = DatabaseIntrospection(self) self.validation = BaseDatabaseValidation(self)
def settings_to_cmd_args_env(self, settings_dict, parameters=None): if parameters is None: parameters = [] return DatabaseClient.settings_to_cmd_args_env(settings_dict, parameters)
def test_exec(self): client = DatabaseClient(connection) run_result = client.runshell(input=b'\\dt', stdout=PIPE, stderr=PIPE) self.assertTrue(run_result.stdout) self.assertFalse(run_result.stderr)