github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/postsql/initialization_test.go (about)

     1  package postsql_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/storage/postsql"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  const (
    15  	maxDbAccessRetries = 20
    16  )
    17  
    18  func TestInitialization(t *testing.T) {
    19  
    20  	ctx := context.Background()
    21  
    22  	t.Run("Should initialize database when schema not applied", func(t *testing.T) {
    23  		containerCleanupFunc, cfg, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_1")
    24  		require.NoError(t, err)
    25  		defer containerCleanupFunc()
    26  
    27  		// when
    28  		connection, err := postsql.InitializeDatabase(cfg.ConnectionURL(), maxDbAccessRetries, logrus.New())
    29  		require.NoError(t, err)
    30  		require.NotNil(t, connection)
    31  
    32  		defer storage.CloseDatabase(t, connection)
    33  
    34  		// then
    35  		assert.NoError(t, err)
    36  	})
    37  
    38  	t.Run("Should return error when failed to connect to the database", func(t *testing.T) {
    39  		containerCleanupFunc, _, err := storage.InitTestDBContainer(t.Logf, ctx, "test_DB_3")
    40  		require.NoError(t, err)
    41  		defer containerCleanupFunc()
    42  
    43  		// given
    44  		connString := "bad connection string"
    45  
    46  		// when
    47  		connection, err := postsql.InitializeDatabase(connString, maxDbAccessRetries, logrus.New())
    48  
    49  		// then
    50  		assert.Error(t, err)
    51  		assert.Nil(t, connection)
    52  	})
    53  }