Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/store/postgres/fakes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package postgres

import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"testing"

"github.com/jmoiron/sqlx"
"github.com/raystack/frontier/pkg/db"
)

// txnFailClient returns a client whose connections always fail, so any
// transaction begin returns an error.
func txnFailClient(t *testing.T) *db.Client {
t.Helper()
client := &db.Client{DB: sqlx.NewDb(sql.OpenDB(failConnector{}), "postgres")}
t.Cleanup(func() { _ = client.Close() })
return client
}

type failConnector struct{}

func (failConnector) Connect(context.Context) (driver.Conn, error) {
return nil, errors.New("connection failed")
}

func (failConnector) Driver() driver.Driver { return nil }
2 changes: 1 addition & 1 deletion internal/store/postgres/org_billing_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (r OrgBillingRepository) Search(ctx context.Context, rql *rql.Query) (svc.O
ReadOnly: true,
}

r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTimeout(ctx, TABLE_ORGANIZATIONS, "GetOrgBilling", func(ctx context.Context) error {
return tx.SelectContext(ctx, &orgBilling, dataQuery, params...)
})
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/org_billing_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgres

import (
"context"
"testing"

"github.com/raystack/salt/rql"
Expand Down Expand Up @@ -216,3 +217,11 @@ func TestPrepareGroupByQuery(t *testing.T) {
})
}
}

func TestOrgBillingRepository_Search(t *testing.T) {
t.Run("should return error when the transaction cannot start", func(t *testing.T) {
repo := NewOrgBillingRepository(txnFailClient(t))
_, err := repo.Search(context.Background(), &rql.Query{Limit: 10})
assert.Error(t, err)
Comment thread
AmanGIT07 marked this conversation as resolved.
})
}
2 changes: 1 addition & 1 deletion internal/store/postgres/org_projects_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r OrgProjectsRepository) Search(ctx context.Context, orgID string, rql *rq
ReadOnly: true,
}

r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTimeout(ctx, TABLE_PROJECTS, "CountProjectMembers", func(ctx context.Context) error {
return tx.SelectContext(ctx, &orgProjects, dataQuery, params...)
})
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/org_projects_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgres

import (
"context"
"testing"

"github.com/raystack/salt/rql"
Expand Down Expand Up @@ -125,3 +126,11 @@ func TestOrgProjectsRepository_prepareDataQuery(t *testing.T) {
})
}
}

func TestOrgProjectsRepository_Search(t *testing.T) {
t.Run("should return error when the transaction cannot start", func(t *testing.T) {
repo := NewOrgProjectsRepository(txnFailClient(t))
_, err := repo.Search(context.Background(), "org-id", &rql.Query{Limit: 10})
assert.Error(t, err)
})
}
2 changes: 1 addition & 1 deletion internal/store/postgres/org_users_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r OrgUsersRepository) Search(ctx context.Context, orgID string, rql *rql.Q
ReadOnly: true,
}

r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "GetOrgUsers", func(ctx context.Context) error {
return tx.SelectContext(ctx, &orgUsers, dataQuery, params...)
})
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/org_users_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgres

import (
"context"
"testing"

"strings"
Expand Down Expand Up @@ -306,3 +307,11 @@ func TestOrgUsersRepository_BuildRoleFilterCondition(t *testing.T) {
})
}
}

func TestOrgUsersRepository_Search(t *testing.T) {
t.Run("should return error when the transaction cannot start", func(t *testing.T) {
repo := NewOrgUsersRepository(txnFailClient(t))
_, err := repo.Search(context.Background(), "org-id", &rql.Query{Limit: 10})
assert.Error(t, err)
})
}
2 changes: 1 addition & 1 deletion internal/store/postgres/project_users_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r ProjectUsersRepository) Search(ctx context.Context, projectID string, rq
ReadOnly: true,
}

r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTxn(ctx, txOpts, func(tx *sqlx.Tx) error {
err = r.dbc.WithTimeout(ctx, TABLE_POLICIES, "GetProjectUsers", func(ctx context.Context) error {
return tx.SelectContext(ctx, &projectUsers, dataQuery, params...)
})
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/project_users_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgres

import (
"context"
"testing"

"github.com/raystack/salt/rql"
Expand Down Expand Up @@ -58,3 +59,11 @@ func TestProjectUsersRepository_PrepareDataQuery(t *testing.T) {
})
}
}

func TestProjectUsersRepository_Search(t *testing.T) {
t.Run("should return error when the transaction cannot start", func(t *testing.T) {
repo := NewProjectUsersRepository(txnFailClient(t))
_, err := repo.Search(context.Background(), "project-123", &rql.Query{Limit: 10})
assert.Error(t, err)
})
}
Loading