def mysql_execute_raising(self, exception):
        mysql_cursor = mock.Mock()
        mysql_cursor.execute.return_value = future_raising(exception)
        mysql_cursor.close.return_value = future_returning(None)

        self.mysql.cursor.return_value = future_returning(mysql_cursor)

        return mysql_cursor
    def mysql_execute_returning(self, result):
        mysql_cursor = mock.Mock()
        mysql_cursor.execute.return_value = future_returning(result)
        mysql_cursor.close.return_value = future_returning(None)

        self.mysql.cursor.return_value = future_returning(mysql_cursor)

        return mysql_cursor
    def test_get_returns_500_if_mysql_is_down(self):
        self.mongo_db.command.return_value = future_returning({"ok": 1.0})
        self.redis.ping.return_value = future_returning(b"PONG")
        mysql_cursor = self.mysql_execute_raising(Exception("mysql error"))

        response = self.fetch("/api/v1/healthcheck")

        self.assertEqual(500, response.code)

        mysql_cursor.close.assert_called_once_with()
    async def test_connect_mysql_connects_to_mysql(self, mock_connect):
        expected_result = mock.Mock()

        mock_connect.return_value = future_returning(expected_result)

        result = await run_service.connect_mysql({
            "MYSQL_HOST":
            "mysql-host",
            "MYSQL_PORT":
            "5555",
            "MYSQL_DATABASE":
            "mysql-database",
            "MYSQL_USER":
            "******",
            "MYSQL_PASSWORD":
            "******"
        })

        self.assertEqual(expected_result, result)

        mock_connect.assert_called_once_with(host="mysql-host",
                                             port=5555,
                                             db="mysql-database",
                                             user="******",
                                             password="******")
    def test_get_returns_500_if_redis_is_down(self):
        self.mongo_db.command.return_value = future_returning({"ok": 1.0})
        self.redis.ping.return_value = future_raising(Exception("redis error"))
        self.mysql_execute_returning(None)

        response = self.fetch("/api/v1/healthcheck")

        self.assertEqual(500, response.code)
    def test_get_returns_200_when_healthy(self):
        self.mongo_db.command.return_value = future_returning({"ok": 1.0})
        self.redis.ping.return_value = future_returning(b"PONG")
        mysql_cursor = self.mysql_execute_returning(None)

        response = self.fetch("/api/v1/healthcheck")

        self.assertEqual(200, response.code)
        self.assertEqual("OK", response.body.decode("utf8"))

        self.mongo_db.command.assert_called_once_with("ping")

        self.redis.ping.assert_called_once_with()

        self.mysql.cursor.assert_called_once_with()
        mysql_cursor.execute.assert_called_once_with("SHOW TABLES;")
        mysql_cursor.close.assert_called_once_with()
    async def test_connect_redis_connects_to_redis(self, mock_create_redis):
        expected_result = mock.Mock()

        mock_create_redis.return_value = future_returning(expected_result)

        result = await run_service.connect_redis({
            "REDIS_HOST": "redis-host",
            "REDIS_PORT": "redis-port"
        })

        self.assertEqual(expected_result, result)

        mock_create_redis.assert_called_once_with(("redis-host", "redis-port"))