github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/comment_on_table_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql_test
    12  
    13  import (
    14  	"context"
    15  	gosql "database/sql"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/sql/tests"
    19  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    20  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    21  	"github.com/cockroachdb/errors"
    22  )
    23  
    24  func TestCommentOnTable(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  
    27  	params, _ := tests.CreateTestServerParams()
    28  	s, db, _ := serverutils.StartServer(t, params)
    29  	defer s.Stopper().Stop(context.Background())
    30  
    31  	if _, err := db.Exec(`
    32  		CREATE DATABASE d;
    33  		SET DATABASE = d;
    34  		CREATE TABLE t (i INT );
    35  	`); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	testCases := []struct {
    40  		exec   string
    41  		query  string
    42  		expect gosql.NullString
    43  	}{
    44  		{
    45  			`COMMENT ON TABLE t IS 'foo'`,
    46  			`SELECT obj_description('t'::regclass)`,
    47  			gosql.NullString{String: `foo`, Valid: true},
    48  		},
    49  		{
    50  			`TRUNCATE t`,
    51  			`SELECT obj_description('t'::regclass)`,
    52  			gosql.NullString{String: `foo`, Valid: true},
    53  		},
    54  		{
    55  			`COMMENT ON TABLE t IS NULL`,
    56  			`SELECT obj_description('t'::regclass)`,
    57  			gosql.NullString{Valid: false},
    58  		},
    59  	}
    60  
    61  	for _, tc := range testCases {
    62  		if _, err := db.Exec(tc.exec); err != nil {
    63  			t.Fatal(err)
    64  		}
    65  
    66  		row := db.QueryRow(tc.query)
    67  		var comment gosql.NullString
    68  		if err := row.Scan(&comment); err != nil {
    69  			t.Fatal(err)
    70  		}
    71  		if tc.expect != comment {
    72  			t.Fatalf("expected comment %v, got %v", tc.expect, comment)
    73  		}
    74  	}
    75  }
    76  
    77  func TestCommentOnTableWhenDrop(t *testing.T) {
    78  	defer leaktest.AfterTest(t)()
    79  
    80  	params, _ := tests.CreateTestServerParams()
    81  	s, db, _ := serverutils.StartServer(t, params)
    82  	defer s.Stopper().Stop(context.Background())
    83  
    84  	if _, err := db.Exec(`
    85  		CREATE DATABASE d;
    86  		SET DATABASE = d;
    87  		CREATE TABLE t (i INT );
    88  	`); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	if _, err := db.Exec(`COMMENT ON TABLE t IS 'foo'`); err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	if _, err := db.Exec(`DROP TABLE t`); err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	row := db.QueryRow(`SELECT comment FROM system.comments LIMIT 1`)
   101  	var comment string
   102  	err := row.Scan(&comment)
   103  	if !errors.Is(err, gosql.ErrNoRows) {
   104  		if err != nil {
   105  			t.Fatal(err)
   106  		}
   107  
   108  		t.Fatal("dropped comment remain comment")
   109  	}
   110  }