TestCompletion tests the Transact-SQL auto-completion, all the test cases are stored in the file. - Description: The description of the test case. - Input: The input statement with the caret position marked by "|". - Want: The expected completion candidates. Our Test suite will determine the car
(t *testing.T)
| 34 | // Our Test suite will determine the caret position - line(0-based) and column(1-based) by the position of the "|", actually, |
| 35 | // this caret position is as same as the position in the monaco-editor(LSP?). |
| 36 | func TestCompletion(t *testing.T) { |
| 37 | tests := []candidatesTest{} |
| 38 | |
| 39 | const ( |
| 40 | record = false |
| 41 | ) |
| 42 | var ( |
| 43 | filepath = "test-data/test_completion.yaml" |
| 44 | ) |
| 45 | |
| 46 | a := require.New(t) |
| 47 | yamlFile, err := os.Open(filepath) |
| 48 | a.NoError(err) |
| 49 | |
| 50 | byteValue, err := io.ReadAll(yamlFile) |
| 51 | a.NoError(yamlFile.Close()) |
| 52 | a.NoError(err) |
| 53 | a.NoError(yaml.Unmarshal(byteValue, &tests)) |
| 54 | |
| 55 | for i, t := range tests { |
| 56 | statement, caretLine, caretPosition := getCaretPosition(t.Input) |
| 57 | getter, lister := buildMockDatabaseMetadataGetterLister() |
| 58 | results, err := Completion(context.Background(), base.CompletionContext{ |
| 59 | Scene: base.SceneTypeAll, |
| 60 | DefaultDatabase: "xx-ap-east-1", |
| 61 | Metadata: getter, |
| 62 | ListDatabaseNames: lister, |
| 63 | }, statement, caretLine, caretPosition) |
| 64 | a.NoErrorf(err, "Case %02d: %s", i, t.Description) |
| 65 | var filteredResult []base.Candidate |
| 66 | for _, r := range results { |
| 67 | switch r.Type { |
| 68 | case base.CandidateTypeKeyword, base.CandidateTypeFunction: |
| 69 | continue |
| 70 | default: |
| 71 | filteredResult = append(filteredResult, r) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if record { |
| 76 | tests[i].Want = filteredResult |
| 77 | } else { |
| 78 | a.Equalf(t.Want, filteredResult, "Case %02d: %s", i, t.Input) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if record { |
| 83 | yamltest.Record(t, filepath, tests) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func getCaretPosition(statement string) (string, int, int) { |
| 88 | lines := strings.Split(statement, "\n") |
nothing calls this directly
no test coverage detected