satellite/nodeevents: add method GetByID

Add method GetByID to get a node event by its ID. For easier testing.

Change-Id: Ic9e0d201ffd682a2ff3bd7b4d02b58b06d0f8282
This commit is contained in:
Cameron 2022-11-18 16:58:08 -05:00 committed by Storj Robot
parent 602f0cc132
commit b27c6dd032
3 changed files with 42 additions and 0 deletions

View File

@ -21,6 +21,8 @@ type DB interface {
GetLatestByEmailAndEvent(ctx context.Context, email string, event Type) (nodeEvent NodeEvent, err error) 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 gets the next batch of events to combine into an email.
GetNextBatch(ctx context.Context, firstSeenBefore time.Time) (events []NodeEvent, err error) 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 updates email_sent for a group of rows.
UpdateEmailSent(ctx context.Context, ids []uuid.UUID, timestamp time.Time) (err error) UpdateEmailSent(ctx context.Context, ids []uuid.UUID, timestamp time.Time) (err error)
} }

View File

@ -56,6 +56,17 @@ func (ne *nodeEvents) GetLatestByEmailAndEvent(ctx context.Context, email string
return fromDBX(dbxNE) 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. // GetNextBatch gets the next batch of events to combine into an email.
// It selects one item that was inserted before 'firstSeenBefore', then selects // 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 // all entries with the same email and event so that they can be combined into a

View File

@ -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) { func TestNodeEventsGetNextBatch(t *testing.T) {
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
testID1 := teststorj.NodeIDFromString("test1") testID1 := teststorj.NodeIDFromString("test1")