def test_db_command_status(self): mock_args = mock.MagicMock() mock_args.command = 'status' db_command(mock_args) self.mock_run_process.assert_called_with( ['pg_ctl', 'status', '-D', 'foo/data'], 'Checking status of Postgresql database server...', '')
def test_db_command_createsuperuser(self, mock_load_apps, mock_create_superuser, _): mock_args = mock.MagicMock() mock_args.command = 'createsuperuser' db_command(mock_args) mock_load_apps.assert_called() mock_create_superuser.assert_called_with('PFoo', 'PEmail', 'PBar')
def test_db_command_migrate(self, mock_get_manage_path): mock_args = mock.MagicMock() mock_args.command = 'migrate' db_command(mock_args) mock_get_manage_path.assert_called() self.mock_run_process.assert_called_with( ['python', 'foo/manage.py', 'migrate', '--database', 'test'], 'Running migrations for Tethys database...')
def test_db_command_stop(self): mock_args = mock.MagicMock() mock_args.command = 'stop' db_command(mock_args) self.mock_run_process.assert_called_with( ['pg_ctl', '-U', 'postgres', '-D', 'foo/data', 'stop'], 'Stopping Postgresql database server...', 'There was an error while stopping the Posgresql database.')
def test_db_command_init(self): mock_args = mock.MagicMock() mock_args.command = 'init' db_command(mock_args) self.mock_run_process.assert_called_with( ['initdb', '-U', 'postgres', '-D', 'foo/data'], 'Initializing Postgresql database server in "foo/data"...', 'Could not initialize the Postgresql database.')
def test_db_command_start(self): mock_args = mock.MagicMock() mock_args.command = 'start' db_command(mock_args) kwargs = self._get_kwargs(remove=['db_dir', 'port']) self.mock_run_process.assert_called_with(['pg_ctl', '-U', 'postgres', '-D', 'foo/data', '-l', 'foo/logfile', 'start', '-o', '-p 0000'], 'Starting Postgresql database server in "foo/data" on port 0000...', 'There was an error while starting the Postgresql database.', **kwargs)
def test_db_command_sync(self, MockSingletonHarvester, _): # mock the input args args = mock.MagicMock(manage='', command='sync', port='8080') # call the testing method with the mock args db_command(args) # mock the singleton harvester MockSingletonHarvester.assert_called() MockSingletonHarvester().harvest.assert_called()
def test_db_command_configure(self, mock_init, mock_start, mock_create, mock_migrate, mock_createsuperuser): mock_args = mock.MagicMock() mock_args.command = 'configure' db_command(mock_args) mock_init.assert_called_with(**self.options) mock_start.assert_called_with(**self.options) mock_create.assert_called_with(**self.options) mock_migrate.assert_called_with(**self.options) mock_createsuperuser.assert_called_with(**self.options)
def test_db_command_createsuperuser(self, mock_load_apps, mock_create_superuser, mock_write_error, _): from django.db.utils import IntegrityError mock_args = mock.MagicMock() mock_args.command = 'createsuperuser' mock_create_superuser.side_effect = IntegrityError db_command(mock_args) mock_load_apps.assert_called() mock_create_superuser.assert_called_with('PFoo', 'PEmail', 'PBar') portal_superuser = self.options['portal_superuser_name'] mock_write_error.assert_called_with(f'Tethys Portal Superuser "{portal_superuser}" already exists.')
def test_purge_db_server(self, mock_stop, mock_write_error, mock_rmtree): mock_args = mock.MagicMock() mock_args.command = 'purge' kwargs = self._get_kwargs() kwargs.update(exit_on_error=False) db_command(mock_args) mock_stop.assert_called_with(**kwargs) mock_write_error.assert_called_once() mock_rmtree.assert_called_with(kwargs['db_dir'])
def test_db_command_create(self, mock_create_db_user): mock_args = mock.MagicMock() mock_args.command = 'create' db_command(mock_args) call_args = mock_create_db_user.call_args_list kwargs = self._get_kwargs(remove=['superuser_name', 'superuser_password']) self.assertEqual(call_args[1], mock.call(**kwargs)) kwargs.pop('db_name') kwargs['username'] = self.options['superuser_name'] kwargs['password'] = self.options['superuser_password'] self.assertEqual(call_args[0], mock.call(is_superuser=True, **kwargs))
def test_db_command_configure(self, mock_prompt_err, mock_migrate, mock_createsuperuser): mock_args = mock.MagicMock() mock_args.command = 'configure' db_command(mock_args) kwargs = self._get_kwargs() calls = [ mock.call(init_db_server, **kwargs), mock.call(start_db_server, **kwargs), mock.call(create_tethys_db, **kwargs), ] mock_prompt_err.assert_has_calls(calls) mock_migrate.assert_called_with(**self.options) mock_createsuperuser.assert_called_with(**self.options)
def test_db_command_create(self, mock_create_db_user): mock_args = mock.MagicMock() mock_args.command = 'create' db_command(mock_args) call_args = mock_create_db_user.call_args_list self.assertEqual( call_args[0], mock.call(hostname=self.options['hostname'], port=self.options['port'], username=self.options['username'], password=self.options['password'], db_name=self.options['db_name'])) mock_create_db_user.assert_called_with( hostname=self.options['hostname'], port=self.options['port'], username=self.options['superuser_name'], password=self.options['superuser_password'], is_superuser=True)