Skip to content

Commit

Permalink
incorporate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul2393 committed Oct 27, 2023
1 parent 057db9b commit c7b3bf2
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 317 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

1. [File an issue](https://github.com/googleapis/go-gorm/issues/new/choose).
1. [File an issue](https://github.com/googleapis/go-gorm-spanner/issues/new/choose).
The issue will be used to discuss the bug or feature and should be created
before sending a PR.

Expand All @@ -15,7 +15,7 @@
[contributor license agreements](#contributor-license-agreements) below.

1. Clone the repo:
`git clone https://github.com/googleapis/go-gorm`
`git clone https://github.com/googleapis/go-gorm-spanner`

1. Change into the checked out source:
`cd go-gorm`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# go-gorm

[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/googleapis/go-gorm)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/googleapis/go-gorm-spanner)

[Google Cloud Spanner](https://cloud.google.com/spanner) ORM for
Go's [GORM](https://gorm.io/) implementation.
Expand All @@ -9,7 +9,7 @@ Go's [GORM](https://gorm.io/) implementation.
import (
_ "github.com/googleapis/go-sql-spanner"

spannergorm "github.com/googleapis/go-gorm"
spannergorm "github.com/googleapis/go-gorm-spanner"
)

db, err := gorm.Open(spannergorm.New(spannergorm.Config{
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"gorm.io/datatypes"
"gorm.io/gorm"

spannergorm "github.com/googleapis/go-gorm"
spannergorm "github.com/googleapis/go-gorm-spanner"
databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
)

Expand Down
6 changes: 3 additions & 3 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/googleapis/go-gorm/benchmarks
module github.com/googleapis/go-gorm-spanner/benchmarks

go 1.20

replace github.com/googleapis/go-gorm => ../
replace github.com/googleapis/go-gorm-spanner => ../

require (
cloud.google.com/go/spanner v1.45.1
github.com/google/uuid v1.3.0
github.com/googleapis/go-gorm v0.0.0-00010101000000-000000000000
github.com/googleapis/go-gorm-spanner v0.0.0-00010101000000-000000000000
github.com/googleapis/go-sql-spanner v1.0.1
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.54.0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/googleapis/go-gorm
module github.com/googleapis/go-gorm-spanner

go 1.20

Expand Down
20 changes: 8 additions & 12 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"os"
"reflect"
"regexp"
"strconv"
"testing"
Expand All @@ -36,7 +37,7 @@ import (
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"

"github.com/googleapis/go-gorm/testutil"
"github.com/googleapis/go-gorm-spanner/testutil"
)

var projectId, instanceId string
Expand Down Expand Up @@ -219,7 +220,6 @@ func TestMain(m *testing.M) {
cleanup, err := initIntegrationTests()
if err != nil {
log.Fatalf("could not init integration tests: %v", err)
os.Exit(1)
}
res := m.Run()
cleanup()
Expand All @@ -241,7 +241,6 @@ func TestDefaultValue(t *testing.T) {
dsn, cleanup, err := createTestDB(context.Background())
if err != nil {
log.Fatalf("could not init integration tests while creating database: %v", err)
os.Exit(1)
}
defer cleanup()
// Open db.
Expand Down Expand Up @@ -293,7 +292,6 @@ func TestDistinct(t *testing.T) {
dsn, cleanup, err := createTestDB(context.Background())
if err != nil {
log.Fatalf("could not init integration tests while creating database: %v", err)
os.Exit(1)
}
defer cleanup()
// Open db.
Expand Down Expand Up @@ -324,18 +322,18 @@ func TestDistinct(t *testing.T) {

var names []string
db.Table("users").Where("name like ?", "distinct%").Order("name").Pluck("name", &names)
testutil.AssertEqual(t, names, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"})
require.True(t, reflect.DeepEqual(names, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"}))

var names1 []string
db.Model(&testutil.User{}).Where("name like ?", "distinct%").Distinct().Order("name").Pluck("Name", &names1)

testutil.AssertEqual(t, names1, []string{"distinct", "distinct-2", "distinct-3"})
require.True(t, reflect.DeepEqual(names1, []string{"distinct", "distinct-2", "distinct-3"}))

var names2 []string
db.Scopes(func(db *gorm.DB) *gorm.DB {
return db.Table("users")
}).Where("name like ?", "distinct%").Order("name").Pluck("name", &names2)
testutil.AssertEqual(t, names2, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"})
require.True(t, reflect.DeepEqual(names2, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"}))

var results []testutil.User
if err := db.Distinct("name", "age").Where("name like ?", "distinct%").Order("name, age desc").Find(&results).Error; err != nil {
Expand All @@ -349,13 +347,10 @@ func TestDistinct(t *testing.T) {
{Name: "distinct-3", Age: 18},
}

if len(results) != 4 {
if len(results) != len(expects) {
t.Fatalf("invalid results length found, expects: %v, got %v", len(expects), len(results))
}

for idx, expect := range expects {
testutil.AssertObjEqual(t, results[idx], expect, "Name", "Age")
}
require.True(t, reflect.DeepEqual(results, expects))

var count int64
if err := db.Model(&testutil.User{}).Where("name like ?", "distinct%").Count(&count).Error; err != nil || count != 5 {
Expand All @@ -366,6 +361,7 @@ func TestDistinct(t *testing.T) {
t.Errorf("failed to query users count, got error: %v, count %v", err, count)
}

// test for distinct with select
dryDB := db.Session(&gorm.Session{DryRun: true})
r := dryDB.Distinct("u.id, u.*").Table("user_speaks as s").Joins("inner join users as u on u.id = s.user_id").Where("s.language_code ='US' or s.language_code ='ES'").Find(&testutil.User{})
if !regexp.MustCompile(`SELECT DISTINCT u\.id, u\.\* FROM user_speaks as s inner join users as u`).MatchString(r.Statement.SQL.String()) {
Expand Down
6 changes: 3 additions & 3 deletions samples/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/googleapis/go-gorm/samples
module github.com/googleapis/go-gorm-spanner/samples

go 1.20

replace github.com/googleapis/go-gorm => ../
replace github.com/googleapis/go-gorm-spanner => ../

require (
cloud.google.com/go/spanner v1.45.1
github.com/docker/docker v23.0.6+incompatible
github.com/docker/go-connections v0.4.0
github.com/googleapis/go-gorm v0.0.0-00010101000000-000000000000
github.com/googleapis/go-gorm-spanner v0.0.0-00010101000000-000000000000
github.com/googleapis/go-sql-spanner v1.0.1
gorm.io/gorm v1.25.0
)
Expand Down
4 changes: 2 additions & 2 deletions samples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"errors"
"fmt"

spannergorm "github.com/googleapis/go-gorm"
"github.com/googleapis/go-gorm/samples"
spannergorm "github.com/googleapis/go-gorm-spanner"
"github.com/googleapis/go-gorm-spanner/samples"
_ "github.com/googleapis/go-sql-spanner"
"gorm.io/gorm"
)
Expand Down
Loading

0 comments on commit c7b3bf2

Please sign in to comment.