7a04f298d2
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package participants
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tgmock"
|
|
)
|
|
|
|
func generateParticipants(count int) []tg.ChannelParticipantClass {
|
|
r := make([]tg.ChannelParticipantClass, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
r = append(r, &tg.ChannelParticipant{
|
|
UserID: int64(i),
|
|
Date: i,
|
|
})
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func result(r []tg.ChannelParticipantClass, count int) tg.ChannelsChannelParticipantsClass {
|
|
return &tg.ChannelsChannelParticipants{
|
|
Participants: r,
|
|
Count: count,
|
|
}
|
|
}
|
|
|
|
func TestIterator(t *testing.T) {
|
|
ctx := context.Background()
|
|
mock := tgmock.NewRequire(t)
|
|
limit := 10
|
|
totalRecords := 3 * limit
|
|
expected := generateParticipants(totalRecords)
|
|
raw := tg.NewClient(mock)
|
|
ch := &tg.InputChannel{
|
|
ChannelID: 10,
|
|
AccessHash: 10,
|
|
}
|
|
|
|
mock.ExpectCall(&tg.ChannelsGetParticipantsRequest{
|
|
Channel: ch,
|
|
Filter: &tg.ChannelParticipantsRecent{},
|
|
Offset: 0,
|
|
Limit: limit,
|
|
}).ThenResult(result(expected[0:limit], totalRecords))
|
|
mock.ExpectCall(&tg.ChannelsGetParticipantsRequest{
|
|
Channel: ch,
|
|
Filter: &tg.ChannelParticipantsRecent{},
|
|
Offset: limit,
|
|
Limit: limit,
|
|
}).ThenResult(result(expected[limit:2*limit], totalRecords))
|
|
mock.ExpectCall(&tg.ChannelsGetParticipantsRequest{
|
|
Channel: ch,
|
|
Filter: &tg.ChannelParticipantsRecent{},
|
|
Offset: 2 * limit,
|
|
Limit: limit,
|
|
}).ThenResult(result(expected[2*limit:3*limit], totalRecords))
|
|
mock.ExpectCall(&tg.ChannelsGetParticipantsRequest{
|
|
Channel: ch,
|
|
Filter: &tg.ChannelParticipantsRecent{},
|
|
Offset: 3 * limit,
|
|
Limit: limit,
|
|
}).ThenResult(result(expected[3*limit:], totalRecords))
|
|
|
|
iter := NewQueryBuilder(raw).GetParticipants(ch).BatchSize(10).Iter()
|
|
i := 0
|
|
for iter.Next(ctx) {
|
|
require.Equal(t, expected[i], iter.Value().Participant)
|
|
i++
|
|
}
|
|
require.NoError(t, iter.Err())
|
|
require.Equal(t, totalRecords, i)
|
|
|
|
total, err := iter.Total(ctx)
|
|
require.NoError(t, err)
|
|
require.Equal(t, totalRecords, total)
|
|
|
|
mock.ExpectCall(&tg.ChannelsGetParticipantsRequest{
|
|
Channel: ch,
|
|
Filter: &tg.ChannelParticipantsRecent{},
|
|
Offset: 0,
|
|
Limit: 1,
|
|
}).ThenResult(result(expected[:0], totalRecords))
|
|
total, err = iter.FetchTotal(ctx)
|
|
require.NoError(t, err)
|
|
require.Equal(t, totalRecords, total)
|
|
}
|