(t *testing.T)
| 17 | ) |
| 18 | |
| 19 | func 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{} |
nothing calls this directly
no test coverage detected