Skip to content
Open
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
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
28 changes: 28 additions & 0 deletions internal/store/postgres/org_billing_repository_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package postgres

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

"github.com/jmoiron/sqlx"
"github.com/raystack/frontier/pkg/db"
"github.com/raystack/salt/rql"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -216,3 +222,25 @@ func TestPrepareGroupByQuery(t *testing.T) {
})
}
}

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

type failConnector struct{}

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

func (failConnector) Driver() driver.Driver { return nil }

func TestOrgBillingRepository_Search(t *testing.T) {
t.Run("should return error when the transaction cannot start", func(t *testing.T) {
repo := NewOrgBillingRepository(txnFailClient())
_, err := repo.Search(context.Background(), &rql.Query{Limit: 10})
assert.Error(t, err)
})
}
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())
_, 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())
_, 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())
_, err := repo.Search(context.Background(), "project-123", &rql.Query{Limit: 10})
assert.Error(t, err)
})
}
Loading