mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-22 07:47:29 +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>
113 lines
2.3 KiB
Go
113 lines
2.3 KiB
Go
package query
|
|
|
|
import (
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
|
|
"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,
|
|
intervalDriverValue(t, time.Hour*24),
|
|
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...)
|
|
})
|
|
}
|
|
}
|