1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-12-04 11:39:46 +02:00
pocketbase/models/base_test.go

135 lines
2.3 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package models_test
import (
"testing"
"github.com/pocketbase/pocketbase/models"
)
func TestBaseModelHasId(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
scenarios := []struct {
model models.BaseModel
expected bool
}{
{
models.BaseModel{},
false,
},
{
models.BaseModel{Id: ""},
false,
},
{
models.BaseModel{Id: "abc"},
true,
},
}
for i, s := range scenarios {
result := s.model.HasId()
if result != s.expected {
t.Errorf("(%d) Expected %v, got %v", i, s.expected, result)
}
}
}
2022-08-07 19:58:21 +02:00
func TestBaseModelId(t *testing.T) {
t.Parallel()
2022-08-07 19:58:21 +02:00
m := models.BaseModel{}
if m.GetId() != "" {
t.Fatalf("Expected empty id value, got %v", m.GetId())
2022-07-06 23:19:05 +02:00
}
2022-08-07 19:58:21 +02:00
m.SetId("test")
if m.GetId() != "test" {
t.Fatalf("Expected %q id, got %v", "test", m.GetId())
2022-07-06 23:19:05 +02:00
}
m.RefreshId()
2022-08-07 19:58:21 +02:00
if len(m.GetId()) != 15 {
t.Fatalf("Expected 15 chars id, got %v", m.GetId())
}
}
func TestBaseModelIsNew(t *testing.T) {
t.Parallel()
2022-08-07 19:58:21 +02:00
m0 := models.BaseModel{}
m1 := models.BaseModel{Id: ""}
m2 := models.BaseModel{Id: "test"}
m3 := models.BaseModel{}
m3.MarkAsNotNew()
2022-08-07 19:58:21 +02:00
m4 := models.BaseModel{Id: "test"}
m4.MarkAsNotNew()
2022-08-07 19:58:21 +02:00
m5 := models.BaseModel{Id: "test"}
m5.MarkAsNew()
m5.MarkAsNotNew()
2022-10-30 10:28:14 +02:00
m6 := models.BaseModel{}
m6.RefreshId()
m7 := models.BaseModel{}
m7.MarkAsNotNew()
m7.RefreshId()
m8 := models.BaseModel{}
m8.PostScan()
2022-08-07 19:58:21 +02:00
scenarios := []struct {
model models.BaseModel
expected bool
}{
{m0, true},
{m1, true},
{m2, true},
{m3, false},
{m4, false},
2022-08-07 19:58:21 +02:00
{m5, false},
2022-10-30 10:28:14 +02:00
{m6, true},
{m7, false},
{m8, false},
2022-08-07 19:58:21 +02:00
}
for i, s := range scenarios {
result := s.model.IsNew()
if result != s.expected {
t.Errorf("(%d) Expected IsNew %v, got %v", i, s.expected, result)
}
2022-07-06 23:19:05 +02:00
}
}
func TestBaseModelCreated(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
m := models.BaseModel{}
if !m.GetCreated().IsZero() {
t.Fatalf("Expected zero datetime, got %v", m.GetCreated())
}
m.RefreshCreated()
if m.GetCreated().IsZero() {
t.Fatalf("Expected non-zero datetime, got %v", m.GetCreated())
}
}
func TestBaseModelUpdated(t *testing.T) {
t.Parallel()
2022-07-06 23:19:05 +02:00
m := models.BaseModel{}
if !m.GetUpdated().IsZero() {
t.Fatalf("Expected zero datetime, got %v", m.GetUpdated())
}
m.RefreshUpdated()
if m.GetUpdated().IsZero() {
t.Fatalf("Expected non-zero datetime, got %v", m.GetUpdated())
}
}