This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_test.go
130 lines (101 loc) · 3.41 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
package gondolier
import (
"database/sql"
"testing"
"time"
)
type testModel struct {
Id uint64 `gondolier:"type:integer;primarykey;notnull;;"` // accept multiple ;
Name string `gondolier:"type:varchar;unique"`
Age int `gondolier:"type:integer;notnull"`
Array []int `gondolier:"type:integer[]"`
Date time.Time `gondolier:"type:timestamp"`
NullableField sql.NullString `gondolier:"type:text"`
}
type testModelWhitespace struct {
Id uint64 ` gondolier:" type: bigint ;;; pk;;; notnull ; ; " `
Name string `gondolier:" type : character varying(100) ;; " `
}
func TestBuildMetaModel(t *testing.T) {
meta := buildMetaModel(&testModel{})
if meta.ModelName != "testModel" {
t.Fatal("Name must be testModel")
}
}
func TestGetModelName(t *testing.T) {
name := getModelName(&testModel{})
if name != "testModel" {
t.Fatalf("Model name must be testModel, but was %v", name)
}
name = getModelName(testModel{})
if name != "testModel" {
t.Fatalf("Model name must be testModel, but was %v", name)
}
}
func TestGetModelNameStructOnly(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("Calling getModelName with invalid type must panic")
}
}()
val := 42
getModelName(val)
getModelName(&val)
}
func TestGetModelFields(t *testing.T) {
fields := getModelFields(&testModel{})
if len(fields) != 6 {
t.Fatalf("All fields must be returned: %v", len(fields))
}
if fields[0].Name != "Id" ||
fields[1].Name != "Name" ||
fields[2].Name != "Age" ||
fields[3].Name != "Array" ||
fields[4].Name != "Date" ||
fields[5].Name != "NullableField" {
t.Fatal("Field names must be correct")
}
fields = getModelFields(testModel{})
if len(fields) != 6 {
t.Fatalf("All fields must be returned: %v", len(fields))
}
}
func TestParseTag(t *testing.T) {
tags := parseTag("type:varchar(20);primarykey;notnull")
if len(tags) != 3 {
t.Fatal("All elements must be returned")
}
if tags[0].Name != "type" || tags[0].Value != "varchar(20)" ||
tags[1].Name != "" || tags[1].Value != "primarykey" ||
tags[2].Name != "" || tags[2].Value != "notnull" {
t.Fatal("Tag elements must be correct")
}
}
func TestModelWhitespace(t *testing.T) {
meta := buildMetaModel(testModelWhitespace{})
if meta.ModelName != "testModelWhitespace" {
t.Fatal("Name must be testModelWhitespace")
}
if len(meta.Fields) != 2 {
t.Fatalf("Model must have two fields, but was: %v", len(meta.Fields))
}
fields := meta.Fields
if fields[0].Name != "Id" || fields[1].Name != "Name" {
t.Fatal("Model fields must have proper names")
}
if len(fields[0].Tags) != 3 || len(fields[1].Tags) != 1 {
t.Fatalf("Model fields must have proper tags: %v %v", len(fields[0].Tags), len(fields[1].Tags))
}
if fields[0].Tags[0].Name != "type" || fields[0].Tags[0].Value != "bigint" {
t.Fatalf("First field must have type bigint: %v %v", fields[0].Tags[0].Name, fields[0].Tags[0].Value)
}
if fields[0].Tags[1].Name != "" || fields[0].Tags[1].Value != "pk" {
t.Fatal("Second field must have value pk")
}
if fields[0].Tags[2].Name != "" || fields[0].Tags[2].Value != "notnull" {
t.Fatal("Third field must have value notnull")
}
if fields[1].Tags[0].Name != "type" || fields[1].Tags[0].Value != "character varying(100)" {
t.Fatalf("First field must have type character varying(100): %v %v", fields[1].Tags[0].Name, fields[1].Tags[0].Value)
}
}