Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sql_test
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gofrs/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/ory/keto/internal/driver"
|
|
"github.com/ory/keto/internal/persistence/sql"
|
|
"github.com/ory/keto/internal/x/dbx"
|
|
)
|
|
|
|
func assertCheckErr(t assert.TestingT, err error, msgAndArgs ...interface{}) bool {
|
|
t.(*testing.T).Helper()
|
|
if err == nil {
|
|
return assert.Fail(t, "Did not receive an error", msgAndArgs...)
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "keto_uuid_mappings") || // <- normal databases
|
|
strings.Contains(err.Error(), "string_representation") || // <- sqlite
|
|
strings.Contains(err.Error(), "SQLSTATE 23505") || // <- cockroach
|
|
strings.Contains(err.Error(), "SQLSTATE 23514") { // <- mysql
|
|
return true
|
|
}
|
|
return assert.Fail(t, fmt.Sprintf("Did not receive check error, got:\n%+v", err), msgAndArgs...)
|
|
}
|
|
|
|
func TestUUIDMapping(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, dsn := range dbx.GetDSNs(t) {
|
|
t.Run("dsn="+dsn.Name, func(t *testing.T) {
|
|
t.Parallel()
|
|
reg := driver.NewTestRegistry(t, dsn)
|
|
c, err := reg.PopConnection(t.Context())
|
|
require.NoError(t, err)
|
|
|
|
testUUID := uuid.Must(uuid.NewV4())
|
|
|
|
for _, tc := range []struct {
|
|
desc string
|
|
mappings interface{}
|
|
assertErr assert.ErrorAssertionFunc
|
|
}{{
|
|
desc: "empty should not fail on constraint",
|
|
mappings: &sql.UUIDMapping{},
|
|
assertErr: assert.NoError,
|
|
}, {
|
|
desc: "empty strings should not fail on constraint",
|
|
mappings: &sql.UUIDMapping{ID: uuid.Nil},
|
|
assertErr: assert.NoError,
|
|
}, {
|
|
desc: "single with string rep should succeed",
|
|
mappings: &sql.UUIDMapping{StringRepresentation: "foo"},
|
|
assertErr: assert.NoError,
|
|
}, {
|
|
desc: "two with same uuid should fail on constraint",
|
|
mappings: &[]sql.UUIDMapping{
|
|
{ID: testUUID, StringRepresentation: "foo"},
|
|
{ID: testUUID, StringRepresentation: "bar"},
|
|
},
|
|
assertErr: assertCheckErr,
|
|
}, {
|
|
desc: "two with same rep should succeed",
|
|
mappings: &[]sql.UUIDMapping{
|
|
{ID: uuid.Must(uuid.NewV4()), StringRepresentation: "bar"},
|
|
{ID: uuid.Must(uuid.NewV4()), StringRepresentation: "bar"},
|
|
},
|
|
assertErr: assert.NoError,
|
|
}} {
|
|
tc := tc
|
|
t.Run("case="+tc.desc, func(t *testing.T) {
|
|
t.Parallel()
|
|
err := c.Create(tc.mappings)
|
|
tc.assertErr(t, err)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|