diff --git a/internal/store/postgres/org_billing_repository.go b/internal/store/postgres/org_billing_repository.go index 26bc6be5c..5a75820ac 100644 --- a/internal/store/postgres/org_billing_repository.go +++ b/internal/store/postgres/org_billing_repository.go @@ -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...) }) diff --git a/internal/store/postgres/org_billing_repository_test.go b/internal/store/postgres/org_billing_repository_test.go index 96eb6cfdd..374bace95 100644 --- a/internal/store/postgres/org_billing_repository_test.go +++ b/internal/store/postgres/org_billing_repository_test.go @@ -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" ) @@ -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) + }) +} diff --git a/internal/store/postgres/org_projects_repository.go b/internal/store/postgres/org_projects_repository.go index 16faa63b8..bd63e922a 100644 --- a/internal/store/postgres/org_projects_repository.go +++ b/internal/store/postgres/org_projects_repository.go @@ -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...) }) diff --git a/internal/store/postgres/org_projects_repository_test.go b/internal/store/postgres/org_projects_repository_test.go index d3681cac1..35342a377 100644 --- a/internal/store/postgres/org_projects_repository_test.go +++ b/internal/store/postgres/org_projects_repository_test.go @@ -1,6 +1,7 @@ package postgres import ( + "context" "testing" "github.com/raystack/salt/rql" @@ -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) + }) +} diff --git a/internal/store/postgres/org_users_repository.go b/internal/store/postgres/org_users_repository.go index 028ea4d2e..8e8838922 100644 --- a/internal/store/postgres/org_users_repository.go +++ b/internal/store/postgres/org_users_repository.go @@ -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...) }) diff --git a/internal/store/postgres/org_users_repository_test.go b/internal/store/postgres/org_users_repository_test.go index b9434fbb8..4e5d7a8a4 100644 --- a/internal/store/postgres/org_users_repository_test.go +++ b/internal/store/postgres/org_users_repository_test.go @@ -1,6 +1,7 @@ package postgres import ( + "context" "testing" "strings" @@ -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) + }) +} diff --git a/internal/store/postgres/project_users_repository.go b/internal/store/postgres/project_users_repository.go index 5ded89002..a34171b0b 100644 --- a/internal/store/postgres/project_users_repository.go +++ b/internal/store/postgres/project_users_repository.go @@ -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...) }) diff --git a/internal/store/postgres/project_users_repository_test.go b/internal/store/postgres/project_users_repository_test.go index e53580e95..77ac70083 100644 --- a/internal/store/postgres/project_users_repository_test.go +++ b/internal/store/postgres/project_users_repository_test.go @@ -1,6 +1,7 @@ package postgres import ( + "context" "testing" "github.com/raystack/salt/rql" @@ -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) + }) +}