-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table_test.go
149 lines (144 loc) · 4.26 KB
/
create_table_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
package QueryHelper
import (
"testing"
"github.com/stretchr/testify/assert"
)
// Assume MissingPrimaryKeyErr is defined somewhere
func TestBuildCreateTableQueries(t *testing.T) {
// Test cases
tests := []struct {
name string
dataset string
table string
columns map[string]Column
expectedSchemaSQL string
expectedCreateTableSQL string
expectError bool
errorMessage string
}{
{
name: "Create table with single primary key",
dataset: "test_schema",
table: "test_table",
columns: map[string]Column{
"id": {
Name: "id",
Type: "INT",
Primary: true,
Null: false,
},
"name": {
Name: "name",
Type: "VARCHAR(255)",
Null: false,
},
},
expectedSchemaSQL: "CREATE SCHEMA IF NOT EXISTS `test_schema`",
expectedCreateTableSQL: "CREATE TABLE IF NOT EXISTS `test_schema`.`test_table` (`id` INT NOT NULL,`name` VARCHAR(255) NOT NULL,\n\tPRIMARY KEY(`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
expectError: false,
},
{
name: "Create table with composite primary key",
dataset: "test_schema",
table: "test_table",
columns: map[string]Column{
"id": {
Name: "id",
Type: "INT",
Primary: true,
Null: false,
},
"secondary_id": {
Name: "secondary_id",
Type: "INT",
Primary: true,
Null: false,
},
"name": {
Name: "name",
Type: "VARCHAR(255)",
Null: false,
},
},
expectedSchemaSQL: "CREATE SCHEMA IF NOT EXISTS `test_schema`",
expectedCreateTableSQL: "CREATE TABLE IF NOT EXISTS `test_schema`.`test_table` (`id` INT NOT NULL,`secondary_id` INT NOT NULL,`name` VARCHAR(255) NOT NULL,\n\tCONSTRAINT `PK_test_schema_test_table` PRIMARY KEY (`id`,`secondary_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
expectError: false,
},
{
name: "Create table with foreign key",
dataset: "test_schema",
table: "orders",
columns: map[string]Column{
"order_id": {
Name: "order_id",
Type: "INT",
Primary: true,
Null: false,
},
"customer_id": {
Name: "customer_id",
Type: "INT",
Table: "orders",
Null: false,
ForeignKey: "id",
ForeignTable: "customers",
ForeignSchema: "test_schema",
},
},
expectedSchemaSQL: "CREATE SCHEMA IF NOT EXISTS `test_schema`",
expectedCreateTableSQL: "CREATE TABLE IF NOT EXISTS `test_schema`.`orders` (`order_id` INT NOT NULL,`customer_id` INT NOT NULL,\n\tPRIMARY KEY(`order_id`),\n\tCONSTRAINT `FK_orders_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `test_schema`.`customers` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
expectError: false,
},
{
name: "Missing primary key",
dataset: "test_schema",
table: "test_table",
columns: map[string]Column{
"name": {
Name: "name",
Type: "VARCHAR(255)",
Null: false,
},
},
expectError: true,
errorMessage: MissingPrimaryKeyErr.Error(),
},
{
name: "Column with default value",
dataset: "test_schema",
table: "products",
columns: map[string]Column{
"product_id": {
Name: "product_id",
Type: "INT",
Primary: true,
Null: false,
},
"price": {
Name: "price",
Type: "DECIMAL(10,2)",
Null: false,
Default: "0.00",
},
},
expectedSchemaSQL: "CREATE SCHEMA IF NOT EXISTS `test_schema`",
expectedCreateTableSQL: "CREATE TABLE IF NOT EXISTS `test_schema`.`products` (`product_id` INT NOT NULL,`price` DECIMAL(10,2) NOT NULL DEFAULT 0.00,\n\tPRIMARY KEY(`product_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the function under test
s := &SqlDB{}
schemaSQL, createTableSQL, err := s.BuildCreateTableQueries(tt.dataset, tt.table, tt.columns)
if tt.expectError {
assert.Error(t, err)
assert.EqualError(t, err, tt.errorMessage)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedSchemaSQL, schemaSQL)
assert.Equal(t, tt.expectedCreateTableSQL, createTableSQL)
}
})
}
}