package sql import ( "errors" "testing" "github.com/jackc/pgconn" "github.com/lib/pq" "github.com/stretchr/testify/assert" ) const ( msg = "invalid type for uuid" code = "08000" verbose = "long verbose sql jagon" severity = "FATAL" ) func TestSetMessage(t *testing.T) { sqlerr := New(msg) assert.NotNil(t, sqlerr) assert.Equal(t, msg, sqlerr.Message) } func TestSetErrorCode(t *testing.T) { sqlerr := New("").SetErrorCode(code) assert.NotNil(t, sqlerr) assert.Equal(t, pq.ErrorCode(code), sqlerr.ErrorCode) } func TestSetErrorType(t *testing.T) { _type := EdgeSQLErrType sqlerr := New("").SetErrorType(_type) assert.NotNil(t, sqlerr) assert.Equal(t, _type, sqlerr.ErrorType) } func TestSetVerbose(t *testing.T) { sqlerr := New("").SetVerbose(verbose) assert.NotNil(t, sqlerr) assert.Equal(t, verbose, sqlerr.Verbose) } func TestSetSeverity(t *testing.T) { sqlerr := New("").SetSeverity(pq.Efatal) assert.NotNil(t, sqlerr) assert.Equal(t, pq.Efatal, sqlerr.Severity) } func TestError(t *testing.T) { sqlerr := New("").SetMessage(msg) assert.NotNil(t, sqlerr) assert.Equal(t, msg, sqlerr.Error()) } func TestIsError(t *testing.T) { err := errors.New(msg) sqlerr := Wrap(err) assert.NotNil(t, sqlerr) assert.True(t, errors.Is(sqlerr, err)) } func TestUnmarshalError(t *testing.T) { pgErr := &pgconn.PgError{ Severity: pq.Efatal, Code: "22P02", Message: msg, } castedPgErr := error(pgErr) sqlerr := New("").unmarshalError(castedPgErr) assert.Equal(t, pq.ErrorCode(pgErr.Code), sqlerr.ErrorCode) assert.Equal(t, EdgeSQLErrType, sqlerr.ErrorType) assert.Equal(t, pq.Efatal, sqlerr.Severity) } func TestExtensions(t *testing.T) { expectedExts := map[string]interface{}{ "severity": severity, "statusCode": code, "errorType": EdgeSQLErrType, } sqlerr := New("").SetMessage(msg).SetSeverity(pq.Efatal).SetErrorCode(code) actualExts := sqlerr.Extensions() assert.Equal(t, expectedExts["severity"], actualExts["severity"]) assert.Equal(t, pq.ErrorCode(expectedExts["statusCode"].(string)), actualExts["statusCode"]) assert.Equal(t, expectedExts["errorType"].(string), actualExts["errorType"]) }