Adding upstream version 0.28.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
88f1d47ab6
commit
e28c88ef14
933 changed files with 194711 additions and 0 deletions
113
core/db_test.go
Normal file
113
core/db_test.go
Normal file
|
@ -0,0 +1,113 @@
|
|||
package core_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
)
|
||||
|
||||
func TestGenerateDefaultRandomId(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
id1 := core.GenerateDefaultRandomId()
|
||||
id2 := core.GenerateDefaultRandomId()
|
||||
|
||||
if id1 == id2 {
|
||||
t.Fatalf("Expected id1 and id2 to differ, got %q", id1)
|
||||
}
|
||||
|
||||
if l := len(id1); l != 15 {
|
||||
t.Fatalf("Expected id1 length %d, got %d", 15, l)
|
||||
}
|
||||
|
||||
if l := len(id2); l != 15 {
|
||||
t.Fatalf("Expected id2 length %d, got %d", 15, l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
modelsQuery := app.ModelQuery(&core.Collection{})
|
||||
logsModelQuery := app.AuxModelQuery(&core.Collection{})
|
||||
|
||||
if app.ConcurrentDB() == modelsQuery.Info().Builder {
|
||||
t.Fatalf("ModelQuery() is not using app.ConcurrentDB()")
|
||||
}
|
||||
|
||||
if app.AuxConcurrentDB() == logsModelQuery.Info().Builder {
|
||||
t.Fatalf("AuxModelQuery() is not using app.AuxConcurrentDB()")
|
||||
}
|
||||
|
||||
expectedSQL := "SELECT {{_collections}}.* FROM `_collections`"
|
||||
for i, q := range []*dbx.SelectQuery{modelsQuery, logsModelQuery} {
|
||||
sql := q.Build().SQL()
|
||||
if sql != expectedSQL {
|
||||
t.Fatalf("[%d] Expected select\n%s\ngot\n%s", i, expectedSQL, sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
u := &mockSuperusers{}
|
||||
|
||||
testErr := errors.New("test")
|
||||
|
||||
app.OnModelValidate().BindFunc(func(e *core.ModelEvent) error {
|
||||
return testErr
|
||||
})
|
||||
|
||||
err := app.Validate(u)
|
||||
if err != testErr {
|
||||
t.Fatalf("Expected error %v, got %v", testErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateWithContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app, _ := tests.NewTestApp()
|
||||
defer app.Cleanup()
|
||||
|
||||
u := &mockSuperusers{}
|
||||
|
||||
testErr := errors.New("test")
|
||||
|
||||
app.OnModelValidate().BindFunc(func(e *core.ModelEvent) error {
|
||||
if v := e.Context.Value("test"); v != 123 {
|
||||
t.Fatalf("Expected 'test' context value %#v, got %#v", 123, v)
|
||||
}
|
||||
return testErr
|
||||
})
|
||||
|
||||
//nolint:staticcheck
|
||||
ctx := context.WithValue(context.Background(), "test", 123)
|
||||
|
||||
err := app.ValidateWithContext(ctx, u)
|
||||
if err != testErr {
|
||||
t.Fatalf("Expected error %v, got %v", testErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
type mockSuperusers struct {
|
||||
core.BaseModel
|
||||
Email string `db:"email"`
|
||||
}
|
||||
|
||||
func (m *mockSuperusers) TableName() string {
|
||||
return core.CollectionNameSuperusers
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue