-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel_interface_test.go
166 lines (144 loc) · 3.79 KB
/
model_interface_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package lore
import "testing"
const (
_TEST_DB_TABLENAME string = "tests"
_TEST_DB_FIELDNAME_ID string = "id"
_TEST_DB_FIELDNAME_FIELD string = "field"
_TEST_MODEL_ID int = 1
_TEST_MODEL_FIELD int64 = 2
)
type testModel struct {
Id int `db:"id"`
Field int64 `db:"field"`
}
type testModelInvalid struct {
Id int `db:"id"`
Field int64 `db:"field"`
}
/*
Enforce interface.
*/
var _ ModelInterface = (*testModel)(nil)
var _ ModelInterface = (*testModelInvalid)(nil)
/*
DbTableName implementation for testModel ModelInterface.
*/
func (*testModel) DbTableName() string {
return _TEST_DB_TABLENAME
}
/*
DbTableName implementation for testModelInvalid ModelInterface.
*/
func (*testModelInvalid) DbTableName() string {
return ""
}
/*
DbFieldMap implementation for testModel ModelInterface.
*/
func (tm *testModel) DbFieldMap() map[string]interface{} {
return map[string]interface{}{
_TEST_DB_FIELDNAME_FIELD: tm.Field,
}
}
/*
DbFieldMap implementation for testModelInvalid ModelInterface.
*/
func (tm *testModelInvalid) DbFieldMap() map[string]interface{} {
return nil
}
/*
DbPrimaryFieldKey implementation for testModel ModelInterface.
*/
func (*testModel) DbPrimaryFieldKey() string {
return _TEST_DB_FIELDNAME_ID
}
/*
DbPrimaryFieldKey implementation for testModelInvalid ModelInterface.
*/
func (*testModelInvalid) DbPrimaryFieldKey() string {
return ""
}
/*
DbPrimaryFieldValue implementation for testModel ModelInterface.
*/
func (tm *testModel) DbPrimaryFieldValue() interface{} {
return _TEST_MODEL_ID
}
/*
DbPrimaryFieldValue implementation for testModelInvalid ModelInterface.
*/
func (tm *testModelInvalid) DbPrimaryFieldValue() interface{} {
return nil
}
/*
createTestModelInstance creates a new testModel instance with valid values.
*/
func createTestModelInstance() *testModel {
return &testModel{
Id: _TEST_MODEL_ID,
Field: _TEST_MODEL_FIELD,
}
}
/*
createTestModelInvalidInstance creates a new testModelInvalid instance. This should NOT block
compilation, but SHOULD allow us to check invalid conditions at test time.
*/
func createTestModelInvalidInstance() *testModelInvalid {
return &testModelInvalid{
Id: 0,
Field: -1,
}
}
/*
TestModelInterfaceInstance tests the ModelInterface interface by creating a new model instance. This
is a trivial test.
*/
func TestModelInterfaceInstance(t *testing.T) {
tm := createTestModelInstance()
var mi ModelInterface
mi = tm
// Test table name.
dbTableName := mi.DbTableName()
if dbTableName != _TEST_DB_TABLENAME {
t.Errorf("Invalid DbTableName: %s, expected %s", dbTableName, _TEST_DB_TABLENAME)
return
}
// Test primary key/value.
primaryFieldKey := mi.DbPrimaryFieldKey()
primaryFieldValue := mi.DbPrimaryFieldValue()
if primaryFieldKey != _TEST_DB_FIELDNAME_ID || primaryFieldValue != _TEST_MODEL_ID {
t.Errorf("Invalid primary key/value combination: (%s, %+v), expected (%s, %+v)", primaryFieldKey, primaryFieldValue, _TEST_DB_FIELDNAME_ID, _TEST_MODEL_ID)
return
}
// Test field map.
dbFieldMap := mi.DbFieldMap()
if len(dbFieldMap) != 1 || dbFieldMap[_TEST_DB_FIELDNAME_FIELD] != tm.Field {
t.Errorf("Invalid DbFieldMap: %+v", dbFieldMap)
return
}
}
/*
TestInvalidModelInterfaceInstance tests the ModelInterface interface against an invalid model
instance.
*/
func TestInvalidModelInterfaceInstance(t *testing.T) {
tm := createTestModelInvalidInstance()
var mi ModelInterface
mi = tm
if tm == nil || mi == nil {
t.Errorf("Expected non-nil ModelInterface instance, even if invalid")
return
}
}
/*
newTestModelEmpty returns a pointer to a new, empty instance of testModel.
*/
func newTestModelEmpty() *testModel {
return &testModel{}
}
/*
newTestModelEmptyList returns a pointer to a new, empty list for testModels.
*/
func newTestModelEmptyList() *[]testModel {
return &[]testModel{}
}