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.2.tar.gz (22.7 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.2-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spannermockserver-0.1.2.tar.gz
  • Upload date:
  • Size: 22.7 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.2.tar.gz
Algorithm Hash digest
SHA256 8fda8c369649c469e841db758ad48445ba4da110050e3e8294b45db74831d39f
MD5 2b5bec3e382aba6e06fd168bd206f43f
BLAKE2b-256 60428896bda05a571f9c089b6407000bdbb91a7781028ca5c82b6710605c1ab5

See more details on using hashes here.

Provenance

The following attestation bundles were made for spannermockserver-0.1.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for spannermockserver-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 468e81520a72b3aa52e9617d2d2531d726c778da54e7dd721a20e3483e08ec84
MD5 9b3a1ccaf7052472449c6a19d5380b4d
BLAKE2b-256 29a8a13ae5f0bbe725440c5eeb8cdf4afc67ee39825837b2ea6e713ba33dd105

See more details on using hashes here.

Provenance

The following attestation bundles were made for spannermockserver-0.1.2-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