Skip to content

Commit

Permalink
test(utils): expand test coverage for utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgaOzen committed Dec 1, 2023
1 parent a9c5905 commit d59c733
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 101 deletions.
19 changes: 19 additions & 0 deletions internal/storage/postgres/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (
"fmt"
"log/slog"

"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"

"github.com/pkg/errors"

"github.com/Masterminds/squirrel"

base "github.com/Permify/permify/pkg/pb/base/v1"
)

const (
Expand Down Expand Up @@ -117,3 +122,17 @@ func Rollback(tx *sql.Tx) {
slog.Error("failed to rollback transaction", err)
}
}

func HandleError(span trace.Span, err error, errorCode base.ErrorCode) error {
// Record the error on the span
span.RecordError(err)

// Set the status of the span
span.SetStatus(codes.Error, err.Error())

// Log the error
slog.Error("Error encountered", slog.Any("error", err), slog.Any("errorCode", errorCode))

// Return a new standardized error with the provided error code
return errors.New(errorCode.String())
}
61 changes: 37 additions & 24 deletions internal/storage/postgres/utils/common_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package utils_test

import (
"testing"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/Masterminds/squirrel"

"github.com/Permify/permify/internal/storage/postgres/utils"

"github.com/stretchr/testify/assert"
)

func TestSnapshotQuery(t *testing.T) {
sl := squirrel.Select("column").From("table")
revision := uint64(42)

query := utils.SnapshotQuery(sl, revision)
sql, _, err := query.ToSql()

assert.NoError(t, err)
expectedSQL := "SELECT column FROM table WHERE (pg_visible_in_snapshot(created_tx_id, (select snapshot from transactions where id = '42'::xid8)) = true OR created_tx_id = '42'::xid8) AND ((pg_visible_in_snapshot(expired_tx_id, (select snapshot from transactions where id = '42'::xid8)) = false OR expired_tx_id = '0'::xid8) AND expired_tx_id <> '42'::xid8)"
assert.Equal(t, expectedSQL, sql)
}

func TestGarbageCollectQuery(t *testing.T) {
query := utils.GenerateGCQuery("relation_tuples", 100)
sql, _, err := query.ToSql()

assert.NoError(t, err)

expectedSQL := "DELETE FROM relation_tuples WHERE expired_tx_id <> '0'::xid8 AND expired_tx_id < '100'::xid8"
assert.Equal(t, expectedSQL, sql)
}
var _ = Describe("Common", func() {
Context("TestSnapshotQuery", func() {
It("Case 1", func() {
sl := squirrel.Select("column").From("table")
revision := uint64(42)

query := utils.SnapshotQuery(sl, revision)
sql, _, err := query.ToSql()
Expect(err).ShouldNot(HaveOccurred())

expectedSQL := "SELECT column FROM table WHERE (pg_visible_in_snapshot(created_tx_id, (select snapshot from transactions where id = '42'::xid8)) = true OR created_tx_id = '42'::xid8) AND ((pg_visible_in_snapshot(expired_tx_id, (select snapshot from transactions where id = '42'::xid8)) = false OR expired_tx_id = '0'::xid8) AND expired_tx_id <> '42'::xid8)"
Expect(sql).Should(Equal(expectedSQL))
})
})

Context("TestGarbageCollectQuery", func() {
It("Case 1", func() {
query := utils.GenerateGCQuery("relation_tuples", 100)
sql, _, err := query.ToSql()
Expect(err).ShouldNot(HaveOccurred())

expectedSQL := "DELETE FROM relation_tuples WHERE expired_tx_id <> '0'::xid8 AND expired_tx_id < '100'::xid8"
Expect(expectedSQL).Should(Equal(sql))
})
})

Context("BulkEntityFilterQuery", func() {
It("Case 1", func() {
query := utils.BulkEntityFilterQuery("t1", "organization", 100)
Expect(strings.ReplaceAll(strings.ReplaceAll(query, " ", ""), "\n", "")).Should(Equal(strings.ReplaceAll(strings.ReplaceAll("\n WITH entities AS (\n (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM relation_tuples)\n UNION ALL\n (SELECT id, entity_id, entity_type, tenant_id, created_tx_id, expired_tx_id FROM attributes)\n ), filtered_entities AS (\n SELECT DISTINCT ON (entity_id) id, entity_id\n FROM entities\n WHERE tenant_id = 't1'\n AND entity_type = 'organization'\n AND (pg_visible_in_snapshot(created_tx_id, (SELECT snapshot FROM transactions WHERE id = '100'::xid8)) = true OR created_tx_id = '100'::xid8)\n AND ((pg_visible_in_snapshot(expired_tx_id, (SELECT snapshot FROM transactions WHERE id = '100'::xid8)) = false OR expired_tx_id = '0'::xid8) AND expired_tx_id <> '100'::xid8)\n )\n SELECT id, entity_id\n FROM filtered_entities\n", " ", ""), "\n", "")))
})
})
})
Loading

0 comments on commit d59c733

Please sign in to comment.