1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-24 17:07:00 +02:00
pocketbase/daos/table_test.go

141 lines
3.5 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package daos_test
import (
"context"
"database/sql"
2023-02-18 19:33:42 +02:00
"encoding/json"
2022-07-06 23:19:05 +02:00
"testing"
"time"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/tests"
"github.com/pocketbase/pocketbase/tools/list"
)
func TestHasTable(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
tableName string
expected bool
}{
{"", false},
{"test", false},
{"_admins", true},
{"demo3", true},
{"DEMO3", true}, // table names are case insensitives by default
}
for i, scenario := range scenarios {
result := app.Dao().HasTable(scenario.tableName)
if result != scenario.expected {
2023-02-18 19:33:42 +02:00
t.Errorf("[%d] Expected %v, got %v", i, scenario.expected, result)
2022-07-06 23:19:05 +02:00
}
}
}
func TestGetTableColumns(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
tableName string
expected []string
}{
{"", nil},
{"_params", []string{"id", "key", "value", "created", "updated"}},
}
2023-02-18 19:33:42 +02:00
for i, s := range scenarios {
columns, _ := app.Dao().GetTableColumns(s.tableName)
2022-07-06 23:19:05 +02:00
2023-02-18 19:33:42 +02:00
if len(columns) != len(s.expected) {
t.Errorf("[%d] Expected columns %v, got %v", i, s.expected, columns)
continue
2022-07-06 23:19:05 +02:00
}
for _, c := range columns {
2023-02-18 19:33:42 +02:00
if !list.ExistInSlice(c, s.expected) {
t.Errorf("[%d] Didn't expect column %s", i, c)
2022-07-06 23:19:05 +02:00
}
}
}
}
2023-02-18 19:33:42 +02:00
func TestGetTableInfo(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
tableName string
expected string
}{
{"", "[]"},
{"missing", "[]"},
{
"_admins",
`[{"PK":1,"Index":0,"Name":"id","Type":"TEXT","NotNull":false,"DefaultValue":null},{"PK":0,"Index":1,"Name":"avatar","Type":"INTEGER","NotNull":true,"DefaultValue":0},{"PK":0,"Index":2,"Name":"email","Type":"TEXT","NotNull":true,"DefaultValue":null},{"PK":0,"Index":3,"Name":"tokenKey","Type":"TEXT","NotNull":true,"DefaultValue":null},{"PK":0,"Index":4,"Name":"passwordHash","Type":"TEXT","NotNull":true,"DefaultValue":null},{"PK":0,"Index":5,"Name":"lastResetSentAt","Type":"TEXT","NotNull":true,"DefaultValue":""},{"PK":0,"Index":6,"Name":"created","Type":"TEXT","NotNull":true,"DefaultValue":""},{"PK":0,"Index":7,"Name":"updated","Type":"TEXT","NotNull":true,"DefaultValue":""}]`,
},
}
for i, s := range scenarios {
rows, _ := app.Dao().GetTableInfo(s.tableName)
raw, _ := json.Marshal(rows)
rawStr := string(raw)
if rawStr != s.expected {
t.Errorf("[%d] Expected \n%v, \ngot \n%v", i, s.expected, rawStr)
}
}
}
2022-07-06 23:19:05 +02:00
func TestDeleteTable(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
scenarios := []struct {
tableName string
expectError bool
}{
{"", true},
2023-02-18 19:33:42 +02:00
{"test", false}, // missing tables are ignored
2022-07-06 23:19:05 +02:00
{"_admins", false},
{"demo3", false},
}
2023-02-18 19:33:42 +02:00
for i, s := range scenarios {
err := app.Dao().DeleteTable(s.tableName)
2022-07-06 23:19:05 +02:00
hasErr := err != nil
2023-02-18 19:33:42 +02:00
if hasErr != s.expectError {
t.Errorf("[%d] Expected hasErr %v, got %v", i, s.expectError, hasErr)
2022-07-06 23:19:05 +02:00
}
}
}
func TestVacuum(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
calledQueries := []string{}
app.DB().QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) {
calledQueries = append(calledQueries, sql)
}
app.DB().ExecLogFunc = func(ctx context.Context, t time.Duration, sql string, result sql.Result, err error) {
calledQueries = append(calledQueries, sql)
}
if err := app.Dao().Vacuum(); err != nil {
t.Fatal(err)
}
if total := len(calledQueries); total != 1 {
t.Fatalf("Expected 1 query, got %d", total)
}
if calledQueries[0] != "VACUUM" {
t.Fatalf("Expected VACUUM query, got %s", calledQueries[0])
}
}