From b27c6dd03207423d77d9ed020a7d73324bd6c4e1 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 18 Nov 2022 16:58:08 -0500 Subject: [PATCH] satellite/nodeevents: add method GetByID Add method GetByID to get a node event by its ID. For easier testing. Change-Id: Ic9e0d201ffd682a2ff3bd7b4d02b58b06d0f8282 --- satellite/nodeevents/db.go | 2 ++ satellite/satellitedb/nodeevents.go | 11 +++++++++ satellite/satellitedb/nodeevents_test.go | 29 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/satellite/nodeevents/db.go b/satellite/nodeevents/db.go index b24bdee9a..7513d7a1c 100644 --- a/satellite/nodeevents/db.go +++ b/satellite/nodeevents/db.go @@ -21,6 +21,8 @@ type DB interface { GetLatestByEmailAndEvent(ctx context.Context, email string, event Type) (nodeEvent NodeEvent, err error) // GetNextBatch gets the next batch of events to combine into an email. GetNextBatch(ctx context.Context, firstSeenBefore time.Time) (events []NodeEvent, err error) + // GetByID get a node event by id. + GetByID(ctx context.Context, id uuid.UUID) (nodeEvent NodeEvent, err error) // UpdateEmailSent updates email_sent for a group of rows. UpdateEmailSent(ctx context.Context, ids []uuid.UUID, timestamp time.Time) (err error) } diff --git a/satellite/satellitedb/nodeevents.go b/satellite/satellitedb/nodeevents.go index 31b0cba21..59e7f7fa4 100644 --- a/satellite/satellitedb/nodeevents.go +++ b/satellite/satellitedb/nodeevents.go @@ -56,6 +56,17 @@ func (ne *nodeEvents) GetLatestByEmailAndEvent(ctx context.Context, email string return fromDBX(dbxNE) } +// GetByID get a node event by id. +func (ne *nodeEvents) GetByID(ctx context.Context, id uuid.UUID) (nodeEvent nodeevents.NodeEvent, err error) { + defer mon.Task()(&ctx)(&err) + + dbxNE, err := ne.db.Get_NodeEvent_By_Id(ctx, dbx.NodeEvent_Id(id[:])) + if err != nil { + return nodeEvent, err + } + return fromDBX(dbxNE) +} + // GetNextBatch gets the next batch of events to combine into an email. // It selects one item that was inserted before 'firstSeenBefore', then selects // all entries with the same email and event so that they can be combined into a diff --git a/satellite/satellitedb/nodeevents_test.go b/satellite/satellitedb/nodeevents_test.go index 08bc64a78..66ba03bdd 100644 --- a/satellite/satellitedb/nodeevents_test.go +++ b/satellite/satellitedb/nodeevents_test.go @@ -81,6 +81,35 @@ func TestNodeEventsUpdateEmailSent(t *testing.T) { }) } +func TestNodeEventsGetByID(t *testing.T) { + satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { + testID1 := teststorj.NodeIDFromString("test1") + testID2 := teststorj.NodeIDFromString("test2") + testEmail1 := "test1@storj.test" + testEmail2 := "test2@storj.test" + + eventType := nodeevents.Disqualified + + event1, err := db.NodeEvents().Insert(ctx, testEmail1, testID1, eventType) + require.NoError(t, err) + + event2, err := db.NodeEvents().Insert(ctx, testEmail2, testID2, eventType) + require.NoError(t, err) + + res, err := db.NodeEvents().GetByID(ctx, event1.ID) + require.NoError(t, err) + require.Equal(t, event1.Email, res.Email) + require.Equal(t, event1.CreatedAt, res.CreatedAt) + require.Equal(t, event1.Event, res.Event) + + res, err = db.NodeEvents().GetByID(ctx, event2.ID) + require.NoError(t, err) + require.Equal(t, event2.Email, res.Email) + require.Equal(t, event2.CreatedAt, res.CreatedAt) + require.Equal(t, event2.Event, res.Event) + }) +} + func TestNodeEventsGetNextBatch(t *testing.T) { satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { testID1 := teststorj.NodeIDFromString("test1")