-
Notifications
You must be signed in to change notification settings - Fork 67
/
test_run_event.go
165 lines (139 loc) 路 3.59 KB
/
test_run_event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package testdb
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
)
const insertTestRunEventQuery = `
INSERT INTO test_run_events (
"test_id",
"run_id",
"type",
"stage",
"description",
"created_at",
"data_store_connection",
"polling",
"outputs"
) VALUES (
$1, -- test_id
$2, -- run_id
$3, -- type
$4, -- stage
$5, -- description
$6, -- created_at
$7, -- data_store_connection
$8, -- polling
$9 -- outputs
)
RETURNING "id"
`
func (td *postgresDB) CreateTestRunEvent(ctx context.Context, event model.TestRunEvent) error {
dataStoreConnectionJSON, err := json.Marshal(event.DataStoreConnection)
if err != nil {
return fmt.Errorf("could not marshal data store connection into JSON: %w", err)
}
pollingJSON, err := json.Marshal(event.Polling)
if err != nil {
return fmt.Errorf("could not marshal polling into JSON: %w", err)
}
outputsJSON, err := json.Marshal(event.Outputs)
if err != nil {
return fmt.Errorf("could not marshal outputs into JSON: %w", err)
}
if event.CreatedAt.IsZero() {
event.CreatedAt = time.Now()
}
err = td.db.QueryRowContext(
ctx,
insertTestRunEventQuery,
event.TestID,
event.RunID,
event.Type,
event.Stage,
event.Description,
event.CreatedAt,
dataStoreConnectionJSON,
pollingJSON,
outputsJSON,
).Scan(&event.ID)
if err != nil {
return fmt.Errorf("could not insert event into database: %w", err)
}
return nil
}
const getTestRunEventsQuery = `
SELECT
"id",
"test_id",
"run_id",
"type",
"stage",
"description",
"created_at",
"data_store_connection",
"polling",
"outputs"
FROM test_run_events WHERE "test_id" = $1 AND "run_id" = $2 ORDER BY "created_at" ASC;
`
func (td *postgresDB) GetTestRunEvents(ctx context.Context, testID id.ID, runID int) ([]model.TestRunEvent, error) {
rows, err := td.db.QueryContext(ctx, getTestRunEventsQuery, testID, runID)
if err != nil {
return []model.TestRunEvent{}, fmt.Errorf("could not query test runs: %w", err)
}
events := make([]model.TestRunEvent, 0)
for rows.Next() {
event, err := readTestRunEventFromRows(rows)
if err != nil {
return []model.TestRunEvent{}, fmt.Errorf("could not parse row: %w", err)
}
events = append(events, event)
}
return events, nil
}
func readTestRunEventFromRows(rows *sql.Rows) (model.TestRunEvent, error) {
var dataStoreConnectionBytes, pollingBytes, outputsBytes []byte
event := model.TestRunEvent{}
err := rows.Scan(
&event.ID,
&event.TestID,
&event.RunID,
&event.Type,
&event.Stage,
&event.Description,
&event.CreatedAt,
&dataStoreConnectionBytes,
&pollingBytes,
&outputsBytes,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return model.TestRunEvent{}, ErrNotFound
}
return model.TestRunEvent{}, fmt.Errorf("could not scan event: %w", err)
}
var dataStoreConnection model.ConnectionResult
var polling model.PollingInfo
outputs := make([]model.OutputInfo, 0)
err = json.Unmarshal(dataStoreConnectionBytes, &dataStoreConnection)
if err != nil {
return model.TestRunEvent{}, fmt.Errorf("could not unmarshal data store connection: %w", err)
}
err = json.Unmarshal(pollingBytes, &polling)
if err != nil {
return model.TestRunEvent{}, fmt.Errorf("could not unmarshal polling information: %w", err)
}
err = json.Unmarshal(outputsBytes, &outputs)
if err != nil {
return model.TestRunEvent{}, fmt.Errorf("could not unmarshal outputs: %w", err)
}
event.DataStoreConnection = dataStoreConnection
event.Polling = polling
event.Outputs = outputs
return event, nil
}