Skip to content

Commit 6226ca8

Browse files
committed
add tests
1 parent d30ef55 commit 6226ca8

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

pkg/service/protocolDataService/protocol_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,120 @@ func Test_ProtocolDataService(t *testing.T) {
219219
}
220220
})
221221
})
222+
223+
t.Run("Test ListStakersForStrategy", func(t *testing.T) {
224+
strategy := "0x7d704507b76571a51d9cae8addabbfd0ba0e63d3"
225+
blockNumber := uint64(3204393)
226+
227+
t.Run("With DelegationFilterAll (default)", func(t *testing.T) {
228+
// Test with ALL filter - should return all stakers regardless of delegation status
229+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterAll, nil)
230+
assert.Nil(t, err)
231+
assert.True(t, len(stakers) > 0, "Should return stakers for the strategy")
232+
233+
// Verify the structure of returned data
234+
for _, staker := range stakers {
235+
assert.NotEmpty(t, staker.Staker, "Staker address should not be empty")
236+
assert.NotEmpty(t, staker.Shares, "Shares should not be empty")
237+
}
238+
})
239+
240+
t.Run("With DelegationFilterDelegated", func(t *testing.T) {
241+
// Test with DELEGATED filter - should return only delegated stakers
242+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterDelegated, nil)
243+
assert.Nil(t, err)
244+
245+
// All returned stakers should be delegated
246+
for _, staker := range stakers {
247+
assert.True(t, staker.Delegated, "Staker should be delegated")
248+
assert.NotNil(t, staker.Operator, "Delegated staker should have an operator")
249+
if staker.Operator != nil {
250+
assert.NotEmpty(t, *staker.Operator, "Operator address should not be empty for delegated staker")
251+
}
252+
}
253+
})
254+
255+
t.Run("With DelegationFilterUndelegated", func(t *testing.T) {
256+
// Test with UNDELEGATED filter - should return only undelegated stakers
257+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterUndelegated, nil)
258+
assert.Nil(t, err)
259+
260+
// All returned stakers should NOT be delegated
261+
for _, staker := range stakers {
262+
assert.False(t, staker.Delegated, "Staker should not be delegated")
263+
}
264+
})
265+
266+
t.Run("With nil pagination (uses default)", func(t *testing.T) {
267+
// Test with nil pagination to ensure no error occurs
268+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterAll, nil)
269+
assert.Nil(t, err)
270+
assert.True(t, len(stakers) >= 0, "Should return stakers or empty array")
271+
})
272+
273+
t.Run("With custom pagination (first page)", func(t *testing.T) {
274+
// Test with custom pagination on first page (no offset)
275+
pagination := &types.Pagination{
276+
Page: 0,
277+
PageSize: 5,
278+
}
279+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterAll, pagination)
280+
assert.Nil(t, err)
281+
assert.True(t, len(stakers) <= 5, "Should respect custom page size of 5")
282+
})
283+
284+
t.Run("With custom pagination (second page with offset)", func(t *testing.T) {
285+
// Test with page > 0 to test HasOffset template logic
286+
pagination := &types.Pagination{
287+
Page: 1,
288+
PageSize: 5,
289+
}
290+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterAll, pagination)
291+
assert.Nil(t, err)
292+
// May or may not have results depending on total data, but should not error
293+
assert.True(t, len(stakers) <= 5, "Should respect custom page size of 5")
294+
})
295+
296+
t.Run("With delegation filter and pagination combined", func(t *testing.T) {
297+
// Test combining delegation filter with pagination
298+
pagination := &types.Pagination{
299+
Page: 0,
300+
PageSize: 10,
301+
}
302+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterDelegated, pagination)
303+
assert.Nil(t, err)
304+
assert.True(t, len(stakers) <= 10, "Should respect page size")
305+
306+
// All returned stakers should be delegated
307+
for _, staker := range stakers {
308+
assert.True(t, staker.Delegated, "Staker should be delegated")
309+
}
310+
})
311+
312+
t.Run("Stakers are ordered by shares descending", func(t *testing.T) {
313+
// Test that results are ordered by shares descending
314+
pagination := &types.Pagination{
315+
Page: 0,
316+
PageSize: 10,
317+
}
318+
stakers, err := pds.ListStakersForStrategy(context.Background(), strategy, blockNumber, DelegationFilterAll, pagination)
319+
assert.Nil(t, err)
320+
321+
if len(stakers) > 1 {
322+
// Verify ordering (shares should be descending)
323+
// Note: shares are strings representing large numbers, so we can't directly compare
324+
// but we can verify the query runs without error
325+
assert.True(t, len(stakers) > 0, "Should have stakers to verify ordering")
326+
}
327+
})
328+
329+
t.Run("With uppercase strategy address (should be normalized)", func(t *testing.T) {
330+
// Test that uppercase addresses are normalized to lowercase
331+
uppercaseStrategy := "0x7D704507B76571A51D9CAE8ADDABBFD0BA0E63D3"
332+
stakers, err := pds.ListStakersForStrategy(context.Background(), uppercaseStrategy, blockNumber, DelegationFilterAll, nil)
333+
assert.Nil(t, err)
334+
// Should work the same as lowercase
335+
assert.True(t, len(stakers) >= 0)
336+
})
337+
})
222338
}

0 commit comments

Comments
 (0)