-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# yowrap | ||
|
||
[![GoDoc](https://img.shields.io/static/v1?label=godoc&message=reference&color=blue)](https://pkg.go.dev/github.com/jferrl/yowrap) | ||
[![Test Status](https://github.com/jferrl/yowrap/workflows/tests/badge.svg)](https://github.com/jferrl/yowrap/actions?query=workflow%3Atests) | ||
[![codecov](https://codecov.io/gh/jferrl/yowrap/branch/main/graph/badge.svg?token=68I4BZF235)](https://codecov.io/gh/jferrl/yowrap) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/jferrl/yowrap)](https://goreportcard.com/report/github.com/jferrl/yowrap) | ||
|
||
Lightweight Go package designed to effortlessly wrap the [Yo](https://github.com/cloudspannerecosystem/yo) package for enhanced functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Internal | ||
|
||
This directory contains the internal implementation of the project. It is not intended to be used by the end user. | ||
This directory only contains yo test models needed in the test cases. | ||
|
||
## Structure | ||
|
||
The internal directory is structured as follows: | ||
|
||
```text | ||
internal/ | ||
├── sql/ // Contains the DDL to generate the test model using yo tool | ||
│ ├── ddl.go | ||
├── user/ // Contains the test model and the generated code for the test model | ||
│ ├── user.yo.go | ||
│ ├── yo_db.yo.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package yowrap | ||
|
||
// Mutation defines a mutation that can be executed within spanner. | ||
type Mutation int | ||
|
||
const ( | ||
// Insert is a mutation that inserts a row into a table. | ||
Insert Mutation = iota + 1 | ||
// Update is a mutation that updates a row in a table. | ||
Update | ||
// InsertOrUpdate is a mutation that inserts a row into a table. If the row | ||
// already exists, it updates it instead. Any column values not explicitly | ||
// written are preserved. | ||
InsertOrUpdate | ||
// Delete is a mutation that deletes a row from a table. | ||
Delete | ||
) | ||
|
||
// Hooks defines an action that can be executed during a mutation. | ||
func (m Mutation) Hooks() (before, after Hook) { | ||
switch m { | ||
case Insert: | ||
return BeforeInsert, AfterInsert | ||
case Update: | ||
return BeforeUpdate, AfterUpdate | ||
case InsertOrUpdate: | ||
return BeforeInsertOrUpdate, AfterInsertOrUpdate | ||
case Delete: | ||
return BeforeDelete, AfterDelete | ||
default: | ||
return 0, 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package yowrap | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"sort" | ||
"testing" | ||
|
@@ -49,7 +50,7 @@ func TestModel_Insert(t *testing.T) { | |
} | ||
} | ||
|
||
func TestModel_ApplyInsert(t *testing.T) { | ||
func TestModel_Apply(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
model := &user.User{ | ||
|
@@ -66,13 +67,21 @@ func TestModel_ApplyInsert(t *testing.T) { | |
tests := []struct { | ||
name string | ||
fields fields | ||
hooks []HookFunc | ||
mutType Mutation | ||
before HookFunc[*user.User] | ||
after HookFunc[*user.User] | ||
wantErr bool | ||
want []*user.User | ||
}{ | ||
{ | ||
name: "insert a new user into the database", | ||
fields: fields{model: model}, | ||
name: "unknown mutation returns an error", | ||
fields: fields{model: model}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "insert a new user into the database", | ||
fields: fields{model: model}, | ||
mutType: Insert, | ||
want: []*user.User{ | ||
{ | ||
Name: "John Doe", | ||
|
@@ -81,33 +90,52 @@ func TestModel_ApplyInsert(t *testing.T) { | |
}, | ||
}, | ||
{ | ||
name: "insert a new user into the database with a hook", | ||
fields: fields{ | ||
model: model, | ||
}, | ||
hooks: []HookFunc{ | ||
func(ctx context.Context) []*spanner.Mutation { | ||
user := &user.User{ | ||
ID: uuid.NewString(), | ||
Name: "Jane Doe", | ||
Email: "[email protected]", | ||
CreatedAt: spanner.CommitTimestamp, | ||
UpdatedAt: spanner.CommitTimestamp, | ||
} | ||
|
||
return []*spanner.Mutation{ | ||
user.Insert(ctx), | ||
} | ||
name: "insert or update user into the database", | ||
fields: fields{model: model}, | ||
mutType: InsertOrUpdate, | ||
want: []*user.User{ | ||
{ | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "update a non-existent user in the database", | ||
fields: fields{model: model}, | ||
mutType: Update, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "insert a new user into the database with hooks", | ||
fields: fields{model: model}, | ||
mutType: Insert, | ||
before: func(_ context.Context, m *Model[*user.User], _ *spanner.ReadWriteTransaction) error { | ||
// type assertion to access the embedded model | ||
u, ok := m.Yo.(*user.User) | ||
if !ok { | ||
return errors.New("unable to type assert to *user.User") | ||
} | ||
|
||
u.Name = "Jane Doe" | ||
return nil | ||
}, | ||
after: func(ctx context.Context, m *Model[*user.User], rwt *spanner.ReadWriteTransaction) error { | ||
// type assertion to access the embedded model | ||
u, ok := m.Yo.(*user.User) | ||
if !ok { | ||
return errors.New("unable to type assert to *user.User") | ||
} | ||
u.Email = "[email protected]" | ||
|
||
return rwt.BufferWrite([]*spanner.Mutation{ | ||
m.Update(ctx), | ||
}) | ||
}, | ||
want: []*user.User{ | ||
{ | ||
Name: "Jane Doe", | ||
Email: "[email protected]", | ||
}, | ||
{ | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
}, | ||
|
@@ -121,11 +149,15 @@ func TestModel_ApplyInsert(t *testing.T) { | |
WithSpannerClientOption[*user.User](*spannerClient), | ||
) | ||
|
||
for _, h := range tt.hooks { | ||
m.On(Insert, h) | ||
before, after := tt.mutType.Hooks() | ||
if tt.before != nil { | ||
m.On(before, tt.before) | ||
} | ||
if tt.after != nil { | ||
m.On(after, tt.after) | ||
} | ||
|
||
if _, err := m.ApplyInsert(ctx); (err != nil) != tt.wantErr { | ||
if _, err := m.Apply(ctx, tt.mutType); (err != nil) != tt.wantErr { | ||
t.Errorf("Model.ApplyInsert() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
|