Save method
This commit is contained in:
22
app/main.go
22
app/main.go
@ -121,6 +121,25 @@ func doGet(db *gorm.DB) {
|
|||||||
fmt.Printf(">> %+v %s (alive %t)\n", cert.Id, cert.CreatedAt, cert.Alive)
|
fmt.Printf(">> %+v %s (alive %t)\n", cert.Id, cert.CreatedAt, cert.Alive)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doSave(db *gorm.DB) {
|
||||||
|
repo := repository.RepoBase[models.Company]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
company := models.Company{
|
||||||
|
Name: "Test company",
|
||||||
|
Address: "Some address",
|
||||||
|
City: "Some city",
|
||||||
|
Email: "email@example.org",
|
||||||
|
Oib: "123456",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := repo.Save(&company)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("New company id: %s\n", company.Id.String())
|
||||||
|
}
|
||||||
|
|
||||||
func doExists(db *gorm.DB) {
|
func doExists(db *gorm.DB) {
|
||||||
repo := repository.RepoBase[models.Cert]{}
|
repo := repository.RepoBase[models.Cert]{}
|
||||||
repo.Init(db, nil)
|
repo.Init(db, nil)
|
||||||
@ -147,7 +166,8 @@ func main() {
|
|||||||
|
|
||||||
// doMagic(db)
|
// doMagic(db)
|
||||||
// doList(db)
|
// doList(db)
|
||||||
doCount(db)
|
// doCount(db)
|
||||||
|
doSave(db)
|
||||||
// doGet(db)
|
// doGet(db)
|
||||||
// doExists(db)
|
// doExists(db)
|
||||||
// inheritance.DoInheritanceTest()
|
// inheritance.DoInheritanceTest()
|
||||||
|
|||||||
@ -21,7 +21,7 @@ type Company struct {
|
|||||||
UpdatedAt time.Time `faker:"-"`
|
UpdatedAt time.Time `faker:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Company) TableName() string {
|
func (m Company) TableName() string {
|
||||||
return "companies"
|
return "companies"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,9 +33,9 @@ func NewMockDB() (*sql.DB, *gorm.DB, sqlmock.Sqlmock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MyModel struct {
|
type MyModel struct {
|
||||||
Id uuid.UUID
|
Id *uuid.UUID `gorm:"type(uuid);unique"`
|
||||||
Value string
|
Value string
|
||||||
Count int
|
Cnt int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MyModel) TableName() string {
|
func (m MyModel) TableName() string {
|
||||||
@ -46,7 +46,7 @@ type MyModelFilter struct {
|
|||||||
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
Id *uuid.UUID `filterfield:"field=id,operator=EQ"`
|
||||||
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
Ids *[]uuid.UUID `filterfield:"field=id,operator=IN"`
|
||||||
Value *string `filterfield:"field=value,operator=EQ"`
|
Value *string `filterfield:"field=value,operator=EQ"`
|
||||||
Count *int `filterfield:"field=count,operator=GT"`
|
Cnt *int `filterfield:"field=cnt,operator=GT"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListMethod(t *testing.T) {
|
func TestListMethod(t *testing.T) {
|
||||||
@ -65,13 +65,13 @@ func TestListMethod(t *testing.T) {
|
|||||||
Direction: OrderASC,
|
Direction: OrderASC,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Field: "count",
|
Field: "cnt",
|
||||||
Direction: OrderDESC,
|
Direction: OrderDESC,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT * FROM my_models ORDER BY id,count DESC"
|
sql := "SELECT * FROM my_models ORDER BY id,cnt DESC"
|
||||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
||||||
|
|
||||||
_, err := repo.List(filter, &options)
|
_, err := repo.List(filter, &options)
|
||||||
@ -198,10 +198,10 @@ func TestListMethod(t *testing.T) {
|
|||||||
filter := MyModelFilter{
|
filter := MyModelFilter{
|
||||||
Id: &id,
|
Id: &id,
|
||||||
Value: &value,
|
Value: &value,
|
||||||
Count: &count,
|
Cnt: &count,
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3"
|
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3"
|
||||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
WithArgs(id, value, count)
|
WithArgs(id, value, count)
|
||||||
|
|
||||||
@ -226,7 +226,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
filter := MyModelFilter{
|
filter := MyModelFilter{
|
||||||
Id: &id,
|
Id: &id,
|
||||||
Value: &value,
|
Value: &value,
|
||||||
Count: &count,
|
Cnt: &count,
|
||||||
}
|
}
|
||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
Pagination: &Pagination{
|
Pagination: &Pagination{
|
||||||
@ -235,7 +235,7 @@ func TestListMethod(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.count > $3 LIMIT $4 OFFSET $5"
|
sql := "SELECT * FROM my_models WHERE my_models.id = $1 AND my_models.value = $2 AND my_models.cnt > $3 LIMIT $4 OFFSET $5"
|
||||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
|
WithArgs(id, value, count, options.Pagination.Limit, options.Pagination.Offset)
|
||||||
|
|
||||||
@ -258,11 +258,11 @@ func TestListMethod(t *testing.T) {
|
|||||||
options := ListOptions{
|
options := ListOptions{
|
||||||
Only: &[]string{
|
Only: &[]string{
|
||||||
"id",
|
"id",
|
||||||
"count",
|
"cnt",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
sql := "SELECT id,count FROM my_models"
|
sql := "SELECT id,cnt FROM my_models"
|
||||||
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
mock.ExpectQuery(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql)))
|
||||||
|
|
||||||
_, err := repo.List(filter, &options)
|
_, err := repo.List(filter, &options)
|
||||||
|
|||||||
18
app/repository/method_save.go
Normal file
18
app/repository/method_save.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SaveMethod[T schema.Tabler] struct {
|
||||||
|
repo *RepoBase[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SaveMethod[T]) Init(repo *RepoBase[T]) {
|
||||||
|
m.repo = repo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m SaveMethod[T]) Save(model *T) (*T, error) {
|
||||||
|
result := m.repo.dbConn.Save(model)
|
||||||
|
return model, result.Error
|
||||||
|
}
|
||||||
69
app/repository/method_save_test.go
Normal file
69
app/repository/method_save_test.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/DATA-DOG/go-sqlmock"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSaveMethod(t *testing.T) {
|
||||||
|
t.Run("Save new model", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
model := MyModel{
|
||||||
|
Value: "some value",
|
||||||
|
Cnt: 123,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "INSERT INTO my_models (id,value,cnt) VALUES ($1,$2,$3)"
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(model.Id, model.Value, model.Cnt).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
_, err := repo.Save(&model)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Update existing model", func(t *testing.T) {
|
||||||
|
sqldb, db, mock := NewMockDB()
|
||||||
|
defer sqldb.Close()
|
||||||
|
|
||||||
|
repo := RepoBase[MyModel]{}
|
||||||
|
repo.Init(db, nil)
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
model := MyModel{
|
||||||
|
Id: &id,
|
||||||
|
Value: "some value",
|
||||||
|
Cnt: 123,
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "UPDATE my_models SET value=$1,cnt=$2 WHERE id = $3"
|
||||||
|
mock.ExpectBegin()
|
||||||
|
mock.ExpectExec(fmt.Sprintf("^%s$", regexp.QuoteMeta(sql))).
|
||||||
|
WithArgs(model.Value, model.Cnt, model.Id).
|
||||||
|
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||||
|
mock.ExpectCommit()
|
||||||
|
|
||||||
|
_, err := repo.Save(&model)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
if err := mock.ExpectationsWereMet(); err != nil {
|
||||||
|
t.Errorf("there were unfulfilled expectations: %s", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -21,11 +21,11 @@ type RepoBase[T schema.Tabler] struct {
|
|||||||
GetMethod[T]
|
GetMethod[T]
|
||||||
ExistsMethod[T]
|
ExistsMethod[T]
|
||||||
CountMethod[T]
|
CountMethod[T]
|
||||||
methods []MethodInitInterface[T]
|
SaveMethod[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *RepoBase[T]) InitMethods() {
|
func (repo *RepoBase[T]) InitMethods(methods []MethodInitInterface[T]) {
|
||||||
for _, method := range repo.methods {
|
for _, method := range methods {
|
||||||
method.Init(repo)
|
method.Init(repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,11 +42,12 @@ func (m *RepoBase[T]) Init(dbConn *gorm.DB, options *RepoOptions) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.methods = []MethodInitInterface[T]{
|
methods := []MethodInitInterface[T]{
|
||||||
&m.ListMethod,
|
&m.ListMethod,
|
||||||
&m.GetMethod,
|
&m.GetMethod,
|
||||||
&m.ExistsMethod,
|
&m.ExistsMethod,
|
||||||
&m.CountMethod,
|
&m.CountMethod,
|
||||||
|
&m.SaveMethod,
|
||||||
}
|
}
|
||||||
m.InitMethods()
|
m.InitMethods(methods)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user