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

Function TestInserter

enterprise/coderd/usage/inserter_test.go:21–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestInserter(t *testing.T) {
22 t.Parallel()
23
24 t.Run("OK", func(t *testing.T) {
25 t.Parallel()
26
27 ctx := testutil.Context(t, testutil.WaitLong)
28 ctrl := gomock.NewController(t)
29 db := dbmock.NewMockStore(ctrl)
30 clock := quartz.NewMock(t)
31 inserter := usage.NewDBInserter(usage.InserterWithClock(clock))
32
33 now := dbtime.Now()
34 events := []struct {
35 time time.Time
36 event usagetypes.DiscreteEvent
37 }{
38 {
39 time: now,
40 event: usagetypes.DCManagedAgentsV1{
41 Count: 1,
42 },
43 },
44 {
45 time: now.Add(1 * time.Minute),
46 event: usagetypes.DCManagedAgentsV1{
47 Count: 2,
48 },
49 },
50 }
51
52 for _, e := range events {
53 eventJSON := jsoninate(t, e.event)
54 db.EXPECT().InsertUsageEvent(gomock.Any(), gomock.Any()).DoAndReturn(
55 func(ctx interface{}, params database.InsertUsageEventParams) error {
56 _, err := uuid.Parse(params.ID)
57 assert.NoError(t, err)
58 assert.Equal(t, e.event.EventType(), usagetypes.UsageEventType(params.EventType))
59 assert.JSONEq(t, eventJSON, string(params.EventData))
60 assert.Equal(t, e.time, params.CreatedAt)
61 return nil
62 },
63 ).Times(1)
64
65 clock.Set(e.time)
66 err := inserter.InsertDiscreteUsageEvent(ctx, db, e.event)
67 require.NoError(t, err)
68 }
69 })
70
71 t.Run("InvalidEvent", func(t *testing.T) {
72 t.Parallel()
73
74 ctx := testutil.Context(t, testutil.WaitLong)
75 ctrl := gomock.NewController(t)
76 db := dbmock.NewMockStore(ctrl)
77
78 // We should get an error if the event is invalid.

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
ContextFunction · 0.92
NewMockStoreFunction · 0.92
NewDBInserterFunction · 0.92
InserterWithClockFunction · 0.92
NowFunction · 0.92
UsageEventTypeTypeAlias · 0.92
jsoninateFunction · 0.85
RunMethod · 0.65
AddMethod · 0.65
InsertUsageEventMethod · 0.65
ParseMethod · 0.65

Tested by

no test coverage detected