-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseDbModel_test.go
More file actions
127 lines (101 loc) · 2.28 KB
/
baseDbModel_test.go
File metadata and controls
127 lines (101 loc) · 2.28 KB
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
package go_orm
import (
"testing"
"fmt"
)
func TestBaseDbModelInit(t *testing.T) {
var v BaseDbModel
v = BaseDbModel{}
v.GetCache()
if !v.isInit {
t.Error("Base Model should be inited before GetCache")
}
}
func TestBaseDbModelGet(t *testing.T) {
var v BaseDbModel
v = BaseDbModel{}
res := v.FindInCache(1)
if !v.isInit {
t.Error("Base Model should be inited before FindInCache")
}
if res != nil {
t.Error("Not empty result")
}
v = BaseDbModel{}
v.AddToCache(testPotok{true, 1, 2})
if !v.isInit {
t.Error("Base Model should be inited before GetCache")
}
res = v.FindInCache(1)
if res == nil {
t.Error("Empty result after put")
}
}
func TestBaseDbModelGetInactive(t *testing.T) {
var v BaseDbModel
v = BaseDbModel{}
v.AddToCache(testPotok{false, 1, 2})
res := v.FindInCache(1)
if res != nil {
t.Error("Found inactive result")
}
if v.Len() != 1 {
t.Error("Error result count")
}
}
func TestBaseDbModelClear(t *testing.T) {
var v BaseDbModel
v = BaseDbModel{}
v.AddToCache(testPotok{true, 1, 2})
v.ClearCache()
res := v.FindInCache(1)
if res != nil {
t.Error("Not empty result after clear")
}
}
func TestAltIndex(t *testing.T) {
var v BaseDbModel
v = BaseDbModel{}
v.RegisterIntIndex("test", AltIndexTest)
v.RegisterStringIndex("testString", AltStringTest)
v.AddToCache(testPotok{true, 1, 2})
res := v.FindIndex("test", 2, true)
if res == nil {
t.Error("Empty result after put")
}
v.AddToCache(testPotok{true, 1, 3})
res = v.FindIndex("test", 2, true)
if res != nil {
t.Error("Not empty result after put another alter index")
}
res = v.FindIndex("test", 3, true)
if res == nil {
t.Error("Empty result after one more")
}
res = v.FindIndex("testString", "3", true)
if res == nil {
t.Error("Empty result in string alter index")
}
}
func TestPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
v := BaseDbModel{}
v.FindIndex("test", true, true)
}
func AltIndexTest(p PotokOrm) int {
return p.(testPotok).altId
}
func AltStringTest(p PotokOrm) string {
return fmt.Sprintf("%d", p.(testPotok).altId)
}
type testPotok struct {
active bool
id int
altId int
}
func (m testPotok) IsActive() bool { return m.active }
func (m testPotok) GetId() int { return m.id }