github.com/supabase/cli@v1.168.1/internal/utils/parser/token_test.go (about) 1 package parser 2 3 import ( 4 "bufio" 5 _ "embed" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestSplit(t *testing.T) { 17 const testdata = "testdata" 18 19 var fixture []string 20 require.NoError(t, filepath.WalkDir(testdata, func(path string, f fs.DirEntry, err error) error { 21 if err != nil { 22 return err 23 } 24 if !strings.HasPrefix(f.Name(), "split_") { 25 return nil 26 } 27 contents, err := os.ReadFile(path) 28 if err != nil { 29 return err 30 } 31 fixture = append(fixture, string(contents)) 32 return nil 33 })) 34 require.Len(t, fixture, 18) 35 fixture = append(fixture, "\n") 36 37 sql, err := os.Open(filepath.Join(testdata, "all.sql")) 38 require.NoError(t, err) 39 stats, err := Split(sql) 40 require.NoError(t, err) 41 42 assert.ElementsMatch(t, fixture, stats) 43 } 44 45 func TestSplitAndTrim(t *testing.T) { 46 sql := "\tBEGIN; " + strings.Repeat("a", MaxScannerCapacity) 47 stats, err := SplitAndTrim(strings.NewReader(sql)) 48 // Check error 49 assert.ErrorIs(t, err, bufio.ErrTooLong) 50 assert.ErrorContains(t, err, "After statement 1: \tBEGIN;") 51 assert.ElementsMatch(t, []string{"BEGIN"}, stats) 52 }