https://github.com/arhea/go-mock-spanner
Provides container based mock for Google Cloud Spanner so that you can unit test database code.
https://github.com/arhea/go-mock-spanner
go golang golang-package google-cloud google-cloud-spanner spanner testcontainers testcontainers-go testcontainers-go-contrib
Last synced: about 1 year ago
JSON representation
Provides container based mock for Google Cloud Spanner so that you can unit test database code.
- Host: GitHub
- URL: https://github.com/arhea/go-mock-spanner
- Owner: arhea
- License: mit
- Created: 2023-12-02T16:18:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-09T12:45:35.000Z (about 2 years ago)
- Last Synced: 2025-03-11T08:50:13.627Z (over 1 year ago)
- Topics: go, golang, golang-package, google-cloud, google-cloud-spanner, spanner, testcontainers, testcontainers-go, testcontainers-go-contrib
- Language: Go
- Homepage: https://pkg.go.dev/github.com/arhea/go-mock-spanner
- Size: 56.6 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Mock Google Cloud Spanner
 
Provides a mock of [Google Cloud Spanner](https://cloud.google.com/spanner?hl=en) using the official [Google Cloud Spanner Emulator](https://github.com/GoogleCloudPlatform/cloud-spanner-emulator).
These mocks will automatically create a new emulator, wait for it to be available, then create a mock database. You will need to run your database migrations prior to performing your tests.
I recommend reusing the instance across multiple tests to reduce test run times.
This library is built on top of [testcontainers](https://testcontainers.com/).
## Usage
Creating a mock instance for creating a customer connection.
```golang
func TestXXX(t *testing.T) {
ctx := context.Background()
mock, err := mockspanner.NewInstance(ctx, t)
if err != nil {
t.Fatalf("creating the instance: %v", err)
return
}
// close the mock
defer mock.Close(ctx)
// ... my test code
}
```
Creating a mock Spanner client for interacting with Spanner via the Go client.
```golang
func TestXXX(t *testing.T) {
ctx := context.Background()
mock, err := mockspanner.NewClient(ctx, t)
if err != nil {
t.Fatalf("creating the client: %v", err)
return
}
// close the mock
defer mock.Close(ctx)
spannerClient := mock.Client()
t.Run("MyTest1", func(t *testing.T) {
// ... my test code
})
t.Run("MyTest2", func(t *testing.T) {
// ... my test code
})
}
```