Skip to main content

A lightweight in-memory mock server for Google Cloud Spanner. This is an internal library that can make breaking changes without prior notice.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spannermockserver-0.1.1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spannermockserver-0.1.1-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file spannermockserver-0.1.1.tar.gz.

File metadata

  • Download URL: spannermockserver-0.1.1.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spannermockserver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c251fa076037ec65495bb0f2df9fca42790621fe1e5cbdfd81205951909c841
MD5 2b064140b82bbce66c1aae8f4796c623
BLAKE2b-256 6622688c4f936e1a6d7561ea064b38523227d19ac4b87c0d29b654fb8357b557

See more details on using hashes here.

Provenance

The following attestation bundles were made for spannermockserver-0.1.1.tar.gz:

Publisher: release-spannermockserver.yml on googleapis/go-sql-spanner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spannermockserver-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for spannermockserver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77a4296114727fd7d7faedc9c7e8cdc32819db897943a6093291dd3a2c1319fa
MD5 8d9bd04ccd7ef956388e7631c073e2a0
BLAKE2b-256 0f7020f903fe44bba28ea6d1c4b11dec392a2da3a88249d73822cc917ca99b93

See more details on using hashes here.

Provenance

The following attestation bundles were made for spannermockserver-0.1.1-py3-none-any.whl:

Publisher: release-spannermockserver.yml on googleapis/go-sql-spanner

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page