Skip to content

Commit 994d858

Browse files
authored
fix: retain a failed spec's error message when appending a suite run #219 (#220)
1 parent 8a861d2 commit 994d858

3 files changed

Lines changed: 85 additions & 7 deletions

File tree

internal/domains/testing/infrastructure/database_converter.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,23 @@ func (c *DatabaseConverter) ConvertDomainSuiteRunsToDatabase(domainSuiteRuns []d
7272
return dbSuiteRuns
7373
}
7474

75+
// mergeErrorMessage combines a spec's ErrorMessage and FailureMessage into the
76+
// single error_message column the database has. ErrorMessage (status "error")
77+
// takes priority; FailureMessage (status "failed", the common case) is used
78+
// as a fallback when ErrorMessage is empty.
79+
func mergeErrorMessage(errorMessage, failureMessage string) string {
80+
if errorMessage == "" && failureMessage != "" {
81+
return failureMessage
82+
}
83+
return errorMessage
84+
}
85+
7586
// ConvertDomainSpecRunsToDatabase converts domain SpecRuns to database SpecRuns
7687
func (c *DatabaseConverter) ConvertDomainSpecRunsToDatabase(domainSpecRuns []*domain.SpecRun) []database.SpecRun {
7788
dbSpecRuns := make([]database.SpecRun, len(domainSpecRuns))
7889

7990
for i, domainSpec := range domainSpecRuns {
80-
// Combine ErrorMessage and FailureMessage into ErrorMessage
81-
errorMessage := domainSpec.ErrorMessage
82-
if errorMessage == "" && domainSpec.FailureMessage != "" {
83-
errorMessage = domainSpec.FailureMessage
84-
}
91+
errorMessage := mergeErrorMessage(domainSpec.ErrorMessage, domainSpec.FailureMessage)
8592

8693
// Convert tags
8794
dbTags := c.ConvertDomainTagsToDatabase(domainSpec.Tags)

internal/domains/testing/infrastructure/gorm_spec_run_repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *GormSpecRunRepository) Create(ctx context.Context, specRun *domain.Spec
3232
StartTime: specRun.StartTime,
3333
EndTime: specRun.EndTime,
3434
Duration: int64(specRun.Duration / time.Millisecond),
35-
ErrorMessage: specRun.ErrorMessage,
35+
ErrorMessage: mergeErrorMessage(specRun.ErrorMessage, specRun.FailureMessage),
3636
StackTrace: specRun.StackTrace,
3737
RetryCount: specRun.RetryCount,
3838
IsFlaky: specRun.IsFlaky,
@@ -62,7 +62,7 @@ func (r *GormSpecRunRepository) CreateBatch(ctx context.Context, specRuns []*dom
6262
StartTime: specRun.StartTime,
6363
EndTime: specRun.EndTime,
6464
Duration: int64(specRun.Duration / time.Millisecond),
65-
ErrorMessage: specRun.ErrorMessage,
65+
ErrorMessage: mergeErrorMessage(specRun.ErrorMessage, specRun.FailureMessage),
6666
StackTrace: specRun.StackTrace,
6767
RetryCount: specRun.RetryCount,
6868
IsFlaky: specRun.IsFlaky,

internal/domains/testing/infrastructure/gorm_spec_run_repository_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,43 @@ var _ = Describe("GormSpecRunRepository", func() {
112112
})
113113
})
114114

115+
Context("when the spec run has only a FailureMessage (status \"failed\")", func() {
116+
It("should persist FailureMessage into the error_message column", func() {
117+
endTime := time.Now().Add(time.Second)
118+
failedSpec := &domain.SpecRun{
119+
SuiteRunID: 1,
120+
Name: "test-spec-failed",
121+
Status: "failed",
122+
StartTime: time.Now(),
123+
EndTime: &endTime,
124+
Duration: time.Second,
125+
FailureMessage: "expected 2 to equal 3",
126+
}
127+
128+
mock.ExpectBegin()
129+
mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "spec_runs"`)).
130+
WithArgs(
131+
AnyTime{}, AnyTime{}, nil,
132+
failedSpec.SuiteRunID,
133+
failedSpec.Name,
134+
failedSpec.Status,
135+
AnyTime{}, AnyTime{},
136+
int64(1000),
137+
failedSpec.FailureMessage,
138+
failedSpec.StackTrace,
139+
failedSpec.RetryCount,
140+
failedSpec.IsFlaky,
141+
).
142+
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(456))
143+
mock.ExpectCommit()
144+
145+
err := repository.Create(ctx, failedSpec)
146+
147+
Expect(err).NotTo(HaveOccurred())
148+
Expect(failedSpec.ID).To(Equal(uint(456)))
149+
})
150+
})
151+
115152
Context("when creation fails", func() {
116153
It("should return an error", func() {
117154
mock.ExpectBegin()
@@ -180,6 +217,40 @@ var _ = Describe("GormSpecRunRepository", func() {
180217
})
181218
})
182219

220+
Context("when the batch has a spec run with only a FailureMessage", func() {
221+
It("should persist FailureMessage into the error_message column", func() {
222+
now := time.Now()
223+
end := now.Add(time.Second)
224+
225+
specRuns := []*domain.SpecRun{
226+
{
227+
SuiteRunID: 1,
228+
Name: "test-spec-failed",
229+
Status: "failed",
230+
StartTime: now,
231+
EndTime: &end,
232+
Duration: time.Second,
233+
FailureMessage: "expected 2 to equal 3",
234+
},
235+
}
236+
237+
mock.ExpectBegin()
238+
mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "spec_runs"`)).
239+
WithArgs(
240+
AnyTime{}, AnyTime{}, nil,
241+
uint(1), "test-spec-failed", "failed", AnyTime{}, AnyTime{}, int64(1000),
242+
"expected 2 to equal 3", "", 0, false,
243+
).
244+
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
245+
mock.ExpectCommit()
246+
247+
err := repository.CreateBatch(ctx, specRuns)
248+
249+
Expect(err).NotTo(HaveOccurred())
250+
Expect(specRuns[0].ID).To(Equal(uint(1)))
251+
})
252+
})
253+
183254
Context("when batch creation fails", func() {
184255
It("should return an error", func() {
185256
now := time.Now()

0 commit comments

Comments
 (0)