-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfields_test.go
108 lines (97 loc) · 3.54 KB
/
fields_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
package crud_test
import (
"testing"
"github.com/azer/crud/v2"
"github.com/stretchr/testify/assert"
)
func TestFields(t *testing.T) {
fields, err := crud.GetFieldsOf(UserProfile{})
assert.Nil(t, err)
assert.Equal(t, len(fields), 6)
assert.Equal(t, fields[0].Name, "Id")
assert.Equal(t, fields[0].SQL.Name, "id")
assert.Equal(t, fields[0].SQL.Type, "int")
assert.Equal(t, fields[0].SQL.AutoIncrement, 1)
assert.True(t, fields[0].SQL.IsRequired)
assert.True(t, fields[0].SQL.IsPrimaryKey)
assert.Equal(t, fields[1].Name, "Name")
assert.Equal(t, fields[1].SQL.Name, "name")
assert.Equal(t, fields[1].SQL.Type, "varchar")
assert.Equal(t, fields[2].Name, "Bio")
assert.Equal(t, fields[2].SQL.Name, "bio")
assert.Equal(t, fields[2].SQL.Type, "text")
assert.Equal(t, fields[3].Name, "Email")
assert.Equal(t, fields[3].SQL.Name, "email")
assert.Equal(t, fields[3].SQL.Type, "varchar")
assert.Equal(t, fields[4].Name, "Attachment")
assert.Equal(t, fields[4].SQL.Name, "attachment")
assert.Equal(t, fields[4].SQL.Type, "blob")
assert.Equal(t, fields[5].Name, "Modified")
assert.Equal(t, fields[5].SQL.Name, "modified_col")
assert.Equal(t, fields[5].SQL.Type, "bigint")
}
func TestHasPK(t *testing.T) {
fields, err := crud.GetFieldsOf(UserProfile{})
assert.Nil(t, err)
assert.True(t, crud.HasPK(fields))
fields, err = crud.GetFieldsOf(Post{})
assert.Nil(t, err)
assert.True(t, crud.HasPK(fields))
fields, err = crud.GetFieldsOf(Foo{})
assert.Nil(t, err)
assert.False(t, crud.HasPK(fields))
}
func TestSetDefaultPK(t *testing.T) {
fields, err := crud.GetFieldsOf(Foo{})
crud.SetDefaultPK(fields)
assert.Nil(t, err)
assert.True(t, fields[0].SQL.IsPrimaryKey)
}
func TestDefaultPK(t *testing.T) {
fields, err := crud.GetFieldsOf(Foo{})
assert.Nil(t, err)
crud.SetDefaultPK(fields)
assert.Equal(t, len(fields), 4)
assert.Equal(t, fields[0].Name, "Id")
assert.Equal(t, fields[0].SQL.Name, "id")
assert.Equal(t, fields[0].SQL.Type, "int")
assert.Equal(t, fields[0].SQL.AutoIncrement, 1)
assert.True(t, fields[0].SQL.IsRequired)
assert.True(t, fields[0].SQL.IsPrimaryKey)
assert.Equal(t, fields[1].Name, "APIKey")
assert.Equal(t, fields[1].SQL.Name, "api_key")
assert.Equal(t, fields[1].SQL.Type, "varchar")
assert.Equal(t, fields[2].Name, "YOLO")
assert.Equal(t, fields[2].SQL.Name, "yolo")
assert.Equal(t, fields[2].SQL.Type, "tinyint")
assert.Equal(t, fields[3].Name, "Beast")
assert.Equal(t, fields[3].SQL.Name, "beast")
assert.Equal(t, fields[3].SQL.Type, "varchar")
}
func TestEmbeddedFields(t *testing.T) {
fields, err := crud.GetFieldsOf(EmbeddedFoo{})
crud.SetDefaultPK(fields)
assert.Nil(t, err)
assert.Equal(t, len(fields), 6)
assert.Equal(t, fields[0].Name, "Id")
assert.Equal(t, fields[0].SQL.Name, "id")
assert.Equal(t, fields[0].SQL.Type, "int")
assert.Equal(t, fields[0].SQL.AutoIncrement, 1)
assert.True(t, fields[0].SQL.IsRequired)
assert.True(t, fields[0].SQL.IsPrimaryKey)
assert.Equal(t, fields[1].Name, "APIKey")
assert.Equal(t, fields[1].SQL.Name, "api_key")
assert.Equal(t, fields[1].SQL.Type, "varchar")
assert.Equal(t, fields[2].Name, "YOLO")
assert.Equal(t, fields[2].SQL.Name, "yolo")
assert.Equal(t, fields[2].SQL.Type, "tinyint")
assert.Equal(t, fields[3].Name, "Beast")
assert.Equal(t, fields[3].SQL.Name, "beast")
assert.Equal(t, fields[3].SQL.Type, "varchar")
assert.Equal(t, fields[4].Name, "Span")
assert.Equal(t, fields[4].SQL.Name, "span")
assert.Equal(t, fields[4].SQL.Type, "int")
assert.Equal(t, fields[5].Name, "Eggs")
assert.Equal(t, fields[5].SQL.Name, "eggs")
assert.Equal(t, fields[5].SQL.Type, "varchar")
}