Skip to content

Commit

Permalink
1517 [Transactions] Enable Transaction Runs Service Pagination (#1518)
Browse files Browse the repository at this point in the history
* 1517 enabling transaction runs pagination

* 1517 enabling transaction runs pagination
  • Loading branch information
xoscar authored Nov 17, 2022
1 parent e9b8cd7 commit 0f1b1f3
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 14 deletions.
12 changes: 12 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ paths:
schema:
type: string
required: true
- in: query
name: take
description: "indicates how many results can be returned by each page"
schema:
type: integer
default: 20
- in: query
name: skip
description: "indicates how many results will be skipped when paginating"
schema:
type: integer
default: 0
summary: "Get all runs from a particular transaction"
description: "Get all runs from a particular transaction"
operationId: getTransactionRuns
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ services:

otel-collector:
image: otel/opentelemetry-collector-contrib:0.59.0
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "55679:55679"
- "4317:4317"
Expand Down
4 changes: 2 additions & 2 deletions server/http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,8 @@ func (c *controller) GetTransactionRun(ctx context.Context, transactionId string
return openapi.Response(http.StatusOK, openapiRun), nil
}

func (c *controller) GetTransactionRuns(ctx context.Context, transactionId string) (openapi.ImplResponse, error) {
runs, err := c.testDB.GetTransactionsRuns(ctx, transactionId)
func (c *controller) GetTransactionRuns(ctx context.Context, transactionId string, take, skip int32) (openapi.ImplResponse, error) {
runs, err := c.testDB.GetTransactionsRuns(ctx, transactionId, take, skip)
if err != nil {
return handleDBError(err), err
}
Expand Down
2 changes: 1 addition & 1 deletion server/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type TransactionRunRepository interface {
UpdateTransactionRun(context.Context, TransactionRun) error
DeleteTransactionRun(context.Context, TransactionRun) error
GetTransactionRun(context.Context, string, int) (TransactionRun, error)
GetTransactionsRuns(context.Context, string) ([]TransactionRun, error)
GetTransactionsRuns(context.Context, string, int32, int32) ([]TransactionRun, error)
}

type Repository interface {
Expand Down
2 changes: 1 addition & 1 deletion server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type ApiApiServicer interface {
GetTests(context.Context, int32, int32, string, string, string) (ImplResponse, error)
GetTransaction(context.Context, string) (ImplResponse, error)
GetTransactionRun(context.Context, string, int32) (ImplResponse, error)
GetTransactionRuns(context.Context, string) (ImplResponse, error)
GetTransactionRuns(context.Context, string, int32, int32) (ImplResponse, error)
GetTransactionVersion(context.Context, string, int32) (ImplResponse, error)
GetTransactions(context.Context, int32, int32, string, string, string) (ImplResponse, error)
ImportTestRun(context.Context, ExportedTestInformation) (ImplResponse, error)
Expand Down
13 changes: 12 additions & 1 deletion server/openapi/api_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,20 @@ func (c *ApiApiController) GetTransactionRun(w http.ResponseWriter, r *http.Requ
// GetTransactionRuns - Get all runs from a particular transaction
func (c *ApiApiController) GetTransactionRuns(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
transactionIdParam := params["transactionId"]

result, err := c.service.GetTransactionRuns(r.Context(), transactionIdParam)
takeParam, err := parseInt32Parameter(query.Get("take"), false)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
skipParam, err := parseInt32Parameter(query.Get("skip"), false)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetTransactionRuns(r.Context(), transactionIdParam, takeParam, skipParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
Expand Down
4 changes: 2 additions & 2 deletions server/testdb/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func (m *MockRepository) GetTransactionRun(ctx context.Context, transactionID st
}

// GetTransactionsRuns implements model.Repository
func (m *MockRepository) GetTransactionsRuns(ctx context.Context, transactionID string) ([]model.TransactionRun, error) {
args := m.Called(ctx, transactionID)
func (m *MockRepository) GetTransactionsRuns(ctx context.Context, transactionID string, take, skip int32) ([]model.TransactionRun, error) {
args := m.Called(ctx, transactionID, take, skip)
return args.Get(0).([]model.TransactionRun), args.Error(1)
}

Expand Down
6 changes: 3 additions & 3 deletions server/testdb/transaction_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func (td *postgresDB) GetTransactionRun(ctx context.Context, transactionId strin
return run, nil
}

func (td *postgresDB) GetTransactionsRuns(ctx context.Context, transactionId string) ([]model.TransactionRun, error) {
stmt, err := td.db.Prepare(selectTransactionRunQuery + " WHERE transaction_id = $1")
func (td *postgresDB) GetTransactionsRuns(ctx context.Context, transactionId string, take, skip int32) ([]model.TransactionRun, error) {
stmt, err := td.db.Prepare(selectTransactionRunQuery + " WHERE transaction_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3")
if err != nil {
return []model.TransactionRun{}, fmt.Errorf("prepare: %w", err)
}

rows, err := stmt.QueryContext(ctx, transactionId)
rows, err := stmt.QueryContext(ctx, transactionId, take, skip)
if err != nil {
return []model.TransactionRun{}, fmt.Errorf("query: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions server/testdb/transactions_runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestListTransactionRun(t *testing.T) {
newRun3, err := db.CreateTransactionRun(context.TODO(), run3)
require.NoError(t, err)

runs, err := db.GetTransactionsRuns(context.TODO(), transaction.ID.String())
runs, err := db.GetTransactionsRuns(context.TODO(), transaction.ID.String(), 20, 0)
require.NoError(t, err)

assert.Len(t, runs, 2)
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestBug(t *testing.T) {
run2, err = db.CreateTransactionRun(ctx, run2)
require.NoError(t, err)

runs, err := db.GetTransactionsRuns(ctx, transaction.ID.String())
runs, err := db.GetTransactionsRuns(ctx, transaction.ID.String(), 20, 0)
require.NoError(t, err)
assert.Contains(t, runs, run1)
assert.Contains(t, runs, run2)
Expand All @@ -179,7 +179,7 @@ func TestBug(t *testing.T) {
run3, err = db.CreateTransactionRun(ctx, run3)
require.NoError(t, err)

runs, err = db.GetTransactionsRuns(ctx, newTransaction.ID.String())
runs, err = db.GetTransactionsRuns(ctx, newTransaction.ID.String(), 20, 0)
require.NoError(t, err)
assert.Len(t, runs, 3)
assert.Contains(t, runs, run1)
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/ResourceCard/TransactionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const TransactionCard = ({
hasRuns={Boolean(list.length)}
isCollapsed={isCollapsed}
isLoading={isLoading}
onViewAll={() => onViewAll(transactionId, ResourceType.Test)}
onViewAll={() => onViewAll(transactionId, ResourceType.Transaction)}
>
<S.RunsListContainer>
{list.map(run => (
Expand Down

0 comments on commit 0f1b1f3

Please sign in to comment.