class TestMySQL(unittest.TestCase): def setUp(self): logging.getLogger().setLevel(logging.WARN) self.environ = get_environ() self.my = MySQL(self.environ) self.my._conn = mock.MagicMock() def tearDown(self): logging.getLogger().setLevel(logging.DEBUG) def test_parse(self): self.assertEqual(self.my.mysql_db, 'test_mydb') self.assertEqual(self.my.mysql_user, 'test_me') self.assertEqual(self.my.mysql_password, 'test_pass') self.assertEqual(self.my.mysql_root_password, 'test_root_pass') self.assertEqual(self.my.mysql_random_root_password, True) self.assertEqual(self.my.mysql_onetime_password, True) self.assertEqual(self.my.repl_user, 'test_repl_user') self.assertEqual(self.my.repl_password, 'test_repl_pass') self.assertEqual(self.my.datadir, '/var/lib/mysql') self.assertEqual(self.my.pool_size, 100) self.assertIsNotNone(self.my.ip) def test_query_buffer_execute_should_flush(self): self.my.add('query 1', ()) self.assertEqual(len(self.my._query_buffer.items()), 1) self.assertEqual(len(self.my._conn.mock_calls), 0) self.my.execute('query 2', ()) self.assertEqual(len(self.my._query_buffer.items()), 0) exec_calls = [ mock.call.cursor().execute('query 1', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 2', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], exec_calls) def test_query_buffer_execute_many_should_flush(self): self.my.add('query 3', ()) self.my.add('query 4', ()) self.my.add('query 5', ()) self.my.execute_many() self.assertEqual(len(self.my._query_buffer.items()), 0) exec_many_calls = [ mock.call.cursor().execute('query 3', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 4', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 5', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], exec_many_calls) def test_query_buffer_query_should_flush(self): self.my.query('query 6', ()) self.assertEqual(len(self.my._query_buffer.items()), 0) query_calls = [ mock.call.cursor().execute('query 6', params=()), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], query_calls) def test_expected_setup_statements(self): conn = mock.MagicMock() self.my.setup_root_user(conn) self.my.create_db(conn) self.my.create_default_user(conn) self.my.create_repl_user(conn) self.my.expire_root_password(conn) self.assertEqual(len(self.my._conn.mock_calls), 0) # use param, not attr statements = [ args[0] for (name, args, _) in conn.mock_calls if name == 'cursor().execute' ] expected = [ 'SET @@SESSION.SQL_LOG_BIN=0;', "DELETE FROM `mysql`.`user` where user != 'mysql.sys';", 'CREATE USER `root`@`%` IDENTIFIED BY %s ;', 'GRANT ALL ON *.* TO `root`@`%` WITH GRANT OPTION ;', 'DROP DATABASE IF EXISTS test ;', 'FLUSH PRIVILEGES ;', 'CREATE DATABASE IF NOT EXISTS `test_mydb`;', 'CREATE USER `test_me`@`%` IDENTIFIED BY %s;', 'GRANT ALL ON `test_mydb`.* TO `test_me`@`%`;', 'FLUSH PRIVILEGES;', 'CREATE USER `test_repl_user`@`%` IDENTIFIED BY %s; ', ('GRANT SUPER, SELECT, INSERT, REPLICATION SLAVE, RELOAD,' ' LOCK TABLES, GRANT OPTION, REPLICATION CLIENT, RELOAD,' ' DROP, CREATE ON *.* TO `test_repl_user`@`%`; '), 'FLUSH PRIVILEGES;', 'ALTER USER `root`@`%` PASSWORD EXPIRE' ] self.assertEqual(statements, expected)
class TestMySQL(unittest.TestCase): def setUp(self): logging.getLogger().setLevel(logging.WARN) self.environ = get_environ() self.my = MySQL(self.environ) self.my._conn = mock.MagicMock() def tearDown(self): logging.getLogger().setLevel(logging.DEBUG) def test_parse(self): self.assertEqual(self.my.mysql_db, 'test_mydb') self.assertEqual(self.my.mysql_user, 'test_me') self.assertEqual(self.my.mysql_password, 'test_pass') self.assertEqual(self.my.mysql_root_password, 'test_root_pass') self.assertEqual(self.my.mysql_random_root_password, True) self.assertEqual(self.my.mysql_onetime_password, True) self.assertEqual(self.my.repl_user, 'test_repl_user') self.assertEqual(self.my.repl_password, 'test_repl_pass') self.assertEqual(self.my.datadir, '/var/lib/mysql') self.assertEqual(self.my.pool_size, 100) self.assertIsNotNone(self.my.ip) def test_query_buffer_execute_should_flush(self): self.my.add('query 1', ()) self.assertEqual(len(self.my._query_buffer.items()), 1) self.assertEqual(len(self.my._conn.mock_calls), 0) self.my.execute('query 2', ()) self.assertEqual(len(self.my._query_buffer.items()), 0) exec_calls = [ mock.call.cursor().execute('query 1', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 2', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], exec_calls) def test_query_buffer_execute_many_should_flush(self): self.my.add('query 3', ()) self.my.add('query 4', ()) self.my.add('query 5', ()) self.my.execute_many() self.assertEqual(len(self.my._query_buffer.items()), 0) exec_many_calls = [ mock.call.cursor().execute('query 3', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 4', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().execute('query 5', params=()), mock.call.commit(), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], exec_many_calls) def test_query_buffer_query_should_flush(self): self.my.query('query 6', ()) self.assertEqual(len(self.my._query_buffer.items()), 0) query_calls = [ mock.call.cursor().execute('query 6', params=()), mock.call.cursor().fetchall(), mock.call.cursor().close() ] self.assertEqual(self.my._conn.mock_calls[2:], query_calls) def test_expected_setup_statements(self): conn = mock.MagicMock() self.my.setup_root_user(conn) self.my.create_db(conn) self.my.create_default_user(conn) self.my.create_repl_user(conn) self.my.expire_root_password(conn) self.assertEqual(len(self.my._conn.mock_calls), 0) # use param, not attr statements = [args[0] for (name, args, _) in conn.mock_calls if name == 'cursor().execute'] expected = [ 'SET @@SESSION.SQL_LOG_BIN=0;', "DELETE FROM `mysql`.`user` where user != 'mysql.sys';", 'CREATE USER `root`@`%` IDENTIFIED BY %s ;', 'GRANT ALL ON *.* TO `root`@`%` WITH GRANT OPTION ;', 'DROP DATABASE IF EXISTS test ;', 'FLUSH PRIVILEGES ;', 'CREATE DATABASE IF NOT EXISTS `test_mydb`;', 'CREATE USER `test_me`@`%` IDENTIFIED BY %s;', 'GRANT ALL ON `test_mydb`.* TO `test_me`@`%`;', 'FLUSH PRIVILEGES;', 'CREATE USER `test_repl_user`@`%` IDENTIFIED BY %s; ', ('GRANT SUPER, SELECT, INSERT, REPLICATION SLAVE, RELOAD,' ' LOCK TABLES, GRANT OPTION, REPLICATION CLIENT, RELOAD,' ' DROP, CREATE ON *.* TO `test_repl_user`@`%`; '), 'FLUSH PRIVILEGES;', 'ALTER USER `root`@`%` PASSWORD EXPIRE'] self.assertEqual(statements, expected)