Skip to content

Commit

Permalink
ns: Add pagination unit test to MAC settings profile registry
Browse files Browse the repository at this point in the history
  • Loading branch information
halimi committed Dec 5, 2024
1 parent b1f5e46 commit e633570
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/networkserver/redis/mac_settings_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (r *MACSettingsProfileRegistry) List(
limit, offset := ttnredis.PaginationLimitAndOffsetFromContext(ctx)
if limit != 0 {
opts = append(opts,
ttnredis.FindProtosSorted(false),
ttnredis.FindProtosSorted(true),
ttnredis.FindProtosWithOffsetAndCount(offset, limit),
)
}
Expand Down
123 changes: 113 additions & 10 deletions pkg/networkserver/redis/mac_settings_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package redis

import (
"context"
"fmt"
"testing"

"go.thethings.network/lorawan-stack/v3/pkg/errors"
ttnredis "go.thethings.network/lorawan-stack/v3/pkg/redis"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
Expand All @@ -40,7 +42,13 @@ func TestMACSettingsProfileRegistry(t *testing.T) {
ApplicationIds: &ttnpb.ApplicationIdentifiers{
ApplicationId: "myapp",
},
ProfileId: "prof-01",
ProfileId: "prof-00",
}
ids1 := &ttnpb.MACSettingsProfileIdentifiers{
ApplicationIds: &ttnpb.ApplicationIdentifiers{
ApplicationId: "myapp",
},
ProfileId: "prof-00",
}
ids2 := &ttnpb.MACSettingsProfileIdentifiers{
ApplicationIds: &ttnpb.ApplicationIdentifiers{
Expand Down Expand Up @@ -123,41 +131,41 @@ func TestMACSettingsProfileRegistry(t *testing.T) {
t.Run("CreateReadUpdateDelete", func(t *testing.T) {
t.Parallel()
a, ctx := test.New(t)
profile, err := registry.Set(ctx, ids, []string{"ids", "mac_settings"}, createProfileFunc)
profile, err := registry.Set(ctx, ids1, []string{"ids", "mac_settings"}, createProfileFunc)
a.So(err, should.BeNil)
a.So(profile, should.NotBeNil)
a.So(profile.Ids, should.Resemble, ids)
a.So(profile.Ids, should.Resemble, ids1)
a.So(profile.MacSettings, should.NotBeNil)
a.So(profile.MacSettings.ResetsFCnt, should.NotBeNil)
a.So(profile.MacSettings.ResetsFCnt.Value, should.BeTrue)

retrieved, err := registry.Get(ctx, ids, []string{"ids", "mac_settings"})
retrieved, err := registry.Get(ctx, ids1, []string{"ids", "mac_settings"})
a.So(err, should.BeNil)
a.So(retrieved, should.NotBeNil)
a.So(retrieved.Ids, should.Resemble, ids)
a.So(retrieved.Ids, should.Resemble, ids1)
a.So(retrieved.MacSettings, should.NotBeNil)
a.So(retrieved.MacSettings.ResetsFCnt, should.NotBeNil)
a.So(retrieved.MacSettings.ResetsFCnt.Value, should.BeTrue)

updated, err := registry.Set(ctx, ids, []string{"ids", "mac_settings"}, updateProfileFunc)
updated, err := registry.Set(ctx, ids1, []string{"ids", "mac_settings"}, updateProfileFunc)
a.So(err, should.BeNil)
a.So(updated, should.NotBeNil)
a.So(updated.Ids, should.Resemble, ids)
a.So(updated.Ids, should.Resemble, ids1)
a.So(updated.MacSettings, should.NotBeNil)
a.So(updated.MacSettings.ResetsFCnt, should.NotBeNil)
a.So(updated.MacSettings.ResetsFCnt.Value, should.BeFalse)
a.So(updated.MacSettings.FactoryPresetFrequencies, should.Resemble, frequencies)

updated2, err := registry.Set(ctx, ids, []string{"ids", "mac_settings"}, updateFieldMaskProfileFunc)
updated2, err := registry.Set(ctx, ids1, []string{"ids", "mac_settings"}, updateFieldMaskProfileFunc)
a.So(err, should.BeNil)
a.So(updated2, should.NotBeNil)
a.So(updated2.Ids, should.Resemble, ids)
a.So(updated2.Ids, should.Resemble, ids1)
a.So(updated2.MacSettings, should.NotBeNil)
a.So(updated2.MacSettings.ResetsFCnt, should.NotBeNil)
a.So(updated2.MacSettings.ResetsFCnt.Value, should.BeFalse)
a.So(updated2.MacSettings.FactoryPresetFrequencies, should.Resemble, frequencies2)

deleted, err := registry.Set(ctx, ids, []string{"ids", "mac_settings"}, deleteProfileFunc)
deleted, err := registry.Set(ctx, ids1, []string{"ids", "mac_settings"}, deleteProfileFunc)
a.So(err, should.BeNil)
a.So(deleted, should.BeNil)
})
Expand All @@ -182,4 +190,99 @@ func TestMACSettingsProfileRegistry(t *testing.T) {
a.So(profiles[0].MacSettings.ResetsFCnt, should.NotBeNil)
a.So(profiles[0].MacSettings.ResetsFCnt.Value, should.BeTrue)
})

t.Run("Pagination", func(t *testing.T) {
t.Parallel()
a, ctx := test.New(t)

ttnredis.SetPaginationDefaults(ttnredis.PaginationDefaults{DefaultLimit: 10})

for i := 1; i < 21; i++ {
ids3 := &ttnpb.MACSettingsProfileIdentifiers{
ApplicationIds: &ttnpb.ApplicationIdentifiers{
ApplicationId: "myapp-pagination",
},
ProfileId: fmt.Sprintf("listprof-%02d", i),
}

profile, err := registry.Set(
ctx,
ids3,
[]string{"ids", "mac_settings"},
func(_ context.Context, pb *ttnpb.MACSettingsProfile,
) (*ttnpb.MACSettingsProfile, []string, error) { // nolint: unparam
a.So(pb, should.BeNil)
return &ttnpb.MACSettingsProfile{
Ids: ids3,
MacSettings: &ttnpb.MACSettings{
ResetsFCnt: &ttnpb.BoolValue{Value: true},
},
}, []string{"ids", "mac_settings"}, nil
},
)
a.So(err, should.BeNil)
a.So(profile, should.NotBeNil)
}

for _, tc := range []struct {
limit uint32
page uint32
idLow string
idHigh string
length int
}{
{
limit: 10,
page: 0,
idLow: "listprof-01",
idHigh: "listprof-10",
length: 10,
},
{
limit: 10,
page: 1,
idLow: "listprof-01",
idHigh: "listprof-10",
length: 10,
},
{
limit: 10,
page: 2,
idLow: "listprof-11",
idHigh: "listprof-20",
length: 10,
},
{
limit: 10,
page: 3,
length: 0,
},
{
limit: 0,
page: 0,
idLow: "listprof-01",
idHigh: "listprof-10",
length: 10,
},
} {
t.Run(fmt.Sprintf("limit:%v_page:%v", tc.limit, tc.page),
func(t *testing.T) {
t.Parallel()
var total int64
paginationCtx := registry.WithPagination(ctx, tc.limit, tc.page, &total)

profiles, err := registry.List(paginationCtx, &ttnpb.ApplicationIdentifiers{
ApplicationId: "myapp-pagination",
},
[]string{"ids", "mac_settings"},
)
a.So(err, should.BeNil)
a.So(profiles, should.HaveLength, tc.length)
a.So(total, should.Equal, 20)
for _, profile := range profiles {
a.So(profile.Ids.ProfileId, should.BeBetweenOrEqual, tc.idLow, tc.idHigh)
}
})
}
})
}

0 comments on commit e633570

Please sign in to comment.