|
1 | 1 | package sqlparser |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/stretchr/testify/assert" |
5 | | - "github.com/stretchr/testify/require" |
| 4 | + "fmt" |
6 | 5 | "strings" |
7 | 6 | "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | func TestParserSmokeTest(t *testing.T) { |
|
44 | 46 |
|
45 | 47 | assert.Equal(t, "[TestFunc]", c.QuotedName.Value) |
46 | 48 | assert.Equal(t, []string{"[HelloFunc]", "[OtherFunc]"}, c.DependsOnStrings()) |
47 | | - assert.Equal(t, `-- preceding comment 1 |
| 49 | + assert.Equal(t, fmt.Sprintf(`-- preceding comment 1 |
48 | 50 | /* preceding comment 2 |
49 | 51 |
|
50 | | -asdfasdf */create procedure [code].TestFunc as begin |
| 52 | +asdfasdf */create procedure [code].TestFunc as %sbegin |
51 | 53 | refers to [code].OtherFunc [code].HelloFunc; |
52 | 54 | create table x ( int x not null ); -- should be ok |
53 | 55 | end; |
54 | 56 |
|
55 | 57 | /* trailing comment */ |
56 | | -`, c.String()) |
| 58 | +`, fmt.Sprintf(templateRoutineName, "TestFunc")), c.String()) |
57 | 59 |
|
58 | 60 | assert.Equal(t, |
59 | 61 | []Error{ |
@@ -271,6 +273,43 @@ create procedure [code].FirstProc as table (x int) |
271 | 273 | assert.Equal(t, emsg, doc.Errors[0].Message) |
272 | 274 | } |
273 | 275 |
|
| 276 | +func TestCreateProcsAndCheckForRoutineName(t *testing.T) { |
| 277 | + testcases := []struct { |
| 278 | + name string |
| 279 | + doc Document |
| 280 | + expectedProcName string |
| 281 | + expectedIndex int |
| 282 | + }{ |
| 283 | + { |
| 284 | + name: "Test simple proc", |
| 285 | + expectedProcName: "FirstProc", |
| 286 | + doc: ParseString("test.sql", ` |
| 287 | +create procedure [code].FirstProc as |
| 288 | +begin |
| 289 | +end |
| 290 | +`), |
| 291 | + expectedIndex: 10, |
| 292 | + }, |
| 293 | + { |
| 294 | + name: "Test proc with args", |
| 295 | + expectedProcName: "transform:safeguarding.Calculation/HEAD", |
| 296 | + doc: ParseString("test.sql", ` |
| 297 | +create procedure [code].[transform:safeguarding.Calculation/HEAD](@now datetime2, |
| 298 | +@count bigint output) as |
| 299 | +`), |
| 300 | + expectedIndex: 22, |
| 301 | + }, |
| 302 | + } |
| 303 | + for _, tc := range testcases { |
| 304 | + require.Equal(t, 0, len(tc.doc.Errors)) |
| 305 | + assert.Len(t, tc.doc.Creates, 1) |
| 306 | + assert.Greater(t, len(tc.doc.Creates[0].Body), tc.expectedIndex) |
| 307 | + assert.Equal(t, |
| 308 | + fmt.Sprintf(templateRoutineName, tc.expectedProcName), |
| 309 | + tc.doc.Creates[0].Body[tc.expectedIndex].RawValue, |
| 310 | + ) |
| 311 | + } |
| 312 | +} |
274 | 313 |
|
275 | 314 | func TestGoWithoutNewline(t *testing.T) { |
276 | 315 | doc := ParseString("test.sql", ` |
|
0 commit comments