@@ -4,13 +4,20 @@ import (
44 "strings"
55 "testing"
66
7- "github.com/jackc/pgx/v5/stdlib"
87 mssql "github.com/microsoft/go-mssqldb"
98 "github.com/stretchr/testify/assert"
109 "github.com/stretchr/testify/require"
1110 "github.com/vippsas/sqlcode/sqlparser"
11+ mssql17 "github.com/vippsas/sqlcode/sqlparser/mssql"
12+ "github.com/vippsas/sqlcode/sqlparser/sqldocument"
1213)
1314
15+ func ParseString (t * testing.T , file , input string ) * mssql17.TSqlDocument {
16+ d := & mssql17.TSqlDocument {}
17+ assert .NoError (t , d .Parse ([]byte (input ), sqldocument .FileRef (file )))
18+ return d
19+ }
20+
1421func TestLineNumberInInput (t * testing.T ) {
1522
1623 // Scenario:
@@ -64,7 +71,7 @@ func TestSchemaSuffixFromHash(t *testing.T) {
6471 })
6572
6673 t .Run ("returns consistent hash" , func (t * testing.T ) {
67- doc := sqlparser . ParseString ("test.sql" , `
74+ doc := ParseString (t , "test.sql" , `
6875declare @EnumFoo int = 1;
6976go
7077create procedure [code].Test as begin end
@@ -78,12 +85,12 @@ create procedure [code].Test as begin end
7885 })
7986
8087 t .Run ("different content yields different hash" , func (t * testing.T ) {
81- doc1 := sqlparser . ParseString ("test.sql" , `
88+ doc1 := ParseString (t , "test.sql" , `
8289declare @EnumFoo int = 1;
8390go
8491create procedure [code].Test1 as begin end
8592` )
86- doc2 := sqlparser . ParseString ("test.sql" , `
93+ doc2 := ParseString (t , "test.sql" , `
8794declare @EnumFoo int = 2;
8895go
8996create procedure [code].Test2 as begin end
@@ -94,12 +101,6 @@ create procedure [code].Test2 as begin end
94101
95102 assert .NotEqual (t , suffix1 , suffix2 )
96103 })
97-
98- t .Run ("empty document has hash" , func (t * testing.T ) {
99- doc := sqlparser .NewDocumentFromExtension (".pgsql" )
100- suffix := SchemaSuffixFromHash (doc )
101- assert .Len (t , suffix , 12 )
102- })
103104}
104105
105106func TestSchemaName (t * testing.T ) {
@@ -110,7 +111,7 @@ func TestSchemaName(t *testing.T) {
110111func TestBatchLineNumberInInput (t * testing.T ) {
111112 t .Run ("no corrections" , func (t * testing.T ) {
112113 b := Batch {
113- StartPos : sqlparser .Pos {Line : 10 , Col : 1 },
114+ StartPos : sqldocument .Pos {Line : 10 , Col : 1 },
114115 Lines : "line1\n line2\n line3" ,
115116 }
116117
@@ -121,7 +122,7 @@ func TestBatchLineNumberInInput(t *testing.T) {
121122
122123 t .Run ("with corrections" , func (t * testing.T ) {
123124 b := Batch {
124- StartPos : sqlparser .Pos {Line : 10 , Col : 1 },
125+ StartPos : sqldocument .Pos {Line : 10 , Col : 1 },
125126 Lines : "line1\n line2\n extra1\n extra2\n line3" ,
126127 lineNumberCorrections : []lineNumberCorrection {
127128 {inputLineNumber : 2 , extraLinesInOutput : 2 }, // line 2 became 3 lines
@@ -184,7 +185,7 @@ func TestBatchRelativeLineNumberInInput(t *testing.T) {
184185
185186func TestPreprocess (t * testing.T ) {
186187 t .Run ("basic procedure with schema replacement" , func (t * testing.T ) {
187- doc := sqlparser . ParseString ("test.sql" , `
188+ doc := ParseString (t , "test.sql" , `
188189create procedure [code].Test as
189190begin
190191 select 1
199200 assert .NotContains (t , result .Batches [0 ].Lines , "[code]." )
200201 })
201202
202- t .Run ("postgres uses unquoted schema name" , func (t * testing.T ) {
203- doc := sqlparser .ParseString ("test.pgsql" , `
204- create procedure [code].test() as $$
205- begin
206- perform 1;
207- end;
208- $$ language plpgsql;
209- ` )
210- result , err := Preprocess (doc , "abc123" , & stdlib.Driver {})
211- require .NoError (t , err )
212- require .Len (t , result .Batches , 1 )
213-
214- assert .Contains (t , result .Batches [0 ].Lines , `"code@abc123".` )
215- assert .NotContains (t , result .Batches [0 ].Lines , "[code@abc123]." )
216- })
217-
218203 t .Run ("replaces enum constants" , func (t * testing.T ) {
219- doc := sqlparser . ParseString ("test.sql" , `
204+ doc := ParseString (t , "test.sql" , `
220205declare @EnumStatus int = 42;
221206go
222207create procedure [code].Test as
234219 })
235220
236221 t .Run ("handles multiline string constants" , func (t * testing.T ) {
237- doc := sqlparser . ParseString ("test.sql" , `
222+ doc := ParseString (t , "test.sql" , `
238223declare @EnumMulti nvarchar(max) = N'line1
239224line2
240225line3';
256241 })
257242
258243 t .Run ("error on undeclared constant" , func (t * testing.T ) {
259- doc := sqlparser . ParseString ("test.sql" , `
244+ doc := ParseString (t , "test.sql" , `
260245create procedure [code].Test as
261246begin
262247 select @EnumUndeclared
272257 })
273258
274259 t .Run ("error on schema suffix with bracket" , func (t * testing.T ) {
275- doc := sqlparser . ParseString ("test.sql" , `
260+ doc := ParseString (t , "test.sql" , `
276261create procedure [code].Test as begin end
277262` )
278263 _ , err := Preprocess (doc , "abc]123" , & mssql.Driver {})
@@ -281,7 +266,7 @@ create procedure [code].Test as begin end
281266 })
282267
283268 t .Run ("handles multiple creates" , func (t * testing.T ) {
284- doc := sqlparser . ParseString ("test.sql" , `
269+ doc := ParseString (t , "test.sql" , `
285270create procedure [code].Proc1 as begin select 1 end
286271go
287272create procedure [code].Proc2 as begin select 2 end
@@ -295,7 +280,7 @@ create procedure [code].Proc2 as begin select 2 end
295280 })
296281
297282 t .Run ("handles multiple constants in same procedure" , func (t * testing.T ) {
298- doc := sqlparser . ParseString ("test.sql" , `
283+ doc := ParseString (t , "test.sql" , `
299284declare @EnumA int = 1, @EnumB int = 2;
300285go
301286create procedure [code].Test as
313298 })
314299
315300 t .Run ("preserves comments and formatting" , func (t * testing.T ) {
316- doc := sqlparser . ParseString ("test.sql" , `
301+ doc := ParseString (t , "test.sql" , `
317302-- This is a test procedure
318303create procedure [code].Test as
319304begin
333318 })
334319
335320 t .Run ("handles const and global prefixes" , func (t * testing.T ) {
336- doc := sqlparser . ParseString ("test.sql" , `
321+ doc := ParseString (t , "test.sql" , `
337322declare @ConstValue int = 100;
338323declare @GlobalSetting nvarchar(50) = N'test';
339324go
@@ -385,7 +370,7 @@ func TestPreprocessString(t *testing.T) {
385370func TestPreprocessorError (t * testing.T ) {
386371 t .Run ("formats error message" , func (t * testing.T ) {
387372 err := PreprocessorError {
388- Pos : sqlparser .Pos {File : "test.sql" , Line : 10 , Col : 5 },
373+ Pos : sqldocument .Pos {File : "test.sql" , Line : 10 , Col : 5 },
389374 Message : "something went wrong" ,
390375 }
391376
0 commit comments