forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_test.go
134 lines (108 loc) · 2.27 KB
/
base_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package models_test
import (
"testing"
"github.com/pocketbase/pocketbase/models"
)
func TestBaseModelHasId(t *testing.T) {
t.Parallel()
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)
}
}
}
func TestBaseModelId(t *testing.T) {
t.Parallel()
m := models.BaseModel{}
if m.GetId() != "" {
t.Fatalf("Expected empty id value, got %v", m.GetId())
}
m.SetId("test")
if m.GetId() != "test" {
t.Fatalf("Expected %q id, got %v", "test", m.GetId())
}
m.RefreshId()
if len(m.GetId()) != 15 {
t.Fatalf("Expected 15 chars id, got %v", m.GetId())
}
}
func TestBaseModelIsNew(t *testing.T) {
t.Parallel()
m0 := models.BaseModel{}
m1 := models.BaseModel{Id: ""}
m2 := models.BaseModel{Id: "test"}
m3 := models.BaseModel{}
m3.MarkAsNotNew()
m4 := models.BaseModel{Id: "test"}
m4.MarkAsNotNew()
m5 := models.BaseModel{Id: "test"}
m5.MarkAsNew()
m5.MarkAsNotNew()
m6 := models.BaseModel{}
m6.RefreshId()
m7 := models.BaseModel{}
m7.MarkAsNotNew()
m7.RefreshId()
m8 := models.BaseModel{}
m8.PostScan()
scenarios := []struct {
model models.BaseModel
expected bool
}{
{m0, true},
{m1, true},
{m2, true},
{m3, false},
{m4, false},
{m5, false},
{m6, true},
{m7, false},
{m8, false},
}
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)
}
}
}
func TestBaseModelCreated(t *testing.T) {
t.Parallel()
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()
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())
}
}