Skip to content

Commit

Permalink
fix: do not return empty latestExecution with Test/TestSuite (#4636)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Nov 17, 2023
1 parent dad1937 commit 0a17c8d
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion internal/app/api/v1/executions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r MockExecutionResultsRepository) GetByNameAndTest(ctx context.Context, na
panic("not implemented")
}

func (r MockExecutionResultsRepository) GetLatestByTest(ctx context.Context, testName string) (testkube.Execution, error) {
func (r MockExecutionResultsRepository) GetLatestByTest(ctx context.Context, testName string) (*testkube.Execution, error) {
panic("not implemented")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/v1/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s TestkubeAPI) GetTestWithExecutionHandler() fiber.Handler {

return c.JSON(testkube.TestWithExecution{
Test: &test,
LatestExecution: &execution,
LatestExecution: execution,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/v1/testsuites.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (s TestkubeAPI) GetTestSuiteWithExecutionHandler() fiber.Handler {

return c.JSON(testkube.TestSuiteWithExecution{
TestSuite: &testSuite,
LatestExecution: &execution,
LatestExecution: execution,
})
}
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/cloud/data/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,28 @@ func (r *CloudRepository) getLatestByTest(ctx context.Context, testName, sortFie
}

// TODO: When it will be implemented, replace with a new Cloud command, to avoid 2 calls with 2 sort fields
func (r *CloudRepository) GetLatestByTest(ctx context.Context, testName string) (testkube.Execution, error) {
func (r *CloudRepository) GetLatestByTest(ctx context.Context, testName string) (*testkube.Execution, error) {
startExecution, startErr := r.getLatestByTest(ctx, testName, "starttime")
if startErr != nil && startErr != mongo.ErrNoDocuments {
return testkube.Execution{}, startErr
return nil, startErr
}
endExecution, endErr := r.getLatestByTest(ctx, testName, "endtime")
if endErr != nil && endErr != mongo.ErrNoDocuments {
return testkube.Execution{}, endErr
return nil, endErr
}

if startErr == nil && endErr == nil {
if startExecution.StartTime.After(endExecution.EndTime) {
return startExecution, nil
return &startExecution, nil
} else {
return endExecution, nil
return &endExecution, nil
}
} else if startErr == nil {
return startExecution, nil
return &startExecution, nil
} else if endErr == nil {
return endExecution, nil
return &endExecution, nil
}
return testkube.Execution{}, startErr
return nil, startErr
}

// TODO: When it will be implemented, replace with a new Cloud command, to avoid 2 calls with 2 sort fields
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/data/result/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestCloudResultRepository_GetLatestByTest(t *testing.T) {

result, err := repo.GetLatestByTest(ctx, testName)
assert.NoError(t, err)
assert.Equal(t, endExecution, result)
assert.Equal(t, &endExecution, result)
}

func TestCloudResultRepository_Insert(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/cloud/data/testresult/testresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ func (r *CloudRepository) getLatestByTestSuite(ctx context.Context, testSuiteNam
}

// TODO: When it will be implemented, replace with a new Cloud command, to avoid 2 calls with 2 sort fields
func (r *CloudRepository) GetLatestByTestSuite(ctx context.Context, testSuiteName string) (testkube.TestSuiteExecution, error) {
func (r *CloudRepository) GetLatestByTestSuite(ctx context.Context, testSuiteName string) (*testkube.TestSuiteExecution, error) {
startExecution, startErr := r.getLatestByTestSuite(ctx, testSuiteName, "starttime")
if startErr != nil && startErr != mongo.ErrNoDocuments {
return testkube.TestSuiteExecution{}, startErr
return nil, startErr
}
endExecution, endErr := r.getLatestByTestSuite(ctx, testSuiteName, "endtime")
if endErr != nil && endErr != mongo.ErrNoDocuments {
return testkube.TestSuiteExecution{}, endErr
return nil, endErr
}

if startErr == nil && endErr == nil {
if startExecution.StartTime.After(endExecution.EndTime) {
return startExecution, nil
return &startExecution, nil
} else {
return endExecution, nil
return &endExecution, nil
}
} else if startErr == nil {
return startExecution, nil
return &startExecution, nil
} else if endErr == nil {
return endExecution, nil
return &endExecution, nil
}
return testkube.TestSuiteExecution{}, startErr
return nil, startErr
}

func (r *CloudRepository) getLatestByTestSuites(ctx context.Context, testSuiteNames []string, sortField string) (executions []testkube.TestSuiteExecution, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/containerexecutor/containerexecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (r FakeResultRepository) GetByNameAndTest(ctx context.Context, name, testNa
panic("implement me")
}

func (r FakeResultRepository) GetLatestByTest(ctx context.Context, testName string) (testkube.Execution, error) {
func (r FakeResultRepository) GetLatestByTest(ctx context.Context, testName string) (*testkube.Execution, error) {
//TODO implement me
panic("implement me")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/repository/result/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Repository interface {
// GetByNameAndTest gets execution result by name and test name
GetByNameAndTest(ctx context.Context, name, testName string) (testkube.Execution, error)
// GetLatestByTest gets latest execution result by test
GetLatestByTest(ctx context.Context, testName string) (testkube.Execution, error)
GetLatestByTest(ctx context.Context, testName string) (*testkube.Execution, error)
// GetLatestByTests gets latest execution results by test names
GetLatestByTests(ctx context.Context, testNames []string) (executions []testkube.Execution, err error)
// GetExecutions gets executions using a filter, use filter with no data for all
Expand Down
4 changes: 2 additions & 2 deletions pkg/repository/result/mock_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/repository/result/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (r *MongoRepository) GetLatestTestNumber(ctx context.Context, testName stri
return result.Number, err
}

func (r *MongoRepository) GetLatestByTest(ctx context.Context, testName string) (result testkube.Execution, err error) {
func (r *MongoRepository) GetLatestByTest(ctx context.Context, testName string) (*testkube.Execution, error) {
opts := options.Aggregate()
pipeline := []bson.M{
{"$documents": bson.A{bson.M{"name": testName}}},
Expand Down Expand Up @@ -173,24 +173,24 @@ func (r *MongoRepository) GetLatestByTest(ctx context.Context, testName string)
}
cursor, err := r.db.Aggregate(ctx, pipeline, opts)
if err != nil {
return result, err
return nil, err
}
var items []testkube.Execution
err = cursor.All(ctx, &items)
if err != nil {
return result, err
return nil, err
}
if len(items) == 0 {
return result, mongo.ErrNoDocuments
return nil, mongo.ErrNoDocuments
}
result = items[0]
result := (&items[0]).UnscapeDots()
if len(result.ExecutionResult.Output) == 0 {
result.ExecutionResult.Output, err = r.OutputRepository.GetOutput(ctx, result.Id, result.TestName, "")
if err == mongo.ErrNoDocuments {
err = nil
}
}
return *result.UnscapeDots(), err
return result, err
}

func (r *MongoRepository) GetLatestByTests(ctx context.Context, testNames []string) (executions []testkube.Execution, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/repository/testresult/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Repository interface {
// GetByNameAndTestSuite gets execution result by name
GetByNameAndTestSuite(ctx context.Context, name, testSuiteName string) (testkube.TestSuiteExecution, error)
// GetLatestByTestSuite gets latest execution result by test suite
GetLatestByTestSuite(ctx context.Context, testSuiteName string) (testkube.TestSuiteExecution, error)
GetLatestByTestSuite(ctx context.Context, testSuiteName string) (*testkube.TestSuiteExecution, error)
// GetLatestByTestSuites gets latest execution results by test suite names
GetLatestByTestSuites(ctx context.Context, testSuiteNames []string) (executions []testkube.TestSuiteExecution, err error)
// GetExecutionsTotals gets executions total stats using a filter, use filter with no data for all
Expand Down
4 changes: 2 additions & 2 deletions pkg/repository/testresult/mock_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pkg/repository/testresult/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *MongoRepository) GetByNameAndTestSuite(ctx context.Context, name, testS
return *result.UnscapeDots(), err
}

func (r *MongoRepository) GetLatestByTestSuite(ctx context.Context, testSuiteName string) (result testkube.TestSuiteExecution, err error) {
func (r *MongoRepository) GetLatestByTestSuite(ctx context.Context, testSuiteName string) (*testkube.TestSuiteExecution, error) {
opts := options.Aggregate()
pipeline := []bson.M{
{"$documents": bson.A{bson.M{"name": testSuiteName}}},
Expand Down Expand Up @@ -88,17 +88,17 @@ func (r *MongoRepository) GetLatestByTestSuite(ctx context.Context, testSuiteNam
}
cursor, err := r.db.Aggregate(ctx, pipeline, opts)
if err != nil {
return result, err
return nil, err
}
var items []testkube.TestSuiteExecution
err = cursor.All(ctx, &items)
if err != nil {
return result, err
return nil, err
}
if len(items) == 0 {
return result, mongo.ErrNoDocuments
return nil, mongo.ErrNoDocuments
}
return *items[0].UnscapeDots(), err
return items[0].UnscapeDots(), err
}

func (r *MongoRepository) GetLatestByTestSuites(ctx context.Context, testSuiteNames []string) (executions []testkube.TestSuiteExecution, err error) {
Expand Down

0 comments on commit 0a17c8d

Please sign in to comment.