Ejemplo n.º 1
0
    def test_executemany_insert_batch_failed(self):
        from google.cloud.spanner_dbapi import connect
        from google.cloud.spanner_dbapi.exceptions import OperationalError
        from google.cloud.spanner_v1.types.spanner import Session
        from google.rpc.code_pb2 import UNKNOWN

        sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""
        err_details = "Details here"

        with mock.patch("google.cloud.spanner_v1.instance.Instance.exists",
                        return_value=True):
            with mock.patch(
                    "google.cloud.spanner_v1.database.Database.exists",
                    return_value=True,
            ):
                connection = connect("test-instance", "test-database")

        connection.autocommit = True
        cursor = connection.cursor()

        transaction = mock.Mock(committed=False, rolled_back=False)
        transaction.batch_update = mock.Mock(
            return_value=(mock.Mock(code=UNKNOWN, details=err_details), []))

        with mock.patch(
                "google.cloud.spanner_v1.services.spanner.client.SpannerClient.create_session",
                return_value=Session(),
        ):
            with mock.patch(
                    "google.cloud.spanner_v1.session.Session.transaction",
                    return_value=transaction,
            ):
                with self.assertRaisesRegex(OperationalError, err_details):
                    cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])
Ejemplo n.º 2
0
    def test_executemany_insert_batch_non_autocommit(self):
        from google.cloud.spanner_dbapi import connect
        from google.cloud.spanner_v1.param_types import INT64
        from google.cloud.spanner_v1.types.spanner import Session

        sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""

        with mock.patch("google.cloud.spanner_v1.instance.Instance.exists",
                        return_value=True):
            with mock.patch(
                    "google.cloud.spanner_v1.database.Database.exists",
                    return_value=True,
            ):
                connection = connect("test-instance", "test-database")

        transaction = self._transaction_mock()

        cursor = connection.cursor()
        with mock.patch(
                "google.cloud.spanner_v1.services.spanner.client.SpannerClient.create_session",
                return_value=Session(),
        ):
            with mock.patch(
                    "google.cloud.spanner_v1.session.Session.transaction",
                    return_value=transaction,
            ):
                cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])

        transaction.batch_update.assert_called_once_with([
            (
                """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
                {
                    "a0": 1,
                    "a1": 2,
                    "a2": 3,
                    "a3": 4
                },
                {
                    "a0": INT64,
                    "a1": INT64,
                    "a2": INT64,
                    "a3": INT64
                },
            ),
            (
                """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
                {
                    "a0": 5,
                    "a1": 6,
                    "a2": 7,
                    "a3": 8
                },
                {
                    "a0": INT64,
                    "a1": INT64,
                    "a2": INT64,
                    "a3": INT64
                },
            ),
        ])
Ejemplo n.º 3
0
    def test_executemany_delete_batch_autocommit(self):
        from google.cloud.spanner_dbapi import connect
        from google.cloud.spanner_v1.param_types import INT64
        from google.cloud.spanner_v1.types.spanner import Session

        sql = "DELETE FROM table WHERE col1 = %s"

        connection = connect("test-instance", "test-database")

        connection.autocommit = True
        transaction = self._transaction_mock()
        cursor = connection.cursor()

        with mock.patch(
            "google.cloud.spanner_v1.services.spanner.client.SpannerClient.create_session",
            return_value=Session(),
        ):
            with mock.patch(
                "google.cloud.spanner_v1.session.Session.transaction",
                return_value=transaction,
            ):
                cursor.executemany(sql, [(1,), (2,), (3,)])

        transaction.batch_update.assert_called_once_with(
            [
                ("DELETE FROM table WHERE col1 = @a0", {"a0": 1}, {"a0": INT64}),
                ("DELETE FROM table WHERE col1 = @a0", {"a0": 2}, {"a0": INT64}),
                ("DELETE FROM table WHERE col1 = @a0", {"a0": 3}, {"a0": INT64}),
            ]
        )
Ejemplo n.º 4
0
    def test_executemany_update_batch_autocommit(self):
        from google.cloud.spanner_dbapi import connect
        from google.cloud.spanner_v1.param_types import INT64, STRING
        from google.cloud.spanner_v1.types.spanner import Session

        sql = "UPDATE table SET col1 = %s WHERE col2 = %s"

        connection = connect("test-instance", "test-database")

        connection.autocommit = True
        transaction = self._transaction_mock()
        cursor = connection.cursor()

        with mock.patch(
                "google.cloud.spanner_v1.services.spanner.client.SpannerClient.create_session",
                return_value=Session(),
        ):
            with mock.patch(
                    "google.cloud.spanner_v1.session.Session.transaction",
                    return_value=transaction,
            ):
                cursor.executemany(sql, [(1, "a"), (2, "b"), (3, "c")])

        transaction.batch_update.assert_called_once_with([
            (
                "UPDATE table SET col1 = @a0 WHERE col2 = @a1",
                {
                    "a0": 1,
                    "a1": "a"
                },
                {
                    "a0": INT64,
                    "a1": STRING
                },
            ),
            (
                "UPDATE table SET col1 = @a0 WHERE col2 = @a1",
                {
                    "a0": 2,
                    "a1": "b"
                },
                {
                    "a0": INT64,
                    "a1": STRING
                },
            ),
            (
                "UPDATE table SET col1 = @a0 WHERE col2 = @a1",
                {
                    "a0": 3,
                    "a1": "c"
                },
                {
                    "a0": INT64,
                    "a1": STRING
                },
            ),
        ])