Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
 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()
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 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)