Repository navigation
Expand file tree
/
Copy pathsqlite_test.go
More file actions
174 lines (167 loc) · 4.43 KB
/
Copy pathsqlite_test.go
File metadata and controls
174 lines (167 loc) · 4.43 KB
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
package sqlite
import (
"strings"
"testing"
"github.com/mstgnz/sqlmapper"
"github.com/stretchr/testify/assert"
)
func TestSQLite_Parse(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
validate func(*testing.T, *sqlmapper.Schema)
}{
{
name: "Empty content",
content: "",
wantErr: true,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.Nil(t, schema)
},
},
{
name: "Valid content",
content: "CREATE TABLE test (id INTEGER PRIMARY KEY);",
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
},
},
{
name: "CREATE TABLE",
content: "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT);",
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
// Additional validation logic can be added here
},
},
{
name: "CREATE INDEX",
content: "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT); CREATE INDEX idx_name ON test (name);",
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
assert.Len(t, schema.Tables, 1)
assert.Len(t, schema.Tables[0].Indexes, 1)
assert.Equal(t, "idx_name", schema.Tables[0].Indexes[0].Name)
assert.Equal(t, []string{"name"}, schema.Tables[0].Indexes[0].Columns)
},
},
{
name: "CREATE VIEW",
content: "CREATE VIEW test_view AS SELECT id, name FROM test;",
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
// Additional validation logic for views can be added here
},
},
{
name: "CREATE TRIGGER",
content: "CREATE TRIGGER test_trigger AFTER INSERT ON test BEGIN UPDATE test SET name = 'updated' WHERE id = NEW.id; END;",
wantErr: false,
validate: func(t *testing.T, schema *sqlmapper.Schema) {
assert.NotNil(t, schema)
// Additional validation logic for triggers can be added here
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSQLite()
schema, err := s.Parse(tt.content)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.validate != nil {
tt.validate(t, schema)
}
})
}
}
func TestSQLite_Generate(t *testing.T) {
tests := []struct {
name string
schema *sqlmapper.Schema
want string
wantErr bool
}{
{
name: "Nil schema",
schema: nil,
wantErr: true,
},
{
name: "Basic schema with one table",
schema: &sqlmapper.Schema{
Tables: []sqlmapper.Table{
{
Name: "users",
Columns: []sqlmapper.Column{
{Name: "id", DataType: "INTEGER", IsPrimaryKey: true},
{Name: "name", DataType: "TEXT", Length: 100, IsNullable: false},
{Name: "email", DataType: "TEXT", Length: 255, IsNullable: false, IsUnique: true},
},
},
},
},
want: strings.TrimSpace(`
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);`),
wantErr: false,
},
{
name: "Schema with table and indexes",
schema: &sqlmapper.Schema{
Tables: []sqlmapper.Table{
{
Name: "products",
Columns: []sqlmapper.Column{
{Name: "id", DataType: "INTEGER", IsPrimaryKey: true},
{Name: "name", DataType: "TEXT", Length: 100, IsNullable: false},
{Name: "price", DataType: "REAL", Length: 10, Scale: 2, IsNullable: true},
},
Indexes: []sqlmapper.Index{
{Name: "idx_name", Columns: []string{"name"}},
{Name: "idx_price", Columns: []string{"price"}, IsUnique: true},
},
},
},
},
want: strings.TrimSpace(`
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price REAL(10,2)
);
CREATE INDEX idx_name ON products(name);
CREATE UNIQUE INDEX idx_price ON products(price);`),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewSQLite()
result, err := s.Generate(tt.schema)
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.want != "" {
assert.Equal(t, tt.want, strings.TrimSpace(result))
}
})
}
}
func TestSQLite_Generate_ComplexSchema(t *testing.T) {
schema := &sqlmapper.Schema{
// Assuming a complex schema object with tables, views, and triggers
}
s := NewSQLite()
_, err := s.Generate(schema)
assert.NoError(t, err)
}