golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/relui/store_test.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package relui
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"testing"
    11  )
    12  
    13  func TestCreateDBIfNotExists(t *testing.T) {
    14  	ctx, cancel := context.WithCancel(context.Background())
    15  	defer cancel()
    16  	db := testDB(ctx, t)
    17  
    18  	testCfg := db.Config().ConnConfig.Copy()
    19  	testCfg.Database = "relui-test-nonexistent"
    20  	if err := DropDB(ctx, testCfg); err != nil && !errors.Is(err, errDBNotExist) {
    21  		t.Fatalf("p.DropDB() = %v, wanted %q or nil", err, errDBNotExist)
    22  	}
    23  	exists, err := checkIfDBExists(ctx, testCfg)
    24  	if exists || err != nil {
    25  		t.Fatalf("p.checkIfDBExists() = %t, %v, wanted %t, nil", exists, err, false)
    26  	}
    27  	if err := CreateDBIfNotExists(ctx, testCfg); err != nil {
    28  		t.Errorf("p.CreateDBIfNotExists() = %v, wanted no error", err)
    29  	}
    30  	exists, err = checkIfDBExists(ctx, testCfg)
    31  	if !exists || err != nil {
    32  		t.Fatalf("p.checkIfDBExists() = %t, %v, wanted %t, nil", exists, err, true)
    33  	}
    34  	defer DropDB(ctx, testCfg)
    35  	// Create again with the same name.
    36  	if err := CreateDBIfNotExists(ctx, testCfg); err != nil {
    37  		t.Errorf("p.CreateDBIfNotExists() = %v, wanted no error", err)
    38  	}
    39  	exists, err = checkIfDBExists(ctx, testCfg)
    40  	if !exists || err != nil {
    41  		t.Fatalf("p.checkIfDBExists() = %t, %v, wanted %t, nil", exists, err, true)
    42  	}
    43  }