diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 4af6a77..757eb95 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,7 +1,10 @@ version: 2 + updates: - package-ecosystem: "gomod" - directory: "/" + directories: + - "/" + - "/tools" schedule: interval: "weekly" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 74d6991..53bb644 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,9 @@ on: - main workflow_dispatch: +permissions: + contents: read + jobs: build: runs-on: ubuntu-latest @@ -18,6 +21,13 @@ jobs: uses: actions/setup-go@v7 with: go-version: "1.27.x" + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Go version + run: go version - name: Verify run: make verify diff --git a/.golangci.yml b/.golangci.yml index 0287e67..ff8bf7f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -78,6 +78,10 @@ linters: - legacy - std-error-handling rules: + - path: ^(sqlite/(testexecution|testsuiteexecution)\.go)$ + linters: + - dupl + - linters: - dogsled - dupl diff --git a/Makefile b/Makefile index 0b0a940..c30afd7 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,11 @@ normalizers: sqlc lint: install-golangci-lint $(GOLANGCI_LINT) run +.PHONY: vulncheck +vulncheck: install-govulncheck + @echo "Checking for known vulnerabilities..." + $(GOVULNCHECK) ./... + .PHONY: test test: @echo "Running tests..." @@ -46,5 +51,5 @@ test-race: @go test ./... -race -count=1 -timeout=600s .PHONY: verify -verify: normalizers lint test-race +verify: normalizers lint vulncheck test-race @echo "All verifications passed successfully." diff --git a/go.mod b/go.mod index 7c2a0ee..305418e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/e2engine/repository go 1.27 require ( - github.com/e2engine/core v0.2.1 + github.com/e2engine/core v0.3.0 github.com/golang-migrate/migrate/v4 v4.20.1 github.com/ygrebnov/errorc v0.6.1 github.com/ygrebnov/keys v0.2.0 @@ -11,6 +11,7 @@ require ( ) require ( + github.com/bufbuild/protocompile v0.14.1 // indirect github.com/dustin/go-humanize v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/mattn/go-isatty v0.0.24 // indirect @@ -18,7 +19,13 @@ require ( github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/ygrebnov/log v0.4.4 // indirect github.com/ygrebnov/model v0.7.5 // indirect + golang.org/x/net v0.61.0 // indirect + golang.org/x/sync v0.24.0 // indirect golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.43.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20261012220534-4341edf3f941 // indirect + google.golang.org/grpc v1.84.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.77.1 // indirect modernc.org/mathutil v1.7.1 // indirect diff --git a/go.sum b/go.sum index a80de91..9f6a947 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,14 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= -github.com/e2engine/core v0.2.1 h1:U0K4Ak4RheCxVX9+pLUyF/k8mrRB9rb0ruxxa6JWUX4= -github.com/e2engine/core v0.2.1/go.mod h1:8oN/239uuU+jNNA+CxDF7h68HKh0cL+vON9D/mcqN/Y= +github.com/e2engine/core v0.3.0 h1:hx7THwTfc9wYtFOW7sE9kfLNFOG8ff/2tD+lD4pvzmM= +github.com/e2engine/core v0.3.0/go.mod h1:8oN/239uuU+jNNA+CxDF7h68HKh0cL+vON9D/mcqN/Y= github.com/golang-migrate/migrate/v4 v4.20.1 h1:2N/ToVTKrKl58ynBpgeVJ4In7VcLCjWTZtm4eP1LxhU= github.com/golang-migrate/migrate/v4 v4.20.1/go.mod h1:DDPgKVb4ovSWc4FwSPfV2Uz1160f4XBiTHTrAJtljmM= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3 h1:LMLX+LgTNWpfvCBdFebv6EsYotImrt/Ppc5cXIriCSo= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3/go.mod h1:jl5iWTm0/hd5PjEYEOuwAJ57L/CibdZfrqZ5XA5GrCk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -46,9 +50,10 @@ golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= -golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU= -golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0= -google.golang.org/genproto v0.0.0-20260630182238-925bb5da69e7 h1:lQG76ePMKmtujel4VIVMiFoHVWVNtJdawbCZJtWlVXU= +golang.org/x/tools v0.51.0 h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI= +golang.org/x/tools v0.51.0/go.mod h1:9eEncMayCV6zRMGhR5eZEC2iBx98qWcF1HZ9Z7wJOoA= +gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= +gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= google.golang.org/genproto/googleapis/rpc v0.0.0-20261012220534-4341edf3f941 h1:QG6hS4zFplRZvOQedzei4t2pT/zkQc3VR1u+S9/rcAw= google.golang.org/genproto/googleapis/rpc v0.0.0-20261012220534-4341edf3f941/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0= diff --git a/sqlite/internal/sqlc/querier.go b/sqlite/internal/sqlc/querier.go index 471da69..5e2be70 100644 --- a/sqlite/internal/sqlc/querier.go +++ b/sqlite/internal/sqlc/querier.go @@ -54,11 +54,12 @@ type Querier interface { // ============================================================================ GetTestSuiteExecutionIDsByID(ctx context.Context, prefix sql.NullString) ([]string, error) GetTestSuiteExecutionIDsByTestSuiteID(ctx context.Context, prefix sql.NullString) ([]string, error) + GetTestSuiteExecutionStats(ctx context.Context, testSuiteExecutionID string) ([]GetTestSuiteExecutionStatsRow, error) // ============================================================================ // Status // ============================================================================ GetTestSuiteExecutionStatus(ctx context.Context, id string) (string, error) - GetTestsByTag(ctx context.Context, tag string) ([]Resource, error) + GetTestsByTag(ctx context.Context, arg GetTestsByTagParams) ([]Resource, error) // ============================================================================ // Cursor page: CreatedAt // ============================================================================ diff --git a/sqlite/internal/sqlc/resources.sql.go b/sqlite/internal/sqlc/resources.sql.go index bd30149..12369ba 100644 --- a/sqlite/internal/sqlc/resources.sql.go +++ b/sqlite/internal/sqlc/resources.sql.go @@ -164,6 +164,7 @@ FROM resources WHERE kind = ?1 AND id LIKE ?2 || '%' ORDER BY id ASC + LIMIT 2 ` type GetResourceIDsByIDParams struct { @@ -203,6 +204,7 @@ FROM resources WHERE kind = ?1 AND name LIKE ?2 || '%' ORDER BY name ASC, id ASC + LIMIT 2 ` type GetResourceIDsByNameParams struct { diff --git a/sqlite/internal/sqlc/test_executions.sql.go b/sqlite/internal/sqlc/test_executions.sql.go index f7dc6c1..47075e6 100644 --- a/sqlite/internal/sqlc/test_executions.sql.go +++ b/sqlite/internal/sqlc/test_executions.sql.go @@ -169,6 +169,7 @@ SELECT id FROM test_executions WHERE id LIKE ?1 || '%' ORDER BY id ASC + LIMIT 2 ` // ============================================================================ @@ -300,6 +301,46 @@ func (q *Queries) GetTestExecutionsByTestSuiteExecutionID(ctx context.Context, t return items, nil } +const getTestSuiteExecutionStats = `-- name: GetTestSuiteExecutionStats :many +SELECT + status, + COUNT(*) AS count, + MAX(finished_at) AS last_test_finished_at +FROM test_executions +WHERE test_suite_execution_id = + CAST(?1 AS TEXT) +GROUP BY status +` + +type GetTestSuiteExecutionStatsRow struct { + Status string `json:"status"` + Count int64 `json:"count"` + LastTestFinishedAt interface{} `json:"last_test_finished_at"` +} + +func (q *Queries) GetTestSuiteExecutionStats(ctx context.Context, testSuiteExecutionID string) ([]GetTestSuiteExecutionStatsRow, error) { + rows, err := q.db.QueryContext(ctx, getTestSuiteExecutionStats, testSuiteExecutionID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []GetTestSuiteExecutionStatsRow{} + for rows.Next() { + var i GetTestSuiteExecutionStatsRow + if err := rows.Scan(&i.Status, &i.Count, &i.LastTestFinishedAt); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listTestExecutionsByFinishedAtAfterAsc = `-- name: ListTestExecutionsByFinishedAtAfterAsc :many SELECT @@ -1715,7 +1756,9 @@ SET status = ?2, summary = ?3 WHERE id = ?4 -RETURNING id + AND status IN ('scheduled', 'running') + AND ?2 IN ('passed', 'failed', 'error') + RETURNING id ` type SetTestExecutionCompletedParams struct { @@ -1744,7 +1787,8 @@ SET started_at = ?1, status = 'running' WHERE id = ?2 -RETURNING id + AND status = 'scheduled' + RETURNING id ` type SetTestExecutionRunningParams struct { diff --git a/sqlite/internal/sqlc/test_suite_executions.sql.go b/sqlite/internal/sqlc/test_suite_executions.sql.go index d2003a8..ce8df61 100644 --- a/sqlite/internal/sqlc/test_suite_executions.sql.go +++ b/sqlite/internal/sqlc/test_suite_executions.sql.go @@ -178,6 +178,7 @@ SELECT id FROM test_suite_executions WHERE id LIKE ?1 || '%' ORDER BY id ASC + LIMIT 2 ` // ============================================================================ @@ -2044,7 +2045,9 @@ SET status = ?2, summary = ?3 WHERE id = ?4 -RETURNING id + AND status IN ('scheduled', 'running') + AND ?2 IN ('passed', 'failed', 'error') + RETURNING id ` type SetTestSuiteExecutionCompletedParams struct { @@ -2073,7 +2076,8 @@ SET started_at = ?1, status = 'running' WHERE id = ?2 -RETURNING id + AND status = 'scheduled' + RETURNING id ` type SetTestSuiteExecutionRunningParams struct { diff --git a/sqlite/internal/sqlc/test_tags.sql.go b/sqlite/internal/sqlc/test_tags.sql.go index 46728f0..753c59c 100644 --- a/sqlite/internal/sqlc/test_tags.sql.go +++ b/sqlite/internal/sqlc/test_tags.sql.go @@ -48,10 +48,16 @@ FROM resources r WHERE tt.tag = ?1 AND r.kind = 'test' ORDER BY r.id ASC + LIMIT ?2 ` -func (q *Queries) GetTestsByTag(ctx context.Context, tag string) ([]Resource, error) { - rows, err := q.db.QueryContext(ctx, getTestsByTag, tag) +type GetTestsByTagParams struct { + Tag string `json:"tag"` + Limit int64 `json:"limit"` +} + +func (q *Queries) GetTestsByTag(ctx context.Context, arg GetTestsByTagParams) ([]Resource, error) { + rows, err := q.db.QueryContext(ctx, getTestsByTag, arg.Tag, arg.Limit) if err != nil { return nil, err } diff --git a/sqlite/query/resources.sql b/sqlite/query/resources.sql index 7799f9c..7223504 100644 --- a/sqlite/query/resources.sql +++ b/sqlite/query/resources.sql @@ -75,7 +75,8 @@ SELECT id FROM resources WHERE kind = sqlc.arg(kind) AND id LIKE sqlc.arg(prefix) || '%' -ORDER BY id ASC; +ORDER BY id ASC + LIMIT 2; -- name: GetResourceIDsByName :many @@ -83,7 +84,8 @@ SELECT id FROM resources WHERE kind = sqlc.arg(kind) AND name LIKE sqlc.arg(prefix) || '%' -ORDER BY name ASC, id ASC; +ORDER BY name ASC, id ASC + LIMIT 2; -- ============================================================================ diff --git a/sqlite/query/test_executions.sql b/sqlite/query/test_executions.sql index 38df9bc..f1db8c0 100644 --- a/sqlite/query/test_executions.sql +++ b/sqlite/query/test_executions.sql @@ -80,7 +80,8 @@ WHERE id = sqlc.arg(id) SELECT id FROM test_executions WHERE id LIKE sqlc.arg(prefix) || '%' -ORDER BY id ASC; +ORDER BY id ASC + LIMIT 2; -- ============================================================================ @@ -621,6 +622,15 @@ SELECT status FROM test_executions WHERE id = sqlc.arg(id); +-- name: GetTestSuiteExecutionStats :many +SELECT + status, + COUNT(*) AS count, + MAX(finished_at) AS last_test_finished_at +FROM test_executions +WHERE test_suite_execution_id = + CAST(sqlc.arg(test_suite_execution_id) AS TEXT) +GROUP BY status; -- ============================================================================ -- State transitions @@ -632,14 +642,17 @@ SET started_at = sqlc.arg(started_at), status = 'running' WHERE id = sqlc.arg(id) -RETURNING id; + AND status = 'scheduled' + RETURNING id; -- name: SetTestExecutionCompleted :one UPDATE test_executions SET - finished_at = sqlc.arg(finished_at), - status = sqlc.arg(status), - summary = sqlc.arg(summary) -WHERE id = sqlc.arg(id) -RETURNING id; \ No newline at end of file + finished_at = ?1, + status = ?2, + summary = ?3 +WHERE id = ?4 + AND status IN ('scheduled', 'running') + AND ?2 IN ('passed', 'failed', 'error') + RETURNING id; \ No newline at end of file diff --git a/sqlite/query/test_suite_executions.sql b/sqlite/query/test_suite_executions.sql index b4fbd2d..d1bf68d 100644 --- a/sqlite/query/test_suite_executions.sql +++ b/sqlite/query/test_suite_executions.sql @@ -78,7 +78,8 @@ WHERE id = sqlc.arg(id) SELECT id FROM test_suite_executions WHERE id LIKE sqlc.arg(prefix) || '%' -ORDER BY id ASC; +ORDER BY id ASC + LIMIT 2; -- name: GetTestSuiteExecutionIDsByTestSuiteID :many @@ -672,14 +673,17 @@ SET started_at = sqlc.arg(started_at), status = 'running' WHERE id = sqlc.arg(id) -RETURNING id; + AND status = 'scheduled' + RETURNING id; -- name: SetTestSuiteExecutionCompleted :one UPDATE test_suite_executions SET - finished_at = sqlc.arg(finished_at), - status = sqlc.arg(status), - summary = sqlc.narg(summary) -WHERE id = sqlc.arg(id) -RETURNING id; \ No newline at end of file + finished_at = ?1, + status = ?2, + summary = ?3 +WHERE id = ?4 + AND status IN ('scheduled', 'running') + AND ?2 IN ('passed', 'failed', 'error') + RETURNING id; \ No newline at end of file diff --git a/sqlite/query/test_tags.sql b/sqlite/query/test_tags.sql index 1fceaf8..5c2735b 100644 --- a/sqlite/query/test_tags.sql +++ b/sqlite/query/test_tags.sql @@ -19,4 +19,5 @@ FROM resources r ON tt.test_id = r.id WHERE tt.tag = sqlc.arg(tag) AND r.kind = 'test' -ORDER BY r.id ASC; \ No newline at end of file +ORDER BY r.id ASC + LIMIT sqlc.arg(limit); \ No newline at end of file diff --git a/sqlite/resources.go b/sqlite/resources.go index 10d3070..1cbc5e7 100644 --- a/sqlite/resources.go +++ b/sqlite/resources.go @@ -22,8 +22,8 @@ type resource interface { } type resourceRepository[E resource] struct { - queries db.Querier - kind string + store db.Store + kind string } var ( @@ -32,30 +32,30 @@ var ( ) func newEnvironmentRepository( - queries db.Querier, + store db.Store, ) *resourceRepository[repository.Environment] { return newResourceRepository[repository.Environment]( - queries, + store, resourceKindEnvironment, ) } func newTestSuiteRepository( - queries db.Querier, + store db.Store, ) *resourceRepository[repository.TestSuite] { return newResourceRepository[repository.TestSuite]( - queries, + store, resourceKindTestSuite, ) } func newResourceRepository[E resource]( - queries db.Querier, + store db.Store, kind string, ) *resourceRepository[E] { return &resourceRepository[E]{ - queries: queries, - kind: kind, + store: store, + kind: kind, } } @@ -65,7 +65,7 @@ func (r *resourceRepository[E]) Create( ) (*E, error) { res := repository.Resource(*params) - created, err := r.queries.CreateResource( + created, err := r.store.CreateResource( ctx, db.CreateResourceParams{ ID: res.ID, @@ -91,7 +91,7 @@ func (r *resourceRepository[E]) Get( ctx context.Context, id string, ) (*E, error) { - res, err := r.queries.GetResource( + res, err := r.store.GetResource( ctx, db.GetResourceParams{ Kind: r.kind, @@ -111,7 +111,7 @@ func (r *resourceRepository[E]) Delete( ctx context.Context, id string, ) (*E, error) { - res, err := r.queries.DeleteResource( + res, err := r.store.DeleteResource( ctx, db.DeleteResourceParams{ Kind: r.kind, @@ -131,7 +131,7 @@ func (r *resourceRepository[E]) GetIDsByID( ctx context.Context, id string, ) ([]string, error) { - ids, err := r.queries.GetResourceIDsByID( + ids, err := r.store.GetResourceIDsByID( ctx, db.GetResourceIDsByIDParams{ Kind: r.kind, @@ -152,7 +152,7 @@ func (r *resourceRepository[E]) GetIDsByName( ctx context.Context, name string, ) ([]string, error) { - ids, err := r.queries.GetResourceIDsByName( + ids, err := r.store.GetResourceIDsByName( ctx, db.GetResourceIDsByNameParams{ Kind: r.kind, @@ -215,7 +215,7 @@ func (r *resourceRepository[E]) listFirstPage( switch { case orderBy == repository.OrderByID && direction == repository.OrderDirectionAsc: - return r.queries.ListResourcesByIDFirstAsc( + return r.store.ListResourcesByIDFirstAsc( ctx, db.ListResourcesByIDFirstAscParams{ Kind: r.kind, @@ -225,7 +225,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByID && direction == repository.OrderDirectionDesc: - return r.queries.ListResourcesByIDFirstDesc( + return r.store.ListResourcesByIDFirstDesc( ctx, db.ListResourcesByIDFirstDescParams{ Kind: r.kind, @@ -235,7 +235,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByName && direction == repository.OrderDirectionAsc: - return r.queries.ListResourcesByNameFirstAsc( + return r.store.ListResourcesByNameFirstAsc( ctx, db.ListResourcesByNameFirstAscParams{ Kind: r.kind, @@ -245,7 +245,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByName && direction == repository.OrderDirectionDesc: - return r.queries.ListResourcesByNameFirstDesc( + return r.store.ListResourcesByNameFirstDesc( ctx, db.ListResourcesByNameFirstDescParams{ Kind: r.kind, @@ -255,7 +255,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByVersion && direction == repository.OrderDirectionAsc: - return r.queries.ListResourcesByVersionFirstAsc( + return r.store.ListResourcesByVersionFirstAsc( ctx, db.ListResourcesByVersionFirstAscParams{ Kind: r.kind, @@ -265,7 +265,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByVersion && direction == repository.OrderDirectionDesc: - return r.queries.ListResourcesByVersionFirstDesc( + return r.store.ListResourcesByVersionFirstDesc( ctx, db.ListResourcesByVersionFirstDescParams{ Kind: r.kind, @@ -275,7 +275,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByCreatedAt && direction == repository.OrderDirectionAsc: - return r.queries.ListResourcesByCreatedAtFirstAsc( + return r.store.ListResourcesByCreatedAtFirstAsc( ctx, db.ListResourcesByCreatedAtFirstAscParams{ Kind: r.kind, @@ -285,7 +285,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByCreatedAt && direction == repository.OrderDirectionDesc: - return r.queries.ListResourcesByCreatedAtFirstDesc( + return r.store.ListResourcesByCreatedAtFirstDesc( ctx, db.ListResourcesByCreatedAtFirstDescParams{ Kind: r.kind, @@ -295,7 +295,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByUpdatedAt && direction == repository.OrderDirectionAsc: - return r.queries.ListResourcesByUpdatedAtFirstAsc( + return r.store.ListResourcesByUpdatedAtFirstAsc( ctx, db.ListResourcesByUpdatedAtFirstAscParams{ Kind: r.kind, @@ -305,7 +305,7 @@ func (r *resourceRepository[E]) listFirstPage( case orderBy == repository.OrderByUpdatedAt && direction == repository.OrderDirectionDesc: - return r.queries.ListResourcesByUpdatedAtFirstDesc( + return r.store.ListResourcesByUpdatedAtFirstDesc( ctx, db.ListResourcesByUpdatedAtFirstDescParams{ Kind: r.kind, @@ -353,7 +353,7 @@ func (r *resourceRepository[E]) listByID( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByIDAfterAsc( + return r.store.ListResourcesByIDAfterAsc( ctx, db.ListResourcesByIDAfterAscParams{ Kind: r.kind, @@ -364,7 +364,7 @@ func (r *resourceRepository[E]) listByID( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByIDAfterDesc( + return r.store.ListResourcesByIDAfterDesc( ctx, db.ListResourcesByIDAfterDescParams{ Kind: r.kind, @@ -375,7 +375,7 @@ func (r *resourceRepository[E]) listByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByIDBeforeAsc( + return r.store.ListResourcesByIDBeforeAsc( ctx, db.ListResourcesByIDBeforeAscParams{ Kind: r.kind, @@ -386,7 +386,7 @@ func (r *resourceRepository[E]) listByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByIDBeforeDesc( + return r.store.ListResourcesByIDBeforeDesc( ctx, db.ListResourcesByIDBeforeDescParams{ Kind: r.kind, @@ -409,7 +409,7 @@ func (r *resourceRepository[E]) listByName( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByNameAfterAsc( + return r.store.ListResourcesByNameAfterAsc( ctx, db.ListResourcesByNameAfterAscParams{ Kind: r.kind, @@ -421,7 +421,7 @@ func (r *resourceRepository[E]) listByName( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByNameAfterDesc( + return r.store.ListResourcesByNameAfterDesc( ctx, db.ListResourcesByNameAfterDescParams{ Kind: r.kind, @@ -433,7 +433,7 @@ func (r *resourceRepository[E]) listByName( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByNameBeforeAsc( + return r.store.ListResourcesByNameBeforeAsc( ctx, db.ListResourcesByNameBeforeAscParams{ Kind: r.kind, @@ -445,7 +445,7 @@ func (r *resourceRepository[E]) listByName( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByNameBeforeDesc( + return r.store.ListResourcesByNameBeforeDesc( ctx, db.ListResourcesByNameBeforeDescParams{ Kind: r.kind, @@ -469,7 +469,7 @@ func (r *resourceRepository[E]) listByVersion( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByVersionAfterAsc( + return r.store.ListResourcesByVersionAfterAsc( ctx, db.ListResourcesByVersionAfterAscParams{ Kind: r.kind, @@ -481,7 +481,7 @@ func (r *resourceRepository[E]) listByVersion( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByVersionAfterDesc( + return r.store.ListResourcesByVersionAfterDesc( ctx, db.ListResourcesByVersionAfterDescParams{ Kind: r.kind, @@ -493,7 +493,7 @@ func (r *resourceRepository[E]) listByVersion( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByVersionBeforeAsc( + return r.store.ListResourcesByVersionBeforeAsc( ctx, db.ListResourcesByVersionBeforeAscParams{ Kind: r.kind, @@ -505,7 +505,7 @@ func (r *resourceRepository[E]) listByVersion( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByVersionBeforeDesc( + return r.store.ListResourcesByVersionBeforeDesc( ctx, db.ListResourcesByVersionBeforeDescParams{ Kind: r.kind, @@ -529,7 +529,7 @@ func (r *resourceRepository[E]) listByCreatedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByCreatedAtAfterAsc( + return r.store.ListResourcesByCreatedAtAfterAsc( ctx, db.ListResourcesByCreatedAtAfterAscParams{ Kind: r.kind, @@ -541,7 +541,7 @@ func (r *resourceRepository[E]) listByCreatedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByCreatedAtAfterDesc( + return r.store.ListResourcesByCreatedAtAfterDesc( ctx, db.ListResourcesByCreatedAtAfterDescParams{ Kind: r.kind, @@ -553,7 +553,7 @@ func (r *resourceRepository[E]) listByCreatedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByCreatedAtBeforeAsc( + return r.store.ListResourcesByCreatedAtBeforeAsc( ctx, db.ListResourcesByCreatedAtBeforeAscParams{ Kind: r.kind, @@ -565,7 +565,7 @@ func (r *resourceRepository[E]) listByCreatedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByCreatedAtBeforeDesc( + return r.store.ListResourcesByCreatedAtBeforeDesc( ctx, db.ListResourcesByCreatedAtBeforeDescParams{ Kind: r.kind, @@ -589,7 +589,7 @@ func (r *resourceRepository[E]) listByUpdatedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByUpdatedAtAfterAsc( + return r.store.ListResourcesByUpdatedAtAfterAsc( ctx, db.ListResourcesByUpdatedAtAfterAscParams{ Kind: r.kind, @@ -601,7 +601,7 @@ func (r *resourceRepository[E]) listByUpdatedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByUpdatedAtAfterDesc( + return r.store.ListResourcesByUpdatedAtAfterDesc( ctx, db.ListResourcesByUpdatedAtAfterDescParams{ Kind: r.kind, @@ -613,7 +613,7 @@ func (r *resourceRepository[E]) listByUpdatedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListResourcesByUpdatedAtBeforeAsc( + return r.store.ListResourcesByUpdatedAtBeforeAsc( ctx, db.ListResourcesByUpdatedAtBeforeAscParams{ Kind: r.kind, @@ -625,7 +625,7 @@ func (r *resourceRepository[E]) listByUpdatedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListResourcesByUpdatedAtBeforeDesc( + return r.store.ListResourcesByUpdatedAtBeforeDesc( ctx, db.ListResourcesByUpdatedAtBeforeDescParams{ Kind: r.kind, diff --git a/sqlite/test.go b/sqlite/test.go index 89035b5..b30996e 100644 --- a/sqlite/test.go +++ b/sqlite/test.go @@ -88,8 +88,13 @@ func (r *testRepository) Create( func (r *testRepository) GetByTag( ctx context.Context, tag string, + limit int, ) ([]repository.Test, error) { - tests, err := r.store.GetTestsByTag(ctx, tag) + arg := db.GetTestsByTagParams{ + Tag: tag, + Limit: int64(limit), + } + tests, err := r.store.GetTestsByTag(ctx, arg) if err != nil { return nil, err } diff --git a/sqlite/testexecution.go b/sqlite/testexecution.go index edd0c03..2765cd5 100644 --- a/sqlite/testexecution.go +++ b/sqlite/testexecution.go @@ -3,21 +3,26 @@ package sqlite import ( "context" "database/sql" + "errors" + "math" + "time" + "github.com/e2engine/core/pkg/keys" "github.com/e2engine/core/repository" + "github.com/ygrebnov/errorc" db "github.com/e2engine/repository/sqlite/internal/sqlc" ) type testExecutionRepository struct { - queries db.Querier + store db.Store } var _ repository.TestExecutionRepository = (*testExecutionRepository)(nil) -func newTestExecutionRepository(queries db.Querier) *testExecutionRepository { +func newTestExecutionRepository(store db.Store) *testExecutionRepository { return &testExecutionRepository{ - queries: queries, + store: store, } } @@ -25,7 +30,7 @@ func (r *testExecutionRepository) Create( ctx context.Context, params *repository.TestExecution, ) (*repository.TestExecution, error) { - created, err := r.queries.CreateTestExecution( + created, err := r.store.CreateTestExecution( ctx, db.CreateTestExecutionParams{ ID: params.ID, @@ -35,7 +40,7 @@ func (r *testExecutionRepository) Create( }, StartedAt: params.StartedAt, FinishedAt: params.FinishedAt, - Status: params.Status, + Status: string(params.Status), EnvironmentID: params.EnvironmentID, EnvironmentName: params.EnvironmentName, TestID: params.TestID, @@ -56,7 +61,7 @@ func (r *testExecutionRepository) Get( ctx context.Context, id string, ) (*repository.TestExecution, error) { - res, err := r.queries.GetTestExecution(ctx, id) + res, err := r.store.GetTestExecution(ctx, id) if err != nil { return nil, mapError(err) } @@ -69,20 +74,20 @@ func (r *testExecutionRepository) Get( func (r *testExecutionRepository) GetStatus( ctx context.Context, id string, -) (string, error) { - res, err := r.queries.GetTestExecutionStatus(ctx, id) +) (repository.ExecutionStatus, error) { + res, err := r.store.GetTestExecutionStatus(ctx, id) if err != nil { return "", mapError(err) } - return res, nil + return repository.ExecutionStatus(res), nil } func (r *testExecutionRepository) Delete( ctx context.Context, id string, ) (*repository.TestExecution, error) { - res, err := r.queries.DeleteTestExecution(ctx, id) + res, err := r.store.DeleteTestExecution(ctx, id) if err != nil { return nil, mapError(err) } @@ -96,7 +101,7 @@ func (r *testExecutionRepository) GetIDsByID( ctx context.Context, id string, ) ([]string, error) { - ids, err := r.queries.GetTestExecutionIDsByID( + ids, err := r.store.GetTestExecutionIDsByID( ctx, sql.NullString{ String: id, @@ -114,7 +119,7 @@ func (r *testExecutionRepository) GetByTestSuiteExecutionID( ctx context.Context, testSuiteExecutionID string, ) ([]repository.TestExecution, error) { - values, err := r.queries.GetTestExecutionsByTestSuiteExecutionID( + values, err := r.store.GetTestExecutionsByTestSuiteExecutionID( ctx, testSuiteExecutionID, ) @@ -171,56 +176,56 @@ func (r *testExecutionRepository) listTestExecutionsFirstPage( switch { case orderBy == repository.OrderByID && direction == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByIDFirstAsc( + return r.store.ListTestExecutionsByIDFirstAsc( ctx, pageLimit, ) case orderBy == repository.OrderByID && direction == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByIDFirstDesc( + return r.store.ListTestExecutionsByIDFirstDesc( ctx, pageLimit, ) case orderBy == repository.OrderByStartedAt && direction == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStartedAtFirstAsc( + return r.store.ListTestExecutionsByStartedAtFirstAsc( ctx, pageLimit, ) case orderBy == repository.OrderByStartedAt && direction == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStartedAtFirstDesc( + return r.store.ListTestExecutionsByStartedAtFirstDesc( ctx, pageLimit, ) case orderBy == repository.OrderByFinishedAt && direction == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByFinishedAtFirstAsc( + return r.store.ListTestExecutionsByFinishedAtFirstAsc( ctx, pageLimit, ) case orderBy == repository.OrderByFinishedAt && direction == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByFinishedAtFirstDesc( + return r.store.ListTestExecutionsByFinishedAtFirstDesc( ctx, pageLimit, ) case orderBy == repository.OrderByStatus && direction == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStatusFirstAsc( + return r.store.ListTestExecutionsByStatusFirstAsc( ctx, pageLimit, ) case orderBy == repository.OrderByStatus && direction == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStatusFirstDesc( + return r.store.ListTestExecutionsByStatusFirstDesc( ctx, pageLimit, ) @@ -277,7 +282,7 @@ func (r *testExecutionRepository) listTestExecutionsByID( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByIDAfterAsc( + return r.store.ListTestExecutionsByIDAfterAsc( ctx, db.ListTestExecutionsByIDAfterAscParams{ AnchorID: params.ID, @@ -287,7 +292,7 @@ func (r *testExecutionRepository) listTestExecutionsByID( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByIDAfterDesc( + return r.store.ListTestExecutionsByIDAfterDesc( ctx, db.ListTestExecutionsByIDAfterDescParams{ AnchorID: params.ID, @@ -297,7 +302,7 @@ func (r *testExecutionRepository) listTestExecutionsByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByIDBeforeAsc( + return r.store.ListTestExecutionsByIDBeforeAsc( ctx, db.ListTestExecutionsByIDBeforeAscParams{ AnchorID: params.ID, @@ -307,7 +312,7 @@ func (r *testExecutionRepository) listTestExecutionsByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByIDBeforeDesc( + return r.store.ListTestExecutionsByIDBeforeDesc( ctx, db.ListTestExecutionsByIDBeforeDescParams{ AnchorID: params.ID, @@ -320,7 +325,6 @@ func (r *testExecutionRepository) listTestExecutionsByID( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testExecutionRepository) listTestExecutionsByStartedAt( ctx context.Context, params *repository.ListParams, @@ -329,7 +333,7 @@ func (r *testExecutionRepository) listTestExecutionsByStartedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStartedAtAfterAsc( + return r.store.ListTestExecutionsByStartedAtAfterAsc( ctx, db.ListTestExecutionsByStartedAtAfterAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -340,7 +344,7 @@ func (r *testExecutionRepository) listTestExecutionsByStartedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStartedAtAfterDesc( + return r.store.ListTestExecutionsByStartedAtAfterDesc( ctx, db.ListTestExecutionsByStartedAtAfterDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -351,7 +355,7 @@ func (r *testExecutionRepository) listTestExecutionsByStartedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStartedAtBeforeAsc( + return r.store.ListTestExecutionsByStartedAtBeforeAsc( ctx, db.ListTestExecutionsByStartedAtBeforeAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -362,7 +366,7 @@ func (r *testExecutionRepository) listTestExecutionsByStartedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStartedAtBeforeDesc( + return r.store.ListTestExecutionsByStartedAtBeforeDesc( ctx, db.ListTestExecutionsByStartedAtBeforeDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -376,7 +380,6 @@ func (r *testExecutionRepository) listTestExecutionsByStartedAt( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testExecutionRepository) listTestExecutionsByFinishedAt( ctx context.Context, params *repository.ListParams, @@ -385,7 +388,7 @@ func (r *testExecutionRepository) listTestExecutionsByFinishedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByFinishedAtAfterAsc( + return r.store.ListTestExecutionsByFinishedAtAfterAsc( ctx, db.ListTestExecutionsByFinishedAtAfterAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -396,7 +399,7 @@ func (r *testExecutionRepository) listTestExecutionsByFinishedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByFinishedAtAfterDesc( + return r.store.ListTestExecutionsByFinishedAtAfterDesc( ctx, db.ListTestExecutionsByFinishedAtAfterDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -407,7 +410,7 @@ func (r *testExecutionRepository) listTestExecutionsByFinishedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByFinishedAtBeforeAsc( + return r.store.ListTestExecutionsByFinishedAtBeforeAsc( ctx, db.ListTestExecutionsByFinishedAtBeforeAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -418,7 +421,7 @@ func (r *testExecutionRepository) listTestExecutionsByFinishedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByFinishedAtBeforeDesc( + return r.store.ListTestExecutionsByFinishedAtBeforeDesc( ctx, db.ListTestExecutionsByFinishedAtBeforeDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -432,7 +435,6 @@ func (r *testExecutionRepository) listTestExecutionsByFinishedAt( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testExecutionRepository) listTestExecutionsByStatus( ctx context.Context, params *repository.ListParams, @@ -441,7 +443,7 @@ func (r *testExecutionRepository) listTestExecutionsByStatus( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStatusAfterAsc( + return r.store.ListTestExecutionsByStatusAfterAsc( ctx, db.ListTestExecutionsByStatusAfterAscParams{ AnchorValue: params.ValueString, @@ -452,7 +454,7 @@ func (r *testExecutionRepository) listTestExecutionsByStatus( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStatusAfterDesc( + return r.store.ListTestExecutionsByStatusAfterDesc( ctx, db.ListTestExecutionsByStatusAfterDescParams{ AnchorValue: params.ValueString, @@ -463,7 +465,7 @@ func (r *testExecutionRepository) listTestExecutionsByStatus( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - return r.queries.ListTestExecutionsByStatusBeforeAsc( + return r.store.ListTestExecutionsByStatusBeforeAsc( ctx, db.ListTestExecutionsByStatusBeforeAscParams{ AnchorValue: params.ValueString, @@ -474,7 +476,7 @@ func (r *testExecutionRepository) listTestExecutionsByStatus( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - return r.queries.ListTestExecutionsByStatusBeforeDesc( + return r.store.ListTestExecutionsByStatusBeforeDesc( ctx, db.ListTestExecutionsByStatusBeforeDescParams{ AnchorValue: params.ValueString, @@ -488,40 +490,187 @@ func (r *testExecutionRepository) listTestExecutionsByStatus( } } +func (r *testExecutionRepository) GetTestSuiteExecutionStates( + ctx context.Context, + testSuiteExecutionID string, +) ([]repository.TestSuiteExecutionState, bool, error) { + var ( + states []repository.TestSuiteExecutionState + alreadyCompleted bool + ) + + err := r.store.ExecTx(ctx, func(q *db.Queries) error { + status, err := q.GetTestSuiteExecutionStatus( + ctx, + testSuiteExecutionID, + ) + if err != nil { + return mapError(err) + } + + switch repository.ExecutionStatus(status) { + case repository.ExecutionStatusScheduled, + repository.ExecutionStatusRunning: + // Continue with child state aggregation. + + case repository.ExecutionStatusPassed, + repository.ExecutionStatusFailed, + repository.ExecutionStatusError: + alreadyCompleted = true + return nil + + default: + return repository.ErrInvalidExecutionStatus + } + + rows, err := q.GetTestSuiteExecutionStats( + ctx, + testSuiteExecutionID, + ) + if err != nil { + return mapError(err) + } + + states = make([]repository.TestSuiteExecutionState, len(rows)) + + for i, row := range rows { + if row.Count <= 0 || row.Count > int64(math.MaxInt) { + return repository.ErrTooManyResults + } + + finishedAt, err := timeFromInterface(row.LastTestFinishedAt) + if err != nil { + return err + } + + states[i] = repository.TestSuiteExecutionState{ + Status: repository.ExecutionStatus(row.Status), + Count: int(row.Count), + LastTestFinishedAt: finishedAt, + } + } + + return nil + }) + + if err != nil { + return nil, false, err + } + + return states, alreadyCompleted, nil +} + +func timeFromInterface(value any) (time.Time, error) { + switch v := value.(type) { + case nil: + return time.Time{}, nil + + case time.Time: + return v, nil + + case *time.Time: + if v == nil { + return time.Time{}, nil + } + + return *v, nil + + case string: + return parseSQLiteTime(v) + + default: + return time.Time{}, errorc.With( + repository.ErrInvalidType, + errorc.String(keys.Validation, "expected timestamp"), + ) + } +} + +func parseSQLiteTime(value string) (time.Time, error) { + formats := []string{ + time.RFC3339Nano, + "2006-01-02 15:04:05.999999999 -0700 MST", + } + + for _, format := range formats { + if timestamp, err := time.Parse(format, value); err == nil { + return timestamp, nil + } + } + + return time.Time{}, errorc.With( + repository.ErrInvalidType, + errorc.String(keys.Validation, "invalid timestamp"), + ) +} + func (r *testExecutionRepository) SetRunning( ctx context.Context, params *repository.SetRunningParams, ) error { - if _, err := r.queries.SetTestExecutionRunning( - ctx, - db.SetTestExecutionRunningParams{ - ID: params.ID, - StartedAt: params.StartedAt, - }, - ); err != nil { - return mapError(err) - } + return r.store.ExecTx(ctx, func(q *db.Queries) error { + _, err := q.SetTestExecutionRunning( + ctx, + db.SetTestExecutionRunningParams{ + ID: params.ID, + StartedAt: params.StartedAt, + }, + ) + if err == nil { + return nil + } + + if !errors.Is(err, sql.ErrNoRows) { + return mapError(err) + } + + _, err = q.GetTestExecutionStatus(ctx, params.ID) + if err != nil { + return mapError(err) + } - return nil + return repository.ErrInvalidExecutionStateTransition + }) } func (r *testExecutionRepository) SetCompleted( ctx context.Context, params *repository.SetCompletedParams, ) error { - if _, err := r.queries.SetTestExecutionCompleted( - ctx, - db.SetTestExecutionCompletedParams{ - ID: params.ID, - FinishedAt: params.FinishedAt, - Status: params.Status, - Summary: params.Summary, - }, - ); err != nil { - return mapError(err) + switch params.Status { + case repository.ExecutionStatusPassed, + repository.ExecutionStatusFailed, + repository.ExecutionStatusError: + // Valid terminal status. + default: + return repository.ErrInvalidExecutionStatus } - return nil + return r.store.ExecTx(ctx, func(q *db.Queries) error { + _, err := q.SetTestExecutionCompleted( + ctx, + db.SetTestExecutionCompletedParams{ + ID: params.ID, + FinishedAt: params.FinishedAt, + Status: string(params.Status), + Summary: params.Summary, + }, + ) + if err == nil { + return nil + } + + if !errors.Is(err, sql.ErrNoRows) { + return mapError(err) + } + + _, err = q.GetTestExecutionStatus(ctx, params.ID) + if err != nil { + return mapError(err) + } + + return repository.ErrInvalidExecutionStateTransition + }) } func testExecutionsFromDB( @@ -544,7 +693,7 @@ func testExecutionFromDB( TestSuiteExecutionID: fromNullString(value.TestSuiteExecutionID), StartedAt: value.StartedAt, FinishedAt: value.FinishedAt, - Status: value.Status, + Status: repository.ExecutionStatus(value.Status), EnvironmentID: value.EnvironmentID, EnvironmentName: value.EnvironmentName, TestID: value.TestID, diff --git a/sqlite/testexecution_test.go b/sqlite/testexecution_test.go new file mode 100644 index 0000000..e3af8d3 --- /dev/null +++ b/sqlite/testexecution_test.go @@ -0,0 +1,92 @@ +package sqlite + +import ( + "testing" + "time" +) + +func TestTimeFromInterface(t *testing.T) { + expected := time.Date( + 2026, time.October, 26, + 9, 30, 0, 123456789, + time.UTC, + ) + + tests := []struct { + name string + value any + expected time.Time + expectError bool + }{ + { + name: "nil", + value: nil, + expected: time.Time{}, + }, + { + name: "time", + value: expected, + expected: expected, + }, + { + name: "time pointer", + value: &expected, + expected: expected, + }, + { + name: "RFC3339 timestamp", + value: expected.Format(time.RFC3339Nano), + expected: expected, + }, + { + name: "invalid timestamp", + value: "invalid", + expectError: true, + }, + { + name: "unsupported type", + value: 123, + expectError: true, + }, + { + name: "Go timestamp representation", + value: "0001-01-01 00:00:00 +0000 UTC", + expected: time.Time{}, + }, + { + name: "Go timestamp with fractional seconds", + value: "2026-10-26 09:30:00.123456789 +0000 UTC", + expected: time.Date( + 2026, time.October, 26, + 9, 30, 0, 123456789, + time.UTC, + ), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual, err := timeFromInterface(tt.value) + + if tt.expectError { + if err == nil { + t.Fatal("expected error") + } + + return + } + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if !actual.Equal(tt.expected) { + t.Errorf( + "expected %v, got %v", + tt.expected, + actual, + ) + } + }) + } +} diff --git a/sqlite/testsuiteexecution.go b/sqlite/testsuiteexecution.go index dd140b7..91a6d42 100644 --- a/sqlite/testsuiteexecution.go +++ b/sqlite/testsuiteexecution.go @@ -3,6 +3,7 @@ package sqlite import ( "context" "database/sql" + "errors" "time" "github.com/e2engine/core/repository" @@ -13,16 +14,16 @@ import ( //go:generate go run ./internal/generate/testsuiteexecution type testSuiteExecutionRepository struct { - queries db.Querier + store db.Store } var _ repository.TestSuiteExecutionRepository = (*testSuiteExecutionRepository)(nil) func newTestSuiteExecutionRepository( - queries db.Querier, + store db.Store, ) *testSuiteExecutionRepository { return &testSuiteExecutionRepository{ - queries: queries, + store: store, } } @@ -30,13 +31,13 @@ func (r *testSuiteExecutionRepository) Create( ctx context.Context, params *repository.TestSuiteExecution, ) (*repository.TestSuiteExecution, error) { - raw, err := r.queries.CreateTestSuiteExecution( + raw, err := r.store.CreateTestSuiteExecution( ctx, db.CreateTestSuiteExecutionParams{ ID: params.ID, StartedAt: params.StartedAt, FinishedAt: params.FinishedAt, - Status: params.Status, + Status: string(params.Status), EnvironmentID: params.EnvironmentID, EnvironmentName: params.EnvironmentName, TestSuiteID: params.TestSuiteID, @@ -51,7 +52,7 @@ func (r *testSuiteExecutionRepository) Create( ID: raw.ID, StartedAt: raw.StartedAt, FinishedAt: raw.FinishedAt, - Status: raw.Status, + Status: repository.ExecutionStatus(raw.Status), EnvironmentID: raw.EnvironmentID, EnvironmentName: raw.EnvironmentName, TestSuiteID: raw.TestSuiteID, @@ -64,7 +65,7 @@ func (r *testSuiteExecutionRepository) Get( ctx context.Context, id string, ) (*repository.TestSuiteExecution, error) { - raw, err := r.queries.GetTestSuiteExecution(ctx, id) + raw, err := r.store.GetTestSuiteExecution(ctx, id) if err != nil { return nil, mapError(err) } @@ -73,7 +74,7 @@ func (r *testSuiteExecutionRepository) Get( ID: raw.ID, StartedAt: raw.StartedAt, FinishedAt: raw.FinishedAt, - Status: raw.Status, + Status: repository.ExecutionStatus(raw.Status), EnvironmentID: raw.EnvironmentID, EnvironmentName: raw.EnvironmentName, TestSuiteID: raw.TestSuiteID, @@ -86,20 +87,20 @@ func (r *testSuiteExecutionRepository) Get( func (r *testSuiteExecutionRepository) GetStatus( ctx context.Context, id string, -) (string, error) { - res, err := r.queries.GetTestSuiteExecutionStatus(ctx, id) +) (repository.ExecutionStatus, error) { + res, err := r.store.GetTestSuiteExecutionStatus(ctx, id) if err != nil { return "", mapError(err) } - return res, nil + return repository.ExecutionStatus(res), nil } func (r *testSuiteExecutionRepository) Delete( ctx context.Context, id string, ) (*repository.TestSuiteExecution, error) { - raw, err := r.queries.DeleteTestSuiteExecution(ctx, id) + raw, err := r.store.DeleteTestSuiteExecution(ctx, id) if err != nil { return nil, mapError(err) } @@ -108,7 +109,7 @@ func (r *testSuiteExecutionRepository) Delete( ID: raw.ID, StartedAt: raw.StartedAt, FinishedAt: raw.FinishedAt, - Status: raw.Status, + Status: repository.ExecutionStatus(raw.Status), EnvironmentID: raw.EnvironmentID, EnvironmentName: raw.EnvironmentName, TestSuiteID: raw.TestSuiteID, @@ -121,7 +122,7 @@ func (r *testSuiteExecutionRepository) GetIDsByID( ctx context.Context, id string, ) ([]string, error) { - ids, err := r.queries.GetTestSuiteExecutionIDsByID( + ids, err := r.store.GetTestSuiteExecutionIDsByID( ctx, sql.NullString{ String: id, @@ -181,7 +182,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( switch { case orderBy == repository.OrderByID && direction == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByIDFirstAsc( + list, err := r.store.ListTestSuiteExecutionsByIDFirstAsc( ctx, pageLimit, ) @@ -192,7 +193,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByID && direction == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByIDFirstDesc( + list, err := r.store.ListTestSuiteExecutionsByIDFirstDesc( ctx, pageLimit, ) @@ -203,7 +204,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByStartedAt && direction == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtFirstAsc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtFirstAsc( ctx, pageLimit, ) @@ -214,7 +215,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByStartedAt && direction == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtFirstDesc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtFirstDesc( ctx, pageLimit, ) @@ -225,7 +226,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByFinishedAt && direction == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtFirstAsc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtFirstAsc( ctx, pageLimit, ) @@ -236,7 +237,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByFinishedAt && direction == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtFirstDesc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtFirstDesc( ctx, pageLimit, ) @@ -247,7 +248,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByStatus && direction == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStatusFirstAsc( + list, err := r.store.ListTestSuiteExecutionsByStatusFirstAsc( ctx, pageLimit, ) @@ -258,7 +259,7 @@ func (r *testSuiteExecutionRepository) listFirstPage( case orderBy == repository.OrderByStatus && direction == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStatusFirstDesc( + list, err := r.store.ListTestSuiteExecutionsByStatusFirstDesc( ctx, pageLimit, ) @@ -303,7 +304,7 @@ func (r *testSuiteExecutionRepository) listByID( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByIDAfterAsc( + list, err := r.store.ListTestSuiteExecutionsByIDAfterAsc( ctx, db.ListTestSuiteExecutionsByIDAfterAscParams{ AnchorID: params.ID, @@ -317,7 +318,7 @@ func (r *testSuiteExecutionRepository) listByID( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByIDAfterDesc( + list, err := r.store.ListTestSuiteExecutionsByIDAfterDesc( ctx, db.ListTestSuiteExecutionsByIDAfterDescParams{ AnchorID: params.ID, @@ -331,7 +332,7 @@ func (r *testSuiteExecutionRepository) listByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByIDBeforeAsc( + list, err := r.store.ListTestSuiteExecutionsByIDBeforeAsc( ctx, db.ListTestSuiteExecutionsByIDBeforeAscParams{ AnchorID: params.ID, @@ -345,7 +346,7 @@ func (r *testSuiteExecutionRepository) listByID( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByIDBeforeDesc( + list, err := r.store.ListTestSuiteExecutionsByIDBeforeDesc( ctx, db.ListTestSuiteExecutionsByIDBeforeDescParams{ AnchorID: params.ID, @@ -362,7 +363,6 @@ func (r *testSuiteExecutionRepository) listByID( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testSuiteExecutionRepository) listByStartedAt( ctx context.Context, params *repository.ListParams, @@ -371,7 +371,7 @@ func (r *testSuiteExecutionRepository) listByStartedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtAfterAsc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtAfterAsc( ctx, db.ListTestSuiteExecutionsByStartedAtAfterAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -386,7 +386,7 @@ func (r *testSuiteExecutionRepository) listByStartedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtAfterDesc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtAfterDesc( ctx, db.ListTestSuiteExecutionsByStartedAtAfterDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -401,7 +401,7 @@ func (r *testSuiteExecutionRepository) listByStartedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtBeforeAsc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtBeforeAsc( ctx, db.ListTestSuiteExecutionsByStartedAtBeforeAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -416,7 +416,7 @@ func (r *testSuiteExecutionRepository) listByStartedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStartedAtBeforeDesc( + list, err := r.store.ListTestSuiteExecutionsByStartedAtBeforeDesc( ctx, db.ListTestSuiteExecutionsByStartedAtBeforeDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -434,7 +434,6 @@ func (r *testSuiteExecutionRepository) listByStartedAt( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testSuiteExecutionRepository) listByFinishedAt( ctx context.Context, params *repository.ListParams, @@ -443,7 +442,7 @@ func (r *testSuiteExecutionRepository) listByFinishedAt( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtAfterAsc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtAfterAsc( ctx, db.ListTestSuiteExecutionsByFinishedAtAfterAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -458,7 +457,7 @@ func (r *testSuiteExecutionRepository) listByFinishedAt( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtAfterDesc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtAfterDesc( ctx, db.ListTestSuiteExecutionsByFinishedAtAfterDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -473,7 +472,7 @@ func (r *testSuiteExecutionRepository) listByFinishedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtBeforeAsc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtBeforeAsc( ctx, db.ListTestSuiteExecutionsByFinishedAtBeforeAscParams{ AnchorTimestamp: params.ValueTimestamp, @@ -488,7 +487,7 @@ func (r *testSuiteExecutionRepository) listByFinishedAt( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByFinishedAtBeforeDesc( + list, err := r.store.ListTestSuiteExecutionsByFinishedAtBeforeDesc( ctx, db.ListTestSuiteExecutionsByFinishedAtBeforeDescParams{ AnchorTimestamp: params.ValueTimestamp, @@ -506,7 +505,6 @@ func (r *testSuiteExecutionRepository) listByFinishedAt( } } -//nolint:dupl // Explicit dispatch to distinct sqlc-generated query methods is clearer than abstracting it. func (r *testSuiteExecutionRepository) listByStatus( ctx context.Context, params *repository.ListParams, @@ -515,7 +513,7 @@ func (r *testSuiteExecutionRepository) listByStatus( switch { case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStatusAfterAsc( + list, err := r.store.ListTestSuiteExecutionsByStatusAfterAsc( ctx, db.ListTestSuiteExecutionsByStatusAfterAscParams{ AnchorValue: params.ValueString, @@ -530,7 +528,7 @@ func (r *testSuiteExecutionRepository) listByStatus( case params.Position == repository.PositionAfter && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStatusAfterDesc( + list, err := r.store.ListTestSuiteExecutionsByStatusAfterDesc( ctx, db.ListTestSuiteExecutionsByStatusAfterDescParams{ AnchorValue: params.ValueString, @@ -545,7 +543,7 @@ func (r *testSuiteExecutionRepository) listByStatus( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionAsc: - list, err := r.queries.ListTestSuiteExecutionsByStatusBeforeAsc( + list, err := r.store.ListTestSuiteExecutionsByStatusBeforeAsc( ctx, db.ListTestSuiteExecutionsByStatusBeforeAscParams{ AnchorValue: params.ValueString, @@ -560,7 +558,7 @@ func (r *testSuiteExecutionRepository) listByStatus( case params.Position == repository.PositionBefore && params.OrderDirection == repository.OrderDirectionDesc: - list, err := r.queries.ListTestSuiteExecutionsByStatusBeforeDesc( + list, err := r.store.ListTestSuiteExecutionsByStatusBeforeDesc( ctx, db.ListTestSuiteExecutionsByStatusBeforeDescParams{ AnchorValue: params.ValueString, @@ -582,36 +580,69 @@ func (r *testSuiteExecutionRepository) SetRunning( ctx context.Context, params *repository.SetRunningParams, ) error { - if _, err := r.queries.SetTestSuiteExecutionRunning( - ctx, - db.SetTestSuiteExecutionRunningParams{ - ID: params.ID, - StartedAt: params.StartedAt, - }, - ); err != nil { - return mapError(err) - } + return r.store.ExecTx(ctx, func(q *db.Queries) error { + _, err := q.SetTestSuiteExecutionRunning( + ctx, + db.SetTestSuiteExecutionRunningParams{ + ID: params.ID, + StartedAt: params.StartedAt, + }, + ) + if err == nil { + return nil + } - return nil + if !errors.Is(err, sql.ErrNoRows) { + return mapError(err) + } + + _, err = q.GetTestSuiteExecutionStatus(ctx, params.ID) + if err != nil { + return mapError(err) + } + + return repository.ErrInvalidExecutionStateTransition + }) } func (r *testSuiteExecutionRepository) SetCompleted( ctx context.Context, params *repository.SetCompletedParams, ) error { - if _, err := r.queries.SetTestSuiteExecutionCompleted( - ctx, - db.SetTestSuiteExecutionCompletedParams{ - ID: params.ID, - FinishedAt: params.FinishedAt, - Status: params.Status, - Summary: params.Summary, - }, - ); err != nil { - return mapError(err) + switch params.Status { + case repository.ExecutionStatusPassed, + repository.ExecutionStatusFailed, + repository.ExecutionStatusError: + // Valid terminal status. + default: + return repository.ErrInvalidExecutionStatus } - return nil + return r.store.ExecTx(ctx, func(q *db.Queries) error { + _, err := q.SetTestSuiteExecutionCompleted( + ctx, + db.SetTestSuiteExecutionCompletedParams{ + ID: params.ID, + FinishedAt: params.FinishedAt, + Status: string(params.Status), + Summary: params.Summary, + }, + ) + if err == nil { + return nil + } + + if !errors.Is(err, sql.ErrNoRows) { + return mapError(err) + } + + _, err = q.GetTestSuiteExecutionStatus(ctx, params.ID) + if err != nil { + return mapError(err) + } + + return repository.ErrInvalidExecutionStateTransition + }) } type testSuiteExecutionRow struct { @@ -646,7 +677,7 @@ func testSuiteExecutionFromDB( ID: value.ID, StartedAt: value.StartedAt, FinishedAt: value.FinishedAt, - Status: value.Status, + Status: repository.ExecutionStatus(value.Status), EnvironmentID: value.EnvironmentID, EnvironmentName: value.EnvironmentName, TestSuiteID: value.TestSuiteID, diff --git a/tests/environment_test.go b/tests/environment_test.go index 3694e67..af5b15a 100644 --- a/tests/environment_test.go +++ b/tests/environment_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "slices" + "strings" "testing" "time" @@ -396,7 +397,7 @@ func TestEnvironmentGetIDsByID(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: allIDs[:2], }, { name: "query error", @@ -464,11 +465,18 @@ func TestEnvironmentGetIDsByName(t *testing.T) { resources := getResourcesForList(t, "environment") createEnvironmentResources(t, repository, resources) - allIDs := make([]string, len(resources)) - for i := range resources { - allIDs[i] = resources[i].ID + slices.SortFunc(resources, func(a, b corerepository.Resource) int { + if c := strings.Compare(a.Name, b.Name); c != 0 { + return c + } + + return strings.Compare(a.ID, b.ID) + }) + + expectedIDs := []string{ + resources[0].ID, + resources[1].ID, } - slices.Sort(allIDs) testCases := []struct { name string @@ -483,7 +491,7 @@ func TestEnvironmentGetIDsByName(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: expectedIDs, }, { name: "query error", diff --git a/tests/main_test.go b/tests/main_test.go index a935269..ba40e42 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -63,7 +63,7 @@ func getTestExecution( i int, testSuiteExecutionID string, startedAt, finishedAt time.Time, - status string, + status corerepository.ExecutionStatus, ) corerepository.TestExecution { t.Helper() @@ -87,16 +87,29 @@ func getDefaultTestExecution(t *testing.T, i int) corerepository.TestExecution { testSuiteExecutionID := idPrefixTestSuiteExecution + idBody + "1" startedAt := time.Now().Add(-time.Duration(i) * time.Minute) finishedAt := startedAt.Add(time.Duration(i+1) * time.Minute) - status := "passed" + status := corerepository.ExecutionStatusPassed return getTestExecution(t, i, testSuiteExecutionID, startedAt, finishedAt, status) } +func getUnfinishedTestExecution( + t *testing.T, + i int, +) corerepository.TestExecution { + t.Helper() + + execution := getDefaultTestExecution(t, i) + execution.Status = corerepository.ExecutionStatusScheduled + execution.FinishedAt = time.Time{} + + return execution +} + func getTestSuiteExecution( t *testing.T, i int, startedAt, finishedAt time.Time, - status string, + status corerepository.ExecutionStatus, summary json.RawMessage, ) corerepository.TestSuiteExecution { t.Helper() @@ -119,12 +132,25 @@ func getDefaultTestSuiteExecution(t *testing.T, i int) corerepository.TestSuiteE startedAt := time.Now().Add(-time.Duration(i) * time.Minute) finishedAt := startedAt.Add(time.Duration(i+1) * time.Minute) - status := "passed" + status := corerepository.ExecutionStatusPassed // summary := json.RawMessage(`{"key": "TestSuiteExecution` + strconv.Itoa(i) + `"}`) return getTestSuiteExecution(t, i, startedAt, finishedAt, status, nil) } +func getUnfinishedTestSuiteExecution( + t *testing.T, + i int, +) corerepository.TestSuiteExecution { + t.Helper() + + execution := getDefaultTestSuiteExecution(t, i) + execution.Status = corerepository.ExecutionStatusScheduled + execution.FinishedAt = time.Time{} + + return execution +} + func getResourcesForList(t *testing.T, kind string) []corerepository.Resource { t.Helper() diff --git a/tests/test_test.go b/tests/test_test.go index e7db6a3..824d6cf 100644 --- a/tests/test_test.go +++ b/tests/test_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "slices" + "strings" "testing" "time" @@ -76,7 +77,7 @@ func TestTestCRUD(t *testing.T) { t.Fatalf("Expected test IDs %v, got %v", []string{test1.ID, test2.ID}, ids) } - testsByTag, err := repository.GetByTag(t.Context(), "shared") + testsByTag, err := repository.GetByTag(t.Context(), "shared", 10) if err != nil { t.Fatalf("Unexpected tests by tag error: %v", err) } @@ -356,7 +357,7 @@ func TestTestGetIDsByID(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: allIDs[:2], }, { name: "query error", @@ -424,11 +425,18 @@ func TestTestGetIDsByName(t *testing.T) { resources := getResourcesForList(t, "test") createTestResources(t, repository, resources) - allIDs := make([]string, len(resources)) - for i := range resources { - allIDs[i] = resources[i].ID + slices.SortFunc(resources, func(a, b corerepository.Resource) int { + if c := strings.Compare(a.Name, b.Name); c != 0 { + return c + } + + return strings.Compare(a.ID, b.ID) + }) + + expectedIDs := []string{ + resources[0].ID, + resources[1].ID, } - slices.Sort(allIDs) testCases := []struct { name string @@ -443,7 +451,7 @@ func TestTestGetIDsByName(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: expectedIDs, }, { name: "query error", @@ -586,6 +594,7 @@ func TestTestGetByTag(t *testing.T) { actual, err := repository.GetByTag( testCase.context(), testCase.tag, + 10, ) if testCase.expectedError != nil { diff --git a/tests/testexecution_list_test.go b/tests/testexecution_list_test.go index e9f9e0e..619b8f5 100644 --- a/tests/testexecution_list_test.go +++ b/tests/testexecution_list_test.go @@ -349,7 +349,7 @@ func TestTestExecutionListCursorEqualSortValue(t *testing.T) { testExecutions[4].Status = "running" }, setAnchorValue: func(params *corerepository.ListParams, testExecution corerepository.TestExecution) { - params.ValueString = testExecution.Status + params.ValueString = string(testExecution.Status) }, expectedIndexes: map[corerepository.OrderDirection][]int{ corerepository.OrderDirectionAsc: {3, 4}, @@ -757,7 +757,7 @@ func testExecutionListOrderByCases() []testExecutionOrderByCase { orderBy: corerepository.OrderByStatus, ascendingIndexes: []int{2, 1, 3, 0, 4}, setAnchorValue: func(params *corerepository.ListParams, testExecution corerepository.TestExecution) { - params.ValueString = testExecution.Status + params.ValueString = string(testExecution.Status) }, }, } diff --git a/tests/testexecution_test.go b/tests/testexecution_test.go index 1ccfe9d..93f4530 100644 --- a/tests/testexecution_test.go +++ b/tests/testexecution_test.go @@ -16,7 +16,7 @@ func TestTestExecutionCRUD(t *testing.T) { testSuiteExecutionRepository := repositories.TestSuiteExecution // create a testSuiteExecution to use for the testExecutions - testSuiteExecution := getDefaultTestSuiteExecution(t, 1) + testSuiteExecution := getUnfinishedTestSuiteExecution(t, 1) createdSuite, err := testSuiteExecutionRepository.Create(t.Context(), &testSuiteExecution) if err != nil { t.Fatalf("Unexpected testSuiteExecution create error: %v", err) @@ -44,7 +44,7 @@ func TestTestExecutionCRUD(t *testing.T) { } // create two testexecutions - testExecution1 := getDefaultTestExecution(t, 1) + testExecution1 := getUnfinishedTestExecution(t, 1) created1, err := repository.Create(t.Context(), &testExecution1) if err != nil { t.Fatalf("Unexpected testExecution1 create error: %v", err) @@ -54,7 +54,7 @@ func TestTestExecutionCRUD(t *testing.T) { } assertEqualTestExecutions(t, testExecution1, *created1) - testExecution2 := getDefaultTestExecution(t, 2) + testExecution2 := getUnfinishedTestExecution(t, 2) created2, err := repository.Create(t.Context(), &testExecution2) if err != nil { t.Fatalf("Unexpected testExecution2 create error: %v", err) @@ -130,16 +130,34 @@ func TestTestExecutionCRUD(t *testing.T) { t.Fatalf("Expected testExecution IDs %v, got %v", []string{testExecution1.ID, testExecution2.ID}, ids) } - // get the testExecutions by testSuiteExecutionID prefix and check that they match - testExecutions3, err := repository.GetByTestSuiteExecutionID(t.Context(), idPrefixTestSuiteExecution+idBody+"1") + // Get aggregated execution states for the suite. + states, alreadyCompleted, err := repository.GetTestSuiteExecutionStates( + t.Context(), + testSuiteExecution.ID, + ) if err != nil { - t.Fatalf("Unexpected testExecution IDs by testSuiteExecutionID error: %v", err) + t.Fatalf("Unexpected GetTestSuiteExecutionStates error: %v", err) + } + + if alreadyCompleted { + t.Fatal("Expected suite execution to be unfinished") } - if len(testExecutions3) != 2 { - t.Fatalf("Expected 2 testExecutions by testSuiteExecutionID, got %d", len(testExecutions3)) + + if len(states) != 1 { + t.Fatalf("Expected 1 status group, got %d", len(states)) + } + + if states[0].Status != testExecution1.Status { + t.Fatalf( + "Expected status %q, got %q", + testExecution1.Status, + states[0].Status, + ) + } + + if states[0].Count != 2 { + t.Fatalf("Expected count 2, got %d", states[0].Count) } - assertEqualTestExecutions(t, testExecution1, testExecutions3[0]) - assertEqualTestExecutions(t, testExecution2, testExecutions3[1]) // set the first testExecution to running and check that it was updated testExecution1.StartedAt = testExecution1.StartedAt.Add(time.Hour) @@ -666,6 +684,11 @@ func TestTestExecutionSetRunning(t *testing.T) { } testExecutions := getTestExecutionsForList(t, testSuiteExecution.ID) + testExecutions[0].Status = "scheduled" + testExecutions[0].FinishedAt = time.Time{} + testExecutions[1].Status = "scheduled" + testExecutions[1].FinishedAt = time.Time{} + createTestExecutions(t, repository, testExecutions[:2]) testCases := []struct { @@ -762,6 +785,14 @@ func TestTestExecutionSetCompleted(t *testing.T) { } testExecutions := getTestExecutionsForList(t, testSuiteExecution.ID) + testExecutions[0].Status = "scheduled" + testExecutions[0].FinishedAt = time.Time{} + testExecutions[1].Status = "scheduled" + testExecutions[1].FinishedAt = time.Time{} + testExecutions[2].Status = "scheduled" + testExecutions[2].FinishedAt = time.Time{} + testExecutions[3].Status = "scheduled" + testExecutions[3].FinishedAt = time.Time{} createTestExecutions(t, repository, testExecutions[:4]) testCases := []struct { @@ -830,7 +861,7 @@ func TestTestExecutionSetCompleted(t *testing.T) { Summary: []byte(`{"result":"error"}`), }, expected: testExecutions[3], - expectedError: corerepository.ErrInvalidPayload, + expectedError: corerepository.ErrInvalidExecutionStatus, }, { name: "nonexistent test execution", @@ -918,7 +949,7 @@ func TestTestExecutionGetIDsByID(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: allIDs[:2], }, { name: "query error", @@ -985,7 +1016,7 @@ func TestTestExecutionCascadeDeleteWithTestSuiteExecution(t *testing.T) { testExecutionRepository := repositories.TestExecution testSuiteExecutionRepository := repositories.TestSuiteExecution - testSuiteExecution := getDefaultTestSuiteExecution(t, 1) + testSuiteExecution := getUnfinishedTestSuiteExecution(t, 1) createdSuite, err := testSuiteExecutionRepository.Create( t.Context(), &testSuiteExecution, @@ -1000,20 +1031,31 @@ func TestTestExecutionCascadeDeleteWithTestSuiteExecution(t *testing.T) { testExecutions := getTestExecutionsForList(t, testSuiteExecution.ID) createTestExecutions(t, testExecutionRepository, testExecutions[:3]) - beforeDelete, err := testExecutionRepository.GetByTestSuiteExecutionID( + beforeDelete, alreadyCompleted, err := testExecutionRepository.GetTestSuiteExecutionStates( t.Context(), testSuiteExecution.ID, ) if err != nil { t.Fatalf( - "Unexpected GetByTestSuiteExecutionID error before parent deletion: %v", + "Unexpected GetTestSuiteExecutionStates error before parent deletion: %v", err, ) } - if len(beforeDelete) != 3 { + + if alreadyCompleted { + t.Fatal("Expected suite execution to be unfinished") + } + + var count int + + for _, state := range beforeDelete { + count += state.Count + } + + if count != 3 { t.Fatalf( "Expected 3 test executions before parent deletion, got %d", - len(beforeDelete), + count, ) } @@ -1028,23 +1070,29 @@ func TestTestExecutionCascadeDeleteWithTestSuiteExecution(t *testing.T) { t.Fatalf("Expected deleted testSuiteExecution to be non-nil") } - afterDelete, err := testExecutionRepository.GetByTestSuiteExecutionID( + afterDelete, alreadyCompleted, err := testExecutionRepository.GetTestSuiteExecutionStates( t.Context(), testSuiteExecution.ID, ) - if err != nil { + + if !errors.Is(err, corerepository.ErrNotFound) { t.Fatalf( - "Unexpected GetByTestSuiteExecutionID error after parent deletion: %v", + "Expected ErrNotFound after parent deletion, got %v", err, ) } - if len(afterDelete) != 0 { + + if afterDelete != nil { t.Fatalf( - "Expected test executions to be deleted with parent, got %d", - len(afterDelete), + "Expected nil states after parent deletion, got %v", + afterDelete, ) } + if alreadyCompleted { + t.Fatal("Expected alreadyCompleted to be false") + } + for _, expected := range testExecutions[:3] { actual, err := testExecutionRepository.Get(t.Context(), expected.ID) if err == nil { @@ -1069,3 +1117,274 @@ func TestTestExecutionCascadeDeleteWithTestSuiteExecution(t *testing.T) { } } } + +func TestTestExecutionGetTestSuiteExecutionStates(t *testing.T) { + repositories := getRepositories(t) + repo := repositories.TestExecution + suiteRepo := repositories.TestSuiteExecution + + // Create two suites to verify that aggregation is scoped correctly. + suite1 := getUnfinishedTestSuiteExecution(t, 1) + if _, err := suiteRepo.Create(t.Context(), &suite1); err != nil { + t.Fatalf("Create suite1: %v", err) + } + + suite2 := getUnfinishedTestSuiteExecution(t, 2) + if _, err := suiteRepo.Create(t.Context(), &suite2); err != nil { + t.Fatalf("Create suite2: %v", err) + } + + baseTime := time.Date( + 2026, time.October, 25, + 10, 0, 0, 0, + time.UTC, + ) + + t.Run("suite without test executions", func(t *testing.T) { + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + t.Context(), + suite1.ID, + ) + if err != nil { + t.Fatalf("GetTestSuiteExecutionStates: %v", err) + } + + if alreadyCompleted { + t.Fatal("Expected suite execution to be unfinished") + } + + if len(states) != 0 { + t.Fatalf("Expected empty states, got %v", states) + } + }) + + // Create two completed tests with different completion timestamps, + // another completed test with a different status, and a scheduled test. + executions := []struct { + suiteID string + status corerepository.ExecutionStatus + finishedAt time.Time + }{ + { + suiteID: suite1.ID, + status: corerepository.ExecutionStatusPassed, + finishedAt: baseTime.Add(time.Minute), + }, + { + suiteID: suite1.ID, + status: corerepository.ExecutionStatusPassed, + finishedAt: baseTime.Add(3 * time.Minute), + }, + { + suiteID: suite1.ID, + status: corerepository.ExecutionStatusFailed, + finishedAt: baseTime.Add(2 * time.Minute), + }, + { + suiteID: suite1.ID, + status: corerepository.ExecutionStatusScheduled, + finishedAt: time.Time{}, + }, + { + suiteID: suite2.ID, + status: corerepository.ExecutionStatusPassed, + finishedAt: baseTime.Add(10 * time.Minute), + }, + } + + for i, execution := range executions { + testExecution := getDefaultTestExecution(t, i+1) + testExecution.TestSuiteExecutionID = execution.suiteID + testExecution.Status = execution.status + testExecution.FinishedAt = execution.finishedAt + + if _, err := repo.Create(t.Context(), &testExecution); err != nil { + t.Fatalf("Create test execution %d: %v", i+1, err) + } + } + + t.Run("aggregates statuses and timestamps", func(t *testing.T) { + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + t.Context(), + suite1.ID, + ) + if err != nil { + t.Fatalf("GetTestSuiteExecutionStates: %v", err) + } + + if alreadyCompleted { + t.Fatal("Expected suite execution to be unfinished") + } + + if len(states) != 3 { + t.Fatalf("Expected 3 status groups, got %d", len(states)) + } + + // GROUP BY does not guarantee ordering. + byStatus := make(map[corerepository.ExecutionStatus]corerepository.TestSuiteExecutionState) + + for _, state := range states { + byStatus[state.Status] = state + } + + testCases := []struct { + status corerepository.ExecutionStatus + count int + finishedAt time.Time + }{ + { + status: corerepository.ExecutionStatusPassed, + count: 2, + finishedAt: baseTime.Add(3 * time.Minute), + }, + { + status: corerepository.ExecutionStatusFailed, + count: 1, + finishedAt: baseTime.Add(2 * time.Minute), + }, + { + status: corerepository.ExecutionStatusScheduled, + count: 1, + finishedAt: time.Time{}, + }, + } + + for _, tc := range testCases { + t.Run(string(tc.status), func(t *testing.T) { + state, ok := byStatus[tc.status] + if !ok { + t.Fatalf("Missing status group %q", tc.status) + } + + if state.Count != tc.count { + t.Errorf( + "Expected count %d, got %d", + tc.count, + state.Count, + ) + } + + if !state.LastTestFinishedAt.Equal(tc.finishedAt) { + t.Errorf( + "Expected LastTestFinishedAt %v, got %v", + tc.finishedAt, + state.LastTestFinishedAt, + ) + } + }) + } + }) + + t.Run("scopes results to suite", func(t *testing.T) { + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + t.Context(), + suite2.ID, + ) + if err != nil { + t.Fatalf("GetTestSuiteExecutionStates: %v", err) + } + + if alreadyCompleted { + t.Fatal("Expected suite execution to be unfinished") + } + + if len(states) != 1 { + t.Fatalf("Expected 1 status group, got %d", len(states)) + } + + if states[0].Status != "passed" { + t.Errorf("Expected status passed, got %q", states[0].Status) + } + + if states[0].Count != 1 { + t.Errorf("Expected count 1, got %d", states[0].Count) + } + + expected := baseTime.Add(10 * time.Minute) + + if !states[0].LastTestFinishedAt.Equal(expected) { + t.Errorf( + "Expected LastTestFinishedAt %v, got %v", + expected, + states[0].LastTestFinishedAt, + ) + } + }) + + t.Run("already completed suite", func(t *testing.T) { + err := suiteRepo.SetCompleted( + t.Context(), + &corerepository.SetCompletedParams{ + ID: suite2.ID, + Status: "passed", + FinishedAt: baseTime.Add(20 * time.Minute), + }, + ) + if err != nil { + t.Fatalf("SetCompleted: %v", err) + } + + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + t.Context(), + suite2.ID, + ) + if err != nil { + t.Fatalf("GetTestSuiteExecutionStates: %v", err) + } + + if !alreadyCompleted { + t.Fatal("Expected alreadyCompleted to be true") + } + + if states != nil { + t.Fatalf( + "Expected nil states for completed suite, got %v", + states, + ) + } + }) + + t.Run("nonexistent suite", func(t *testing.T) { + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + t.Context(), + "does-not-exist", + ) + + if !errors.Is(err, corerepository.ErrNotFound) { + t.Fatalf( + "Expected ErrNotFound, got %v", + err, + ) + } + + if states != nil { + t.Fatalf("Expected nil states, got %v", states) + } + + if alreadyCompleted { + t.Fatal("Expected alreadyCompleted to be false") + } + }) + + t.Run("canceled context", func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + cancel() + + states, alreadyCompleted, err := repo.GetTestSuiteExecutionStates( + ctx, + suite1.ID, + ) + + if !errors.Is(err, context.Canceled) { + t.Fatalf("Expected context.Canceled, got %v", err) + } + + if states != nil { + t.Fatalf("Expected nil states, got %v", states) + } + + if alreadyCompleted { + t.Fatal("Expected alreadyCompleted to be false") + } + }) +} diff --git a/tests/testsuite_test.go b/tests/testsuite_test.go index 7f01f65..b605ed5 100644 --- a/tests/testsuite_test.go +++ b/tests/testsuite_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "slices" + "strings" "testing" "time" @@ -337,7 +338,7 @@ func TestTestSuiteGetIDsByID(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: allIDs[:2], }, { name: "query error", @@ -374,11 +375,18 @@ func TestTestSuiteGetIDsByName(t *testing.T) { resources := getResourcesForList(t, "test_suite") createTestSuiteResources(t, repository, resources) - allIDs := make([]string, len(resources)) - for i := range resources { - allIDs[i] = resources[i].ID + slices.SortFunc(resources, func(a, b corerepository.Resource) int { + if c := strings.Compare(a.Name, b.Name); c != 0 { + return c + } + + return strings.Compare(a.ID, b.ID) + }) + + expectedIDs := []string{ + resources[0].ID, + resources[1].ID, } - slices.Sort(allIDs) testCases := []struct { name string @@ -393,7 +401,7 @@ func TestTestSuiteGetIDsByName(t *testing.T) { context: func() context.Context { return t.Context() }, - expectedIDs: allIDs, + expectedIDs: expectedIDs, }, { name: "query error", diff --git a/tests/testsuiteexecution_list_test.go b/tests/testsuiteexecution_list_test.go index 2b413c3..e0594df 100644 --- a/tests/testsuiteexecution_list_test.go +++ b/tests/testsuiteexecution_list_test.go @@ -177,7 +177,7 @@ func TestTestSuiteExecutionListCursorEqualSortValue(t *testing.T) { executions[0].Status, executions[1].Status, executions[2].Status, executions[3].Status, executions[4].Status = "error", "failed", "failed", "failed", "running" }, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }, }, } @@ -508,7 +508,7 @@ func TestTestSuiteExecutionListQueryErrors(t *testing.T) { corerepository.PositionAfter, corerepository.OrderDirectionAsc, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }, }, { @@ -517,7 +517,7 @@ func TestTestSuiteExecutionListQueryErrors(t *testing.T) { corerepository.PositionAfter, corerepository.OrderDirectionDesc, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }, }, { @@ -526,7 +526,7 @@ func TestTestSuiteExecutionListQueryErrors(t *testing.T) { corerepository.PositionBefore, corerepository.OrderDirectionAsc, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }, }, { @@ -535,7 +535,7 @@ func TestTestSuiteExecutionListQueryErrors(t *testing.T) { corerepository.PositionBefore, corerepository.OrderDirectionDesc, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }, }, } @@ -600,7 +600,13 @@ func getTestSuiteExecutionsForList(t *testing.T) []corerepository.TestSuiteExecu finishedAt := []time.Duration{4 * time.Second, time.Second, 3 * time.Second, time.Second, 2 * time.Second} base := time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC) executions := make([]corerepository.TestSuiteExecution, len(startedAt)) - statuses := []string{"passed", "failed", "error", "failed", "running"} + statuses := []corerepository.ExecutionStatus{ + corerepository.ExecutionStatusPassed, + corerepository.ExecutionStatusFailed, + corerepository.ExecutionStatusError, + corerepository.ExecutionStatusFailed, + corerepository.ExecutionStatusRunning, + } for i := range executions { executions[i] = getTestSuiteExecution(t, i+1, base.Add(startedAt[i]), base.Add(finishedAt[i]), statuses[i], nil) } @@ -619,7 +625,7 @@ func testSuiteExecutionListOrderByCases() []testSuiteExecutionOrderByCase { params.ValueTimestamp = execution.FinishedAt }}, {"status", corerepository.OrderByStatus, []int{2, 1, 3, 0, 4}, func(params *corerepository.ListParams, execution corerepository.TestSuiteExecution) { - params.ValueString = execution.Status + params.ValueString = string(execution.Status) }}, } } diff --git a/tests/testsuiteexecution_test.go b/tests/testsuiteexecution_test.go index 01fac4f..97e08ec 100644 --- a/tests/testsuiteexecution_test.go +++ b/tests/testsuiteexecution_test.go @@ -25,8 +25,8 @@ func TestTestSuiteExecutionCRUD(t *testing.T) { t.Fatalf("Expected empty test suite executions list, got %d test suite executions", len(executions)) } - execution1 := createTestSuiteExecution(t, repository, 1) - execution2 := createTestSuiteExecution(t, repository, 2) + execution1 := createUnfinishedTestSuiteExecution(t, repository, 1) + execution2 := createUnfinishedTestSuiteExecution(t, repository, 2) childExecution1 := getDefaultTestExecution(t, 1) childExecution1.TestSuiteExecutionID = execution1.ID @@ -312,8 +312,8 @@ func TestTestSuiteExecutionSetRunning(t *testing.T) { repositories := getRepositories(t) repository := repositories.TestSuiteExecution executions := []corerepository.TestSuiteExecution{ - createTestSuiteExecution(t, repository, 1), - createTestSuiteExecution(t, repository, 2), + createUnfinishedTestSuiteExecution(t, repository, 1), + createUnfinishedTestSuiteExecution(t, repository, 2), } for i, offset := range []time.Duration{time.Hour, 2 * time.Hour} { @@ -376,17 +376,17 @@ func TestTestSuiteExecutionSetCompleted(t *testing.T) { repositories := getRepositories(t) repository := repositories.TestSuiteExecution executions := []corerepository.TestSuiteExecution{ - createTestSuiteExecution(t, repository, 1), - createTestSuiteExecution(t, repository, 2), - createTestSuiteExecution(t, repository, 3), - createTestSuiteExecution(t, repository, 4), + createUnfinishedTestSuiteExecution(t, repository, 1), + createUnfinishedTestSuiteExecution(t, repository, 2), + createUnfinishedTestSuiteExecution(t, repository, 3), + createUnfinishedTestSuiteExecution(t, repository, 4), } testCases := []struct { name string index int offset time.Duration - status string + status corerepository.ExecutionStatus summary []byte expectedError error }{ @@ -394,30 +394,30 @@ func TestTestSuiteExecutionSetCompleted(t *testing.T) { name: "passed test suite execution", index: 0, offset: time.Hour, - status: "passed", + status: corerepository.ExecutionStatusPassed, summary: []byte(`{"result":"passed"}`), }, { name: "failed test suite execution", index: 1, offset: 2 * time.Hour, - status: "failed", + status: corerepository.ExecutionStatusFailed, summary: []byte(`{"result":"failed"}`), }, { name: "errored test suite execution", index: 2, offset: 3 * time.Hour, - status: "error", + status: corerepository.ExecutionStatusError, summary: []byte(`{"result":"error"}`), }, { name: "test suite execution invalid status", index: 3, offset: 4 * time.Hour, - status: "invalid", + status: corerepository.ExecutionStatus("invalid"), summary: []byte(`{"result":"invalid"}`), - expectedError: corerepository.ErrInvalidPayload, + expectedError: corerepository.ErrInvalidExecutionStatus, }, } @@ -575,6 +575,20 @@ func createTestSuiteExecution(t *testing.T, repository corerepository.TestSuiteE return execution } +func createUnfinishedTestSuiteExecution(t *testing.T, repository corerepository.TestSuiteExecutionRepository, index int) corerepository.TestSuiteExecution { + t.Helper() + execution := getUnfinishedTestSuiteExecution(t, index) + created, err := repository.Create(t.Context(), &execution) + if err != nil { + t.Fatalf("Unexpected test suite execution%d create error: %v", index, err) + } + if created == nil { + t.Fatalf("Expected created%d test suite execution to be non-nil", index) + } + assertEqualTestSuiteExecutions(t, execution, *created) + return execution +} + func testTestSuiteExecutionGetOrDeleteErrors( t *testing.T, operation string, diff --git a/tools/go.mod b/tools/go.mod index e9b10ce..bb2785e 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -5,6 +5,7 @@ go 1.27 require ( github.com/golangci/golangci-lint/v2 v2.14.0 github.com/sqlc-dev/sqlc v1.31.1 + golang.org/x/vuln v1.9.0 ) require ( @@ -233,12 +234,13 @@ require ( go.yaml.in/yaml/v3 v3.0.5 // indirect golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect golang.org/x/exp/typeparams v0.0.0-20260811152304-ee035b5b010f // indirect - golang.org/x/mod v0.41.0 // indirect - golang.org/x/net v0.59.0 // indirect - golang.org/x/sync v0.23.0 // indirect - golang.org/x/sys v0.48.0 // indirect - golang.org/x/text v0.42.0 // indirect - golang.org/x/tools v0.50.0 // indirect + golang.org/x/mod v0.42.0 // indirect + golang.org/x/net v0.61.0 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/telemetry v0.0.0-20261010193011-f71dc92da45e // indirect + golang.org/x/text v0.43.0 // indirect + golang.org/x/tools v0.52.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20260817212433-ac3dfec99bb1 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 // indirect google.golang.org/grpc v1.85.0-dev.0.20260825072537-93e31b48545e // indirect diff --git a/tools/go.sum b/tools/go.sum index 9b2f7ec..8b0b773 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -315,6 +315,8 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/cel-go v0.30.0 h1:ll54AkzKunWkBn9wSoiUXbFZXYZTkdJGNXTBXUoolGo= github.com/google/cel-go v0.30.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8= +github.com/google/go-cmdtest v0.4.1-0.20220921163831-55ab3332a786 h1:rcv+Ippz6RAtvaGgKxc+8FQIpxHgsF+HBzPyYL2cyVU= +github.com/google/go-cmdtest v0.4.1-0.20220921163831-55ab3332a786/go.mod h1:apVn/GCasLZUVpAJ6oWAuyP7Ne7CEsQbTnc0plM3m+o= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -340,6 +342,7 @@ github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3 h1:LMLX+LgTNWpfvCBdFebv6EsYotImrt/Ppc5cXIriCSo= github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3/go.mod h1:jl5iWTm0/hd5PjEYEOuwAJ57L/CibdZfrqZ5XA5GrCk= +github.com/google/renameio v0.1.0 h1:GOZbcHa3HfsPKPlmyPyN2KEohoMXOhdMbHrvbpl2QaA= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -751,8 +754,8 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= +golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= +golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -787,8 +790,8 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= -golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/net v0.61.0 h1:RR+6j/BTBCrggOLugxa+66R8FvoN6MHlzDlZIbN9YFw= +golang.org/x/net v0.61.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -808,8 +811,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= -golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -853,8 +856,10 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/telemetry v0.0.0-20261010193011-f71dc92da45e h1:P6NGsugec4oASJ+yKMNgTndeo0h2CbixZLvQIe9h5R4= +golang.org/x/telemetry v0.0.0-20261010193011-f71dc92da45e/go.mod h1:GNu+nb+E5axfGmP+Uu3ZVQu5lE/Xk42MjZOHWUD94/o= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -866,8 +871,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= -golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -920,12 +925,14 @@ golang.org/x/tools v0.1.1-0.20210302220138-2ac05c832e1a/go.mod h1:9bzcO0MWcOuT0t golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.50.0 h1:c2ifzfcuY7L90lZ2aKd8S4K2NpASF08SZx9ZuJkHmSU= -golang.org/x/tools v0.50.0/go.mod h1:7ulVMw3831Mwi5EZD6RomGyffr4VFjuNYXf2BbCEAV0= +golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= +golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= golang.org/x/tools/go/expect v0.1.1-deprecated h1:jpBZDwmgPhXsKZC6WhL20P4b/wmnpsEAGHaNy0n/rJM= golang.org/x/tools/go/expect v0.1.1-deprecated/go.mod h1:eihoPOH+FgIqa3FpoTwguz/bVUSGBlGQU67vpBeOrBY= golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated h1:1h2MnaIAIXISqTFKdENegdpAgUXz6NrPEsbIeWaBRvM= golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated/go.mod h1:RVAQXBGNv1ib0J382/DPCRS/BPnsGebyM1Gj5VSDpG8= +golang.org/x/vuln v1.9.0 h1:12VaqnI9kJfqwRoPptxoqsUhQtdfrSbdksT7X/KY/sc= +golang.org/x/vuln v1.9.0/go.mod h1:E/Frwm0xp49fTyBW+6CQHdkvobknbwUM49yEpMGUauw= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/tools/tools.go b/tools/tools.go index 2df0b04..7654229 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -8,4 +8,5 @@ package tools import ( _ "github.com/golangci/golangci-lint/v2/cmd/golangci-lint" _ "github.com/sqlc-dev/sqlc/cmd/sqlc" + _ "golang.org/x/vuln/cmd/govulncheck" ) diff --git a/tools/tools.mk b/tools/tools.mk index fd30879..1b20d10 100644 --- a/tools/tools.mk +++ b/tools/tools.mk @@ -12,9 +12,11 @@ ARCH := $(shell uname -m) GOLANGCI_LINT_VERSION ?= $(shell grep 'github.com/golangci/golangci-lint/v2 ' $(TOOLS_MOD_FILE) | awk '{print $$2}') SQLC_VERSION ?= $(shell grep 'github.com/sqlc-dev/sqlc ' $(TOOLS_MOD_FILE) | awk '{print $$2}') +GOVULNCHECK_VERSION ?= $(shell go -C $(TOOLS_DIR) list -m -f '{{.Version}}' golang.org/x/vuln) GOLANGCI_LINT := $(BIN_DIR)/golangci-lint-$(OS)-$(ARCH)-$(GOLANGCI_LINT_VERSION) SQLC := $(BIN_DIR)/sqlc-$(OS)-$(ARCH)-$(SQLC_VERSION) +GOVULNCHECK := $(BIN_DIR)/govulncheck-$(OS)-$(ARCH)-$(GOVULNCHECK_VERSION) $(GOLANGCI_LINT): $(call go-install-tool,$@,github.com/golangci/golangci-lint/v2/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) @@ -22,8 +24,12 @@ $(GOLANGCI_LINT): $(SQLC): $(call go-install-tool,$@,github.com/sqlc-dev/sqlc/cmd/sqlc,$(SQLC_VERSION)) +$(GOVULNCHECK): + $(call go-install-tool,$@,golang.org/x/vuln/cmd/govulncheck,$(GOVULNCHECK_VERSION)) + GOLANGCI_LINT_LINK := $(BIN_DIR)/golangci-lint SQLC_LINK := $(BIN_DIR)/sqlc +GOVULNCHECK_LINK := $(BIN_DIR)/govulncheck .PHONY: $(GOLANGCI_LINT_LINK) $(GOLANGCI_LINT_LINK): $(GOLANGCI_LINT) @@ -33,7 +39,11 @@ $(GOLANGCI_LINT_LINK): $(GOLANGCI_LINT) $(SQLC_LINK): $(SQLC) $(call create-symlink,$(SQLC),$(SQLC_LINK)) -TOOLS := install-golangci-lint install-sqlc +.PHONY: $(GOVULNCHECK_LINK) +$(GOVULNCHECK_LINK): $(GOVULNCHECK) + $(call create-symlink,$(GOVULNCHECK),$(GOVULNCHECK_LINK)) + +TOOLS := install-golangci-lint install-sqlc install-govulncheck .PHONY: install-tools install-tools: $(TOOLS) @@ -44,6 +54,9 @@ install-golangci-lint: $(GOLANGCI_LINT) $(GOLANGCI_LINT_LINK) .PHONY: install-sqlc install-sqlc: $(SQLC) $(SQLC_LINK) +.PHONY: install-govulncheck +install-govulncheck: $(GOVULNCHECK) $(GOVULNCHECK_LINK) + .PHONY: clean-tools clean-tools: rm -rf $(BIN_DIR)/*