MCPcopy Index your code
hub / github.com/coder/coder / TestDatabase

Function TestDatabase

coderd/healthcheck/database_test.go:19–149  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestDatabase(t *testing.T) {
20 t.Parallel()
21
22 t.Run("OK", func(t *testing.T) {
23 t.Parallel()
24
25 var (
26 ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
27 report = healthcheck.DatabaseReport{}
28 db = dbmock.NewMockStore(gomock.NewController(t))
29 ping = 10 * time.Millisecond
30 )
31 defer cancel()
32
33 db.EXPECT().Ping(gomock.Any()).Return(ping, nil).Times(5)
34
35 report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db})
36
37 assert.True(t, report.Healthy)
38 assert.True(t, report.Reachable)
39 assert.Equal(t, health.SeverityOK, report.Severity)
40 assert.Equal(t, ping.String(), report.Latency)
41 assert.Equal(t, ping.Milliseconds(), report.LatencyMS)
42 assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
43 assert.Nil(t, report.Error)
44 })
45
46 t.Run("Error", func(t *testing.T) {
47 t.Parallel()
48
49 var (
50 ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
51 report = healthcheck.DatabaseReport{}
52 db = dbmock.NewMockStore(gomock.NewController(t))
53 err = xerrors.New("ping error")
54 )
55 defer cancel()
56
57 db.EXPECT().Ping(gomock.Any()).Return(time.Duration(0), err)
58
59 report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db})
60
61 assert.False(t, report.Healthy)
62 assert.False(t, report.Reachable)
63 assert.Equal(t, health.SeverityError, report.Severity)
64 assert.Zero(t, report.Latency)
65 require.NotNil(t, report.Error)
66 assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
67 assert.Contains(t, *report.Error, err.Error())
68 assert.Contains(t, *report.Error, health.CodeDatabasePingFailed)
69 })
70
71 t.Run("DismissedError", func(t *testing.T) {
72 t.Parallel()
73
74 var (
75 ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
76 report = healthcheck.DatabaseReport{}

Callers

nothing calls this directly

Calls 12

NewMockStoreFunction · 0.92
DurationMethod · 0.80
NotEmptyMethod · 0.80
RunMethod · 0.65
PingMethod · 0.65
NewMethod · 0.65
EXPECTMethod · 0.45
EqualMethod · 0.45
StringMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected