2022-09-20 14:32:09 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-30 16:01:17 +00:00
|
|
|
orgMetadataQuery = `SELECT projections.org_metadata2.creation_date,` +
|
|
|
|
` projections.org_metadata2.change_date,` +
|
|
|
|
` projections.org_metadata2.resource_owner,` +
|
|
|
|
` projections.org_metadata2.sequence,` +
|
|
|
|
` projections.org_metadata2.key,` +
|
|
|
|
` projections.org_metadata2.value` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` FROM projections.org_metadata2` +
|
|
|
|
` AS OF SYSTEM TIME '-1 ms'`
|
2022-09-20 14:32:09 +00:00
|
|
|
orgMetadataCols = []string{
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"resource_owner",
|
|
|
|
"sequence",
|
|
|
|
"key",
|
|
|
|
"value",
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
orgMetadataListQuery = `SELECT projections.org_metadata2.creation_date,` +
|
|
|
|
` projections.org_metadata2.change_date,` +
|
|
|
|
` projections.org_metadata2.resource_owner,` +
|
|
|
|
` projections.org_metadata2.sequence,` +
|
|
|
|
` projections.org_metadata2.key,` +
|
|
|
|
` projections.org_metadata2.value,` +
|
2022-09-20 14:32:09 +00:00
|
|
|
` COUNT(*) OVER ()` +
|
2023-02-27 21:36:43 +00:00
|
|
|
` FROM projections.org_metadata2` +
|
|
|
|
` AS OF SYSTEM TIME '-1 ms'`
|
2022-09-20 14:32:09 +00:00
|
|
|
orgMetadataListCols = []string{
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"resource_owner",
|
|
|
|
"sequence",
|
|
|
|
"key",
|
|
|
|
"value",
|
|
|
|
"count",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_OrgMetadataPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataQuery no result",
|
|
|
|
prepare: prepareOrgMetadataQuery,
|
|
|
|
want: want{
|
2023-08-22 12:49:02 +00:00
|
|
|
sqlExpectations: mockQueryScanErr(
|
2022-09-20 14:32:09 +00:00
|
|
|
regexp.QuoteMeta(orgMetadataQuery),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errs.IsNotFound(err) {
|
|
|
|
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: (*OrgMetadata)(nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataQuery found",
|
|
|
|
prepare: prepareOrgMetadataQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQuery(
|
|
|
|
regexp.QuoteMeta(orgMetadataQuery),
|
|
|
|
orgMetadataCols,
|
|
|
|
[]driver.Value{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
"resource_owner",
|
|
|
|
uint64(20211108),
|
|
|
|
"key",
|
|
|
|
[]byte("value"),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &OrgMetadata{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
ResourceOwner: "resource_owner",
|
|
|
|
Sequence: 20211108,
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataQuery sql err",
|
|
|
|
prepare: prepareOrgMetadataQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
regexp.QuoteMeta(orgMetadataQuery),
|
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 12:49:02 +00:00
|
|
|
object: (*OrgMetadata)(nil),
|
2022-09-20 14:32:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataListQuery no result",
|
|
|
|
prepare: prepareOrgMetadataListQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(orgMetadataListQuery),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errs.IsNotFound(err) {
|
|
|
|
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: &OrgMetadataList{Metadata: []*OrgMetadata{}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataListQuery one result",
|
|
|
|
prepare: prepareOrgMetadataListQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(orgMetadataListQuery),
|
|
|
|
orgMetadataListCols,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
"resource_owner",
|
|
|
|
uint64(20211108),
|
|
|
|
"key",
|
|
|
|
[]byte("value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &OrgMetadataList{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 1,
|
|
|
|
},
|
|
|
|
Metadata: []*OrgMetadata{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
ResourceOwner: "resource_owner",
|
|
|
|
Sequence: 20211108,
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataListQuery multiple results",
|
|
|
|
prepare: prepareOrgMetadataListQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
regexp.QuoteMeta(orgMetadataListQuery),
|
|
|
|
orgMetadataListCols,
|
|
|
|
[][]driver.Value{
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
"resource_owner",
|
|
|
|
uint64(20211108),
|
|
|
|
"key",
|
|
|
|
[]byte("value"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
"resource_owner",
|
|
|
|
uint64(20211108),
|
|
|
|
"key2",
|
|
|
|
[]byte("value2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &OrgMetadataList{
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: 2,
|
|
|
|
},
|
|
|
|
Metadata: []*OrgMetadata{
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
ResourceOwner: "resource_owner",
|
|
|
|
Sequence: 20211108,
|
|
|
|
Key: "key",
|
|
|
|
Value: []byte("value"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
ResourceOwner: "resource_owner",
|
|
|
|
Sequence: 20211108,
|
|
|
|
Key: "key2",
|
|
|
|
Value: []byte("value2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareOrgMetadataListQuery sql err",
|
|
|
|
prepare: prepareOrgMetadataListQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
regexp.QuoteMeta(orgMetadataListQuery),
|
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 12:49:02 +00:00
|
|
|
object: (*OrgMetadataList)(nil),
|
2022-09-20 14:32:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-27 21:36:43 +00:00
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
2022-09-20 14:32:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|