vitess.io/vitess@v0.16.2/go/vt/schemamanager/ui_controller_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schemamanager
    18  
    19  import (
    20  	"fmt"
    21  	"net/http/httptest"
    22  	"strings"
    23  	"testing"
    24  
    25  	"context"
    26  )
    27  
    28  func TestUIController(t *testing.T) {
    29  	sql := "CREATE TABLE test_table (pk int)"
    30  	response := httptest.NewRecorder()
    31  	controller := NewUIController(sql, "test_keyspace", response)
    32  	ctx := context.Background()
    33  
    34  	err := controller.Open(ctx)
    35  	if err != nil {
    36  		t.Fatalf("controller.Open should succeed, but got error: %v", err)
    37  	}
    38  
    39  	keyspace := controller.Keyspace()
    40  	if keyspace != "test_keyspace" {
    41  		t.Fatalf("expect to get keyspace: 'test_keyspace', but got keyspace: '%s'", keyspace)
    42  	}
    43  
    44  	sqls, err := controller.Read(ctx)
    45  	if err != nil {
    46  		t.Fatalf("controller.Read should succeed, but got error: %v", err)
    47  	}
    48  	if len(sqls) != 1 {
    49  		t.Fatalf("controller should only get one sql, but got: %v", sqls)
    50  	}
    51  	if sqls[0] != sql {
    52  		t.Fatalf("expect to get sql: '%s', but got: '%s'", sql, sqls[0])
    53  	}
    54  	defer controller.Close()
    55  	err = controller.OnReadSuccess(ctx)
    56  	if err != nil {
    57  		t.Fatalf("OnDataSourcerReadSuccess should succeed")
    58  	}
    59  	if !strings.Contains(response.Body.String(), "OnReadSuccess, sqls") {
    60  		t.Fatalf("controller.OnReadSuccess should write to http response")
    61  	}
    62  	errReadFail := fmt.Errorf("read fail")
    63  	err = controller.OnReadFail(ctx, errReadFail)
    64  	if err != errReadFail {
    65  		t.Fatalf("should get error:%v, but get: %v", errReadFail, err)
    66  	}
    67  
    68  	if !strings.Contains(response.Body.String(), "OnReadFail, error") {
    69  		t.Fatalf("controller.OnReadFail should write to http response")
    70  	}
    71  
    72  	err = controller.OnValidationSuccess(ctx)
    73  	if err != nil {
    74  		t.Fatalf("OnValidationSuccess should succeed")
    75  	}
    76  
    77  	if !strings.Contains(response.Body.String(), "OnValidationSuccess, sqls") {
    78  		t.Fatalf("controller.OnValidationSuccess should write to http response")
    79  	}
    80  
    81  	errValidationFail := fmt.Errorf("validation fail")
    82  	err = controller.OnValidationFail(ctx, errValidationFail)
    83  	if err != errValidationFail {
    84  		t.Fatalf("should get error:%v, but get: %v", errValidationFail, err)
    85  	}
    86  
    87  	if !strings.Contains(response.Body.String(), "OnValidationFail, error") {
    88  		t.Fatalf("controller.OnValidationFail should write to http response")
    89  	}
    90  
    91  	err = controller.OnExecutorComplete(ctx, &ExecuteResult{})
    92  	if err != nil {
    93  		t.Fatalf("OnExecutorComplete should succeed")
    94  	}
    95  
    96  	if !strings.Contains(response.Body.String(), "Executor succeeds") {
    97  		t.Fatalf("controller.OnExecutorComplete should write to http response")
    98  	}
    99  }