From 06a7e37d0e8c23594193e1c065edc5d444e2a88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Mon, 14 Sep 2020 15:13:54 -0300 Subject: [PATCH] Update Delete function to receive ids --- postgres.go | 11 ++++++----- postgres_test.go | 30 ++++++++++++++++++------------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/postgres.go b/postgres.go index 33d2423..7e0a443 100644 --- a/postgres.go +++ b/postgres.go @@ -70,10 +70,10 @@ func (c Client) Insert( // Delete deletes one or more instances from the database by id func (c Client) Delete( ctx context.Context, - entities ...interface{}, + ids ...interface{}, ) error { - for _, entity := range entities { - r := c.db.Table(c.tableName).Delete(entity) + for _, id := range ids { + r := c.db.Table(c.tableName).Delete(id) if r.Error != nil { return r.Error } @@ -111,7 +111,7 @@ func (c Client) Update( var tagNamesCache = map[reflect.Type]map[int]string{} // structToMap converts any type to a map based on the -// tag named `sql`, i.e. `sql:"map_key_name"` +// tag named `gorm`, i.e. `gorm:"map_key_name"` // // This function is efficient in the fact that it caches // the slower steps of the reflection required to do perform @@ -145,6 +145,7 @@ func structToMap(obj interface{}) (map[string]interface{}, error) { field = field.Elem() } + m[names[i]] = field.Interface() } @@ -159,7 +160,7 @@ func structToMap(obj interface{}) (map[string]interface{}, error) { func getTagNames(t reflect.Type) map[int]string { resp := map[int]string{} for i := 0; i < t.NumField(); i++ { - name := t.Field(i).Tag.Get("sql") + name := t.Field(i).Tag.Get("gorm") if name == "" { continue } diff --git a/postgres_test.go b/postgres_test.go index 6957d00..85f258a 100644 --- a/postgres_test.go +++ b/postgres_test.go @@ -11,9 +11,9 @@ import ( ) type User struct { - ID uint - Name string - CreatedAt time.Time + ID uint `gorm:"id"` + Name string `gorm:"name"` + CreatedAt time.Time `gorm:"created_at"` } func TestFind(t *testing.T) { @@ -155,7 +155,7 @@ func TestDelete(t *testing.T) { t.Fatal("could not create test table!") } - t.Run("should ignore empty lists of users", func(t *testing.T) { + t.Run("should ignore empty lists of ids", func(t *testing.T) { db := connectDB(t) defer db.Close() @@ -169,7 +169,7 @@ func TestDelete(t *testing.T) { assert.Equal(t, err, nil) }) - t.Run("should delete one user correctly", func(t *testing.T) { + t.Run("should delete one id correctly", func(t *testing.T) { db := connectDB(t) defer db.Close() @@ -186,12 +186,18 @@ func TestDelete(t *testing.T) { err := c.Insert(ctx, &u) assert.Equal(t, err, nil) - err = c.Delete(ctx, &u) - assert.Equal(t, err, nil) - + assert.NotEqual(t, 0, u.ID) result := User{} it := c.db.Raw("SELECT * FROM users WHERE id=?", u.ID) it.Scan(&result) + assert.Equal(t, u.ID, result.ID) + + err = c.Delete(ctx, u.ID) + assert.Equal(t, err, nil) + + result = User{} + it = c.db.Raw("SELECT * FROM users WHERE id=?", u.ID) + it.Scan(&result) assert.Equal(t, it.Error, nil) assert.Equal(t, uint(0), result.ID) @@ -201,8 +207,8 @@ func TestDelete(t *testing.T) { func TestStructToMap(t *testing.T) { type S1 struct { - Name string `sql:"name_attr"` - Age int `sql:"age_attr"` + Name string `gorm:"name_attr"` + Age int `gorm:"age_attr"` } t.Run("should convert plain structs to maps", func(t *testing.T) { m, err := structToMap(S1{ @@ -231,8 +237,8 @@ func TestStructToMap(t *testing.T) { }) type S2 struct { - Name *string `sql:"name"` - Age *int `sql:"age"` + Name *string `gorm:"name"` + Age *int `gorm:"age"` } t.Run("should not ignore not nil pointers", func(t *testing.T) {