Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 2 months 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 (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-09T12:45:35.000Z (9 months ago)
- Last Synced: 2024-08-29T05:42:54.661Z (4 months 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
![Tests](https://github.com/arhea/go-mock-spanner/actions/workflows/main.yml/badge.svg?branch=main) ![goreportcard](https://goreportcard.com/badge/github.com/arhea/go-mock-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
})
}
```