-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathanalyze_test.go
More file actions
123 lines (105 loc) · 2.62 KB
/
analyze_test.go
File metadata and controls
123 lines (105 loc) · 2.62 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
package analyzer
import (
"context"
"testing"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
func TestAnalyzer_Analyze(t *testing.T) {
db := config.Database{
Managed: true,
}
a := New(db)
defer a.Close(context.Background())
ctx := context.Background()
migrations := []string{
`CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT
)`,
}
query := `SELECT id, name, email FROM users WHERE id = ?`
node := &ast.TODO{}
result, err := a.Analyze(ctx, node, query, migrations, nil)
if err != nil {
t.Fatalf("Analyze failed: %v", err)
}
if len(result.Columns) != 3 {
t.Errorf("Expected 3 columns, got %d", len(result.Columns))
}
expectedCols := []struct {
name string
dataType string
notNull bool
}{
{"id", "integer", false},
{"name", "text", true},
{"email", "text", false},
}
for i, expected := range expectedCols {
if i >= len(result.Columns) {
break
}
col := result.Columns[i]
if col.Name != expected.name {
t.Errorf("Column %d: expected name %q, got %q", i, expected.name, col.Name)
}
if col.DataType != expected.dataType {
t.Errorf("Column %d: expected dataType %q, got %q", i, expected.dataType, col.DataType)
}
if col.NotNull != expected.notNull {
t.Errorf("Column %d: expected notNull %v, got %v", i, expected.notNull, col.NotNull)
}
if col.Table == nil || col.Table.Name != "users" {
t.Errorf("Column %d: expected table 'users', got %v", i, col.Table)
}
}
if len(result.Params) != 1 {
t.Errorf("Expected 1 parameter, got %d", len(result.Params))
}
}
func TestAnalyzer_InvalidQuery(t *testing.T) {
db := config.Database{
Managed: true,
}
a := New(db)
defer a.Close(context.Background())
ctx := context.Background()
migrations := []string{
`CREATE TABLE users (id INTEGER PRIMARY KEY)`,
}
query := `SELECT * FROM nonexistent`
node := &ast.TODO{}
_, err := a.Analyze(ctx, node, query, migrations, nil)
if err == nil {
t.Error("Expected error for invalid query, got nil")
}
}
func TestNormalizeType(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"INTEGER", "integer"},
{"INT", "integer"},
{"BIGINT", "integer"},
{"TEXT", "text"},
{"VARCHAR(255)", "text"},
{"BLOB", "blob"},
{"REAL", "real"},
{"FLOAT", "real"},
{"DOUBLE", "real"},
{"BOOLEAN", "boolean"},
{"DATETIME", "datetime"},
{"", "any"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := normalizeType(tt.input)
if result != tt.expected {
t.Errorf("normalizeType(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}