mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zitadel/zitadel/internal/database/mock"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
func TestQueryJSONObject(t *testing.T) {
|
|
type dst struct {
|
|
A int `json:"a,omitempty"`
|
|
}
|
|
const (
|
|
query = `select $1;`
|
|
arg = 1
|
|
)
|
|
|
|
tests := []struct {
|
|
name string
|
|
mock func(*testing.T) *mock.SQLMock
|
|
want *dst
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "tx error",
|
|
mock: func(t *testing.T) *mock.SQLMock {
|
|
return mock.NewSQLMock(t, mock.ExpectBegin(sql.ErrConnDone))
|
|
},
|
|
wantErr: zerrors.ThrowInternal(sql.ErrConnDone, "DATAB-Oath6", "Errors.Internal"),
|
|
},
|
|
{
|
|
name: "no rows",
|
|
mock: func(t *testing.T) *mock.SQLMock {
|
|
return mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(query,
|
|
mock.WithQueryArgs(arg),
|
|
mock.WithQueryResult([]string{"json"}, [][]driver.Value{}),
|
|
),
|
|
)
|
|
},
|
|
wantErr: sql.ErrNoRows,
|
|
},
|
|
{
|
|
name: "unmarshal error",
|
|
mock: func(t *testing.T) *mock.SQLMock {
|
|
return mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(query,
|
|
mock.WithQueryArgs(arg),
|
|
mock.WithQueryResult([]string{"json"}, [][]driver.Value{{`~~~`}}),
|
|
),
|
|
mock.ExpectCommit(nil),
|
|
)
|
|
},
|
|
wantErr: zerrors.ThrowInternal(nil, "DATAB-Vohs6", "Errors.Internal"),
|
|
},
|
|
{
|
|
name: "success",
|
|
mock: func(t *testing.T) *mock.SQLMock {
|
|
return mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(query,
|
|
mock.WithQueryArgs(arg),
|
|
mock.WithQueryResult([]string{"json"}, [][]driver.Value{{`{"a":1}`}}),
|
|
),
|
|
mock.ExpectCommit(nil),
|
|
)
|
|
},
|
|
want: &dst{A: 1},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mock := tt.mock(t)
|
|
defer mock.Assert(t)
|
|
db := &DB{
|
|
DB: mock.DB,
|
|
}
|
|
got, err := QueryJSONObject[dst](context.Background(), db, query, arg)
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|