mirror of https://github.com/VinGarcia/ksql.git
Remove GetByID function because it was redundant
parent
1a08c61198
commit
54fa1f023a
15
kiss_orm.go
15
kiss_orm.go
|
@ -12,7 +12,6 @@ import (
|
||||||
// ORMProvider describes the public behavior of this ORM
|
// ORMProvider describes the public behavior of this ORM
|
||||||
type ORMProvider interface {
|
type ORMProvider interface {
|
||||||
Find(ctx context.Context, item interface{}, query string, params ...interface{}) error
|
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
|
Insert(ctx context.Context, items ...interface{}) error
|
||||||
Delete(ctx context.Context, ids ...interface{}) error
|
Delete(ctx context.Context, ids ...interface{}) error
|
||||||
Update(ctx context.Context, items ...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)
|
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
|
// Insert one or more instances on the database
|
||||||
//
|
//
|
||||||
// If the original instances have been passed by reference
|
// If the original instances have been passed by reference
|
||||||
|
|
|
@ -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) {
|
func TestInsert(t *testing.T) {
|
||||||
err := createTable()
|
err := createTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -233,7 +189,9 @@ func TestUpdate(t *testing.T) {
|
||||||
assert.Equal(t, err, nil)
|
assert.Equal(t, err, nil)
|
||||||
|
|
||||||
result := User{}
|
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, err, nil)
|
||||||
|
|
||||||
assert.Equal(t, "Thay", result.Name)
|
assert.Equal(t, "Thay", result.Name)
|
||||||
|
|
Loading…
Reference in New Issue