github.com/blend/go-sdk@v1.20220411.3/db/connection_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package db
     9  
    10  import (
    11  	"context"
    12  	"database/sql"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/ex"
    17  )
    18  
    19  // Test_Connection_useBeforeOpen tests if we can connect to the db, a.k.a., if the underlying driver works.
    20  func Test_Connection_useBeforeOpen(t *testing.T) {
    21  	t.Parallel()
    22  	its := assert.New(t)
    23  
    24  	conn, err := New()
    25  	its.Nil(err)
    26  
    27  	tx, err := conn.Begin()
    28  	its.NotNil(err)
    29  	its.True(ex.Is(ErrConnectionClosed, err))
    30  	its.Nil(tx)
    31  
    32  	inv := conn.Invoke()
    33  	its.Nil(inv.DB)
    34  	its.True(inv.DB == nil)
    35  
    36  	any, err := conn.Query("select 1").Any()
    37  	its.NotNil(err)
    38  	its.True(ex.Is(ErrConnectionClosed, err), err.Error())
    39  	its.False(any)
    40  }
    41  
    42  // TestConnectionSanityCheck tests if we can connect to the db, a.k.a., if the underlying driver works.
    43  func TestConnectionSanityCheck(t *testing.T) {
    44  	assert := assert.New(t)
    45  
    46  	conn, err := OpenTestConnection()
    47  	assert.Nil(err)
    48  	str := conn.Config.CreateDSN()
    49  	_, err = sql.Open("pgx", str)
    50  	assert.Nil(err)
    51  }
    52  
    53  func TestPrepareContext(t *testing.T) {
    54  	a := assert.New(t)
    55  
    56  	conn, err := OpenTestConnection()
    57  	a.Nil(err)
    58  
    59  	var calledPrepare, calledFinish bool
    60  	conn.Tracer = mockTracer{
    61  		PrepareHandler: func(_ context.Context, _ Config, _ string) {
    62  			calledPrepare = true
    63  		},
    64  		FinishPrepareHandler: func(_ context.Context, _ error) {
    65  			calledFinish = true
    66  		},
    67  	}
    68  
    69  	stmt, err := conn.PrepareContext(context.TODO(), "select 'ok!'", nil)
    70  	a.Nil(err)
    71  	defer stmt.Close()
    72  	a.NotNil(stmt)
    73  	a.True(calledPrepare)
    74  	a.True(calledFinish)
    75  }
    76  
    77  func TestQuery(t *testing.T) {
    78  	a := assert.New(t)
    79  	tx, err := defaultDB().Begin()
    80  	a.Nil(err)
    81  	defer func() { _ = tx.Rollback() }()
    82  
    83  	a.Equal(DefaultSchema, defaultDB().Config.SchemaOrDefault())
    84  	err = seedObjects(100, tx)
    85  	a.Nil(err)
    86  
    87  	objs := []benchObj{}
    88  	err = defaultDB().Invoke(OptTx(tx)).Query("select * from bench_object").OutMany(&objs)
    89  	a.Nil(err)
    90  	a.NotEmpty(objs)
    91  
    92  	err = defaultDB().Invoke(OptTx(tx), OptInvocationStatementInterceptor(failInterceptor)).Query("select * from bench_object").OutMany(&objs)
    93  	a.Equal("this is just an interceptor error", err.Error())
    94  }
    95  
    96  func Test_Connection_Open(t *testing.T) {
    97  	a := assert.New(t)
    98  
    99  	conn, err := New(OptConfigFromEnv())
   100  	a.Nil(err)
   101  	a.Nil(conn.Open())
   102  	defer conn.Close()
   103  
   104  	a.NotNil(conn.BufferPool)
   105  	a.NotNil(conn.Connection)
   106  }
   107  
   108  func TestExec(t *testing.T) {
   109  	a := assert.New(t)
   110  	tx, err := defaultDB().Begin()
   111  	a.Nil(err)
   112  	defer func() { _ = tx.Rollback() }()
   113  
   114  	err = IgnoreExecResult(defaultDB().Invoke(OptTx(tx)).Exec("select 'ok!'"))
   115  	a.Nil(err)
   116  
   117  	err = IgnoreExecResult(defaultDB().Invoke(OptTx(tx), OptInvocationStatementInterceptor(failInterceptor)).Exec("select 'ok!'"))
   118  	a.Equal("this is just an interceptor error", err.Error())
   119  }
   120  
   121  // TestConnectionConfigSetsDatabase tests if we set the .database property on open.
   122  func TestConnectionConfigSetsDatabase(t *testing.T) {
   123  	assert := assert.New(t)
   124  	conn, err := New(OptConfigFromEnv())
   125  	assert.Nil(err)
   126  	assert.Nil(conn.Open())
   127  	defer conn.Close()
   128  	assert.NotEmpty(conn.Config.DatabaseOrDefault())
   129  }