Skip to content

Commit

Permalink
test: fix too long relation name and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Feb 3, 2021
1 parent 57babd9 commit fd35cbc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/e2e/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func runCases(c client, nspaces []*namespace.Namespace) func(*testing.T) {
t.Run("case=gets result paginated", func(t *testing.T) {
const nTuples = 10

rel := t.Name()
rel := fmt.Sprintf("some unique relation %T", c)
for i := 0; i < nTuples; i++ {
c.createTuple(t, &relationtuple.InternalRelationTuple{
Namespace: nspaces[0].Name,
Expand All @@ -98,6 +98,7 @@ func runCases(c client, nspaces []*namespace.Namespace) func(*testing.T) {
x.WithToken(resp.NextPageToken),
x.WithSize(1),
)
assert.Len(t, resp.RelationTuples, 1)
}

assert.Equal(t, nTuples, nPages)
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (g *grpcClient) queryTuple(t require.TestingT, q *relationtuple.RelationQue

var resp relationtuple.GetResponse
require.NoError(t, json.Unmarshal([]byte(out), &resp), "%s", out)
fmt.Println(out)

return &resp
}
Expand Down
4 changes: 3 additions & 1 deletion internal/persistence/sql/relationtuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (p *Persister) insertRelationTuple(ctx context.Context, rel *relationtuple.
// TODO sharding
shardID := "default"

p.l.WithFields(rel.ToLoggerFields()).Trace("creating in database")

return p.connection(ctx).RawQuery(fmt.Sprintf(
"INSERT INTO %s (shard_id, object, relation, subject, commit_time) VALUES (?, ?, ?, ?, ?)", tableFromNamespace(n)),
shardID, rel.Object, rel.Relation, rel.Subject.String(), commitTime,
Expand Down Expand Up @@ -111,7 +113,7 @@ func (p *Persister) GetRelationTuples(ctx context.Context, query *relationtuple.
pagination.Offset,
)
} else {
rawQuery = fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT %d OFFSET %d",
rawQuery = fmt.Sprintf("SELECT * FROM %s WHERE %s ORDER BY object, relation, subject, commit_time LIMIT %d OFFSET %d",
tableFromNamespace(n),
strings.Join(where, " AND "),
pagination.Limit+1,
Expand Down
11 changes: 11 additions & 0 deletions internal/relationtuple/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"testing"

"github.com/sirupsen/logrus"

acl "github.com/ory/keto/proto/ory/keto/acl/v1alpha1"

"github.com/tidwall/sjson"
Expand Down Expand Up @@ -279,6 +281,15 @@ func (r *InternalRelationTuple) ToURLQuery() url.Values {
return vals
}

func (r *InternalRelationTuple) ToLoggerFields() logrus.Fields {
return logrus.Fields{
"namespace": r.Namespace,
"object": r.Object,
"relation": r.Relation,
"subject": r.Subject,
}
}

func (q *RelationQuery) FromProto(query *acl.ListRelationTuplesRequest_Query) *RelationQuery {
return &RelationQuery{
Namespace: query.Namespace,
Expand Down
6 changes: 6 additions & 0 deletions internal/relationtuple/read_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (h *handler) getRelations(w http.ResponseWriter, r *http.Request, _ httprou
return
}

l := h.d.Logger()
for k := range q {
l = l.WithField(k, q.Get(k))
}
l.Debug("querying relation tuples")

var paginationOpts []x.PaginationOptionSetter
if pageToken := q.Get("page_token"); pageToken != "" {
paginationOpts = append(paginationOpts, x.WithToken(pageToken))
Expand Down
2 changes: 2 additions & 0 deletions internal/relationtuple/write_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (h *handler) createRelation(w http.ResponseWriter, r *http.Request, _ httpr
return
}

h.d.Logger().WithFields(rel.ToLoggerFields()).Debug("creating relation tuple")

if err := h.d.RelationTupleManager().WriteRelationTuples(r.Context(), &rel); err != nil {
h.d.Logger().WithError(err).WithField("relationtuple", rel).Errorf("got an error while creating the relation tuple")
h.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError))
Expand Down

0 comments on commit fd35cbc

Please sign in to comment.