zitadel/internal/query/quota_test.go
Silvan 56df515e5f
chore: use pgx v5 (#7577)
* chore: use pgx v5

* chore: update go version

* remove direct pq dependency

* remove unnecessary type

* scan test

* map scanner

* converter

* uint8 number array

* duration

* most unit tests work

* unit tests work

* chore: coverage

* go 1.21

* linting

* int64 gopfertammi

* retry go 1.22

* retry go 1.22

* revert to go v1.21.5

* update go toolchain to 1.21.8

* go 1.21.8

* remove test flag

* go 1.21.5

* linting

* update toolchain

* use correct array

* use correct array

* add byte array

* correct value

* correct error message

* go 1.21 compatible
2024-03-27 15:48:22 +02:00

117 lines
2.3 KiB
Go

package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/zitadel/zitadel/internal/zerrors"
)
var (
expectedQuotaQuery = regexp.QuoteMeta(`SELECT projections.quotas.id,` +
` projections.quotas.from_anchor,` +
` projections.quotas.interval,` +
` projections.quotas.amount,` +
` projections.quotas.limit_usage,` +
` now()` +
` FROM projections.quotas`)
quotaCols = []string{
"id",
"from_anchor",
"interval",
"amount",
"limit_usage",
"now",
}
)
func Test_QuotaPrepare(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareQuotaQuery no result",
prepare: prepareQuotaQuery,
want: want{
sqlExpectations: mockQueriesScanErr(
expectedQuotaQuery,
nil,
nil,
),
err: func(err error) (error, bool) {
if !zerrors.IsNotFound(err) {
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
}
return nil, true
},
},
object: (*Quota)(nil),
},
{
name: "prepareQuotaQuery",
prepare: prepareQuotaQuery,
want: want{
sqlExpectations: mockQuery(
expectedQuotaQuery,
quotaCols,
[]driver.Value{
"quota-id",
dayNow,
&pgtype.Interval{
Days: 1,
},
uint64(1000),
true,
testNow,
},
),
},
object: &Quota{
ID: "quota-id",
From: dayNow,
ResetInterval: time.Hour * 24,
CurrentPeriodStart: dayNow,
Amount: 1000,
Limit: true,
},
},
{
name: "prepareQuotaQuery sql err",
prepare: prepareQuotaQuery,
want: want{
sqlExpectations: mockQueryErr(
expectedQuotaQuery,
sql.ErrConnDone,
),
err: func(err error) (error, bool) {
if !errors.Is(err, sql.ErrConnDone) {
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
}
return nil, true
},
},
object: (*Quota)(nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
})
}
}