@@ -18,8 +18,9 @@ package main
1818
1919import (
2020 "os"
21- "strings"
2221 "testing"
22+
23+ "github.com/stretchr/testify/assert"
2324)
2425
2526func TestCheckGCPCredentials (t * testing.T ) {
@@ -40,30 +41,159 @@ func TestCheckGCPCredentials(t *testing.T) {
4041 })
4142}
4243
43- func TestExtractQueries (t * testing.T ) {
44- content := "CREATE TABLE test (id INT PRIMARY KEY);\n -- This is a comment\n CREATE TABLE another_test (id INT PRIMARY KEY);\n "
45- filepath := "test.cql"
46-
47- // Write the content to a temporary file
48- if err := os .WriteFile (filepath , []byte (content ), 0644 ); err != nil {
49- t .Fatalf ("Failed to write test file: %v" , err )
44+ func TestStmtExtractor (t * testing.T ) {
45+ testCases := []struct {
46+ name string
47+ content string
48+ expectedStmts []string
49+ expectError bool
50+ expectedErrorMsg string
51+ }{
52+ {
53+ name : "Basic" ,
54+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
55+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
56+ expectedStmts : []string {
57+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
58+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
59+ },
60+ expectError : false ,
61+ expectedErrorMsg : "" ,
62+ },
63+ {
64+ name : "No semicolon in the end of the last stmt" ,
65+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
66+ "CREATE TABLE another_test (id INT PRIMARY KEY)" ,
67+ expectedStmts : []string {
68+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
69+ "CREATE TABLE another_test (id INT PRIMARY KEY)" ,
70+ },
71+ expectError : false ,
72+ expectedErrorMsg : "" ,
73+ },
74+ {
75+ name : "Line comment at the beginning" ,
76+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
77+ "// CREATE TABLE test (id INT PRIMARY KEY);\n " +
78+ "-- CREATE TABLE test (id INT PRIMARY KEY);\n " +
79+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
80+ expectedStmts : []string {
81+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
82+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
83+ },
84+ expectError : false ,
85+ expectedErrorMsg : "" ,
86+ },
87+ {
88+ name : "Line comment at the end" ,
89+ content : "CREATE TABLE test (id INT PRIMARY KEY); // test 123\n " +
90+ "// CREATE TABLE test (id INT PRIMARY KEY);\n " +
91+ "-- CREATE TABLE test (id INT PRIMARY KEY);\n " +
92+ "CREATE TABLE another_test (id INT PRIMARY KEY); -- comment" ,
93+ expectedStmts : []string {
94+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
95+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
96+ },
97+ expectError : false ,
98+ expectedErrorMsg : "" ,
99+ },
100+ {
101+ name : "Block comment" ,
102+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
103+ "/* \n " +
104+ "CREATE TABLE test (id INT PRIMARY KEY);\n " +
105+ "CREATE TABLE test (id INT PRIMARY KEY);*/" +
106+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
107+ expectedStmts : []string {
108+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
109+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
110+ },
111+ expectError : false ,
112+ expectedErrorMsg : "" ,
113+ },
114+ {
115+ name : "Unterminated Block comment" ,
116+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
117+ "/* \n " +
118+ "CREATE TABLE test (id INT PRIMARY KEY);\n " +
119+ "CREATE TABLE test (id INT PRIMARY KEY);\n " +
120+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
121+ expectedStmts : []string {
122+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
123+ },
124+ expectError : false ,
125+ expectedErrorMsg : "" ,
126+ },
127+ {
128+ name : "Inline block comment" ,
129+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
130+ "/*CREATE TABLE test (id INT PRIMARY KEY);*/ CREATE TABLE test2 (id2 INT PRIMARY KEY);\n " +
131+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
132+ expectedStmts : []string {
133+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
134+ "CREATE TABLE test2 (id2 INT PRIMARY KEY);" ,
135+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
136+ },
137+ expectError : false ,
138+ expectedErrorMsg : "" ,
139+ },
140+ {
141+ name : "Two block comments in the same line" ,
142+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
143+ "/*CREATE TABLE test (id INT PRIMARY KEY);*/ CREATE TABLE test2 (id2 INT PRIMARY KEY); /* test */\n " +
144+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
145+ expectedStmts : []string {
146+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
147+ "CREATE TABLE test2 (id2 INT PRIMARY KEY);" ,
148+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
149+ },
150+ expectError : false ,
151+ expectedErrorMsg : "" ,
152+ },
153+ {
154+ name : "Double-slash in block comment" ,
155+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
156+ "/* test // */CREATE TABLE test2 (id INT PRIMARY KEY);\n " +
157+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
158+ expectedStmts : []string {
159+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
160+ "CREATE TABLE test2 (id INT PRIMARY KEY);" ,
161+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
162+ },
163+ expectError : false ,
164+ expectedErrorMsg : "" ,
165+ },
166+ {
167+ name : "Double-slash after block comment" ,
168+ content : "CREATE TABLE test (id INT PRIMARY KEY);\n " +
169+ "/* test */ //CREATE TABLE test2 (id INT PRIMARY KEY);\n " +
170+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
171+ expectedStmts : []string {
172+ "CREATE TABLE test (id INT PRIMARY KEY);" ,
173+ "CREATE TABLE another_test (id INT PRIMARY KEY);" ,
174+ },
175+ expectError : false ,
176+ expectedErrorMsg : "" ,
177+ },
50178 }
51- defer os .Remove (filepath )
52179
53- expectedQueries := []string {
54- "CREATE TABLE test (id INT PRIMARY KEY); " ,
55- "CREATE TABLE another_test (id INT PRIMARY KEY); " ,
56- }
180+ // TODO: Use os.CreateTemp to create the test file.
181+ filepath := "test.cql"
182+ for _ , tc := range testCases {
183+ t .Run (tc .name , func (t * testing.T ) {
184+ // Write the content to a temporary file
185+ if err := os .WriteFile (filepath , []byte (tc .content ), 0644 ); err != nil {
186+ t .Fatalf ("Failed to write test file: %v" , err )
187+ }
188+ defer os .Remove (filepath )
57189
58- t .Run ("Extract Queries Successfully" , func (t * testing.T ) {
59- queries , err := extractQueries (filepath )
60- if err != nil {
61- t .Fatalf ("Expected no error, got %v" , err )
62- }
63- for i , expected := range expectedQueries {
64- if strings .TrimSpace (queries [i ]) != strings .TrimSpace (expected ) {
65- t .Errorf ("Expected %v, got %v" , expected , queries [i ])
190+ stmts , err := parseCqlFile (filepath )
191+ if tc .expectError {
192+ assert .Equal (t , tc .expectedErrorMsg , err .Error ())
193+ return
66194 }
67- }
68- })
195+ assert .NoError (t , err )
196+ assert .Equal (t , tc .expectedStmts , stmts )
197+ })
198+ }
69199}
0 commit comments