Skip to content

Commit 2f2b619

Browse files
committed
feat: beginning of testing for sqlx parser
1 parent 9b4bcfe commit 2f2b619

File tree

3 files changed

+174
-6
lines changed

3 files changed

+174
-6
lines changed

cmd/sqlx_parser.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ func sqlxParser(filepath string) (sqlxParserMeta, error) {
148148
} else if strings.Contains(lineContents, "}") && isInInnerMajorBlock && innerMajorBlockCount >= 1 && !inMajorBlock {
149149
innerMajorBlockCount -= 1
150150
currentBlockContent += lineContents
151-
} else if lineContents != "" && !inMajorBlock {
151+
} else if lineContents != "\n" && !inMajorBlock {
152152
if startOfSqlBlock == 0 {
153153
startOfSqlBlock = i
154154
sqlBlockExsists = true
155155
sqlBlockContent += lineContents
156+
endOfSqlBlock = i
156157
} else {
157158
sqlBlockContent += lineContents
158159
endOfSqlBlock = i
@@ -162,11 +163,6 @@ func sqlxParser(filepath string) (sqlxParserMeta, error) {
162163
}
163164
}
164165

165-
// fmt.Println("configBlockContent: ", configBlockContent)
166-
// fmt.Println("preOpsBlockContent: ", postOpsBlocksMeta[0].postOpsBlockContent)
167-
// fmt.Println("postOpsBlockContent: ", postOpsBlocksMeta[0].postOpsBlockContent)
168-
// fmt.Println("sqlBlockContent: ", sqlBlockContent)
169-
170166
return sqlxParserMeta{
171167
filepath: filepath,
172168
numLines: i,

cmd/sqlx_parser_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestSqlxParser(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
content string
13+
expected sqlxParserMeta
14+
wantErr bool
15+
}{
16+
{
17+
name: "Nested config blocks and single line query",
18+
content: `
19+
config {
20+
type: "table",
21+
schema: "electric_cars",
22+
dependencies: 'ALL_EV_CARS_DATA',
23+
bigquery: {
24+
partitionBy: "MODEL",
25+
requirePartitionFilter : true,
26+
clusterBy: ["CITY", "STATE"]
27+
},
28+
tags: ["TAG_1"]
29+
}
30+
SELECT * FROM electric_cars WHERE model = $1;`,
31+
expected: sqlxParserMeta{
32+
numLines: 13,
33+
configBlockMeta: ConfigBlockMeta {
34+
exsists: true,
35+
startOfConfigBlock: 2,
36+
endOfConfigBlock: 12,
37+
configBlockContent: `config {
38+
type: "table",
39+
schema: "electric_cars",
40+
dependencies: 'ALL_EV_CARS_DATA',
41+
bigquery: {
42+
partitionBy: "MODEL",
43+
requirePartitionFilter : true,
44+
clusterBy: ["CITY", "STATE"]
45+
},
46+
tags: ["TAG_1"]
47+
}
48+
`},
49+
sqlBlocksMeta: SqlBlockMeta {
50+
exsists: true,
51+
startOfSqlBlock: 13,
52+
endOfSqlBlock: 13,
53+
sqlBlockContent: `SELECT * FROM electric_cars WHERE model = $1;`,
54+
formattedSqlBlockContent: "",
55+
},
56+
},
57+
wantErr: false,
58+
},
59+
{
60+
name: "Pre operations query after config block",
61+
content: `
62+
config {
63+
type: "table",
64+
schema: "electric_cars",
65+
dependencies: 'ALL_EV_CARS_DATA',
66+
bigquery: {
67+
partitionBy: "MODEL",
68+
requirePartitionFilter : true,
69+
clusterBy: ["CITY", "STATE"]
70+
},
71+
tags: ["TAG_1"]
72+
}
73+
pre_operations {
74+
${when(incremental(), ` + "`" + `DELETE
75+
FROM
76+
${self()}
77+
WHERE
78+
DATE(PIPELINE_RUN_DATETIME) = CURRENT_DATE()` + "`" + `)}
79+
}
80+
81+
SELECT * FROM electric_cars WHERE model = $1;`,
82+
expected: sqlxParserMeta{
83+
numLines: 21,
84+
configBlockMeta: ConfigBlockMeta {
85+
exsists: true,
86+
startOfConfigBlock: 2,
87+
endOfConfigBlock: 12,
88+
configBlockContent: `
89+
config {
90+
type: "table",
91+
schema: "electric_cars",
92+
dependencies: 'ALL_EV_CARS_DATA',
93+
bigquery: {
94+
partitionBy: "MODEL",
95+
requirePartitionFilter : true,
96+
clusterBy: ["CITY", "STATE"]
97+
},
98+
tags: ["TAG_1"]
99+
}`,
100+
},
101+
sqlBlocksMeta: SqlBlockMeta {
102+
exsists: true,
103+
startOfSqlBlock: 21,
104+
endOfSqlBlock: 21,
105+
sqlBlockContent: `SELECT * FROM electric_cars WHERE model = $1;`,
106+
formattedSqlBlockContent: "",
107+
},
108+
},
109+
wantErr: false,
110+
},
111+
}
112+
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
// Create a temporary file
116+
tmpfile, err := os.CreateTemp("", "test*.sqlx")
117+
if err != nil {
118+
t.Fatalf("Failed to create temp file: %v", err)
119+
}
120+
defer os.Remove(tmpfile.Name())
121+
122+
// Write content to the file
123+
if _, err := tmpfile.Write([]byte(tt.content)); err != nil {
124+
t.Fatalf("Failed to write to temp file: %v", err)
125+
}
126+
if err := tmpfile.Close(); err != nil {
127+
t.Fatalf("Failed to close temp file: %v", err)
128+
}
129+
130+
// Call the function
131+
got, err := sqlxParser(tmpfile.Name())
132+
133+
// Check for errors
134+
if (err != nil) != tt.wantErr {
135+
t.Errorf("getSqlxFileMetaData() error = %v, wantErr %v", err, tt.wantErr)
136+
return
137+
}
138+
139+
// Set the filepath in the expected result
140+
tt.expected.filepath = tmpfile.Name()
141+
142+
// Compare each field separately
143+
if got.filepath != tt.expected.filepath {
144+
t.Errorf("[got]: filepath = %v, [want]: %v", got.filepath, tt.expected.filepath)
145+
}
146+
if got.numLines != tt.expected.numLines {
147+
t.Errorf("[got]: numLines = %v, [want]: %v", got.numLines, tt.expected.numLines)
148+
}
149+
if got.configBlockMeta.startOfConfigBlock != tt.expected.configBlockMeta.startOfConfigBlock {
150+
t.Errorf("[got]: configStartLine = %v, [want]: %v", got.configBlockMeta.startOfConfigBlock, tt.expected.configBlockMeta.startOfConfigBlock)
151+
}
152+
if got.configBlockMeta.endOfConfigBlock != tt.expected.configBlockMeta.endOfConfigBlock {
153+
t.Errorf("[got]: configEndLine = %v, [want]: %v", got.configBlockMeta.endOfConfigBlock, tt.expected.configBlockMeta.endOfConfigBlock)
154+
}
155+
if strings.TrimSpace(got.configBlockMeta.configBlockContent) != strings.TrimSpace(tt.expected.configBlockMeta.configBlockContent) {
156+
t.Errorf("[got]: configString = %v, [want]: %v", got.configBlockMeta.configBlockContent, tt.expected.configBlockMeta.configBlockContent)
157+
}
158+
if strings.TrimSpace(got.sqlBlocksMeta.sqlBlockContent) != strings.TrimSpace(tt.expected.sqlBlocksMeta.sqlBlockContent) {
159+
t.Errorf("[got]: sqlBlockContent = %v, [want]: %v", got.sqlBlocksMeta.sqlBlockContent, tt.expected.sqlBlocksMeta.sqlBlockContent)
160+
}
161+
162+
if (got.sqlBlocksMeta.startOfSqlBlock != tt.expected.sqlBlocksMeta.startOfSqlBlock) {
163+
t.Errorf("[got]: startOfSqlBlock = %v, [want]: %v", got.sqlBlocksMeta.startOfSqlBlock, tt.expected.sqlBlocksMeta.startOfSqlBlock)
164+
}
165+
if (got.sqlBlocksMeta.endOfSqlBlock != tt.expected.sqlBlocksMeta.endOfSqlBlock) {
166+
t.Errorf("[got]: endOfSqlBlock = %v, [want]: %v", got.sqlBlocksMeta.endOfSqlBlock, tt.expected.sqlBlocksMeta.endOfSqlBlock)
167+
}
168+
169+
})
170+
}
171+
}

cmd/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func writeContentsToFileInPlace(sqlxFileMetaData *sqlxParserMeta, formattingErro
159159

160160
func formatSqlxFile(sqlxFilePath string, inplace bool, sqlfluffConfigPath string, pythonExecutable string, logger *slog.Logger) {
161161
sqlxFileMetaData, err := sqlxParser(sqlxFilePath)
162+
fmt.Printf("%+v\n", sqlxFileMetaData)
162163
if err != nil {
163164
fmt.Println("Error finding config blocks:", err)
164165
} else {

0 commit comments

Comments
 (0)