Skip to content

Commit

Permalink
test: fix inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Feb 21, 2022
1 parent e815cb0 commit dab1b1d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
6 changes: 3 additions & 3 deletions internal/driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ func (r *RegistryDefault) MigrateDown(ctx context.Context) error {
if err != nil {
return err
}
return mb.Up(ctx)
return mb.Down(ctx, -1)
}

func (r *RegistryDefault) determineNetwork(ctx context.Context) (*networkx.Network, error) {
func (r *RegistryDefault) DetermineNetwork(ctx context.Context) (*networkx.Network, error) {
c, err := r.PopConnection(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -247,7 +247,7 @@ func (r *RegistryDefault) Init(ctx context.Context) (err error) {
return err
}

network, err := r.determineNetwork(ctx)
network, err := r.DetermineNetwork(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/driver/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ func NewTestRegistry(t *testing.T, dsn *dbx.DsnT) *RegistryDefault {
require.NoError(t, r.Init(ctx))

t.Cleanup(func() {
if dsn.MigrateDown {
if !dsn.MigrateDown {
t.Log("Skipping down migration")
return
}

t.Log("Migrating down")
require.NoError(t, r.MigrateDown(ctx))
})

Expand Down
12 changes: 8 additions & 4 deletions internal/persistence/sql/full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/gofrs/uuid"
"github.com/ory/x/networkx"

"github.com/ory/keto/internal/x/dbx"

Expand All @@ -24,8 +24,8 @@ func TestPersister(t *testing.T) {
setup := func(t *testing.T, dsn *dbx.DsnT) (p *sql.Persister, r *driver.RegistryDefault, hook *test.Hook) {
r = driver.NewTestRegistry(t, dsn)

p, err := sql.NewPersister(context.Background(), r, uuid.Must(uuid.NewV4()))
require.NoError(t, err)
p, ok := r.Persister().(*sql.Persister)
require.True(t, ok)

require.NoError(t, r.MigrateUp(context.Background()))

Expand Down Expand Up @@ -60,7 +60,11 @@ func TestPersister(t *testing.T) {
t.Run("relationtuple.IsolationTest", func(t *testing.T) {
var nspaces []*namespace.Namespace
p0, r, _ := setup(t, dsn)
p1, err := sql.NewPersister(context.Background(), r, uuid.Must(uuid.NewV4()))
n1 := networkx.NewNetwork()
conn, err := r.PopConnection()
require.NoError(t, err)
require.NoError(t, conn.Create(n1))
p1, err := sql.NewPersister(context.Background(), r, n1.ID)
require.NoError(t, err)

// same registry, but different persisters only differing in the network ID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO networks (id, created_at, updated_at) VALUES ('77fdc5e0-2260-49da-8aae-c36ba255d05b', '2015-01-01 00:00:00', '2015-01-01 00:00:00');
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ DELETE FROM keto_relation_tuples WHERE nid NOT IN (SELECT nid FROM networks);

CREATE TABLE keto_relation_tuples_with_fk
(
shard_id UUID NOT NULL,
nid UUID NOT NULL,
shard_id TEXT NOT NULL,
nid TEXT NOT NULL,
namespace_id INTEGER NOT NULL,
object VARCHAR(64) NOT NULL,
relation VARCHAR(64) NOT NULL,
Expand All @@ -17,7 +17,7 @@ CREATE TABLE keto_relation_tuples_with_fk

PRIMARY KEY (shard_id, nid),

CONSTRAINT keto_relation_tuples_nid_fk FOREIGN KEY (nid) REFERENCES networks (nid),
CONSTRAINT keto_relation_tuples_nid_fk FOREIGN KEY (nid) REFERENCES networks (id),

-- enforce to have exactly one of subject_id or subject_set
CONSTRAINT chk_keto_rt_subject_type CHECK
Expand All @@ -33,3 +33,27 @@ INSERT INTO keto_relation_tuples_with_fk (shard_id, nid, namespace_id, object, r
DROP TABLE keto_relation_tuples;

ALTER TABLE keto_relation_tuples_with_fk RENAME TO keto_relation_tuples;

CREATE INDEX keto_relation_tuples_subject_ids_idx ON keto_relation_tuples (nid,
namespace_id,
object,
relation,
subject_id) WHERE subject_set_namespace_id IS NULL AND subject_set_object IS NULL AND subject_set_relation IS NULL;

CREATE INDEX keto_relation_tuples_subject_sets_idx ON keto_relation_tuples (nid,
namespace_id,
object,
relation,
subject_set_namespace_id,
subject_set_object,
subject_set_relation) WHERE subject_id IS NULL;

CREATE INDEX keto_relation_tuples_full_idx ON keto_relation_tuples (nid,
namespace_id,
object,
relation,
subject_id,
subject_set_namespace_id,
subject_set_object,
subject_set_relation,
commit_time);
11 changes: 8 additions & 3 deletions internal/persistence/sql/relationtuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"
"time"

"github.com/ory/x/networkx"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,10 +18,10 @@ import (
"github.com/ory/keto/internal/x/dbx"
)

func rt(setSID, setNID, setO, setR bool) *sql.RelationTuple {
func rt(nw *networkx.Network, setSID, setNID, setO, setR bool) *sql.RelationTuple {
return &sql.RelationTuple{
ID: uuid.Must(uuid.NewV4()),
NetworkID: uuid.Must(uuid.NewV4()),
NetworkID: nw.ID,
SubjectID: stdSql.NullString{
Valid: setSID,
},
Expand All @@ -39,9 +41,12 @@ func rt(setSID, setNID, setO, setR bool) *sql.RelationTuple {
func TestRelationTupleSubjectTypeCheck(t *testing.T) {
for _, dsn := range dbx.GetDSNs(t, false) {
t.Run("dsn="+dsn.Name, func(t *testing.T) {
ctx := context.Background()
reg := driver.NewTestRegistry(t, dsn)
c, err := reg.PopConnection(context.Background())
require.NoError(t, err)
nw, err := reg.DetermineNetwork(ctx)
require.NoError(t, err)

for _, tc := range []struct {
desc string
Expand Down Expand Up @@ -89,7 +94,7 @@ func TestRelationTupleSubjectTypeCheck(t *testing.T) {
},
} {
t.Run("case="+tc.desc, func(t *testing.T) {
err = c.Create(rt(tc.setSID, tc.setNID, tc.setO, tc.setR))
err = c.Create(rt(nw, tc.setSID, tc.setNID, tc.setO, tc.setR))

if tc.success {
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions scripts/test-resetdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ docker rm -f test_keto_postgres || true
docker rm -f test_keto_mysql || true
docker rm -f test_keto_cockroach || true

postgres_port="$(docker port "$(docker run --name test_keto_postgres -e "POSTGRES_PASSWORD=secret" -e "POSTGRES_DB=postgres" -p 0:5432 -d postgres:11.8)" 5432 | sed 's/.*:\([0-9]*\)/\1/')"
mysql_port="$(docker port "$(docker run --name test_keto_mysql -e "MYSQL_ROOT_PASSWORD=secret" -p 0:3306 -d mysql:8.0)" 3306 | sed 's/.*:\([0-9]*\)/\1/')"
cockroach_port="$(docker port "$(docker run --name test_keto_cockroach -p 0:26257 -d cockroachdb/cockroach:v20.2.4 start-single-node --insecure)" 26257 | sed 's/.*:\([0-9]*\)/\1/')"
postgres_port="$(docker port "$(docker run --name test_keto_postgres -e "POSTGRES_PASSWORD=secret" -e "POSTGRES_DB=postgres" -p 0.0.0.0:0:5432 -d postgres:11.8)" 5432 | sed 's/.*:\([0-9]*\)/\1/')"
mysql_port="$(docker port "$(docker run --name test_keto_mysql -e "MYSQL_ROOT_PASSWORD=secret" -p 0.0.0.0:0:3306 -d mysql:8.0)" 3306 | sed 's/.*:\([0-9]*\)/\1/')"
cockroach_port="$(docker port "$(docker run --name test_keto_cockroach -p 0.0.0.0:0:26257 -d cockroachdb/cockroach:v20.2.4 start-single-node --insecure)" 26257 | sed 's/.*:\([0-9]*\)/\1/')"

TEST_DATABASE_POSTGRESQL=$(printf "postgres://postgres:secret@localhost:%s/postgres?sslmode=disable" "$postgres_port")
TEST_DATABASE_MYSQL=$(printf "mysql://root:secret@(localhost:%s)/mysql?parseTime=true&multiStatements=true" "$mysql_port")
Expand Down

0 comments on commit dab1b1d

Please sign in to comment.