forked from AirspaceTechnologies/go-psql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_test.go
144 lines (126 loc) · 3.31 KB
/
model_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
package psql
import (
"testing"
"time"
)
const modelsTable = `
create table mock_models (
id bigserial primary key,
string_field text,
null_string_field text,
int_field integer,
float_field numeric,
bool_field boolean,
time_field timestamp,
json_object jsonb,
"table" text,
encryptable text,
created_at timestamp default CURRENT_TIMESTAMP not null
)
`
type MockModel struct {
ID int `sql:"id"`
NonSQLField string
StringField string `sql:"string_field"`
NullStringField NullString `sql:"null_string_field"`
IntField int `sql:"int_field"`
FloatField float64 `sql:"float_field"`
BoolField bool `sql:"bool_field"`
JSONObject JSONObject `sql:"json_object"`
TimeField time.Time `sql:"time_field"`
Table string `sql:"table"` // making sure reserved words work
Encryptable EncryptableString `sql:"encryptable"`
CreatedAt time.Time `sql:"created_at"`
}
func (m MockModel) TableName() string {
return "mock_models"
}
type MockModelNullableID struct {
ID NullInt64 `sql:"id"`
NonSQLField string
StringField string `sql:"string_field"`
NullStringField NullString `sql:"null_string_field"`
IntField int `sql:"int_field"`
FloatField float64 `sql:"float_field"`
BoolField bool `sql:"bool_field"`
TimeField time.Time `sql:"time_field"`
Table string `sql:"table"`
JSONObject JSONObject `sql:"json_object"`
}
func (m MockModelNullableID) TableName() string {
return "mock_models"
}
type ExtendedNullInt64 struct {
NullInt64
}
type MockModelExtendedID struct {
ID ExtendedNullInt64 `sql:"id"`
NonSQLField string
StringField string `sql:"string_field"`
NullStringField NullString `sql:"null_string_field"`
IntField int `sql:"int_field"`
FloatField float64 `sql:"float_field"`
BoolField bool `sql:"bool_field"`
TimeField time.Time `sql:"time_field"`
Table string `sql:"table"`
JSONObject JSONObject `sql:"json_object"`
}
func (m MockModelExtendedID) TableName() string {
return "mock_models"
}
func TestModelHelper_SetId(t *testing.T) {
// int id
m := &MockModel{}
mh := &ModelHelper{Model: m}
if err := mh.SetID(5); err != nil || m.ID != 5 {
t.Fatalf("error setting id %v", err)
}
// NullableInt64 id
mn := &MockModelNullableID{}
mh = &ModelHelper{Model: mn}
if err := mh.SetID(5); err != nil || !mn.ID.Valid || mn.ID.Int64 != 5 {
t.Fatalf("error setting id %v", err)
}
// Non pointer
mh = &ModelHelper{Model: *mn}
if err := mh.SetID(5); err == nil {
t.Fatalf("expected error")
}
}
func TestModelHelper_Id(t *testing.T) {
tcs := []struct {
Model Model
ExpectedID int64
}{
{
Model: MockModel{
ID: 10,
},
ExpectedID: 10,
},
{
Model: &MockModel{
ID: 10,
},
ExpectedID: 10,
},
{
Model: MockModelNullableID{
ID: NewNullInt64(10),
},
ExpectedID: 10,
},
{
Model: MockModelExtendedID{
ID: ExtendedNullInt64{NewNullInt64(10)},
},
ExpectedID: 10,
},
}
for i, tc := range tcs {
mh := &ModelHelper{Model: tc.Model}
if id, err := mh.ID(); err != nil || id != tc.ExpectedID {
t.Fatalf("tc %d error getting id %v", i, err)
}
}
}