Esempio n. 1
0
 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)
Esempio n. 2
0
 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')
Esempio n. 3
0
 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')
Esempio n. 4
0
 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')
Esempio n. 5
0
 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': '******'})
Esempio n. 6
0
 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')
Esempio n. 7
0
 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')
Esempio n. 8
0
 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')
Esempio n. 9
0
 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')
Esempio n. 10
0
 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'})
Esempio n. 11
0
 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,
     })
Esempio n. 12
0
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))
Esempio n. 13
0
 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,
     })
Esempio n. 14
0
 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'")
Esempio n. 15
0
 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]')
Esempio n. 16
0
 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]')
Esempio n. 17
0
 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":')
Esempio n. 18
0
 def test_mysql_backend_schema_not_allowed(self):
     with self.assertRaises(RuntimeError):
         be = agnostic.create_backend('mysql', 'localhost', None,
             'root', 'password', 'testdb', 'testschema')
Esempio n. 19
0
 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]')
Esempio n. 20
0
 def test_invalid_backend(self):
     with self.assertRaises(ValueError):
         be = agnostic.create_backend('bogusdb', 'localhost', None, 'root',
             'password', 'testdb', None)
Esempio n. 21
0
 def test_sqlite_backend_required_arguments(self):
     with self.assertRaises(RuntimeError):
         be = agnostic.create_backend('sqlite', None, None,
             None, None, None, None)