xorm/executor/executor_test.go

49 lines
1.1 KiB
Go
Raw Normal View History

2023-10-28 03:58:27 +00:00
// Copyright 2023 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2023-10-28 09:45:30 +00:00
package executor
2023-10-28 03:58:27 +00:00
import (
"context"
"testing"
2023-10-30 05:40:19 +00:00
"xorm.io/builder"
2023-10-28 03:58:27 +00:00
"xorm.io/xorm/v2"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
2023-10-28 09:45:30 +00:00
func TestExecutor(t *testing.T) {
2023-10-28 03:58:27 +00:00
type User struct {
Id int64
Name string
}
engine, err := xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
assert.NoError(t, err)
assert.NoError(t, engine.Sync(new(User)))
// create querier
2023-10-30 05:40:19 +00:00
executor := New[User](engine)
2023-10-28 03:58:27 +00:00
2023-10-30 05:40:19 +00:00
err = executor.InsertOne(context.Background(), &User{
Name: "test",
})
assert.NoError(t, err)
user, err := executor.Get(context.Background())
assert.NoError(t, err)
assert.Equal(t, user.Name, "test")
assert.Equal(t, user.Id, int64(1))
users, err := executor.All(context.Background())
assert.NoError(t, err)
assert.Equal(t, len(users), 1)
users, err = executor.Where(builder.Eq{"id": 1}).All(context.Background())
assert.NoError(t, err)
assert.Equal(t, len(users), 1)
2023-10-28 03:58:27 +00:00
}