-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_test.go
185 lines (161 loc) · 5.37 KB
/
processor_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package postgres
import (
"testing"
"github.com/stretchr/testify/suite"
"github.com/goravel/framework/contracts/database/schema"
)
type ProcessorTestSuite struct {
suite.Suite
processor *Processor
}
func TestProcessorTestSuite(t *testing.T) {
suite.Run(t, new(ProcessorTestSuite))
}
func (s *ProcessorTestSuite) SetupTest() {
s.processor = NewProcessor()
}
func (s *ProcessorTestSuite) TestProcessColumns() {
tests := []struct {
name string
dbColumns []schema.DBColumn
expected []schema.Column
}{
{
name: "ValidInput",
dbColumns: []schema.DBColumn{
{Name: "id", Type: "int", TypeName: "INT", Nullable: "NO", Extra: "auto_increment", Collation: "utf8_general_ci", Comment: "primary key", Default: "nextval('id_seq'::regclass)"},
{Name: "name", Type: "varchar", TypeName: "VARCHAR", Nullable: "true", Extra: "", Collation: "utf8_general_ci", Comment: "user name", Default: ""},
},
expected: []schema.Column{
{Autoincrement: true, Collation: "utf8_general_ci", Comment: "primary key", Default: "nextval('id_seq'::regclass)", Name: "id", Nullable: false, Type: "int", TypeName: "INT"},
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "user name", Default: "", Name: "name", Nullable: true, Type: "varchar", TypeName: "VARCHAR"},
},
},
{
name: "EmptyInput",
dbColumns: []schema.DBColumn{},
},
{
name: "NullableColumn",
dbColumns: []schema.DBColumn{
{Name: "description", Type: "text", TypeName: "TEXT", Nullable: "true", Extra: "", Collation: "utf8_general_ci", Comment: "description", Default: ""},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "utf8_general_ci", Comment: "description", Default: "", Name: "description", Nullable: true, Type: "text", TypeName: "TEXT"},
},
},
{
name: "NonNullableColumn",
dbColumns: []schema.DBColumn{
{Name: "created_at", Type: "timestamp", TypeName: "TIMESTAMP", Nullable: "false", Extra: "", Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP"},
},
expected: []schema.Column{
{Autoincrement: false, Collation: "", Comment: "creation time", Default: "CURRENT_TIMESTAMP", Name: "created_at", Nullable: false, Type: "timestamp", TypeName: "TIMESTAMP"},
},
},
}
processor := NewProcessor()
for _, tt := range tests {
s.Run(tt.name, func() {
result := processor.ProcessColumns(tt.dbColumns)
s.Equal(tt.expected, result)
})
}
}
func (s *ProcessorTestSuite) TestProcessForeignKeys() {
tests := []struct {
name string
dbForeignKeys []schema.DBForeignKey
expected []schema.ForeignKey
}{
{
name: "ValidInput",
dbForeignKeys: []schema.DBForeignKey{
{Name: "fk_user_id", Columns: "user_id", ForeignSchema: "public", ForeignTable: "users", ForeignColumns: "id", OnUpdate: "c", OnDelete: "r"},
},
expected: []schema.ForeignKey{
{Name: "fk_user_id", Columns: []string{"user_id"}, ForeignSchema: "public", ForeignTable: "users", ForeignColumns: []string{"id"}, OnUpdate: "cascade", OnDelete: "restrict"},
},
},
{
name: "EmptyInput",
dbForeignKeys: []schema.DBForeignKey{},
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
result := s.processor.ProcessForeignKeys(tt.dbForeignKeys)
s.Equal(tt.expected, result)
})
}
}
func (s *ProcessorTestSuite) TestProcessIndexes() {
tests := []struct {
name string
dbIndexes []schema.DBIndex
expected []schema.Index
}{
{
name: "ValidInput",
dbIndexes: []schema.DBIndex{
{Name: "users_email_unique", Columns: "email", Type: "BTREE", Primary: false, Unique: true},
{Name: "PRIMARY", Columns: "id", Type: "BTREE", Primary: true, Unique: true},
{Name: "users_name_index", Columns: "first_name,last_name", Type: "BTREE", Primary: false, Unique: false},
},
expected: []schema.Index{
{Name: "users_email_unique", Columns: []string{"email"}, Type: "btree", Primary: false, Unique: true},
{Name: "primary", Columns: []string{"id"}, Type: "btree", Primary: true, Unique: true},
{Name: "users_name_index", Columns: []string{"first_name", "last_name"}, Type: "btree", Primary: false, Unique: false},
},
},
{
name: "EmptyInput",
dbIndexes: []schema.DBIndex{},
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
result := s.processor.ProcessIndexes(tt.dbIndexes)
s.Equal(tt.expected, result)
})
}
}
func (s *ProcessorTestSuite) TestProcessTypes() {
// ValidTypes_ReturnsProcessedTypes
input := []schema.Type{
{Type: "b", Category: "a"},
{Type: "c", Category: "b"},
{Type: "d", Category: "c"},
}
expected := []schema.Type{
{Type: "base", Category: "array"},
{Type: "composite", Category: "boolean"},
{Type: "domain", Category: "composite"},
}
processor := NewProcessor()
result := processor.ProcessTypes(input)
s.Equal(expected, result)
// UnknownType_ReturnsEmptyString
input = []schema.Type{
{Type: "unknown", Category: "a"},
}
expected = []schema.Type{
{Type: "", Category: "array"},
}
result = processor.ProcessTypes(input)
s.Equal(expected, result)
// UnknownCategory_ReturnsEmptyString
input = []schema.Type{
{Type: "b", Category: "unknown"},
}
expected = []schema.Type{
{Type: "base", Category: ""},
}
result = processor.ProcessTypes(input)
s.Equal(expected, result)
// EmptyInput_ReturnsEmptyOutput
input = []schema.Type{}
expected = []schema.Type{}
result = processor.ProcessTypes(input)
s.Equal(expected, result)
}