Skip to content

feat: Spanner Mock Server#16041

Open
bhatt4982 wants to merge 7 commits intomainfrom
sms
Open

feat: Spanner Mock Server#16041
bhatt4982 wants to merge 7 commits intomainfrom
sms

Conversation

@bhatt4982
Copy link

Add Spanner Mock Server to Python Monorepo.

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes#16040 🦕

@bhatt4982 bhatt4982 requested review from a team as code owners March 6, 2026 16:27
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers a crucial tool for Python developers working with Google Cloud Spanner: an in-memory mock server. This server facilitates local development and testing by mimicking the behavior of a real Spanner instance, allowing for faster feedback cycles and more robust automated tests. It abstracts away the complexities of interacting with a live service, making it easier to simulate various scenarios and edge cases during development.

Highlights

  • New Spanner Mock Server Library: Introduced a new Python library, google-cloud-spanner-mockserver, to provide an in-memory mock server for Google Cloud Spanner. This enables developers to test Spanner applications without requiring a live Spanner instance.
  • gRPC Service Mocking: Implemented mock servicers for both the Spanner API and the Spanner Database Admin API, allowing for the simulation of various gRPC calls and responses.
  • Test Utilities and Integration: Provided a MockServerTestBase class to simplify the setup and teardown of the mock server in unit and integration tests, along with helper functions for mocking SQL query results and errors.
  • Automated gRPC Code Generation: Configured Nox sessions to automatically generate gRPC client and server code from Googleapis protobuf definitions, including a post-processing step to ensure compatibility with google-cloud-spanner's proto-plus types.
Changelog
  • packages/google-cloud-spanner-mockserver/.gitignore
    • Added standard Python and IDE-specific ignore patterns.
  • packages/google-cloud-spanner-mockserver/LICENSE
    • Added the Apache License, Version 2.0.
  • packages/google-cloud-spanner-mockserver/README.md
    • Added comprehensive documentation covering installation, usage examples, and API reference for the mock server.
  • packages/google-cloud-spanner-mockserver/noxfile.py
    • Added Nox sessions for code formatting (isort, black), linting (flake8), unit testing, integration testing, gRPC code generation, and package building/installation.
    • Implemented a post_process_generated_files function to modify generated gRPC files for compatibility with google-cloud-spanner types and serialization methods.
  • packages/google-cloud-spanner-mockserver/pyproject.toml
    • Added project metadata, dependencies (grpcio, google-cloud-spanner, protobuf, google-api-core), and optional dependencies for testing, linting, and development.
  • packages/google-cloud-spanner-mockserver/spannermockserver/init.py
    • Initialized the package, defined the version, and exported key components of the mock server and test utilities.
  • packages/google-cloud-spanner-mockserver/spannermockserver/generated/README.md
    • Added documentation explaining the purpose of the generated gRPC code, how to generate it, and the necessity of post-processing.
  • packages/google-cloud-spanner-mockserver/spannermockserver/generated/init.py
    • Added an empty __init__.py file to mark the directory as a Python package.
  • packages/google-cloud-spanner-mockserver/spannermockserver/generated/spanner_database_admin_pb2_grpc.py
    • Added generated gRPC client and server classes for the Spanner Database Admin API, with post-processing applied for type compatibility.
  • packages/google-cloud-spanner-mockserver/spannermockserver/generated/spanner_pb2_grpc.py
    • Added generated gRPC client and server classes for the Spanner API, with post-processing applied for type compatibility.
  • packages/google-cloud-spanner-mockserver/spannermockserver/mock_database_admin.py
    • Implemented DatabaseAdminServicer to mock the Spanner Database Admin API, including request tracking and a basic UpdateDatabaseDdl implementation.
  • packages/google-cloud-spanner-mockserver/spannermockserver/mock_spanner.py
    • Implemented MockSpanner for managing mock results and errors.
    • Implemented SpannerServicer to mock the core Spanner API, handling sessions, transactions, SQL execution, and streaming results.
  • packages/google-cloud-spanner-mockserver/spannermockserver/testbase/init.py
    • Initialized the testbase package and exported common test utilities.
  • packages/google-cloud-spanner-mockserver/spannermockserver/testbase/mock_server_test_base.py
    • Created MockServerTestBase as a base class for tests, providing mock server setup/teardown and convenient access to Spanner client, instance, and database objects.
    • Added helper functions like add_result, set_database_dialect, add_result_select_1, and add_single_result for easy test data setup.
  • packages/google-cloud-spanner-mockserver/tests/init.py
    • Added an empty __init__.py file to mark the directory as a Python package.
  • packages/google-cloud-spanner-mockserver/tests/integration/init.py
    • Added an empty __init__.py file to mark the directory as a Python package.
  • packages/google-cloud-spanner-mockserver/tests/integration/test_mock_spanner.py
    • Added integration tests to verify the functionality of the mock Spanner server for various operations like execute_sql, execute_streaming_sql, commit, rollback, partition_query, and partition_read.
  • packages/google-cloud-spanner-mockserver/tests/unit/init.py
    • Added an empty __init__.py file to mark the directory as a Python package.
  • packages/google-cloud-spanner-mockserver/tests/unit/test_mock_spanner.py
    • Added unit tests for the MockSpanner and SpannerServicer classes, covering result management, error handling, and session/transaction creation.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new google-cloud-spanner-mockserver package, which is a great addition for testing applications that use Spanner. The overall structure, including the use of nox for automation, packaging setup, and the test base class, is well-designed. However, the mock server implementation is currently partial, with several key gRPC methods implemented as placeholders. My review focuses on suggestions to complete these placeholder implementations to make the mock server more comprehensive and useful. I've also identified a critical issue in a generated gRPC client stub that uses incorrect serialization methods, which would cause runtime failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant