def test_mysql_backend_required_arguments(self): with self.assertRaises(RuntimeError): be = agnostic.create_backend('mysql', 'localhost', None, None, 'password', 'testdb', None) with self.assertRaises(RuntimeError): be = agnostic.create_backend('mysql', 'localhost', None, 'root', 'password', None, None)
def test_sqlite_backend_arguments_not_allowed(self): with self.assertRaises(RuntimeError): be = agnostic.create_backend('sqlite', 'localhost', None, None, None, 'test.db', None) with self.assertRaises(RuntimeError): be = agnostic.create_backend('sqlite', None, None, 'root', None, 'test.db', None) with self.assertRaises(RuntimeError): be = agnostic.create_backend('sqlite', None, None, None, 'password', 'test.db', None) with self.assertRaises(RuntimeError): be = agnostic.create_backend('sqlite', None, None, None, None, 'test.db', 'testschema')
def test_postgres_clear_db_with_schema(self): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', 'schema1,public') mock_cursor = MagicMock() be.clear_db(mock_cursor) self.assertTrue(mock_cursor.execute.called) mock_cursor.execute.assert_any_call( 'DROP SCHEMA IF EXISTS schema1 CASCADE')
def test_postgres_backend_with_schema(self): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', '"$user",public') self.assertIsInstance(be, PostgresBackend) self.assertEqual(be.location, 'database [testdb schema="$user",public]') self.assertEqual(be.get_schema_command(), 'SET search_path = "$user",public;\n')
def test_postgres_backup_with_port(self, mock_subprocess): be = agnostic.create_backend('postgres', 'localhost', 5433, 'root', 'password', 'testdb', None) be.backup_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], [ 'pg_dump', '-h', 'localhost', '-U', 'root', '-p', '5433', 'testdb' ]) self.assertEqual(args[1]['env'], {'PGPASSWORD': '******'})
def test_postgres_backup_with_schema(self, mock_subprocess): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', 'testschema') be.backup_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], ['pg_dump', '-h', 'localhost', '-U', 'root', '-n', 'testschema', 'testdb']) self.assertIn('PGPASSWORD', args[1]['env']) self.assertEqual(args[1]['env']['PGPASSWORD'], 'password')
def test_postgres_restore_with_port(self, mock_subprocess): be = agnostic.create_backend('postgres', 'localhost', 5433, 'root', 'password', 'testdb', None) be.restore_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], ['psql', '-h', 'localhost', '-U', 'root', '-v', 'ON_ERROR_STOP=1', '-p', '5433', 'testdb']) self.assertIn('PGPASSWORD', args[1]['env']) self.assertEqual(args[1]['env']['PGPASSWORD'], 'password')
def test_mysql_backup_with_port(self, mock_subprocess): be = agnostic.create_backend('mysql', 'localhost', 3307, 'root', 'password', 'testdb', None) be.backup_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], ['mysqldump', '-h', 'localhost', '-u', 'root', '-P', '3307', 'testdb']) self.assertIn('MYSQL_PWD', args[1]['env']) self.assertEqual(args[1]['env']['MYSQL_PWD'], 'password')
def test_postgres_snapshot_with_schema(self, mock_subprocess): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', '"$user",public') be.snapshot_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], ['pg_dump', '-h', 'localhost', '-U', 'root', '-s', '-x', '-O', '--no-tablespaces', '-n', 'root', '-n', 'public', 'testdb']) self.assertIn('PGPASSWORD', args[1]['env']) self.assertEqual(args[1]['env']['PGPASSWORD'], 'password')
def test_mysql_snapshot_with_port(self, mock_subprocess): be = agnostic.create_backend('mysql', 'localhost', 3307, 'root', 'password', 'testdb', None) be.snapshot_db('test-file') self.assertTrue(mock_subprocess.Popen.called) args = mock_subprocess.Popen.call_args self.assertEqual(args[0][0], [ 'mysqldump', '-h', 'localhost', '-u', 'root', '--no-create-db', '--no-data', '--compact', '-P', '3307', 'testdb' ]) self.assertEqual(args[1]['env'], {'MYSQL_PWD': 'password'})
def test_postgres_connect_with_port(self, mock_pymysql): be = agnostic.create_backend('postgres', 'localhost', 5433, 'root', 'password', 'testdb', None) be.connect_db() self.assertTrue(mock_pymysql.connect.called) self.assertEqual(mock_pymysql.connect.call_args[1], { 'host': 'localhost', 'user': '******', 'password': '******', 'database': 'testdb', 'port': 5433, })
def main(config, db_type, host, port, user, password, database, schema, migrations_dir, debug): ''' Agnostic database migrations: upgrade schemas, save your sanity. ''' config.debug = debug config.migrations_dir = migrations_dir try: config.backend = create_backend(db_type, host, port, user, password, database, schema) except RuntimeError as re: raise click.ClickException(str(re))
def test_mysql_connect_with_port(self, mock_pymysql): be = agnostic.create_backend('mysql', 'localhost', 3307, 'root', 'password', 'testdb', None) be.connect_db() self.assertTrue(mock_pymysql.connect.called) self.assertEqual(mock_pymysql.connect.call_args[1], { 'host': 'localhost', 'user': '******', 'password': '******', 'database': 'testdb', 'autocommit': True, 'port': 3307, })
def test_postgres_connect_with_schema(self, mock_pymysql): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', 'testschema') db = be.connect_db() self.assertTrue(mock_pymysql.connect.called) self.assertEqual(mock_pymysql.connect.call_args[1], { 'host': 'localhost', 'user': '******', 'password': '******', 'database': 'testdb', }) cursor = db.cursor.return_value cursor.execute.assert_called_with("SET SCHEMA 'testschema'")
def test_sqlite_backend(self): be = agnostic.create_backend('sqlite', None, None, None, None, 'test.db', None) self.assertIsInstance(be, SqlLiteBackend) self.assertEqual(be.location, 'database [test.db]')
def test_postgres_backend(self): be = agnostic.create_backend('postgres', 'localhost', None, 'root', 'password', 'testdb', None) self.assertIsInstance(be, PostgresBackend) self.assertEqual(be.location, 'database [testdb]')
def test_mysql_backend_no_pass(self, mock_getpass): be = agnostic.create_backend('mysql', 'localhost', None, 'root', None, 'testdb', None) mock_getpass.assert_called_with('Enter password for "root" on "testdb":')
def test_mysql_backend_schema_not_allowed(self): with self.assertRaises(RuntimeError): be = agnostic.create_backend('mysql', 'localhost', None, 'root', 'password', 'testdb', 'testschema')
def test_mysql_backend(self): be = agnostic.create_backend('mysql', 'localhost', None, 'root', 'password', 'testdb', None) self.assertIsInstance(be, MysqlBackend) self.assertEqual(be.location, 'database [testdb]')
def test_invalid_backend(self): with self.assertRaises(ValueError): be = agnostic.create_backend('bogusdb', 'localhost', None, 'root', 'password', 'testdb', None)
def test_sqlite_backend_required_arguments(self): with self.assertRaises(RuntimeError): be = agnostic.create_backend('sqlite', None, None, None, None, None, None)