-
Notifications
You must be signed in to change notification settings - Fork 35
/
relationalfield_test.go
80 lines (68 loc) · 1.91 KB
/
relationalfield_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
package gorma_test
import (
"fmt"
"testing"
"github.com/goadesign/gorma"
"github.com/goadesign/gorma/dsl"
)
func TestFieldContext(t *testing.T) {
sg := &gorma.RelationalFieldDefinition{}
sg.FieldName = "SG"
c := sg.Context()
exp := fmt.Sprintf("RelationalField %#v", sg.FieldName)
if c != exp {
t.Errorf("Expected %s, got %s", exp, c)
}
sg.FieldName = ""
c = sg.Context()
exp = "unnamed RelationalField"
if c != exp {
t.Errorf("Expected %s, got %s", exp, c)
}
}
func TestFieldDSL(t *testing.T) {
sg := &gorma.RelationalFieldDefinition{}
f := func() {
return
}
sg.DefinitionDSL = f
c := sg.DSL()
if c == nil {
t.Errorf("Expected %T, got nil", f)
}
}
func TestFieldDefinitions(t *testing.T) {
var fieldtests = []struct {
name string
datatype gorma.FieldType
description string
nullable bool
belongsto string
hasmany string
hasone string
many2many string
expected string
}{
{"id", gorma.Integer, "description", false, "", "", "", "", "ID\tint // description\n"},
{"id", gorma.UUID, "description", false, "", "", "", "", "ID\tuuid.UUID // description\n"},
{"id", gorma.BigInteger, "description", false, "", "", "", "", "ID\tint64 // description\n"},
{"name", gorma.String, "name", true, "", "", "", "", "Name\t*string // name\n"},
{"user", gorma.HasOne, "has one", false, "", "", "User", "", "User\tUser // has one\n"},
{"user_id", gorma.BelongsTo, "belongs to", false, "", "", "", "", "UserID\tint // belongs to\n"},
}
for _, tt := range fieldtests {
f := &gorma.RelationalFieldDefinition{}
f.FieldName = dsl.SanitizeFieldName(tt.name)
f.Datatype = tt.datatype
f.Description = tt.description
f.Nullable = tt.nullable
f.BelongsTo = tt.belongsto
f.HasMany = tt.hasmany
f.HasOne = tt.hasone
f.Many2Many = tt.many2many
def := f.FieldDefinition()
if def != tt.expected {
t.Errorf("expected %s,got %s", tt.expected, def)
}
}
}