forked from k1LoW/tbls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverage_test.go
190 lines (179 loc) · 3.78 KB
/
coverage_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
186
187
188
189
190
package coverage
import (
"database/sql"
"testing"
"github.com/k1LoW/tbls/schema"
)
func TestMeasure(t *testing.T) {
s := newTestSchema(t)
got := Measure(s)
if want := 10; got.Covered != want {
t.Errorf("got %v want %v", got.Covered, want)
}
if want := 17; got.Total != want {
t.Errorf("got %v want %v", got.Total, want)
}
}
func TestRound(t *testing.T) {
tests := []struct {
in float64
want float64
}{
{0.3, 0.3},
{0.33, 0.3},
{0.333333, 0.3},
{0.34, 0.3},
{0.35, 0.4},
}
for _, tt := range tests {
got := round(tt.in)
if got != tt.want {
t.Errorf("got %v want %v", got, tt.want)
}
}
}
func newTestSchema(t *testing.T) *schema.Schema {
ca := &schema.Column{
Name: "column_a1",
Type: "bigint(20)",
Comment: "column a",
Nullable: false,
}
cb := &schema.Column{
Name: "column_b1",
Type: "text",
Comment: "", // empty comment
Nullable: true,
}
ta := &schema.Table{
Name: "table_a",
Labels: schema.Labels{
&schema.Label{Name: "bq-invalid", Virtual: false},
},
Type: "BASE TABLE",
Comment: "", // empty comment
Columns: []*schema.Column{
ca,
&schema.Column{
Name: "column_a2",
Type: "datetime",
Comment: "column a2",
Nullable: false,
Default: sql.NullString{
String: "CURRENT_TIMESTAMP",
Valid: true,
},
},
},
}
tb := &schema.Table{
Name: "table_b",
Type: "BASE TABLE",
Comment: "table b",
Columns: []*schema.Column{
cb,
&schema.Column{
Name: "column_b2",
Comment: "column b2",
Type: "text",
Nullable: true,
},
},
}
tc := &schema.Table{
Name: "table_c",
Type: "BASE TABLE",
Comment: "table c",
Columns: []*schema.Column{
&schema.Column{
Name: "column_c1",
Type: "text",
Comment: "column c1",
Nullable: false,
},
&schema.Column{
Name: "column_c2",
Type: "text",
Comment: "column c2",
Nullable: false,
},
&schema.Column{
Name: "column_c3",
Type: "text",
Comment: "column c3",
Nullable: false,
},
&schema.Column{
Name: "column_c4",
Type: "text",
Comment: "column c4",
Nullable: false,
},
},
}
r := &schema.Relation{
Table: ta,
Columns: []*schema.Column{ca},
ParentTable: tb,
ParentColumns: []*schema.Column{cb},
}
ca.ParentRelations = []*schema.Relation{r}
cb.ChildRelations = []*schema.Relation{r}
ta.Indexes = []*schema.Index{
&schema.Index{
Name: "a2_idx",
Def: "a2 index",
Table: &ta.Name,
Columns: []string{
"column_a2",
},
},
}
ta.Constraints = []*schema.Constraint{
&schema.Constraint{
Name: "a1_b1_fk",
Type: schema.TypeFK,
Table: &ta.Name,
ReferencedTable: &tb.Name,
Columns: []string{"column_a1"},
ReferencedColumns: []string{"column_b1"},
},
&schema.Constraint{
Name: "a1_unique",
Type: "UNIQUE",
Table: &ta.Name,
ReferencedTable: nil,
Columns: []string{"column_a1"},
},
}
ta.Triggers = []*schema.Trigger{
&schema.Trigger{
Name: "update_table_a_column_a1",
Def: "CREATE CONSTRAINT TRIGGER update_table_a_column_a1 AFTER INSERT OR UPDATE ON table_a",
Comment: "Update column_a1 when update table",
},
&schema.Trigger{
Name: "update_table_a_column_a2",
Def: "CREATE CONSTRAINT TRIGGER update_table_a_column_a2 AFTER INSERT OR UPDATE ON table_a",
},
}
s := &schema.Schema{
Name: "testschema",
Labels: schema.Labels{
&schema.Label{Name: "bq-invalid", Virtual: false},
},
Tables: []*schema.Table{
ta,
tb,
tc,
},
Relations: []*schema.Relation{
r,
},
Driver: &schema.Driver{
Name: "testdriver",
DatabaseVersion: "1.0.0",
},
}
return s
}