Remove GetByID function because it was redundant

pull/2/head
Vinícius Garcia 2020-09-30 19:41:54 -03:00
parent 1a08c61198
commit 54fa1f023a
2 changed files with 3 additions and 60 deletions

View File

@ -12,7 +12,6 @@ import (
// ORMProvider describes the public behavior of this ORM
type ORMProvider interface {
Find(ctx context.Context, item interface{}, query string, params ...interface{}) error
GetByID(ctx context.Context, item interface{}, id interface{}) error
Insert(ctx context.Context, items ...interface{}) error
Delete(ctx context.Context, ids ...interface{}) error
Update(ctx context.Context, items ...interface{}) error
@ -138,20 +137,6 @@ func (c Client) QueryNext(
return false, c.db.ScanRows(it.rows, item)
}
// GetByID recovers a single entity from the database by the ID field.
func (c Client) GetByID(
ctx context.Context,
item interface{},
id interface{},
) error {
it := c.db.Raw(fmt.Sprintf("select * from %s where id = ?", c.tableName), id)
if it.Error != nil {
return it.Error
}
it = it.Scan(item)
return it.Error
}
// Insert one or more instances on the database
//
// If the original instances have been passed by reference

View File

@ -59,50 +59,6 @@ func TestFind(t *testing.T) {
})
}
func TestGetByID(t *testing.T) {
err := createTable()
if err != nil {
t.Fatal("could not create test table!")
}
t.Run("should return 0 results correctly", func(t *testing.T) {
db := connectDB(t)
defer db.Close()
ctx := context.Background()
c := Client{
db: db,
tableName: "users",
}
u := User{}
err := c.GetByID(ctx, &u, 999)
assert.NotEqual(t, nil, err)
assert.Equal(t, User{}, u)
})
t.Run("should return a user correctly", func(t *testing.T) {
db := connectDB(t)
defer db.Close()
bia := &User{
Name: "Bia",
}
db.Create(&bia)
ctx := context.Background()
c := Client{
db: db,
tableName: "users",
}
result := User{}
err = c.GetByID(ctx, &result, bia.ID)
assert.Equal(t, err, nil)
assert.Equal(t, "Bia", result.Name)
assert.Equal(t, bia.ID, result.ID)
})
}
func TestInsert(t *testing.T) {
err := createTable()
if err != nil {
@ -233,7 +189,9 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, err, nil)
result := User{}
err = c.GetByID(ctx, &result, u.ID)
it := c.db.Raw("SELECT * FROM users WHERE id=?", u.ID)
it.Scan(&result)
it.Close()
assert.Equal(t, err, nil)
assert.Equal(t, "Thay", result.Name)