You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: sql_domain_store_test.go
    path of file: ./repos/cadence/common/persistence/sql
    
    the code of the file until where you have to start completion: // Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package sql

import (
	"context"
	"database/sql"
	"errors"
	"testing"
	"time"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"

	"github.com/uber/cadence/common"
	"github.com/uber/cadence/common/persistence"
	"github.com/uber/cadence/common/persistence/serialization"
	"github.com/uber/cadence/common/persistence/sql/sqlplugin"
	"github.com/uber/cadence/common/types"
)

type sqlResult struct {
	lastInsertID int64
	rowsAffected int64
	err          error
}

func (r *sqlResult) LastInsertId() (int64, error) {
	if r.err != nil {
		return 0, r.err
	}
	return r.lastInsertID, nil
}

func (r *sqlResult) RowsAffected() (int64, error) {
	if r.err != nil {
		return 0, r.err
	}
	return r.rowsAffected, nil
}

func TestGetMetadata(t *testing.T) {
	testCases := []struct {
		name      string
		mockSetup func(*sqlplugin.MockDB)
		want      *persistence.GetMetadataResponse
		wantErr   bool
	}{
		{
			name: "Success case",
			mockSetup: func(mockDB *sqlplugin.MockDB) {
				mockDB.EXPECT().SelectFromDomainMetadata(gomock.Any()).Return(&sqlplugin.DomainMetadataRow{NotificationVersion: 2}, nil)
			},
			want:    &persistence.GetMetadataResponse{NotificationVersion: 2},
			wantErr: false,
		},
		{
			name: "Error case",
			mockSetup: func(mockDB *sqlplugin.MockDB) {
				err := errors.New("some error")
				mockDB.EXPECT().SelectFromDomainMetadata(gomock.Any()).Return(nil, err)
				mockDB.EXPECT().IsNotFoundError(err).Return(true)
			},
			wantErr: true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			defer ctrl.Finish()

			mockDB := sqlplugin.NewMockDB(ctrl)
			store := &sqlDomainStore{
				sqlStore: sqlStore{db: mockDB},
			}

			tc.mockSetup(mockDB)

			got, err := store.GetMetadata(context.Background())
			if tc.wantErr {
				assert.Error(t, err, "Expected an error for test case")
			} else {
				assert.NoError(t, err, "Did not expect an error for test case")
				assert.Equal(t, tc.want, got, "Unexpected result for test case")
			}
		})
	}
}

func TestListDomains(t *testing.T) {
	testCases := []struct {
		name              string
		activeClusterName string
		req               *persistence.ListDomainsRequest
		mockSetup         func(*sqlplugin.MockDB, *serialization.MockParser)
		want              *persistence.InternalListDomainsResponse
		wantErr           bool
	}{
		{
			name:              "Success case",
			activeClusterName: "active",
			req: &persistence.ListDomainsRequest{
				NextPageToken: []byte(`7a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c`),
				PageSize:      1,
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				uuid := serialization.UUID(`7a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c`)
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					GreaterThanID: &uuid,
					PageSize:      common.IntPtr(1),
				}).Return([]sqlplugin.DomainRow{
					{
						ID:           serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c"),
						Name:         "test",
						Data:         []byte(`aaaa`),
						DataEncoding: string(common.EncodingTypeThriftRW),
					},
				}, nil)
				mockParser.EXPECT().DomainInfoFromBlob([]byte(`aaaa`), string(common.EncodingTypeThriftRW)).Return(&serialization.DomainInfo{}, nil)
			},
			want: &persistence.InternalListDomainsResponse{
				Domains: []*persistence.InternalGetDomainResponse{
					{
						Info: &persistence.DomainInfo{
							ID:   "9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c",
							Name: "test",
						},
						Config: &persistence.InternalDomainConfig{},
						ReplicationConfig: &persistence.DomainReplicationConfig{
							ActiveClusterName: "active",
							Clusters: []*persistence.ClusterReplicationConfig{
								&persistence.ClusterReplicationConfig{
									ClusterName: "active",
								},
							},
						},
					},
				},
				NextPageToken: serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c"),
			},
			wantErr: false,
		},
		{
			name:              "No Record case",
			activeClusterName: "active",
			req: &persistence.ListDomainsRequest{
				NextPageToken: []byte(`7a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c`),
				PageSize:      1,
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				uuid := serialization.UUID(`7a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c`)
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					GreaterThanID: &uuid,
					PageSize:      common.IntPtr(1),
				}).Return(nil, sql.ErrNoRows)
			},
			want:    &persistence.InternalListDomainsResponse{},
			wantErr: false,
		},
		{
			name:              "Error from database case",
			activeClusterName: "active",
			req: &persistence.ListDomainsRequest{
				NextPageToken: []byte(`7a3dc7e2-1e67-41aa-8eaf-6d6e27f7
    
func TestGetDomain(t *testing.T) {
	testCases := []struct {
		name              string
		activeClusterName string
		req               *persistence.GetDomainRequest
		mockSetup         func(*sqlplugin.MockDB, *serialization.MockParser)
		want              *persistence.InternalGetDomainResponse
		wantErr           bool
		assertErr         func(*testing.T, error)
	}{
		{
			name:              "Success case - get domain by name",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				Name: "test",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					Name: common.StringPtr("test"),
				}).Return([]sqlplugin.DomainRow{
					{
						ID:           serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c"),
						Name:         "test",
						Data:         []byte(`aaaa`),
						DataEncoding: string(common.EncodingTypeThriftRW),
					},
				}, nil)
				mockParser.EXPECT().DomainInfoFromBlob([]byte(`aaaa`), string(common.EncodingTypeThriftRW)).Return(&serialization.DomainInfo{}, nil)
			},
			want: &persistence.InternalGetDomainResponse{
				Info: &persistence.DomainInfo{
					ID:   "9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c",
					Name: "test",
				},
				Config: &persistence.InternalDomainConfig{},
				ReplicationConfig: &persistence.DomainReplicationConfig{
					ActiveClusterName: "active",
					Clusters: []*persistence.ClusterReplicationConfig{
						&persistence.ClusterReplicationConfig{
							ClusterName: "active",
						},
					},
				},
			},
			wantErr: false,
		},
		{
			name:              "Success case - get domain by ID",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				ID: "9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				uuid := serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c")
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					ID: &uuid,
				}).Return([]sqlplugin.DomainRow{
					{
						ID:           serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c"),
						Name:         "test",
						Data:         []byte(`aaaa`),
						DataEncoding: string(common.EncodingTypeThriftRW),
					},
				}, nil)
				mockParser.EXPECT().DomainInfoFromBlob([]byte(`aaaa`), string(common.EncodingTypeThriftRW)).Return(&serialization.DomainInfo{}, nil)
			},
			want: &persistence.InternalGetDomainResponse{
				Info: &persistence.DomainInfo{
					ID:   "9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c",
					Name: "test",
				},
				Config: &persistence.InternalDomainConfig{},
				ReplicationConfig: &persistence.DomainReplicationConfig{
					ActiveClusterName: "active",
					Clusters: []*persistence.ClusterReplicationConfig{
						&persistence.ClusterReplicationConfig{
							ClusterName: "active",
						},
					},
				},
			},
			wantErr: false,
		},
		{
			name:              "Error case - both domain name and ID are set in request",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				Name: "test",
				ID:   "9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {},
			wantErr:   true,
			assertErr: func(t *testing.T, err error) {
				var expectedErr *types.BadRequestError
				assert.True(t, errors.As(err, &expectedErr), "Expected the error to be BadRequestError")
				assert.Contains(t, err.Error(), "GetDomain operation failed.  Both ID and Name specified in request.")
			},
		},
		{
			name:              "Error case - both domain name and ID are NOT set in request",
			activeClusterName: "active",
			req:               &persistence.GetDomainRequest{},
			mockSetup:         func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {},
			wantErr:           true,
			assertErr: func(t *testing.T, err error) {
				var expectedErr *types.BadRequestError
				assert.True(t, errors.As(err, &expectedErr), "Expected the error to be BadRequestError")
				assert.Contains(t, err.Error(), "GetDomain operation failed.  Both ID and Name are empty.")
			},
		},
		{
			name:              "No Record case",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				Name: "test",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					Name: common.StringPtr("test"),
				}).Return(nil, sql.ErrNoRows)
			},
			wantErr: true,
			assertErr: func(t *testing.T, err error) {
				var expectedErr *types.EntityNotExistsError
				assert.True(t, errors.As(err, &expectedErr), "Expected the error to be EntityNotExistsError")
			},
		},
		{
			name:              "Error from database case",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				Name: "test",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				err := errors.New("some error")
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					Name: common.StringPtr("test"),
				}).Return(nil, err)
				mockDB.EXPECT().IsNotFoundError(err).Return(true)
			},
			wantErr: true,
		},
		{
			name:              "Error from corrupted data case",
			activeClusterName: "active",
			req: &persistence.GetDomainRequest{
				Name: "test",
			},
			mockSetup: func(mockDB *sqlplugin.MockDB, mockParser *serialization.MockParser) {
				mockDB.EXPECT().SelectFromDomain(gomock.Any(), &sqlplugin.DomainFilter{
					Name: common.StringPtr("test"),
				}).Return([]sqlplugin.DomainRow{
					{
						ID:           serialization.MustParseUUID("9a3dc7e2-1e67-41aa-8eaf-6d6e27f7e47c"),
						Name:         "test",
						Data:         []byte(`aaaa`),
						DataEncoding: string(common.EncodingTypeThriftRW),
					},
				}, nil)
				mockParser.EXPECT().DomainInfoFromBlob([]byte(`aaaa`), string(common.EncodingTypeThriftRW)).Return(nil, errors.New("corrupted blob"))
			},
			wantErr: true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			defer ctrl.Finish()

			mockDB := sqlplugin.NewMockDB(ctrl)
			mockParser := serialization.NewMockParser(ctrl)
			store := &sqlDomainStore{
				sqlStore:          sqlStore{db: mockDB, parser: mockParser},
				activeClusterName: tc.activeClusterName,
			}

			tc.mockSetup(mockDB, mockParser)

			got, err := store.GetDomain(context.Background(), tc.req)
			if tc.wantErr {
				assert.Error(t, err, "Expected an error for test case")
				if tc.assertErr != nil {
					tc.assertErr(t, err)
				}
			} else {
				assert.NoError(t, err, "Did not expect an error for test case")
				assert.Equal(t, tc.want, got, "Unexpected result for test case")
			}
		})
	}
}