Skip to main content

Spanner Python Mockserver

NOTICE: This is an internal library that can make breaking changes without prior notice.

Introduction

The spannermockserver is a lightweight in-memory mock server for Google Cloud Spanner.

Installation

To install the library, use pip:

pip install spannermockserver

Usage

Here is a simple example of how to use the mock server to test your Spanner application.

1. Start the Mock Server

from spannermockserver import start_mock_server

# Start the server
server, spanner_servicer, database_admin_servicer, port = start_mock_server()

2. Configure Your Client

Configure your Spanner client to connect to the mock server.

import os
from google.cloud import spanner
from google.auth.credentials import AnonymousCredentials

# Set environment variable to use the emulator/mock server
os.environ["SPANNER_EMULATOR_HOST"] = f"localhost:{port}"

# Create a client with anonymous credentials
client = spanner.Client(
    project="test-project",
    credentials=AnonymousCredentials()
)
instance = client.instance("test-instance")
database = instance.database("test-database")

3. Mock Results

Use spanner_servicer.mock_spanner to define the results for your queries.

from google.cloud.spanner_v1.types import ResultSet, ResultSetMetadata, StructType, Type, TypeCode

# Define the SQL you expect
sql = "SELECT 1"

# Create a mock result set with schema definition
metadata = ResultSetMetadata(
    row_type=StructType(
        fields=[
            StructType.Field(
                name="Result",
                type=Type(code=TypeCode.INT64)
            )
        ]
    )
)
result = ResultSet(metadata=metadata)
result.rows.append(["1"])

# Add the result to the mock spanner
spanner_servicer.mock_spanner.add_result(sql, result)

4. Run Your Code

Execute your application code that queries Spanner.

with database.snapshot() as snapshot:
    results = snapshot.execute_sql(sql)
    for row in results:
        print(f"Row: {row}")

5. Verify Requests (Optional)

You can inspect spanner_servicer.requests to verify that your code made the expected calls.

print(f"Total requests received: {len(spanner_servicer.requests)}")

6. Stop the Server

server.stop(None)

API Reference

start_mock_server()

Starts the gRPC server and returns a tuple containing:

  • server: The gRPC server instance.
  • spanner_servicer: The SpannerServicer instance.
  • database_admin_servicer: The DatabaseAdminServicer instance.
  • port: The port number the server is listening on.

SpannerServicer

The implementation of the Spanner gRPC service.

  • mock_spanner: Access the MockSpanner instance to configure behavior.
  • requests: A list of all request objects received by the servicer.
  • clear_requests(): Clears the list of received requests.

MockSpanner

Handles the in-memory state and responses.

  • add_result(sql, result): Maps a SQL string to a ResultSet response.
  • add_error(method, error): Maps a gRPC method name (e.g., "ExecuteSql") to an error (an instance of grpc_status.rpc_status._Status) to simulate failures.

Metadata

Release files for spannermockserver 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spannermockserver 0.1.2
File Size Uploaded Provenance Actions
spannermockserver-0.1.2.tar.gz 22.7 kB With provenance Details for spannermockserver-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spannermockserver 0.1.2
File Interpreter ABI Platform Actions
spannermockserver-0.1.2-py3-none-any.whl Python 3 none any Details for spannermockserver-0.1.2-py3-none-any.whl

Total release size: 48.0 kB

Release files / spannermockserver-0.1.2.tar.gz

Download URL spannermockserver-0.1.2.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8fda8c369649c469e841db758ad48445ba4da110050e3e8294b45db74831d39f
BLAKE2b-256 checksum
How to use checksums
60428896bda05a571f9c089b6407000bdbb91a7781028ca5c82b6710605c1ab5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2026.

Transparency log

Release files / spannermockserver-0.1.2-py3-none-any.whl

Download URL spannermockserver-0.1.2-py3-none-any.whl
Size 25.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
468e81520a72b3aa52e9617d2d2531d726c778da54e7dd721a20e3483e08ec84
BLAKE2b-256 checksum
How to use checksums
29a8a13ae5f0bbe725440c5eeb8cdf4afc67ee39825837b2ea6e713ba33dd105
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2026.

Transparency log

Security for spannermockserver 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for spannermockserver-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page