|
| 1 | +package completion |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + bq "cloud.google.com/go/bigquery" |
| 9 | + "github.com/golang/mock/gomock" |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/kitagry/bqls/langserver/internal/bigquery/mock_bigquery" |
| 12 | + "github.com/kitagry/bqls/langserver/internal/lsp" |
| 13 | + "github.com/kitagry/bqls/langserver/internal/source/file" |
| 14 | + "github.com/kitagry/bqls/langserver/internal/source/helper" |
| 15 | + "github.com/sirupsen/logrus" |
| 16 | +) |
| 17 | + |
| 18 | +func TestProject_CompleteDeclaration(t *testing.T) { |
| 19 | + tests := map[string]struct { |
| 20 | + files map[string]string |
| 21 | + bqTableMetadataMap map[string]*bq.TableMetadata |
| 22 | + |
| 23 | + expectCompletionItems []CompletionItem |
| 24 | + }{ |
| 25 | + "Complete variable declaration": { |
| 26 | + files: map[string]string{ |
| 27 | + "file1.sql": "DECLARE a INT64;\n" + "SELECT | FROM `project.dataset.table`", |
| 28 | + }, |
| 29 | + bqTableMetadataMap: map[string]*bq.TableMetadata{ |
| 30 | + "project.dataset.table": { |
| 31 | + Schema: bq.Schema{ |
| 32 | + { |
| 33 | + Name: "id", |
| 34 | + Type: bq.IntegerFieldType, |
| 35 | + Description: "id description", |
| 36 | + }, |
| 37 | + { |
| 38 | + Name: "name", |
| 39 | + Type: bq.StringFieldType, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + expectCompletionItems: []CompletionItem{ |
| 45 | + { |
| 46 | + Kind: lsp.CIKVariable, |
| 47 | + NewText: "a", |
| 48 | + Documentation: lsp.MarkupContent{ |
| 49 | + Kind: lsp.MKMarkdown, |
| 50 | + Value: "```sql\nDECLARE a INT64\n```", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for n, tt := range tests { |
| 58 | + t.Run(n, func(t *testing.T) { |
| 59 | + ctrl := gomock.NewController(t) |
| 60 | + bqClient := mock_bigquery.NewMockClient(ctrl) |
| 61 | + for tablePath, schema := range tt.bqTableMetadataMap { |
| 62 | + tablePathSplitted := strings.Split(tablePath, ".") |
| 63 | + if len(tablePathSplitted) != 3 { |
| 64 | + t.Fatalf("table path length should be 3, got %s", tablePath) |
| 65 | + } |
| 66 | + bqClient.EXPECT().GetTableMetadata(gomock.Any(), tablePathSplitted[0], tablePathSplitted[1], tablePathSplitted[2]).Return(schema, nil).MinTimes(0) |
| 67 | + } |
| 68 | + bqClient.EXPECT().ListTables(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).MinTimes(0) |
| 69 | + logger := logrus.New() |
| 70 | + logger.SetLevel(logrus.DebugLevel) |
| 71 | + |
| 72 | + analyzer := file.NewAnalyzer(logger, bqClient) |
| 73 | + completor := New(logger, analyzer, bqClient) |
| 74 | + |
| 75 | + files, path, position, err := helper.GetLspPosition(tt.files) |
| 76 | + if err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + |
| 80 | + parsedFile := analyzer.ParseFile(path, files[path]) |
| 81 | + |
| 82 | + got := completor.completeDeclaration(context.Background(), parsedFile, position) |
| 83 | + if diff := cmp.Diff(got, tt.expectCompletionItems); diff != "" { |
| 84 | + t.Errorf("(-got, +want)\n%s", diff) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
0 commit comments