1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-28 18:11:17 +02:00
pocketbase/daos/log_test.go

159 lines
3.7 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package daos_test
import (
"encoding/json"
"testing"
"time"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tests"
"github.com/pocketbase/pocketbase/tools/types"
)
2023-11-26 13:33:17 +02:00
func TestLogQuery(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
app, _ := tests.NewTestApp()
defer app.Cleanup()
2023-11-26 13:33:17 +02:00
expected := "SELECT {{_logs}}.* FROM `_logs`"
2022-07-06 23:19:05 +02:00
2023-11-26 13:33:17 +02:00
sql := app.Dao().LogQuery().Build().SQL()
2022-07-06 23:19:05 +02:00
if sql != expected {
t.Errorf("Expected sql %s, got %s", expected, sql)
}
}
2023-11-26 13:33:17 +02:00
func TestFindLogById(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
app, _ := tests.NewTestApp()
defer app.Cleanup()
2023-11-26 13:33:17 +02:00
tests.MockLogsData(app)
2022-07-06 23:19:05 +02:00
scenarios := []struct {
id string
expectError bool
}{
{"", true},
{"invalid", true},
{"00000000-9f38-44fb-bf82-c8f53b310d91", true},
{"873f2133-9f38-44fb-bf82-c8f53b310d91", false},
}
for i, scenario := range scenarios {
2023-11-26 13:33:17 +02:00
admin, err := app.LogsDao().FindLogById(scenario.id)
2022-07-06 23:19:05 +02:00
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err)
}
if admin != nil && admin.Id != scenario.id {
t.Errorf("(%d) Expected admin with id %s, got %s", i, scenario.id, admin.Id)
}
}
}
2023-11-26 13:33:17 +02:00
func TestLogsStats(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
app, _ := tests.NewTestApp()
defer app.Cleanup()
2023-11-26 13:33:17 +02:00
tests.MockLogsData(app)
2022-07-06 23:19:05 +02:00
2022-10-30 10:28:14 +02:00
expected := `[{"total":1,"date":"2022-05-01 10:00:00.000Z"},{"total":1,"date":"2022-05-02 10:00:00.000Z"}]`
2022-07-06 23:19:05 +02:00
now := time.Now().UTC().Format(types.DefaultDateLayout)
exp := dbx.NewExp("[[created]] <= {:date}", dbx.Params{"date": now})
2023-11-26 13:33:17 +02:00
result, err := app.LogsDao().LogsStats(exp)
2022-07-06 23:19:05 +02:00
if err != nil {
t.Fatal(err)
}
encoded, _ := json.Marshal(result)
if string(encoded) != expected {
t.Fatalf("Expected %s, got %s", expected, string(encoded))
}
}
2023-11-26 13:33:17 +02:00
func TestDeleteOldLogs(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
app, _ := tests.NewTestApp()
defer app.Cleanup()
2023-11-26 13:33:17 +02:00
tests.MockLogsData(app)
2022-07-06 23:19:05 +02:00
scenarios := []struct {
date string
expectedTotal int
}{
2023-11-26 13:33:17 +02:00
{"2022-01-01 10:00:00.000Z", 2}, // no logs to delete before that time
{"2022-05-01 11:00:00.000Z", 1}, // only 1 log should have left
{"2022-05-03 11:00:00.000Z", 0}, // no more logs should have left
{"2022-05-04 11:00:00.000Z", 0}, // no more logs should have left
2022-07-06 23:19:05 +02:00
}
for i, scenario := range scenarios {
date, dateErr := time.Parse(types.DefaultDateLayout, scenario.date)
if dateErr != nil {
t.Errorf("(%d) Date error %v", i, dateErr)
}
2023-11-26 13:33:17 +02:00
deleteErr := app.LogsDao().DeleteOldLogs(date)
2022-07-06 23:19:05 +02:00
if deleteErr != nil {
t.Errorf("(%d) Delete error %v", i, deleteErr)
}
2023-11-26 13:33:17 +02:00
// check total remaining logs
2022-07-06 23:19:05 +02:00
var total int
2023-11-26 13:33:17 +02:00
countErr := app.LogsDao().LogQuery().Select("count(*)").Row(&total)
2022-07-06 23:19:05 +02:00
if countErr != nil {
t.Errorf("(%d) Count error %v", i, countErr)
}
if total != scenario.expectedTotal {
2023-11-26 13:33:17 +02:00
t.Errorf("(%d) Expected %d remaining logs, got %d", i, scenario.expectedTotal, total)
2022-07-06 23:19:05 +02:00
}
}
}
2023-11-26 13:33:17 +02:00
func TestSaveLog(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
app, _ := tests.NewTestApp()
defer app.Cleanup()
2023-11-26 13:33:17 +02:00
tests.MockLogsData(app)
2022-07-06 23:19:05 +02:00
2023-11-26 13:33:17 +02:00
// create new log
newLog := &models.Log{}
newLog.Level = -4
newLog.Data = types.JsonMap{}
createErr := app.LogsDao().SaveLog(newLog)
2022-07-06 23:19:05 +02:00
if createErr != nil {
t.Fatal(createErr)
}
// check if it was really created
2023-11-26 13:33:17 +02:00
existingLog, fetchErr := app.LogsDao().FindLogById(newLog.Id)
2022-07-06 23:19:05 +02:00
if fetchErr != nil {
t.Fatal(fetchErr)
}
2023-11-26 13:33:17 +02:00
existingLog.Level = 4
updateErr := app.LogsDao().SaveLog(existingLog)
2022-07-06 23:19:05 +02:00
if updateErr != nil {
t.Fatal(updateErr)
}
// refresh instance to check if it was really updated
2023-11-26 13:33:17 +02:00
existingLog, _ = app.LogsDao().FindLogById(existingLog.Id)
if existingLog.Level != 4 {
t.Fatalf("Expected log level to be %d, got %d", 4, existingLog.Level)
2022-07-06 23:19:05 +02:00
}
}