go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/connection_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package db 9 10 import ( 11 "context" 12 "errors" 13 "testing" 14 15 . "go.charczuk.com/sdk/assert" 16 ) 17 18 // Test_Connection_useBeforeOpen tests if we can connect to the db, a.k.a., if the underlying driver works. 19 func Test_Connection_useBeforeOpen(t *testing.T) { 20 conn, err := New() 21 ItsNil(t, err) 22 23 tx, err := conn.BeginTx(context.Background()) 24 ItsNotNil(t, err) 25 ItsEqual(t, true, errors.Is(ErrConnectionClosed, err)) 26 ItsNil(t, tx) 27 28 inv := conn.Invoke() 29 ItsNil(t, inv.db) 30 ItsEqual(t, true, inv.db == nil) 31 32 any, err := conn.Query("select 1").Any() 33 ItsNotNil(t, err) 34 ItsEqual(t, true, errors.Is(ErrConnectionClosed, err), err.Error()) 35 ItsEqual(t, false, any) 36 } 37 38 func Test_Connection_Open(t *testing.T) { 39 conn, err := New(OptConfigFromEnv()) 40 ItsNil(t, err) 41 ItsNil(t, conn.Open()) 42 defer conn.Close() 43 44 ItsNotNil(t, conn.bp) 45 ItsNotNil(t, conn.conn) 46 ItsNotNil(t, conn.mc) 47 }